Forum Discussion
Use time range value in kusto query to calculate % uptime
Hi, are you asking about System Up time (from Servers, VMs, desktops) - you can get that if you collect the below Perf counter (for Windows devices), an example would be:
Perf | where ObjectName == "System" and CounterName == "System Up Time" | extend UpTime = CounterValue * 1s | summarize arg_max(TimeGenerated, *) by Computer | project Computer, UpTime, TimeGenerated | sort by Computer asc | project Computer, UpTime, TimeGenerated
There is also the example query (when you open a new Log Analytics Query Tab)
// Availability rate // Calculate the availability rate of each connected computer Heartbeat // bin_at is used to set the time grain to 1 hour, starting exactly 24 hours ago | summarize heartbeatPerHour = count() by bin_at(TimeGenerated, 1h, ago(24h)), Computer | extend availablePerHour = iff(heartbeatPerHour > 0, true, false) | summarize totalAvailableHours = countif(availablePerHour == true) by Computer | extend availabilityRate = totalAvailableHours*100.0/24
Time and Date is explained: https://docs.microsoft.com/en-us/azure/azure-monitor/log-query/datetime-operations
CliveWatson Not exactly. What I am trying to do is to find the time range selected from portal and use it inside the log analytics query.
- CliveWatsonApr 01, 2019Former Employee
abhijitchaudhari I don't think you can get the last Time Range that was set in the portal. All you can do is re-create it in your query with TimeGenerated filters.
If you don't specify a time range in a query, then the one selected in the current portal window is used by default.
Are you trying to check to see what default was used. to display it in some way?
You could check the start / end data and workout the interval from that?
Event | summarize min_time = min(TimeGenerated), max_time = max(TimeGenerated) | extend my_duration = max_time - min_time // days / hrs/ min / seconds // do other things... | project "All done " , my_duration
- bfd_jamesJun 26, 2019Copper Contributor
CliveWatson - This is the workaround that I ended up doing as well, but I really hate it from a code cleanliness perspective, as it seems like hacky to basically min/max a dataset of thousands/millions of rows just to get a begin end.
In my scenario, I had a Kusto query that had been written against an Application Insights workspace, that included a moving average/series gap filling function for some analysis we were working on internally for request information (trying to flatten out 1-off spikes in request duration skewing graphs). While the query worked in the analytics portal, once pinned to the dashboard, the graph showed no information.
I opened a ticket with Azure Support, but as I opened the ticket, I found/realized that the reason the widget wasn't showing information is that internally the query was failing (found on the network tab of the browser) due to a difference in windowing/dates from the portal vs those that had been defined in the query.
I've since modified the query to utilize min/max on the timestamp information, but it'd definitely be nice to have some way of accessing this information via query, similar to how log analytics has the injectable query/parameter information - ${LikeThis}, or just as a function @query_windowBegin().
Anyways - I didn't see a request for the feature yet, but not sure if I was using the write search terms. Are you aware of any open/outstanding feature requests?
Thanks!
- abhijitchaudhariApr 04, 2019Copper Contributor
But if I do not get any call for first n minutes and last m minutes , I will not able able calculate the % correctly
more details here: https://stackoverflow.com/questions/55379172/find-the-start-and-end-time-or-time-span-of-the-kusto-query-is-running-on-azur
- CliveWatsonApr 04, 2019Former Employee
abhijitchaudhari Just a thought, but if this is something that is running 24hrs, could you use the https://docs.microsoft.com/en-us/azure/kusto/query/startofdayfunction operator to get a % up time for a 24hr period - i.e I was up for 10% of the 24hr day.
You may consider submitting a user Voice request: https://feedback.azure.com/forums/267889-log-analytics for your feature to retrieve the portal time span.