{"id":580,"date":"2010-06-21T01:45:48","date_gmt":"2010-06-21T06:45:48","guid":{"rendered":"http:\/\/yourLinuxGuy.com\/?p=580"},"modified":"2010-06-21T07:37:00","modified_gmt":"2010-06-21T12:37:00","slug":"ping-per-second-notify-script","status":"publish","type":"post","link":"https:\/\/yourLinuxGuy.com\/?p=580","title":{"rendered":"Ping-per-second \/ Notify Script"},"content":{"rendered":"<p>As a sysadmin (probably like you), sometimes I need to whip up special-purpose mini-scripts to do simple monitoring tasks that I don&#8217;t want to bother our main monitoring system with&#8230; This is one of them&#8230;<\/p>\n<p>I needed to monitor a network device (an appliance, actually) constantly and rapidly, to see if the interface was flapping up \/ down.\u00a0 So, we settled on one ping per second, with a one-time email notification when it drops, and a one-time notification when it returns.<\/p>\n<p>Below is that script;\u00a0 all you have to do is copy the contents into a text file (watch out for line wrapping), change the &#8220;<code>PINGDEST<\/code>&#8221; and &#8220;<code>EMAILADDRESSES<\/code>&#8221; variables, flag it as executable (<code>chmod 700<\/code> &lt;scriptname&gt;) , and you should be good to go!<\/p>\n<pre><code>#!\/bin\/bash\r\n\r\nPINGDEST=\"192.168.1.12\"\r\nEMAILADDRESSES=\"jpavlov@yourLinuxGuy.com,questions@yourLinuxGuy.com\"\r\nBITFLAG=0\r\nSENTMAIL=0\r\n\r\nwhile true\r\ndo\r\n   \/bin\/ping -q -c1 -W1 $PINGDEST &gt; \/dev\/null 2&gt;&amp;1\r\n   PINGRESULT=\"$?\"\r\n   if [ \"$PINGRESULT\" != \"0\" ]; then\r\n      BITFLAG=1\r\n   else\r\n      BITFLAG=0\r\n      sleep 1\r\n   fi\r\n   if [ \"$BITFLAG\" == \"1\" ] &amp;&amp; [ \"$SENTMAIL\" == \"0\" ]; then\r\n      echo \"Ping to $PINGDEST FAILED! Generated by $0 on $HOSTNAME\" |mailx -s \"Ping failure to $PINGDEST\"  $EMAILADDRESSES\r\n      SENTMAIL=1\r\n   elif [ \"$BITFLAG\" == \"0\" ] &amp;&amp; [ \"$SENTMAIL\" == \"1\" ]; then\r\n      echo \"Ping to $PINGDEST resumed. Generated by $0 on $HOSTNAME\" |mailx -s \"Ping responding again on $PINGDEST\"  $EMAILADDRESSES\r\n      SENTMAIL=0\r\n   fi\r\ndone<\/code><\/pre>\n<p>A goofy, single-purpose script, to be sure; but you never know, maybe it will help someone&#8230;<\/p>\n<p>8)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a sysadmin (probably like you), sometimes I need to whip up special-purpose mini-scripts to do simple monitoring tasks that I don&#8217;t want to bother our main monitoring system with&#8230; This is one of them&#8230; I needed to monitor a&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/yourLinuxGuy.com\/?p=580\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11,47],"tags":[],"class_list":["post-580","post","type-post","status-publish","format-standard","hentry","category-intermediate","category-linuxgeneral"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnjn1-9m","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=580"}],"version-history":[{"count":5,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/580\/revisions"}],"predecessor-version":[{"id":587,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/580\/revisions\/587"}],"wp:attachment":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}