May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Monit process

# mysql
check process mysqld with pidfile /var/lib/mysql/ns388683.pid
group database
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
# nginx
check process nginx with pidfile /opt/nginx/logs/nginx.pid
start program = “/etc/init.d/nginx start”
stop program = “/etc/init.d/nginx stop”
if failed host 127.0.0.1 port 80 then restart
if cpu is greater than 40% for 2 cycles then alert
if cpu > 60% for 5 cycles then restart
if 10 restarts within 10 cycles then timeout
# redis
check process redis with pidfile /var/run/redis.pid
start program = “/etc/init.d/redis-server start”
stop program = “/etc/init.d/redis-server stop”
group redis
check file dump.rdb with path /var/lib/redis/dump.rdb
if size > 100 MB then alert
# tomcat
check process tomcat with pidfile /var/run/tomcat/tomcat.pid
start program = “/etc/init.d/tomcat start”
as uid solr gid solr
stop program = “/etc/init.d/tomcat stop”
as uid solr gid solr
if failed port 8080 then alert
if failed port 8080 for 5 cycles then restart

Now for the tomcat part – this is based on tomcat being in /usr/local/tomcat where our typical setup script puts everything.
Tomcat, method a (recomended):

Simply run the below snippet to enable monit monitoring of tomcat. This method requires the least work and changes to configurations. Typically monit prefers a bid file to monitor a service as described in method b, but this way works just as well so long as the http port connector is enabled.

echo “check host tomcat with address localhost
stop program = “/etc/init.d/tomcat stop”
start program = “/etc/init.d/tomcat restart”
if failed port 8080 and protocol http
then start
” > /etc/monit.d/tomcat
/etc/init.d/monit restart

Tomcat, method b:

Use this method if you dont have a suitable http connector enabled for your tomcat instance, but be aware that pid files can be left in an inconsistent state in some cases. Which may then require manual intervention anyway. Add a tomcat instance into your config for monit that looks like this (change gid/uid tomcat runs )

check process tomcat with pidfile “/var/run/tomcat/tomcat.pid”
start program = “/usr/local/tomcat/bin/startup.sh”
as uid tomcat gid tomcat
stop program = “/usr/local/tomcat/bin/shutdown.sh”
as uid tomcat gid tomcat
if failed port 8080 then alert
if failed port 8080 for 5 cycles then restart

Then edit your catalina.sh and set

CATALINA_PID to be /var/run/tomcat/tomcat.pid
JAVA_HOME=/usr/java/jdk

Add in at the top

Then of course create the pid directory

mkdir /var/run/tomcat/
chown tomcat.tomcat /var/run/tomcat/

MySQL

Add this to your my.cnf under [mysqld]

pid-file=/var/run/mysqld/mysqld.pid

and this to your monit config

check process mysql with pidfile /var/run/mysqld/mysqld.pid
group database
start program = “/etc/init.d/mysql start”
stop program = “/etc/init.d/mysql stop”
if failed host 127.0.0.1 port 3306 protocol mysql then restart
if 5 restarts within 5 cycles then timeout
depends on mysql_rc

check file mysql_rc with path /etc/init.d/mysql
group database
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor

Disk Space

Add the following to your monit config

check filesystem with path /dev/xvda1
if space usage > 95% then alert
if inode usage > 95% then alert

SSH

Add this to your monit config (change the port if yours is different)

check process sshd with pidfile /var/run/sshd.pid
start program “/etc/init.d/sshd start”
stop program “/etc/init.d/sshd stop”
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

Shell script

 

if failed port 443 protocol https request / with timeout 5 seconds for 2 cycles then exec "/var/lib/monit/scripts/notifyAndExecute.sh" else if succeeded then exec "/etc/monit/pagerduty-resolve authentication"

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>