{"id":964,"date":"2010-07-10T10:07:30","date_gmt":"2010-07-10T14:07:30","guid":{"rendered":"http:\/\/linuxhostingsupport.net\/blog\/?p=964"},"modified":"2013-03-20T08:43:17","modified_gmt":"2013-03-20T12:43:17","slug":"shell-script-to-monitor-load-average-on-a-linux-server","status":"publish","type":"post","link":"https:\/\/linuxhostingsupport.net\/blog\/shell-script-to-monitor-load-average-on-a-linux-server","title":{"rendered":"Shell Script to Monitor Load Average on a Linux server"},"content":{"rendered":"<p>Load Average on a server reflects the current state of the server. Higher the load average, poorer is the server performance hence it is a necessity to monitor the load average on the server. <strong><\/strong><\/p>\n<p>The following shell script monitors the load average on the Linux server and inform the server administrator with the load average and the process that are running if the load average is greater than the defined threshold.<\/p>\n<p>Create a file, say, <strong>\/root\/monit_loadaverage.sh<\/strong> and paste the following script in it:<\/p>\n<blockquote>\n<pre>############### START OF THE SCRIPT ###############\r\n\r\n#!\/bin\/bash\r\n\r\n# Define Variables\r\nCUR_TIME=`date +\"%A %b %e %r\"`\r\nHOSTNAME=`hostname`\r\n\r\n# Retrieve the load average of the past 1 minute\r\nLoad_AVG=`uptime | cut -d'l' -f2 | awk '{print $3}' | cut -d. -f1`\r\nLOAD_CUR=`uptime | cut -d'l' -f2 | awk '{print $3 \" \" $4 \" \" $5}'\\\r\n | sed 's\/,\/\/'`\r\n\r\n# Define Threshold. This value will be compared with the current\r\n# load average. Set the value as per your wish.\r\nLIMIT=5\r\n\r\n# Compare the current load average with the Threshold value and\r\n# email the server administrator if the current load average\r\n# is greater.\r\nif [ $Load_AVG -gt $LIMIT ]\r\nthen\r\n\r\n#Save the current running processes in a file\r\n\/bin\/ps auxf &gt;&gt; \/root\/ps_output\r\n\r\n# Save the other values in a file\r\necho \"Current Time :: $CUR_TIME\" &gt;&gt; \/tmp\/monitload.txt\r\necho \"Current Load Average :: $LOAD_CUR\" &gt;&gt; \/tmp\/monitload.txt\r\necho \"The list of current processes is attached\\\r\n\u00a0with the email for your reference.\" &gt;&gt; \/tmp\/monitload.txt\r\necho \"Please Check... ASAP.\"\u00a0 &gt;&gt; \/tmp\/monitload.txt\r\n\r\n# Send an email to the administrator of the server\r\n\/usr\/bin\/mutt -s \"ALERT!!! High 1 minute load average\\\r\n\u00a0on '$HOSTNAME'\" -a \/root\/ps_output youremail@youremail.com &lt;\\\r\n\/tmp\/monitload.txt\r\n\r\nfi\r\n\r\n# Remove the temporary log files\r\n\/bin\/rm -f \/tmp\/monitload.txt\r\n\/bin\/rm -f \/root\/ps_output\r\n\r\n############### END OF THE SCRIPT ###############<\/pre>\n<\/blockquote>\n<p>Make sure you replace the Treshold (LIMIT) value and your email address with your own values in the script.<\/p>\n<p>Now, schedule a cronjob to execute the script on per minute basis. Edit the cronjob file<\/p>\n<blockquote>\n<pre><span style=\"color: #0000ff;\"># crontab -e<\/span><\/pre>\n<\/blockquote>\n<p>and place the following cronjob at the end of the file<\/p>\n<blockquote>\n<pre><span style=\"color: #0000ff;\">* * * * * \/bin\/sh \/root\/monit_loadaverage.sh<\/span><\/pre>\n<\/blockquote>\n<p>restart the crond service<\/p>\n<blockquote>\n<pre><span style=\"color: #0000ff;\"># service crond restart<\/span><\/pre>\n<\/blockquote>\n<p>In order to use &#8220;mutt&#8221; to send emails, you need to install the mutt package on the server. It allows you to send emails with attachments.<\/p>\n<blockquote>\n<pre><span style=\"color: #0000ff;\"># yum install mutt<\/span><\/pre>\n<\/blockquote>\n<p><strong>Note: <\/strong>Please place a comment if you receive any error message while executing this script OR you need some modifications in the script.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Load Average on a server reflects the current state of the server. Higher the load average, poorer is the server performance hence it is a necessity to monitor the load average on the server.<br \/>\nThe following shell script monitors the load average on the Linux server and inform the server administrator with the load average and the process that are running if the load average is greater than the defined threshold.<br \/>\nCreate a file, say, \/root\/monit_loadaverage.sh and paste the following script in it:<\/p>\n<p>############### START OF THE SCRIPT ###############<\/p>\n<p>#!\/bin\/bash<\/p>\n<p># Define [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106],"tags":[937,940,934,941,943,938,988,939,936,942,935],"_links":{"self":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts\/964"}],"collection":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/comments?post=964"}],"version-history":[{"count":23,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts\/964\/revisions"}],"predecessor-version":[{"id":973,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts\/964\/revisions\/973"}],"wp:attachment":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/media?parent=964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/categories?post=964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/tags?post=964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}