{"id":6546,"date":"2017-03-12T21:35:20","date_gmt":"2017-03-12T13:35:20","guid":{"rendered":"http:\/\/rmohan.com\/?p=6546"},"modified":"2017-03-12T23:53:13","modified_gmt":"2017-03-12T15:53:13","slug":"monit-process","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6546","title":{"rendered":"Monit  process"},"content":{"rendered":"<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC240\" class=\"blob-code blob-code-inner js-file-line\"># mysql<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC241\" class=\"blob-code blob-code-inner js-file-line\">check process mysqld with pidfile \/var\/lib\/mysql\/ns388683.pid<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC242\" class=\"blob-code blob-code-inner js-file-line\">group database<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC243\" class=\"blob-code blob-code-inner js-file-line\">start program = &#8220;\/etc\/init.d\/mysql start&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC244\" class=\"blob-code blob-code-inner js-file-line\">stop program = &#8220;\/etc\/init.d\/mysql stop&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC245\" class=\"blob-code blob-code-inner js-file-line\">if failed host 127.0.0.1 port 3306 then restart<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC246\" class=\"blob-code blob-code-inner js-file-line\">if 5 restarts within 5 cycles then timeout<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC247\" class=\"blob-code blob-code-inner js-file-line\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC248\" class=\"blob-code blob-code-inner js-file-line\"># nginx<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC249\" class=\"blob-code blob-code-inner js-file-line\">check process nginx with pidfile \/opt\/nginx\/logs\/nginx.pid<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC250\" class=\"blob-code blob-code-inner js-file-line\">start program = &#8220;\/etc\/init.d\/nginx start&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC251\" class=\"blob-code blob-code-inner js-file-line\">stop program = &#8220;\/etc\/init.d\/nginx stop&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC252\" class=\"blob-code blob-code-inner js-file-line\">if failed host 127.0.0.1 port 80 then restart<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC253\" class=\"blob-code blob-code-inner js-file-line\">if cpu is greater than 40% for 2 cycles then alert<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC254\" class=\"blob-code blob-code-inner js-file-line\">if cpu &gt; 60% for 5 cycles then restart<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC255\" class=\"blob-code blob-code-inner js-file-line\">if 10 restarts within 10 cycles then timeout<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC256\" class=\"blob-code blob-code-inner js-file-line\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC257\" class=\"blob-code blob-code-inner js-file-line\"># redis<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC258\" class=\"blob-code blob-code-inner js-file-line\">check process redis with pidfile \/var\/run\/redis.pid<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC259\" class=\"blob-code blob-code-inner js-file-line\">start program = &#8220;\/etc\/init.d\/redis-server start&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC260\" class=\"blob-code blob-code-inner js-file-line\">stop program = &#8220;\/etc\/init.d\/redis-server stop&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC261\" class=\"blob-code blob-code-inner js-file-line\">group redis<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC262\" class=\"blob-code blob-code-inner js-file-line\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC263\" class=\"blob-code blob-code-inner js-file-line\">check file dump.rdb with path \/var\/lib\/redis\/dump.rdb<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC264\" class=\"blob-code blob-code-inner js-file-line\">if size &gt; 100 MB then alert<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC265\" class=\"blob-code blob-code-inner js-file-line\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC266\" class=\"blob-code blob-code-inner js-file-line\"># tomcat<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC267\" class=\"blob-code blob-code-inner js-file-line\">check process tomcat with pidfile \/var\/run\/tomcat\/tomcat.pid<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC268\" class=\"blob-code blob-code-inner js-file-line\">start program = &#8220;\/etc\/init.d\/tomcat start&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC269\" class=\"blob-code blob-code-inner js-file-line\">as uid solr gid solr<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC270\" class=\"blob-code blob-code-inner js-file-line\">stop program = &#8220;\/etc\/init.d\/tomcat stop&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC271\" class=\"blob-code blob-code-inner js-file-line\">as uid solr gid solr<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC272\" class=\"blob-code blob-code-inner js-file-line\">if failed port 8080 then alert<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-monitrc-LC273\" class=\"blob-code blob-code-inner js-file-line\">if failed port 8080 for 5 cycles then restart<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now for the tomcat part &#8211; this is based on tomcat being in \/usr\/local\/tomcat where our typical setup script puts everything.<br \/>\nTomcat, method a (recomended):<\/p>\n<p>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.<\/p>\n<p>echo &#8220;check host tomcat with address localhost<br \/>\nstop program = &#8220;\/etc\/init.d\/tomcat stop&#8221;<br \/>\nstart program = &#8220;\/etc\/init.d\/tomcat restart&#8221;<br \/>\nif failed port 8080 and protocol http<br \/>\nthen start<br \/>\n&#8221; &gt; \/etc\/monit.d\/tomcat<br \/>\n\/etc\/init.d\/monit restart<\/p>\n<p>Tomcat, method b:<\/p>\n<p>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 )<\/p>\n<p>check process tomcat with pidfile &#8220;\/var\/run\/tomcat\/tomcat.pid&#8221;<br \/>\nstart program = &#8220;\/usr\/local\/tomcat\/bin\/startup.sh&#8221;<br \/>\nas uid tomcat gid tomcat<br \/>\nstop program = &#8220;\/usr\/local\/tomcat\/bin\/shutdown.sh&#8221;<br \/>\nas uid tomcat gid tomcat<br \/>\nif failed port 8080 then alert<br \/>\nif failed port 8080 for 5 cycles then restart<\/p>\n<p>Then edit your catalina.sh and set<\/p>\n<p>CATALINA_PID to be \/var\/run\/tomcat\/tomcat.pid<br \/>\nJAVA_HOME=\/usr\/java\/jdk<\/p>\n<p>Add in at the top<\/p>\n<p>Then of course create the pid directory<\/p>\n<p>mkdir \/var\/run\/tomcat\/<br \/>\nchown tomcat.tomcat \/var\/run\/tomcat\/<\/p>\n<p>MySQL<\/p>\n<p>Add this to your my.cnf under [mysqld]<\/p>\n<p>pid-file=\/var\/run\/mysqld\/mysqld.pid<\/p>\n<p>and this to your monit config<\/p>\n<p>check process mysql with pidfile \/var\/run\/mysqld\/mysqld.pid<br \/>\ngroup database<br \/>\nstart program = &#8220;\/etc\/init.d\/mysql start&#8221;<br \/>\nstop program = &#8220;\/etc\/init.d\/mysql stop&#8221;<br \/>\nif failed host 127.0.0.1 port 3306 protocol mysql then restart<br \/>\nif 5 restarts within 5 cycles then timeout<br \/>\ndepends on mysql_rc<\/p>\n<p>check file mysql_rc with path \/etc\/init.d\/mysql<br \/>\ngroup database<br \/>\nif failed checksum then unmonitor<br \/>\nif failed permission 755 then unmonitor<br \/>\nif failed uid root then unmonitor<br \/>\nif failed gid root then unmonitor<\/p>\n<p>Disk Space<\/p>\n<p>Add the following to your monit config<\/p>\n<p>check filesystem with path \/dev\/xvda1<br \/>\nif space usage &gt; 95% then alert<br \/>\nif inode usage &gt; 95% then alert<\/p>\n<p>SSH<\/p>\n<p>Add this to your monit config (change the port if yours is different)<\/p>\n<p>check process sshd with pidfile \/var\/run\/sshd.pid<br \/>\nstart program &#8220;\/etc\/init.d\/sshd start&#8221;<br \/>\nstop program &#8220;\/etc\/init.d\/sshd stop&#8221;<br \/>\nif failed port 22 protocol ssh then restart<br \/>\nif 5 restarts within 5 cycles then timeout<\/p>\n<p>Shell script<\/p>\n<p>&nbsp;<\/p>\n<p><code>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\"<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p> # mysql check process mysqld with pidfile \/var\/lib\/mysql\/ns388683.pid group database start program = &#8220;\/etc\/init.d\/mysql start&#8221; stop program = &#8220;\/etc\/init.d\/mysql stop&#8221; 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 = &#8220;\/etc\/init.d\/nginx start&#8221; stop program = &#8220;\/etc\/init.d\/nginx stop&#8221; [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6546"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6546"}],"version-history":[{"count":4,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6546\/revisions"}],"predecessor-version":[{"id":6550,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6546\/revisions\/6550"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}