Skip to main content

System/ Process Monitoring in Linux using MONIT


Install and Configure Monit
sudo apt-get install monit
usudo chmod 700 /etc/monit/monitrc
telnet localhost 2812
sudo ufw allow 2812

Sample MONIT Configuration
username@ServerName:/etc/monit$ sudo cat monitrc
set daemon 60
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: monit@ServerName.localnet }
set alert username.shah@cw.com
set httpd port 2812 and
#     SSL ENABLE
#     PEMFILE  /etc/monit/monit.pem
    allow admin:edgeadmin

check system edge.localnet
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 2 then alert
    if memory usage > 70% then alert
    if swap usage > 20% then alert
    if cpu usage (user) > 70% then alert
    if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 20% then alert

# SENDMAIL
check process sendmail with pidfile /var/run/sendmail/mta/sendmail.pid
  group services
  start program "/etc/init.d/sendmail start"
  stop program "/etc/init.d/sendmail stop"
  if failed port 25 protocol smtp then restart
  if 5 restarts within 5 cycles then timeout

# SSHD
check process sshd with pidfile /var/run/sshd.pid
  group system
  start program  "/sbin/start ssh"
  stop program  "/sbin/stop ssh"
  if failed port 22 protocol ssh then restart
  if 5 restarts within 5 cycles then timeout

# ORACLE XE
check process oracle with pidfile /var/run/oracle-xe.pid
  group services
  start program "/etc/init.d/oracle-xe start"
  stop program "/etc/init.d/oracle-xe stop"
  if cpu usage > 23% for 5 cycles then restart
  if failed host ServerName.localnet port 1521 timeout 60 seconds then restart
  if failed host ServerName.localnet port 8080 timeout 120 seconds then restart
  if 5 restarts within 5 cycles then timeout

# VNC
check process Xvnc4 with pidfile "/home/username/.vnc/ServerName.localnet:1.pid"

  start program "/sbin/initctl start vncserve"
  stop program "/sbin/initctl stop vncserve"
  if failed port 5901 then restart
  if 5 restarts within 5 cycles then timeout

# VNC
#check process Xvnc4 with pidfile "/home/username/.vnc/ServerName.localnet:1.pid"
#   start program "/home/username/vnc.sh"
#   stop program "/home/username/vnc-kill.sh"
#   if failed port 5901 then restart
#   if 5 restarts within 5 cycles then timeout

# SMBD
#check process smbd with pidfile /var/run/samba/smbd.pid
#   group system
#   start program  "/sbin/start smbd"
#   stop program  "/sbin/stop smbd"
#   if failed port 139 then start
#   if 5 restarts within 5 cycles then timeout

# SKYPEVNC
#check process Xvnc4 with pidfile "/home/username/.vnc/servername\:2.pid"
#   start program "/etc/skype-start.sh"
#   stop program "/etc/skype-stop.sh"
#   if failed port 5902 then start
#   if 5 restarts within 5 cycles then timeout

# DNS SERVER
#check process named with pidfile /var/run/bind/run/named.pid
#   group services
#   start program  "/etc/init.d/bind9 start"
#   stop program  "/etc/init.d/bind9 stop"
#   if failed host 127.0.0.1 port 53 type tcp protocol dns then alert
#   if failed host 127.0.0.1 port 53 type udp protocol dns then alert
#   if 5 restarts within 5 cycles then timeout

# MYSQL
#check process mysql with pidfile /var/run/mysqld/mysqld.pid
#   group services
#   start program = "/etc/init.d/mysql start"
#   stop program = "/etc/init.d/mysql stop"
#   if failed host 127.0.0.1 port 3306 then restart
#   if 5 restarts within 5 cycles then timeout

# APACHE
#check process apache with pidfile /var/run/apache2.pid
#   group services
#   start program = "/etc/init.d/apache2 start"
#   stop program  = "/etc/init.d/apache2 stop"
#   if failed host admin.hosting.example.com port 80 protocol http
#      and request "/tools/monin_test_file" then restart
#   if cpu is greater than 60% for 2 cycles then alert
#   if cpu > 90% for 5 cycles then restart
#   if totalmem > 500 MB for 5 cycles then restart
#   if children > 250 then restart
#   if loadavg(5min) greater than 10 for 8 cycles then stop
#   if 3 restarts within 5 cycles then timeout

# POSTFIX
#check process postfix with pidfile /var/spool/postfix/pid/master.pid
#   group services
#   start program = "/etc/init.d/postfix start"
#   stop  program = "/etc/init.d/postfix stop"
#   if failed port 25 protocol smtp then restart
#   if 5 restarts within 5 cycles then timeout

# ISPCP_DAEMON
#check process ispcp_daemon with pidfile /var/run/ispcp-daemon.pid
#   group services
#   start program = "/etc/init.d/ispcp_daemon start"
#   stop  program = "/etc/init.d/ispcp_daemon stop"
## If you active this, your /var/log/daemon will grow every minute,
## so it's not recomended unless you don't want to trust only in .pid
##   if failed port 9876 then restart
#   if 5 restarts within 5 cycles then timeout

Sample VNC Server Startup Script

username@ServerName:~$ cat /home/username/vnc.sh
#!/bin/bash
su -l username -c “/usr/bin/vnc4server :1 -name "my Server Name" -geometry 1280x730”

Sample VNC Server Stop Script

username@ServerName:~$ cat /home/username/vnc-kill.sh
#!/bin/bash
/usr/bin/vncserver -kill :1

Sample Script to create a PID for a non-pid type process
username@ServerName:~$ sudo cat /etc/monit/oracle-xe-pid.sh
#!/bin/bash

pmon=`ps -ef | egrep pmon_XE'\>' | grep -v grep| awk '{ print $2 }'`

if [ "$pmon" = "" ];
then
   rm /var/run/oracle-xe.pid
else
       echo $pmon > /var/run/oracle-xe.pid
fi

Sample Cron to check, create and update PID every minute

username@ServerName:~$ sudo crontab -l
* * * * * /etc/monit/oracle-xe-pid.sh

Popular posts from this blog

Useful website performance and load testing tools

http://tsung.erlang-projects.org/ http://httpd.apache.org/docs/2.0/programs/ab.html http://phantomjs.org/ https://developers.google.com/speed/pagespeed/ http://servermonitoringhq.com/blog/how_to_quickly_stress_test_a_web_server https://code.google.com/p/httperf/ http://loadimpact.com/ http://www.paessler.com/webstress http://loaduiweb.org/ http://en.wikipedia.org/wiki/Web_server_benchmarking http://en.wikipedia.org/wiki/Load_testing http://www.loadui.org/ http://www.loadtestingtool.com/index.shtml http://www.appdynamics.com/blog/devops/load-testing-tools-explained-the-server-side/

Copy files and folders using SCP with spaces in path

Copying data from one system to other with file or folder names that contain spaces in path can be achieved using this guide. In this case I am copying data from Macbook to Windows 10 computer. In order to copy the data easily it is better to use bash commands. Windows computer can support WSL (Windows subsystem for Linux) and you can run one of few linux distributions to use shell commands. I have Ubuntu set up within my Windows 10 using WSL. If you do not have WSL, you can set it up using my guide here . The copy can be performed in two ways: 1) Using SCP Source (MacOs) path: /home/Users/username/Documents/data extract from 2020/ First of all you add escape sequence to the path so it will become:  /home/Users/me/Documents/data\ extract\ from\ 2020/ . While this works on local system for SCP you'll have to double the escape sequences by replacing \ with \\, as below. Figure out your source computer IP address using "ifconfig" command. Now using scp command on target syst

TrueCrypt on macOS X Mojave 10.14

If you have updated your macOS recently to Mojave otherwise known as verison 10.14 you may not be able to install the last version of Truecrypt in order to access your old volumes encrypted with Truecrypt software. This article will guide you to get this working on your MacOS v10.14 (Mjoave) . Download the package from  https://truecrypt.ch/downloads/  or  https://www.truecrypt71a.com/downloads/ . Find downloaded package using Finder in your HDD/Users/username/Downloads folder and will look like  TrueCrypt 7.1a Mac OS X.dmg . Open file location in Finder and open or double click on  TrueCrypt 7.1a Mac OS X.dmg . This will mount Truecrypt 7.1a and will have Truecrypt 7.1a.mpkg in it. Drag the package T rueCrypt 7.1a.mpkg and drop in your Downloads folder. From Locations in Finder you can eject your TrueCrypt mount. Now go to your Downloads location, find the file  TrueCrypt 7.1a.mpkg , right click and select Show Package Contents . Find the file Contents/distribution.di