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

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

Tools you need before you start using Terraform for orchestration infrastructure in cloud

Terraform one of the famous open source tool used for planning, deploying and maintaining infrastructure as a code and the beauty of this tool is that it works across various cloud service providers. Even though I personally like the AWS Cloudformation, it is AWS only and most probably it will always remain limited to Amazon Web Services. It has few advantages over other tools when using with AWS and will be the first to have new AWS features incorporated before other tools and APIs catch up. However if you are not limiting yourself to AWS only or have hybrid environment you may want to use a 3rd party tool compatible for all cloud infrastructures. Terraform being one of them is youngest client only tool and this article will help you setting up your desktop environment and give you understanding of how it works before you can start to code. Prerequisites: Computer running Windows, Linux, Solaris or MacOS operating system. Downloads: Download Terraform directly from the crea

Running VNC Server as a Service on Ubuntu

Method 1: VNC Server as a Service on Ubuntu desktop similar to Redhat sysconfig sudo apt-get install vncserver sudo mkdir -p /etc/sysconfig sudo touch /etc/sysconfig/vncservers sudo vi /etc/sysconfig/vncservers # Add following VNC Server instances where username and arguments are defined for each session. VNCSERVERS=" 1:user1 2:user2 3:user3 " VNCSERVERARGS[1]=" -geometry 1280x992 -depth 16 " VNCSERVERARGS[2]=" -geometry 800x600 -depth 8 " VNCSERVERARGS[3]=" -geometry 980x720 " sudo vi /etc/init.d/vncserver # Add below to the service script #!/bin/bash # # chkconfig: - 91 35 # description: Starts and stops vncserver. \ # used to provide remote X administration services. # Source function library. # . /etc/init.d/functions # Source networking configuration. # . /etc/sysconfig/network # Check that networking is up. # [ ${NETWORKING} = "no" ] && exit 0 unset VNCSERVERARGS VNC