{"id":1187,"date":"2009-12-21T00:45:40","date_gmt":"2009-12-20T22:45:40","guid":{"rendered":"http:\/\/contoso.se\/blog\/?p=1187"},"modified":"2010-09-01T12:16:43","modified_gmt":"2010-09-01T10:16:43","slug":"reading-a-logfile-with-a-3-state-monitor","status":"publish","type":"post","link":"http:\/\/contoso.se\/blog\/?p=1187","title":{"rendered":"Reading a logfile with a 3 state monitor"},"content":{"rendered":"<p>If you build a monitor to monitor a logfile, Operations Manager will remember which line it was reading last. Operations Manager will only look for new keyword below that line, it will not read the whole file again. I did a lot of tests with logfile monitoring, read more about them <a href=\"http:\/\/contoso.se\/blog\/?p=281\">here<\/a>. If you need to get Operations Manager to read the whole logfile each time, you can use a scrip like this:<\/p>\n<blockquote><p>\u00c2\u00a0<br \/>\nConst ForReading = 1<br \/>\nSet oAPI = CreateObject(&#8220;MOM.ScriptAPI&#8221;)<br \/>\nSet oBag = oAPI.CreatePropertyBag()<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<br \/>\nSet objTextFile = objFSO.OpenTextFile _<br \/>\n(&#8220;c:\\temp\\file.txt&#8221;, ForReading)<\/p>\n<p>Do Until objTextFile.AtEndOfStream<br \/>\nstrText = objTextFile.ReadLine<\/p>\n<p>varWarPos = Instr(strText, &#8220;Warning&#8221;)<br \/>\nIf varWarPos &gt; 0 Then<br \/>\nvarStatus = &#8220;Warning&#8221;<br \/>\nvarLine = strText<br \/>\nEnd If<\/p>\n<p>varCriPos = Instr(strText, &#8220;Critical&#8221;)<br \/>\nIf varCriPos &gt; 0 Then<br \/>\nCall oBag.AddValue(&#8220;Line&#8221;, strText)<br \/>\nCall oBag.AddValue(&#8220;Status&#8221;,&#8221;critical&#8221;)<br \/>\nCall oAPI.Return(oBag)<br \/>\nWscript.Quit(0)<br \/>\nEnd If<\/p>\n<p>Loop<br \/>\nobjTextFile.Close<\/p>\n<p>If varStatus = &#8220;Warning&#8221; Then<br \/>\nCall oBag.AddValue(&#8220;Line&#8221;, varLine)<br \/>\nCall oBag.AddValue(&#8220;Status&#8221;,&#8221;warning&#8221;)<br \/>\nCall oAPI.Return(oBag)<br \/>\nWscript.Quit(0)<br \/>\nElse<br \/>\nCall oBag.AddValue(&#8220;Status&#8221;,&#8221;ok&#8221;)<br \/>\nCall oAPI.Return(oBag)<br \/>\nEnd If<\/p><\/blockquote>\n<p>This script will read the file (c:\\temp\\file.txt) line by line. The script is looking for two keywords in the logfile, &#8220;Warning&#8221; and &#8220;Critical&#8221;. If there is a &#8220;Critical&#8221; in a line the script will send back a bag with status=Critical and the script will stop. If there is a &#8220;Warning&#8221; in the line the script will continue, as there might be a &#8220;critical&#8221; somewhere too. If there was only &#8220;Warning&#8221; the script will send back status=Warning. If there was no &#8220;Warning&#8221; or &#8220;Critical&#8221; the script will send back status=ok.<\/p>\n<p>If there is a &#8220;Warning&#8221; or &#8220;Critical&#8221; the script will also put that line into a bag, and send it back to Operations Manager. You will see this line in the alert description. To use this script, you can configure a monitor like this:<\/p>\n<ul>\n<li>Create a new monitor of type Scripting\/Generic\/Timed Script Three State Monitor. Input a suitable name and target. More about targeting <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=125048\">here<\/a>.<\/li>\n<li>Schedule\n<ul>\n<li>Configure your script to run every X minute. The script will rad the whole logfile each time<\/li>\n<\/ul>\n<\/li>\n<li>Script\n<ul>\n<li>Filename and Timeout, for example CheckFile.vbs and 2 minutes<\/li>\n<li>Paste the script in the script field<\/li>\n<\/ul>\n<\/li>\n<li>Unhealthy expression\n<ul>\n<li>Property[@Name=&#8217;Status&#8217;]<\/li>\n<li>Equals<\/li>\n<li>Warning<\/li>\n<\/ul>\n<\/li>\n<li>Degraded expression\n<ul>\n<li>Property[@Name=\u00e2\u20ac\u2122Status\u00e2\u20ac\u2122]<\/li>\n<li>Equals<\/li>\n<li>Critical<\/li>\n<\/ul>\n<\/li>\n<li>Healthy expression\n<ul>\n<li>Property[@Name=\u00e2\u20ac\u2122Status\u00e2\u20ac\u2122]<\/li>\n<li>Equals<\/li>\n<li>ok<\/li>\n<\/ul>\n<\/li>\n<li>Alerting\n<ul>\n<li>Check Generate alerts for this monitor<\/li>\n<li>Generate an alert when: The monitor is in a critical or warning health state<\/li>\n<li>Check Automatically resolve the alert when the monitor returns to a healthy state<\/li>\n<li>Alert name: Input an alert name<\/li>\n<li>Alert Description\n<ul>\n<li>State $Data\/Context\/Property[@Name=\u00e2\u20ac\u2122Status\u00e2\u20ac\u2122]$<\/li>\n<li>Line $Data\/Context\/Property[@Name=\u00e2\u20ac\u2122Line\u00e2\u20ac\u2122]$<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Summary: This monitor, including the script, will read a logfile and generate alerts based on keywords. In will read the whole logfile each time and look for two different keywords.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you build a monitor to monitor a logfile, Operations Manager will remember which line it was reading last. Operations Manager will only look for new keyword below that line, it will not read the whole file again. I did a lot of tests with logfile monitoring, read more about them here. If you need &hellip; <a href=\"http:\/\/contoso.se\/blog\/?p=1187\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[13,19],"tags":[],"_links":{"self":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1187"}],"collection":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1187"}],"version-history":[{"count":11,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1187\/revisions"}],"predecessor-version":[{"id":1747,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1187\/revisions\/1747"}],"wp:attachment":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}