{"id":291777,"date":"2021-07-06T13:00:00","date_gmt":"2021-07-06T10:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/getting-started-with-monit-for-server-monitoring-cloudsavvy-it\/"},"modified":"2021-07-06T13:00:00","modified_gmt":"2021-07-06T10:00:00","slug":"getting-started-with-monit-for-server-monitoring-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/getting-started-with-monit-for-server-monitoring-cloudsavvy-it\/","title":{"rendered":"#Getting Started with Monit for Server Monitoring \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3e3a7db0399\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3e3a7db0399\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-monit-for-server-monitoring-cloudsavvy-it\/#What_Does_Monit_Do\" >What Does Monit Do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-monit-for-server-monitoring-cloudsavvy-it\/#Installing_and_Configuring_Monit\" >Installing and Configuring Monit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-monit-for-server-monitoring-cloudsavvy-it\/#Configuring_Alerts\" >Configuring Alerts<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Getting Started with Monit for Server Monitoring \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-1232\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/07\/6a91c397.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"299\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Monit is a server monitoring program that can check the processes running on your system to ensure that they\u2019re always online and operating within reasonable CPU and memory limits. It also comes with a web interface to monitor your system.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Does_Monit_Do\"><\/span>What Does Monit Do?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Essentially, Monit will check in on a given process every two minutes (by default) to see how it\u2019s doing. It keeps a log of resource usage, and can take action if an error occurs. This includes relaunching crashed processes, and restarting processes using too many resources.<\/p>\n<p>Monit defines its behavior entirely through config files. Here\u2019s the default config for MySQL as an example:<\/p>\n<pre>check process mysql&#13;\n  with pidfile \/var\/run\/mysqld\/mysqld.pid&#13;\n  start program = \"\/usr\/sbin\/service mysql start\" with timeout 60 seconds&#13;\n  stop program = \"\/usr\/sbin\/service mysql stop\" with timeout 60 seconds&#13;\n  if totalmem &gt; 400 MB for 5 cycles then alert&#13;\n  if totalmem &gt; 600 MB for 5 cycles then restart&#13;\n  if cpu &gt; 50% for 5 cycles then alert&#13;\n  if cpu &gt; 90% for 5 cycles then restart&#13;\n  if 3 restarts within 5 cycles then timeout<\/pre>\n<p>Monit is configured to monitor the running MySQL process. If it goes down, it can start it again using the <code>start program =<\/code>\u00a0config. If MySQL starts using too many resources, it can be restarted automatically, though you\u2019ll be warned by email first.<\/p>\n<p>It can also be configured to monitor your system as a whole, and can alert you if your server is experiencing unusual load.<\/p>\n<pre>check system wp01&#13;\n  if loadavg(5min) &gt; 1 then alert&#13;\n  if memory usage &gt; 90% for 5 cycles then alert&#13;\n  if cpu usage (user) &gt; 90% for 5 cycles then alert<\/pre>\n<p>Everything Monit monitors is viewable from the web interface, which runs over port 2812 by default.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1233\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/07\/be7efd13.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Additionally, you can view data for multiple hosts all in one place with the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/mmonit.com\/\">M\/Monit utility<\/a>, which does require a license but is free to try.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Setup Slack Notifications for Low Disk Space On Your Server<\/em><\/strong><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Installing_and_Configuring_Monit\"><\/span>Installing and Configuring Monit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Install Monit from <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/mmonit.com\/monit\/#download\">your distro\u2019s package manager<\/a>; for Debian based systems like Ubuntu, that would be:<\/p>\n<pre>sudo apt-get install monit<\/pre>\n<p>Monit should come with default config, usually located at <code>~\/.monitrc<\/code>. If it didn\u2019t (as was the case with a macOS installation) you can find the default config files <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/gist.github.com\/manuelmeurer\/35491f9bb5b55c5a6c9e\">here<\/a>.<\/p>\n<p>Monit\u2019s web interface is configured to run on port 2812. This is fine, but it\u2019s entirely open by default with a default password. We\u2019ll want to lock it down to an authorized IP address and<\/p>\n<pre>set httpd port 2812&#13;\n    allow your_ip&#13;\n    allow admin:password<\/pre>\n<p>Change this password, of course. There\u2019s a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/mmonit.com\/monit\/documentation\/monit.html#MONIT-HTTPD\">few more configuration options<\/a> for the web interface, but this is good for now.<\/p>\n<p>Monit can then be configured to monitor individual processes. Usually, this is done with the processes PID file (often found in <code>\/var\/run\/<\/code>) which stores the current PID of the running instance of that process, since the PID will change whenever the process restarts.<\/p>\n<pre>check process nginx with pidfile \/var\/run\/nginx.pid&#13;\n    start program = \"\/etc\/init.d\/nginx start\"&#13;\n    stop program = \"\/etc\/init.d\/nginx stop\"<\/pre>\n<p>You\u2019ll need to pass Monit a command to start and stop your process. It should automatically restart if the process goes down, with no additional config, but if you want to restart based on resource usage, you\u2019ll have to specify that yourself with a few <code>if<\/code>\u00a0statements:<\/p>\n<pre>check process nginx with pidfile \/var\/run\/nginx.pid&#13;\n    start program = \"\/etc\/init.d\/nginx start\"&#13;\n    stop program = \"\/etc\/init.d\/nginx stop\"&#13;\n    if totalmem &gt; 600 MB for 5 cycles then restart&#13;\n    if cpu &gt; 90% for 5 cycles then restart&#13;\n    if 3 restarts within 5 cycles then timeout<\/pre>\n<p>Monit will send out alerts whenever something restarts, so you\u2019ll be notified when something is wrong.<\/p>\n<p>Once you\u2019re done with the config, reload the monit daemon with:<\/p>\n<pre>monit reload<\/pre>\n<p>And start monitoring with:<\/p>\n<pre>monit start all<\/pre>\n<p>This should start the web interface as well, which you can use to verify that Monit is working properly, usually running on port 2812.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1236\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/07\/b3c3d755.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This is the basic config to get Monit running out of the box, but Monit has a lot more syntax. You can <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/mmonit.com\/monit\/documentation\/monit.html\">read the full manual<\/a> on everything Monit can do to learn more.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Configuring_Alerts\"><\/span>Configuring Alerts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Monit can be configured to send email alerts whenever major issues occur, or when processes are restarted. You\u2019ll need to add configuration for your mail server:<\/p>\n<pre>set mailserver smtp.gmail.com&#13;\n  port 587&#13;\n  username \"yourserver@gmail.com\"&#13;\n  password \"secret\"&#13;\n  with timeout 60 seconds&#13;\n&#13;\nset eventqueue&#13;\n  basedir \/var\/lib\/monit\/events&#13;\n  slots 100<\/pre>\n<p>The first block defines the mail server to use. The easiest method would be to just use <code>smtp.gmail.com<\/code>\u00a0with a Gmail account, though you could install Postfix on your server to run locally, or use an enterprise email service.<\/p>\n<p>The second line tells Monit to queue alerts, if the mail server isn\u2019t reachable for whatever reason.<\/p>\n<p>Obviously, you\u2019ll also need to set the email address it sends to:<\/p>\n<pre>set alert youremail@gmail.com<\/pre>\n<p>Additionally, since Monit has the ability to run <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>s with the <code>exec<\/code>\u00a0command, you can set up custom alerts however you\u2019d like. For example, you could set up a script to send you Slack notifications, store it as <code>\/usr\/local\/bin\/slack-webhook.sh<\/code>, and run it whenever nginx changes PID or is restarted by Monit:<\/p>\n<pre>check process nginx with pidfile \/var\/run\/nginx.pid&#13;\n  start program = \"\/etc\/init.d\/nginx start\"&#13;\n  stop program = \"\/etc\/init.d\/nginx stop\"&#13;\n  if changed pid then exec \"\/bin\/bash -c 'PROCESS=Nginx \/usr\/local\/bin\/slack-webhook.sh'\"&#13;\n  if 1 restart within 1 cycle then exec \"\/bin\/bash -c 'PROCESS=Nginx \/usr\/local\/bin\/slack-webhook.sh'\"<\/pre>\n<p>Note that all paths must be fully qualified, including <code>\/bin\/bash<\/code>\u00a0and paths to executables in your local <code>bin<\/code>. And after you\u2019re done modifying the config files, you\u2019ll need to reload Monit again.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/1230\/getting-started-with-monit-for-server-monitoring\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Getting Started with Monit for Server Monitoring \u2013 CloudSavvy IT&#8221; Monit is a server monitoring program that can check the processes running on your system to ensure that they\u2019re always online and operating within reasonable CPU and memory limits. It also comes with a web interface to monitor your system. What Does Monit Do? Essentially,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":291778,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/07\/6a91c397.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-291777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/291777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=291777"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/291777\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/291778"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=291777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=291777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=291777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}