{"id":927,"date":"2016-01-04T06:29:56","date_gmt":"2016-01-04T14:29:56","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=927"},"modified":"2016-01-04T06:31:45","modified_gmt":"2016-01-04T14:31:45","slug":"check-logfiles-only-a-few-minutes-back","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/check-logfiles-only-a-few-minutes-back\/","title":{"rendered":"Check Logfiles Only a Few Minutes Back"},"content":{"rendered":"

This is an update post. Previously I had a post here: http:\/\/blog.ls-al.com\/check-logfiles-for-recent-entries-only\/<\/p>\n

The code has been problematic around when a new year starts because of the lack of a year in the log entries. I updated the code a little bit to account for the year ticking over. I may still need to come up with a better way but below seem to work ok.<\/p>\n

\r\n#!\/usr\/bin\/python\r\n#\r\n\r\n#: Script Name  : checkLogs.py\r\n#: Version      : 0.0.1.1\r\n#: Description  : Check messages for last x minutes.  Used in conjunction with checkLogs.sh and a cron schedule\r\n\r\nfrom datetime import datetime, timedelta\r\n\r\n#suppressPhrases = ['ssd','offline']\r\nsuppressPhrases = []\r\n\r\n#now = datetime(2015,3,17,7,28,00)\t\t\t\t\t\t## Get time right now. ie cron job execution\r\nnow = datetime.now()\r\nday_of_year = datetime.now().timetuple().tm_yday   \t\t## Used for special case when year ticks over. Older log entries should be one year older.\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\n#print "log entries newer than " + now.strftime('%b %d %H:%M:%S') + " minus " + str(checkBack) + " minutes"\r\n\r\nwith open('\/var\/adm\/messages', 'r') as f:\r\n    for line in f:\r\n      myDate = str(now.year) + " " + line[:15]          ## Solaris syslog format like this: Mar 11 12:47:23 so need to add year\r\n\r\n      if day_of_year >= 1 and day_of_year <= 31:        ## Brain dead log has no year so special case during January\r\n        if not "Jan" in myDate:         #2015 Dec 30\r\n          myDate = str(now.year -1) + " " + line[:15]\r\n\r\n      if myDate[3] == " ":\t\t\t\t\t\t\t\t## What about "Mar  1" having double space vs "Mar 15". That will break strptime %d.\r\n        myDate = myDate.replace(myDate[3],"0")\t\t\t## zero pad string position 4 to make %d work?\r\n\r\n      #print "myDate: %s and now: %s" % (myDate,now)\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          match = False\r\n          for s in suppressPhrases:\r\n            i = line.find(s)\r\n            if i > -1:\r\n              match = True\r\n          if not match:\r\n            lines.append(line)\r\n\r\nif lines:\r\n    message = '\\n'.join(lines)\r\n    print message\t\t\t\t\t\t\t\t\t\t    # do some grepping for my specific errors here.. send message per mail...\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

This is an update post. Previously I had a post here: http:\/\/blog.ls-al.com\/check-logfiles-for-recent-entries-only\/ The code has been problematic around when a<\/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,14],"tags":[],"class_list":["post-927","post","type-post","status-publish","format-standard","hentry","category-logging","category-python","category-solaris"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/927","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=927"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}