{"id":564,"date":"2017-07-27T22:36:23","date_gmt":"2017-07-28T02:36:23","guid":{"rendered":"http:\/\/jasonralph.org\/?p=564"},"modified":"2017-07-27T22:36:23","modified_gmt":"2017-07-28T02:36:23","slug":"nagios-python-plugin-check-if-file-is-stale","status":"publish","type":"post","link":"https:\/\/jasonralph.org\/?p=564","title":{"rendered":"Nagios Python Plugin Check If File Is Stale"},"content":{"rendered":"<p>Wrote this simple plugin to check if a log file was stale on a server using nagios and nrpe.  This plugin checks multiple files with the app. naming convention. <\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n#!\/usr\/bin\/env python\r\n\r\n__author__ = \"Jason Ralph\"\r\n\r\n\"\"\" IMPORTS \"\"\"\r\nimport datetime\r\nimport time\r\nimport re\r\nimport sys\r\nimport os\r\nimport optparse\r\nimport os.path\r\nimport glob\r\n\r\n\"\"\" NAGIOS API RETURN CODES \"\"\"\r\nnag_ret_dict = {'NagOk': 0,\r\n                'NagWarn': 1,\r\n                'NagCrit': 2,\r\n                'NagUnkown': 3}\r\n\r\ndef main():\r\n    for file in glob.glob('\/var\/log\/httpd\/app.*'):\r\n        now = datetime.datetime.now()\r\n        then = datetime.datetime.fromtimestamp(os.path.getmtime(file))\r\n        tdelta = now - then\r\n        if not file:\r\n            print(\"OK: Waiting for file\")\r\n            sys.exit(nag_ret_dict['NagOk'])\r\n        elif tdelta > datetime.timedelta(days=1):\r\n            print(\"CRIT: %s: Is greater then %s\") % (file, tdelta)\r\n            sys.exit(nag_ret_dict['NagCrit'])\r\n        else:\r\n            print(\"OK: NOW: %s - THEN: %s - DELTA: %s\" ) % (now, then, tdelta)\r\n            sys.exit(nag_ret_dict['NagOk'])\r\n\r\nif __name__ == \"__main__\":\r\n    main()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Wrote this simple plugin to check if a log file was stale on a server using nagios and nrpe. This plugin checks multiple files with the app. naming convention. #!\/usr\/bin\/env python __author__ = &#8220;Jason Ralph&#8221; &#8220;&#8221;&#8221; IMPORTS &#8220;&#8221;&#8221; import datetime import time import re import sys import os import optparse import os.path import glob &#8220;&#8221;&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-564","post","type-post","status-publish","format-standard","hentry","category-general-code"],"_links":{"self":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=564"}],"version-history":[{"count":1,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/564\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/564\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}