SOLVED

Loop through the KQL query result

%3CLINGO-SUB%20id%3D%22lingo-sub-2492264%22%20slang%3D%22en-US%22%3ELoop%20through%20the%20KQL%20query%20result%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2492264%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20trigger%20an%20alert%20if%20windows%20service%20is%20stopped%20in%20one%20of%20the%20node.%3C%2FP%3E%3CP%3EThere%20are%202%20nodes%20and%20service%20will%20be%20running%20in%20both%20nodes%20or%20at%20one%20node%20.%3C%2FP%3E%3CP%3EOnly%20If%20service%20is%20not%20running%20in%20both%20the%20node%20then%20alert%20need%20to%20be%20triggered.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20using%20the%20below%20query%20and%20its%20not%20right.%20because%20alert%20will%20be%20triggered%20if%20the%20service%20is%20stopped%20in%20one%20of%20the%20node%20as%20the%20query%20fetches%20the%20latest%20record%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Elet%20status%20%3D%3CBR%20%2F%3EEvent%3CBR%20%2F%3E%7C%20where%20TimeGenerated%20%26gt%3B%20ago%20(1d)%3CBR%20%2F%3E%7C%20where%20EventLog%20%3D%3D%20'System'%20and%20EventID%20%3D%3D%207036%20and%20Source%20%3D%3D%20'Service%20Control%20Manager'%20and%20RenderedDescription%20has%20%22Apache%20tomcat%22%3CBR%20%2F%3E%7C%20parse%20kind%3Drelaxed%20EventData%20with%20*%20'%3CDATA%20name%3D%22%26quot%3Bparam1%26quot%3B%22%3E'%20Windows_Service_Name%20'%3C%2FDATA%3E%3CDATA%20name%3D%22%26quot%3Bparam2%26quot%3B%22%3E'%20Windows_Service_State%20'%3C%2FDATA%3E'%20*%3CBR%20%2F%3E%7C%20summarize%20(TimeGenerated%2C%20winstatus)%20%3D%20arg_max(TimeGenerated%2C%20Windows_Service_State)%20by%20Windows_Service_Name%2C%20Computer%3B%3CBR%20%2F%3Estatus%3CBR%20%2F%3E%7C%20where%20winstatus%20!%3D%20'running'%3CBR%20%2F%3E%7C%20project%20winstatus%2C%20Windows_Service_Name%2C%20Computer%2C%20TimeGenerated%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20above%20query%20works%20well%20if%20there's%20only%20one%20VM%20but%20for%20multiple%20VM's%20it%20wont%20work.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20to%20count%20the%20result%20if%20service%20is%20stopped%20in%20both%20Vms%20and%20alert%20trigger%20if%20count%20value%20is%202%20but%20then%20again%20in%20Event%20logs%20sometimes%20there%20will%20be%20only%20one%20result%20(if%20there's%20no%20change%20in%20state%20of%20event%20within%20the%20time%20frame%20that%20used%20in%20query)%20so%20this%20method%20will%20not%20work%20either.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Esample%20result%20for%3C%2FP%3E%3CP%3EEvent%3CBR%20%2F%3E%7C%20where%20TimeGenerated%20%26gt%3B%20ago%20(1d)%3CBR%20%2F%3E%7C%20where%20EventLog%20%3D%3D%20'System'%20and%20EventID%20%3D%3D%207036%20and%20Source%20%3D%3D%20'Service%20Control%20Manager'%20and%20RenderedDescription%20has%20%22Apache%20tomcat%22%3CBR%20%2F%3E%7C%20parse%20kind%3Drelaxed%20EventData%20with%20*%20'%3CDATA%20name%3D%22%26quot%3Bparam1%26quot%3B%22%3E'%20Windows_Service_Name%20'%3C%2FDATA%3E%3CDATA%20name%3D%22%26quot%3Bparam2%26quot%3B%22%3E'%20Windows_Service_State%20'%3C%2FDATA%3E'%20*%3CBR%20%2F%3E%7C%20summarize%20(TimeGenerated%2C%20winstatus)%20%3D%20arg_max(TimeGenerated%2C%20Windows_Service_State)%20by%20Windows_Service_Name%2C%20Computer%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%3E6%2F28%2F2021%2C%202%3A01%3A55.930%20AM%3C%2FTD%3E%3CTD%3EApache%20Tomcat%208.5.58%3C%2FTD%3E%3CTD%3EapacheNode1%3C%2FTD%3E%3CTD%3Erunning%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E6%2F28%2F2021%2C%201%3A02%3A54.257%20AM%3C%2FTD%3E%3CTD%3EApache%20Tomcat%208.5.58%3C%2FTD%3E%3CTD%3EapacheNode2%3C%2FTD%3E%3CTD%3Erunning%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20to%20loop%20%2F%20check%20if%20all%20the%20rows%20that%20returned%20for%20winstatus%20are%20!%3D%20'running'.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3ERacheal%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2492386%22%20slang%3D%22en-US%22%3ERe%3A%20Loop%20through%20the%20KQL%20query%20result%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2492386%22%20slang%3D%22en-US%22%3Emaybe%20add%20a%20last%20line%20of%20%3CBR%20%2F%3E%3CBR%20%2F%3E%7C%20summarize%20anyif(winstatus%20!%3D%22stopped%22%2C%20true)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2492750%22%20slang%3D%22en-US%22%3ERe%3A%20Loop%20through%20the%20KQL%20query%20result%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2492750%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F239477%22%20target%3D%22_blank%22%3E%40Clive%20Watson%3C%2FA%3E%26nbsp%3BThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20command%20is%20not%20clear%20to%20me%26nbsp%3B%3C%2FP%3E%3CP%3Ebecause%20I%20used%2C%3CBR%20%2F%3E%7C%20summarize%20anyif(winstatus%20!%3D%22stopped%22%2C%20true)%20--%26gt%3B%20returns%20false%20%2F%2F%20.%20As%20per%20the%20query%20i%20think%20if%20status%20is%20not%20equal%20to%20stopped%20in%20any%20of%20the%20VM%20then%20returns%20true%20else%20returns%20false%20.%20this%20returns%20false%20because%20service%20is%20stopped%20in%20one%20of%20the%20VM%3C%2FP%3E%3CP%3Ealso%20checked%3CBR%20%2F%3E%7C%20summarize%20anyif(winstatus%20!%3D%22running%22%2C%20true)%20-%26gt%3B%20returns%20true%2F%2F%20.%20As%20per%20the%20query%20i%20think%20if%20status%20is%20not%20equal%20to%20running%20in%20any%20of%20the%20VM%20then%20returns%20true%20else%20returns%20false%20.%20this%20returns%20true%20even%20though%20the%20service%20is%20running%20in%20one%20of%20the%20VM%3C%2FP%3E%3CP%3EHere's%20the%20VM%20service%20status%3C%2FP%3E%3CP%3E6%2F28%2F2021%2C%2010%3A00%3A08.173%20AM%20stopped%20apacheNode1%3CBR%20%2F%3E6%2F28%2F2021%2C%2010%3A07%3A53.470%20AM%20running%20apacheNode2%3C%2FP%3E%3CP%3EModified%20query%3C%2FP%3E%3CP%3Elet%20status%20%3D%3CBR%20%2F%3EEvent%3CBR%20%2F%3E%7C%20where%20TimeGenerated%20%26gt%3B%20ago%20(1d)%3CBR%20%2F%3E%7C%20where%20EventLog%20%3D%3D%20'System'%20and%20EventID%20%3D%3D%207036%20and%20Source%20%3D%3D%20'Service%20Control%20Manager'%20and%20RenderedDescription%20has%20%22Apache%22%3CBR%20%2F%3E%7C%20parse%20kind%3Drelaxed%20EventData%20with%20*%20'%3CDATA%20name%3D%22%26quot%3Bparam1%26quot%3B%22%3E'%20Windows_Service_Name%20'%3C%2FDATA%3E%3CDATA%20name%3D%22%26quot%3Bparam2%26quot%3B%22%3E'%20Windows_Service_State%20'%3C%2FDATA%3E'%20*%3CBR%20%2F%3E%7C%20summarize%20(TimeGenerated%2C%20winstatus)%20%3D%20arg_max(TimeGenerated%2C%20Windows_Service_State)%20by%20Windows_Service_Name%2C%20Computer%3CBR%20%2F%3E%7C%20summarize%20status%3D%20anyif(winstatus%20!%3D%20%22stopped%22%2C%20true)%3B%3CBR%20%2F%3Estatus%3CBR%20%2F%3E%7C%20where%20status%20%3D%3D%20'false'%3CBR%20%2F%3E%7C%20project%20status%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2495990%22%20slang%3D%22en-US%22%3ERe%3A%20Loop%20through%20the%20KQL%20query%20result%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2495990%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1020473%22%20target%3D%22_blank%22%3E%40Racheal2k%3C%2FA%3E%26nbsp%3BI%20think%20you%20tried%20this%20before%3F%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3Elet%20status%20%3D%0AEvent%0A%7C%20where%20TimeGenerated%20%26gt%3B%20ago%20(1d)%0A%7C%20where%20EventLog%20%3D%3D%20'System'%20and%20EventID%20%3D%3D%207036%20and%20Source%20%3D%3D%20'Service%20Control%20Manager'%20%20and%20RenderedDescription%20has%20'WMI%20Performance%20Adapter'%20%2F%2F%22Apache%20tomcat%22%0A%7C%20parse%20kind%3Drelaxed%20EventData%20with%20*%20'%3CDATA%20name%3D%22param1%22%3E'%20Windows_Service_Name%20'%3C%2FDATA%3E%3CDATA%20name%3D%22param2%22%3E'%20Windows_Service_State%20'%3C%2FDATA%3E'%20*%0A%7C%20summarize%20count()%2C%20(TimeGenerated%2C%20winstatus)%20%3D%20arg_max(TimeGenerated%2C%20Windows_Service_State)%20by%20Windows_Service_Name%2C%20Computer%3B%0Astatus%0A%7C%20extend%20winstatus%20%3D%20iif(winstatus%20%3D%3D%20'running'%2C1%2C0)%0A%7C%20summarize%20sumif(winstatus%2C%20winstatus%20%26gt%3B%200)%2C%20ComputersOK%20%3D%20make_set_if(Computer%2C%20winstatus%20%26gt%3B%200)%2C%20ComputerNotOk%20%3D%20make_set_if(Computer%2C%20winstatus%20%3D%3D%200)%0A%7C%20extend%20ServiceStatus%20%3D%20iif(sumif_winstatus%20%26gt%3B%200%2C%20%22The%20service%20is%20running%22%2C%22%20The%20Service%20is%20not%20runnimg%22)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2496601%22%20slang%3D%22en-US%22%3ERe%3A%20Loop%20through%20the%20KQL%20query%20result%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2496601%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F239477%22%20target%3D%22_blank%22%3E%40Clive%20Watson%3C%2FA%3E%26nbsp%3B%2C%20Thanks%20and%20that%20worked.%3C%2FP%3E%3CP%3EI%20have%20tried%20until%26nbsp%3B%3C%2FP%3E%3CP%3Estatus%3CBR%20%2F%3E%7C%20extend%20winstatus%20%3D%20iif(winstatus%20%3D%3D%20'running'%2C1%2C0)%20but%20haven't%20tried%20Sumif%20command%20%3A)%3C%2Fimg%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreat%20work%20!%20thanks%20again%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3ERacheal%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi ,

 

I need to trigger an alert if windows service is stopped in one of the node.

There are 2 nodes and service will be running in both nodes or at one node .

Only If service is not running in both the node then alert need to be triggered.

 

I'm using the below query and its not right. because alert will be triggered if the service is stopped in one of the node as the query fetches the latest record

 

let status =
Event
| where TimeGenerated > ago (1d)
| where EventLog == 'System' and EventID == 7036 and Source == 'Service Control Manager' and RenderedDescription has "Apache tomcat"
| parse kind=relaxed EventData with * '<Data Name="param1">' Windows_Service_Name '</Data><Data Name="param2">' Windows_Service_State '</Data>' *
| summarize (TimeGenerated, winstatus) = arg_max(TimeGenerated, Windows_Service_State) by Windows_Service_Name, Computer;
status
| where winstatus != 'running'
| project winstatus, Windows_Service_Name, Computer, TimeGenerated

 

The above query works well if there's only one VM but for multiple VM's it wont work.

 

I tried to count the result if service is stopped in both Vms and alert trigger if count value is 2 but then again in Event logs sometimes there will be only one result (if there's no change in state of event within the time frame that used in query) so this method will not work either.

 

sample result for

Event
| where TimeGenerated > ago (1d)
| where EventLog == 'System' and EventID == 7036 and Source == 'Service Control Manager' and RenderedDescription has "Apache tomcat"
| parse kind=relaxed EventData with * '<Data Name="param1">' Windows_Service_Name '</Data><Data Name="param2">' Windows_Service_State '</Data>' *
| summarize (TimeGenerated, winstatus) = arg_max(TimeGenerated, Windows_Service_State) by Windows_Service_Name, Computer;

 

6/28/2021, 2:01:55.930 AMApache Tomcat 8.5.58apacheNode1running 
6/28/2021, 1:02:54.257 AMApache Tomcat 8.5.58apacheNode2running 

 

How to loop / check if all the rows that returned for winstatus are != 'running'. 

 

Regards,

Racheal

 

 

6 Replies
maybe add a last line of

| summarize anyif(winstatus !="stopped", true)

@Clive Watson Thanks

 

This command is not clear to me 

because I used,
| summarize anyif(winstatus !="stopped", true) --> returns false // . As per the query i think if status is not equal to stopped in any of the VM then returns true else returns false . this returns false because service is stopped in one of the VM

also checked
| summarize anyif(winstatus !="running", true) -> returns true// . As per the query i think if status is not equal to running in any of the VM then returns true else returns false . this returns true even though the service is running in one of the VM

Here's the VM service status

6/28/2021, 10:00:08.173 AM stopped apacheNode1
6/28/2021, 10:07:53.470 AM running apacheNode2

Modified query

let status =
Event
| where TimeGenerated > ago (1d)
| where EventLog == 'System' and EventID == 7036 and Source == 'Service Control Manager' and RenderedDescription has "Apache"
| parse kind=relaxed EventData with * '<Data Name="param1">' Windows_Service_Name '</Data><Data Name="param2">' Windows_Service_State '</Data>' *
| summarize (TimeGenerated, winstatus) = arg_max(TimeGenerated, Windows_Service_State) by Windows_Service_Name, Computer
| summarize status= anyif(winstatus != "stopped", true);
status
| where status == 'false'
| project status

HI ,

Noticed that same query sometimes returns true and sometimes returns False.
I think it returns the status from the last record in the result set.
best response confirmed by Racheal2k (Occasional Contributor)
Solution

@Racheal2k I think you tried this before? 

let status =
Event
| where TimeGenerated > ago (1d)
| where EventLog == 'System' and EventID == 7036 and Source == 'Service Control Manager'  and RenderedDescription has 'WMI Performance Adapter' //"Apache tomcat"
| parse kind=relaxed EventData with * '<Data Name="param1">' Windows_Service_Name '</Data><Data Name="param2">' Windows_Service_State '</Data>' *
| summarize count(), (TimeGenerated, winstatus) = arg_max(TimeGenerated, Windows_Service_State) by Windows_Service_Name, Computer;
status
| extend winstatus = iif(winstatus == 'running',1,0)
| summarize sumif(winstatus, winstatus > 0), ComputersOK = make_set_if(Computer, winstatus > 0), ComputerNotOk = make_set_if(Computer, winstatus == 0)
| extend ServiceStatus = iif(sumif_winstatus > 0, "The service is running"," The Service is not runnimg")

 

 

@Clive Watson , Thanks and that worked.

I have tried until 

status
| extend winstatus = iif(winstatus == 'running',1,0) but haven't tried Sumif command :) 

 

Great work ! thanks again

 

Regards,

Racheal

@Clive Watson ,

 

I'm using the below query to trigger alert . 

 

let status =
Event
| where TimeGenerated > ago(30d)
| where EventLog == 'System' and EventID == 7036 and Source == 'Service Control Manager' and RenderedDescription has "PowerCurve - Job Server"
| parse kind=relaxed EventData with * '<Data Name="param1">' Windows_Service_Name '</Data><Data Name="param2">' Windows_Service_State '</Data>' *
| summarize (TimeGenerated, winstatus) = arg_max(TimeGenerated, Windows_Service_State) by Windows_Service_Name, Computer;
status
| extend winstatus = iif(winstatus == 'running', 1, 0)
| summarize sumif(winstatus, winstatus > 0), ComputersOK = make_set_if(Computer, winstatus > 0), ComputerNotOk = make_set_if(Computer, winstatus == 0)
| extend ServiceStatus = iif(sumif_winstatus > 0, "The service is running"," The Service is not running")
| where sumif_winstatus == 0
| project sumif_winstatus, ComputerNotOk, ComputersOK

 

if no. of result is > 0 then an alert will be triggered.

 

Am facing a weird issue here , if the service is running in one of the VM this query returns null in log analytics logs window which is perfect.

 

But i also receive alert that service is stopped and When i click view 1 results from the alert mail i received 

Racheal2k_1-1625067042260.png

it returns status as 0 which means service is stopped

Racheal2k_2-1625067112798.png

 

but if i execute the query again by selecting it , it returns null.

 

Racheal2k_3-1625067377449.png

 

I don't understand this behavior from Azure. The same query gives different result through alert and when it executed from log analytics log page it gives different answer.

 

Could you help with explaining this?

Regards,

Racheal