{"id":388,"date":"2020-10-19T13:11:19","date_gmt":"2020-10-19T03:11:19","guid":{"rendered":"http:\/\/www.mrmarkyoung.com\/oracle\/?p=388"},"modified":"2020-10-19T13:11:19","modified_gmt":"2020-10-19T03:11:19","slug":"python-to-read-oracle-alert-log","status":"publish","type":"post","link":"http:\/\/www.mrmarkyoung.com\/oracle\/2020\/10\/19\/python-to-read-oracle-alert-log\/","title":{"rendered":"Python to read Oracle alert.log"},"content":{"rendered":"\n<p>I&#8217;m used to working in a UNIX environment and when I was asked to build a database and manage on Windows, I was faced with some new challenges. One of them is searching through the Alert log and without standard UNIX grep I wanted to write something that could perform the same function. Also show the log from a timestamp. Here it goes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import datetime\r\nimport time\r\nimport traceback\r\nimport io\r\nimport sys\r\n\r\ndef printer(*args):\r\nreturn args\r\n\r\nKeyWordList = &#91;'*']\r\n  \r\nif len(sys.argv) == 1:\r\n    SkipMins = 360\r\nelif len(sys.argv) == 2:\r\n    SkipMins = int(sys.argv&#91;1])\r\nelif len(sys.argv) >= 3:\r\n    SkipMins = int(sys.argv&#91;1])\r\n    KeyWordList = printer(sys.argv&#91;2])     \r\n\r\nprint('Searching for',KeyWordList)\r\n       \r\nOutputList =&#91;]\r\nFoundDate = \"\"\r\n\r\nCurrentDate = datetime.datetime.now()\r\nSearchDate = datetime.datetime.now().strftime(\"%Y-%m-%d\")\r\n\r\n\r\nAlertLog=r'E:\\App\\oraservice\\diag\\rdbms\\pcad9401\\pcad9401\\trace\\alert_pcad9401.log'\r\nSkipDelta=datetime.timedelta(minutes=SkipMins)\r\n\r\ntry:\r\n    with io.open(AlertLog,mode='r') as f:\r\n        for line in f:\r\n            line=f.readline()\r\n            if line&#91;0:10] == SearchDate:\r\n                EventDate = line&#91;:-7]\r\n                EventDate =  datetime.datetime.strptime(EventDate.rstrip('\\n'),'%Y-%m-%dT%H:%M:%S.%f')\r\n                FoundDate = True\r\n            elif FoundDate == True:\r\n                if CurrentDate-SkipDelta &lt; EventDate:\r\n                    if '*' not in KeyWordList:\r\n                        for w in KeyWordList:\r\n                            if w in line:\r\n                                OutputList.append(&#91;EventDate,line.rstrip('\\n')])\r\n                    else:\r\n                        OutputList.append(&#91;EventDate,line.rstrip('\\n')])\r\nexcept:\r\n    print(traceback.format_exc())\r\n                   \r\nfor o in OutputList:\r\n    print('&#91;%s] %s' % (o&#91;0],o&#91;1]))     \r\nprint('Found',len(outputList),'records')\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m used to working in a UNIX environment and when I was asked to build a database and manage on Windows, I was faced with some new challenges. One of them is searching through the Alert log and without standard UNIX grep I wanted to write something that could perform the same function. Also show &#8230; <a title=\"Python to read Oracle alert.log\" class=\"read-more\" href=\"http:\/\/www.mrmarkyoung.com\/oracle\/2020\/10\/19\/python-to-read-oracle-alert-log\/\" aria-label=\"Read more about Python to read Oracle alert.log\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,30],"tags":[64,31],"class_list":["post-388","post","type-post","status-publish","format-standard","hentry","category-oracle-database","category-python","tag-oracle","tag-python"],"_links":{"self":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/comments?post=388"}],"version-history":[{"count":2,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/388\/revisions"}],"predecessor-version":[{"id":390,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/388\/revisions\/390"}],"wp:attachment":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/media?parent=388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/categories?post=388"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/tags?post=388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}