{"id":25,"date":"2009-06-27T12:32:01","date_gmt":"2009-06-27T02:32:01","guid":{"rendered":"http:\/\/www.teamburns.com\/blog\/?p=25"},"modified":"2009-12-21T15:43:23","modified_gmt":"2009-12-21T05:43:23","slug":"cpu-monitoring-script","status":"publish","type":"post","link":"https:\/\/www.teamburns.com\/blog\/cpu-monitoring-script\/","title":{"rendered":"CPU Monitoring Script"},"content":{"rendered":"<p>Have you ever had a rogue process chew up all the CPU on your Linux server for a days and nobody notice?<\/p>\n<p>This happens to me quite a lot so I decided to write a bash script that looks for these processes and send an alert email.<\/p>\n<pre><code>#!\/bin\/bash\r\n\r\n#\r\n# cpumon - monitors CPU usage and sends an alert email if limit is exceeded\r\n#\r\n# 2009 - martin [at] teamburns [dot] com\r\n#\r\n\r\nhost=`hostname`\r\nfile=\"\/tmp\/cpumon.txt\"\r\n\r\nrm $file > \/dev\/null 2>&1\r\n\r\nfunction sendEmail() {\r\n   subject=\"High CPU on $host\"\r\n   \/usr\/bin\/mail -s \"$subject\" user@domain.com < $file\r\n}\r\n\r\nwhile read a b c\r\ndo\r\n   pid=\"$a\"\r\n   cmd=\"$b\"\r\n   cpu_percentage=\"$c\"\r\n\r\n   if [[ -z \"$cpu_percentage\" ]]\r\n   then\r\n      echo \"process $pid $cmd is using less than zero percent of the cpu!\"\r\n      continue\r\n   else\r\n      cpu_percentage_integer=$(echo \"$cpu_percentage\"|sed 's\/^\\([^\\.]*\\)\\..*$\/\\1\/')\r\n   fi\r\n\r\n   if [[ $cpu_percentage_integer -gt 10 ]]\r\n   then\r\n      echo \"$pid $cmd is using $cpu_percentage_integer percent of our CPU\" >> $file\r\n   fi\r\ndone <<< \"`ps --no-heading -eo pid,comm,pcpu`\"\r\n\r\nif [[ -f $file ]]\r\nthen\r\n   sendEmail\r\nfi\r\n<\/code><\/pre>\n<p>This script can be scheduled to run periodically by placing an entry in the crontab file. Make sure use replace <b>username<\/b> with a valid name:<\/p>\n<pre><code>*\/30 *  * * *   username    \/home\/username\/cpumon &gt;\/dev\/null 2&gt;&amp;1\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever had a rogue process chew up all the CPU on your Linux server for a days and nobody notice? This happens to me quite a lot so I decided to write a bash script that looks for these processes and send an alert email. #!\/bin\/bash # # cpumon &#8211; monitors CPU usage &hellip; <a href=\"https:\/\/www.teamburns.com\/blog\/cpu-monitoring-script\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;CPU Monitoring Script&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[8,33,9],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-linux","tag-cpu","tag-linux","tag-monitoring"],"_links":{"self":[{"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":9,"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions\/59"}],"wp:attachment":[{"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.teamburns.com\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}