{"id":40,"date":"2009-01-11T08:08:01","date_gmt":"2009-01-11T08:08:01","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=40"},"modified":"2015-03-23T10:25:44","modified_gmt":"2015-03-23T10:25:44","slug":"simple-shell-script-to-monitoring-disk-space-on-a-linux-machine","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/simple-shell-script-to-monitoring-disk-space-on-a-linux-machine","title":{"rendered":"Simple Shell Script to Monitoring Disk Space on a Linux Machine"},"content":{"rendered":"<p>This article explain a way to get a mail as soon as\u00a0the disk\u00a0usage reaches to its critical level to avoid issues later. To set a simple monitor on Linux \/ Unix, I have two simple scripts:<\/p>\n<ul>\n<li>DSAlert.sh :\u00a0Shell script\u00a0for retrieving\u00a0disk space\u00a0percentages and put in to a cron job.<\/li>\n<li>DiskSpace_Alert.pl : Perl script for sending an email.<\/li>\n<\/ul>\n<p>Following is\u00a0the disk\u00a0monitor\u00a0shell script\u00a0which will execute the perl file for sending alert email.<\/p>\n<p><code> #DSAlert.sh<br \/>\n#!\/bin\/sh<br \/>\n#Retrive disk space info<br \/>\ndf=`df -Pl\u00a0 | grep \"^\/dev\" | awk '{print $5, $6}' | sed \"s\/%\/\/\"`<\/code><br \/>\n#Reference: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Df_(Unix)\">df<\/a><br \/>\necho &#8220;$df&#8221; | while read percent fs<br \/>\ndo<br \/>\n#If Disk Usage increases above 90% send an email.<br \/>\nif [ $percent -ge 90 ] ; then<br \/>\n#Calling a perl file with parameters for sending an email.<br \/>\n`\/opt\/lampp\/bin\/perl DiskSpace_Alert.pl $fs is $percent percent full`<br \/>\nfi<br \/>\ndone<\/p>\n<p><strong>Perl Script for sending an email:<\/strong><\/p>\n<p># DiskSpace_Alert.pl<br \/>\n#!perl<br \/>\nuse MIME::Lite;<\/p>\n<p>#Reference : <a href=\"http:\/\/search.cpan.org\/~rjbs\/MIME-Lite-3.023\/lib\/MIME\/Lite.pm\" target=\"_blank\">Mime-Lite<\/a><\/p>\n<p># set up email<br \/>\n$to = &#8220;my-mail-id\\@domain.com&#8221;;<br \/>\n$from = &#8220;Diskmonitor\\@ServerName.com&#8221;;<br \/>\n$subject = &#8220;Disk_Alert&#8221;;<br \/>\n$message = &#8220;Disk Space issue.\\nActions Required:\\n&#8221;.&#8221;@ARGV&#8221;;<br \/>\n# send email<br \/>\nemail($to, $from, $subject, $message, $file);<\/p>\n<p># email function<br \/>\nsub email<br \/>\n{<br \/>\n# get incoming parameters<br \/>\nlocal ($to, $from, $subject, $message, $file) = @_;<br \/>\n# create a new message<br \/>\n$msg = MIME::Lite-&gt;new(<br \/>\nFrom =&gt; $from,<br \/>\nTo =&gt; $to,<br \/>\nSubject =&gt; $subject,<br \/>\nData =&gt; $message);<br \/>\n# send the email<\/p>\n<p>MIME::Lite-&gt;send(&#8216;smtp&#8217;, &#8216;localhost&#8217;, Timeout =&gt; 60);<br \/>\n$msg-&gt;send();<br \/>\n}<br \/>\n<strong>Setting up a<\/strong><strong> <\/strong><strong>crontab<\/strong><strong> <\/strong><strong>for monitoring:<\/strong><\/p>\n<p><code>Crontab \u2013e<br \/>\n* * * * * sh\u00a0 full\/path\/to\/DSAlert.sh<br \/>\n<\/code><\/p>\n<p>And we&#8217;re done with the Linux\/Unix Disk Monitoring alerts.<\/p>\n<p>Instructions for setting up timing for crontab:<\/p>\n<p><code> +----------------&gt; minute (0 - 59)<br \/>\n|\u00a0 +-------------&gt; hour (0 - 23)<br \/>\n|\u00a0 |\u00a0 +----------&gt; day of month (1 - 31)<br \/>\n|\u00a0 |\u00a0 |\u00a0 +-------&gt; month (1 - 12)<br \/>\n|\u00a0 |\u00a0 |\u00a0 |\u00a0 +----&gt; day of week (0 - 6) (Sunday=0 or 7)<br \/>\n|\u00a0 |\u00a0 |\u00a0 |\u00a0 |<br \/>\n*\u00a0 *\u00a0 *\u00a0 *\u00a0 * <\/code><\/p>\n","protected":false},"excerpt":{"rendered":"This article explain a way to get a mail as soon as\u00a0the disk\u00a0usage reaches to its critical level to avoid issues later. To set a simple monitor on Linux \/&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[59,83,105,426],"class_list":{"0":"post-40","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-technical","7":"tag-linux","8":"tag-perl","9":"tag-shell-script","10":"tag-technical"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":1,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":2171,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/2171"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}