{"id":853,"date":"2015-03-15T09:57:53","date_gmt":"2015-03-15T16:57:53","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=853"},"modified":"2015-03-15T09:57:53","modified_gmt":"2015-03-15T16:57:53","slug":"check-logfiles-for-recent-entries-only","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/check-logfiles-for-recent-entries-only\/","title":{"rendered":"Check Logfiles For Recent Entries Only"},"content":{"rendered":"
Frequently I have a cron job to check for specific entries in log files but want to avoid being notified of something already checked. For example I want my 10 minute cron job to only look for most recent 10 minute entries.<\/p>\n
Here is what I did in python.<\/p>\n
\r\nfrom datetime import datetime, timedelta\r\n\r\n## Get time right now. ie cron job execution\r\n#now = datetime(2015,3,15,8,55,00)\r\nnow = datetime.now()\r\n\r\n## How long back to check. Making it 11 mins because cron runs every 10 mins\r\ncheckBack = 11\r\n\r\nlines = []\r\n\r\nprint "log entries newer than " + now.strftime('%b %d %H:%M:%S') + " minus " + str(checkBack) + " minutes"\r\n\r\nwith open('\/var\/log\/syslog', 'r') as f:\r\n for line in f:\r\n ## Linux syslog format like this:\r\n ## Mar 15 08:50:01 EP45-DS3L postfix\/sendmail[6492]: fatal\r\n ## Brain dead log has no year. So this hack will not work close to year ticking over\r\n myDate = str(now.year) + " " + line[:15]\r\n\r\n ## What about "Mar 1" having double space vs "Mar 15". That will break strptime %d.\r\n ## zero pad string position 4 to make %d work?\r\n if myDate[3] == " ":\r\n myDate = myDate.replace(myDate[3],"0")\r\n\r\n lt = datetime.strptime(myDate,'%Y %b %d %H:%M:%S')\r\n diff = now - lt\r\n if diff.days <= 0:\r\n if lt > now - timedelta(minutes=checkBack):\r\n # print myDate + " --- diff: " + str(diff)\r\n lines.append(line)\r\n\r\nif lines:\r\n # message = '\\n'.join(lines)\r\n # do some grepping for my specific errors here..\r\n # send message per mail...\r\n<\/pre>\nJust for reference here is an older test where no year is used. This is doing a string compare but I have not tested this one good enough. Most likely it will fail when month ticks over Apr will not be bigger than Mar. Also what about midnight 23:59 > 00:00?<\/p>\n
\r\nfrom datetime import datetime, timedelta\r\nnow = datetime.now()\r\nlookback = timedelta(minutes=5)\r\n\r\n## Linux syslog format "Mar 15 07:30:10 ..."\r\n## Probably need to zero pad string position 4 to make %d work?\r\noldest = (now - lookback).strftime('%b %d %H:%M:%S')\r\n\r\nwith open('\/var\/log\/syslog', 'r') as f:\r\n for line in f:\r\n if line[:15] > oldest:\r\n print "entry: " + line[:15] + " --- " + line[16:50]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Frequently I have a cron job to check for specific entries in log files but want to avoid being notified<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56,13],"tags":[],"class_list":["post-853","post","type-post","status-publish","format-standard","hentry","category-logging","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=853"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/853\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}