{"id":709,"date":"2017-03-21T20:10:11","date_gmt":"2017-03-21T18:10:11","guid":{"rendered":"https:\/\/helia.ee\/koolitus\/?page_id=709"},"modified":"2017-03-21T20:10:11","modified_gmt":"2017-03-21T18:10:11","slug":"script-millega-monitoorida-kataloogi-suurust","status":"publish","type":"page","link":"https:\/\/helia.ee\/koolitus\/?page_id=709","title":{"rendered":"Script millega monitoorida kataloogi suurust"},"content":{"rendered":"<p>If you would like to <strong>monitor [disk] space usage<\/strong> and receive emails when a certain threshold is passed, you can refer to this excellent example from <a href=\"https:\/\/www.linux.com\/community\/blogs\/133-general-linux\/748863-linux-shell-script-to-monitor-space-usage-and-send-email\">Linix.com<\/a>.<\/p>\n<p><strong>Let&#8217;s begin with creating an empty shell script file:<\/strong><\/p>\n<pre class=\"code-pre \"><code>$ nano monitor_space_usage.sh\r\n<\/code><\/pre>\n<p><strong>Copy and paste the contents of this self explanatory script:<\/strong><\/p>\n<pre class=\"code-pre \"><code>#!\/bin\/bash\r\n\r\nLIMIT='80'\r\n#Here we declare variable LIMIT with max of used spave\r\n\r\nDIR='\/var'\r\n#Here we declare variable DIR with name of directory\r\n\r\nMAILTO='monitor@gmail.com'\r\n#Here we declare variable MAILTO with email address\r\n\r\nSUBJECT=\"$DIR disk usage\"\r\n#Here we declare variable SUBJECT with subject of email\r\n\r\nMAILX='mailx'\r\n#Here we declare variable MAILX with mailx command that will send email\r\n\r\nwhich $MAILX &gt; \/dev\/null 2&gt;&amp;1\r\n#Here we check if mailx command exist\r\n\r\nif ! [ $? -eq 0 ]\r\n#We check exit status of previous command if exit status not 0 this mean that mailx is not installed on system\r\nthen\r\n          echo \"Please install $MAILX\"\r\n#Here we warn user that mailx not installed\r\n          exit 1\r\n#Here we will exit from script\r\nfi\r\n\r\ncd $DIR\r\n#To check real used size, we need to navigate to folder\r\n\r\nUSED=`df . | awk '{print $5}' | sed -ne 2p | cut -d\"%\" -f1`    \r\n#This line will get used space of partition where we currently, this will use df command, and get used space in %, and after cut % from value.\r\n\r\nif [ $USED -gt $LIMIT ]\r\n#If used space is bigger than LIMIT\r\n\r\nthen\r\n      du -sh ${DIR}\/* | $MAILX -s \"$SUBJECT\" \"$MAILTO\"\r\n#This will print space usage by each directory inside directory $DIR, and after MAILX will send email with SUBJECT to MAILTO\r\nfi\r\n<\/code><\/pre>\n<p>After making sure that you have modified it to match your needs (and set your e-mail address as the recipient by modifying MAILTO variable), you can save it by pressing &#8220;CTRL+X&#8221; and confirming with &#8220;Y&#8221;.<\/p>\n<p>Set again the file as <em>executable<\/em> and you have your second Linux system monitoring tool ready.<\/p>\n<p><strong>To give the file execution permission, run the following:<\/strong><\/p>\n<pre class=\"code-pre \"><code>$ chmod +x monitor_space_usage.sh<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you would like to monitor [disk] space usage and receive emails when a certain threshold is passed, you can refer to this excellent example from Linix.com. Let&#8217;s begin with creating an empty shell script file: $ nano monitor_space_usage.sh Copy and paste the contents of this self explanatory script: #!\/bin\/bash LIMIT=&#8217;80&#8217; #Here we declare variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":707,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-709","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=709"}],"version-history":[{"count":1,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/709\/revisions"}],"predecessor-version":[{"id":710,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/709\/revisions\/710"}],"up":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/707"}],"wp:attachment":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}