forked from arcanecode/KQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2-demo-80-percent.csl
417 lines (304 loc) · 11.4 KB
/
m2-demo-80-percent.csl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//------------------------------------------------------------------------------
// Kusto Query Language (KQL) From Scratch
// Module 2 - 80% of the Operators You'll Ever Use
//
// The demos in this module serve as a very basic introduction to the KQL
// language within the Azure Log Analytics environment.
//
// Copyright (c) 2018. Microsoft, Pluralsight, Robert C. Cain.
// All rights reserved. This code may be used in part within your own
// applications.
//
// This code may NOT be redistributed in it's entirely without permission
// of one of it's copyright holders.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Search
//------------------------------------------------------------------------------
// Will search all columns in the Perf table for the value "Memory"
Perf
| search "Memory"
// Search is not case sensitive by default
Perf
| search "memory"
// Although you can make it sensitive using a switch
Perf
| search kind=case_sensitive "memory"
// Without a table, search goes arcross all tables in the current database
// Warning, this takes a looooooooooong time to run, and it is highly likely
// it will time out on a large database, like the Log Analytics Demo database.
search "Memory"
// A better choice is to limit the search to specific tables
search in (Perf, Event, Alert) "Contoso"
// The syntax......: Perf | search "Contoso"
// is the same as..: search in (Perf) "Contoso"
// Within a table, you can search a specific column for an exact value
Perf
| search CounterName=="Available MBytes"
// Can also search for the value anywhere in the text in the specified column
Perf
| search CounterName:"MBytes"
// This searched for a column whose value exactly matched the word Memory
Perf
| search "Memory"
// Can also search across all columns using wildcards
Perf
| search "*Bytes*" // Anywhere in any column's text
Perf
| search * startswith "Bytes" // Begins with Bytes then any text after it
Perf
| search * endswith "Bytes" // Ends with Bytes
Perf
| search "Free*bytes" // Begins with Free, ends with bytes, anything in between
// Searches can be combined logically
Perf
| search "Free*bytes" and ("C:" or "D:")
// Search also supports regular expressions
Perf
| search InstanceName matches regex "[A-Z]:"
//------------------------------------------------------------------------------
// Where
//------------------------------------------------------------------------------
// Similar to search, where limits the result set. Rather than looking across
// columns for values, where limits based on conditions
Perf
| where TimeGenerated >= ago(1h)
// ago allows for relative date ranges. Ago says "start right now, then go back
// in time N quantity. These can be expressed using the following abbreviations
// d - days
// h - hours
// m - minutes
// s - seconds
// ms - milliseconds
// microsecond - microseconds
// Can build up the where by adding to it logically
Perf
| where TimeGenerated >= ago(1h)
and CounterName == "Bytes Received/sec"
// Multiple ands are allowed
Perf
| where TimeGenerated >= ago(1h)
and CounterName == "Bytes Received/sec"
and CounterValue > 0
// OR logic is allowed too!
Perf
| where TimeGenerated >= ago(1h)
and (CounterName == "Bytes Received/sec"
or
CounterName == "% Processor Time"
)
// Combining and's and or's
Perf
| where TimeGenerated >= ago(1h)
and (CounterName == "Bytes Received/sec"
or
CounterName == "% Processor Time"
)
and CounterValue > 0
// Stackable where operators
Perf
| where TimeGenerated >= ago(1h)
| where (CounterName == "Bytes Received/sec"
or
CounterName == "% Processor Time"
)
| where CounterValue > 0
// You can simulate search using where. Here it searches all columns
// in the input for the phrase Bytes somewhere in a column's value
Perf
| where * has "Bytes"
// Similar to search, you can search positional matches
Perf
| where * hasprefix "Bytes" // At the start
Perf
| where * hassuffix "Bytes" // At the end
Perf
| where * contains "Bytes" // contains and has behave the same
// Where supports regex as well
Perf
| where InstanceName matches regex "[A-Z]:"
//------------------------------------------------------------------------------
// Take
//------------------------------------------------------------------------------
// Take is used to grab a random number of rows from the input data
Perf
| take 10
// There is no guarantee to which rows are returned, and the exact same
// query run a second time may result in a different set of rows
// (or it may be the same, depends on various factors)
Perf
| take 10
// Take can be combined with other language operators
Perf
| where TimeGenerated >= ago(1h)
and CounterName == "Bytes Received/sec"
and CounterValue > 0
| take 5
// Limit is a synonym for take
Perf
| limit 10
//------------------------------------------------------------------------------
// Count
//------------------------------------------------------------------------------
// Returns the number of rows in the input dataset
Perf
| count
// Can also use with other filters
Perf
| where TimeGenerated >= ago(1h)
and CounterName == "Bytes Received/sec"
and CounterValue > 0
| count
//------------------------------------------------------------------------------
// Summarize
//------------------------------------------------------------------------------
// Summariaze allows you count number of rows by column using the count()
// aggregation function
Perf
| summarize count() by CounterName
// Can break down by multiple columns
Perf
| summarize count() by ObjectName, CounterName
// You can rename the output column for count
Perf
| summarize PerfCount=count()
by ObjectName, CounterName
// With Summarize, you can use other aggregation functions
Perf
| where CounterName == "% Free Space"
| summarize NumberOfEntries=count()
, AverageFreeSpace=avg(CounterValue)
by CounterName
// Bin allows you to summarize into logical groups, like days
Perf
| summarize NumberOfEntries=count()
by bin(TimeGenerated, 1d)
// Can bin at multiple levels
Perf
| summarize NumberOfEntries=count()
by CounterName
, bin(TimeGenerated, 1d)
// Order in the query determines the order in the output
Perf
| summarize NumberOfEntries=count()
by bin(TimeGenerated, 1d)
, CounterName
// You can bin by values other than dates.
// Here we see number of entries for each level at
// % Free Space
Perf
| where CounterName == "% Free Space"
| summarize NumberOfRowsAtThisPercentLevel=count()
by bin(CounterValue,10)
//------------------------------------------------------------------------------
// Extend
//------------------------------------------------------------------------------
// Extend creates a calculated column and adds to the result set
Perf
| where CounterName == "Free Megabytes"
| extend FreeGB = CounterValue / 1000
// Can extend multiple columns at the same time
Perf
| where CounterName == "Free Megabytes"
| extend FreeGB = CounterValue / 1000
, FreeKB = CounterValue * 1000
// Could use to repeat a column
Perf
| where CounterName == "Free Megabytes"
| extend FreeGB = CounterValue / 1000
, FreeMB = CounterValue
, FreeKB = CounterValue * 1000
// Can also use with strcat to create new string columns
Perf
| extend ObjectCounter = strcat(ObjectName, " - ", CounterName)
//------------------------------------------------------------------------------
// Project
//------------------------------------------------------------------------------
// Project allows you to select a subset of columns
Perf
| project ObjectName
, CounterName
, InstanceName
, CounterValue
, TimeGenerated
// Combine Project with Extend
Perf
| where CounterName == "Free Megabytes"
| project ObjectName
, CounterName
, InstanceName
, CounterValue
, TimeGenerated
| extend FreeGB = CounterValue / 1000
, FreeMB = CounterValue
, FreeKB = CounterValue * 1000
// You can use extend prior to project to calculate on a field
// you don't want in the final output (Counter Value omitted)
Perf
| where CounterName == "Free Megabytes"
| extend FreeGB = CounterValue / 1000
, FreeMB = CounterValue
, FreeKB = CounterValue * 1000
| project ObjectName
, CounterName
, InstanceName
, TimeGenerated
, FreeGB
, FreeKB
, FreeMB
// Project can simulate the extend
Perf
| where CounterName == "Free Megabytes"
| project ObjectName
, CounterName
, InstanceName
, TimeGenerated
, FreeGB = CounterValue / 1000
, FreeMB = CounterValue
, FreeKB = CounterValue * 1000
// There is a variant called project-away. It will project all except the
// columns listed
Perf
| where TimeGenerated > ago(1h)
| project-away TenantId
, SourceSystem
, CounterPath
, MG
// If you only want to rename a column, then another variant of project is
// project-rename. It will rename the specified column but then pass the
// rest of the columns through
Perf
| where TimeGenerated > ago(1h)
| project-rename myRenamedComputer = Computer
//------------------------------------------------------------------------------
// Distinct
//------------------------------------------------------------------------------
// Returns a list of deduplicated values for columns from the input dataset
Perf
| distinct ObjectName, CounterName
// Distinct can be used to limit a result set
// Get a list of all sources that had an error event
Event
| where EventLevelName == "Error"
| distinct Source
//------------------------------------------------------------------------------
// Top
//------------------------------------------------------------------------------
// Top returns the first N rows of the dataset when the dataset is sorted by
// the "by" clause.
Perf
| top 20 by TimeGenerated desc
// When we combine what we've learned to produce something useful.
// Here we're going to get a list of computers that are low on disk space
Perf
| where CounterName == "Free Megabytes" // Get the Free Megabytes
and TimeGenerated >= ago(1h) // ...within the last hour
| project Computer // For each return the Computer Name
, TimeGenerated // ...and when the counter was generated
, CounterName // ...and the Counter Name
, FreeMegabytes=CounterValue // ...and rename the counter value
| distinct Computer // Now weed out duplicate rows as
, TimeGenerated
, CounterName // ...the perf table will have
, FreeMegabytes // ...multiple entries during the day
| top 25 by FreeMegabytes asc // Filter to most critical ones