-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample-queries.kql
More file actions
557 lines (465 loc) · 17.3 KB
/
sample-queries.kql
File metadata and controls
557 lines (465 loc) · 17.3 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// ============================================================================
// Azure File Storage Inventory Scanner - Sample KQL Queries
// ============================================================================
// These queries work with the FileInventory_CL custom table created by the
// Azure File Storage Inventory Scanner runbook.
// ============================================================================
// ----------------------------------------------------------------------------
// SECTION 1: OVERVIEW & DASHBOARD QUERIES
// ----------------------------------------------------------------------------
// 1.1 - Storage Overview by Account and Share
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
TotalFiles = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
AvgFileSizeMB = round(avg(FileSizeMB), 2),
OldestFile = min(LastModified),
NewestFile = max(LastModified)
by StorageAccount, FileShare
| order by TotalSizeGB desc
// 1.2 - Overall Storage Summary
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
TotalFiles = count(),
TotalStorageTB = round(sum(FileSizeGB) / 1024, 3),
UniqueStorageAccounts = dcount(StorageAccount),
UniqueFileShares = dcount(strcat(StorageAccount, "/", FileShare)),
AvgFileAgeInDays = round(avg(AgeInDays), 0),
OldestFileAge = max(AgeInDays)
// 1.3 - Scan Execution History
FileInventory_CL
| where TimeGenerated > ago(7d)
| summarize
FilesScanned = count(),
SizeScannedGB = round(sum(FileSizeGB), 2),
FirstRecord = min(TimeGenerated),
LastRecord = max(TimeGenerated)
by ExecutionId
| order by FirstRecord desc
// ----------------------------------------------------------------------------
// SECTION 2: FILE SIZE ANALYSIS
// ----------------------------------------------------------------------------
// 2.1 - Find Largest Files (Top 100)
FileInventory_CL
| where TimeGenerated > ago(24h)
| project StorageAccount, FileShare, FilePath, FileName, FileSizeGB, FileSizeMB, LastModified, AgeInDays, FileCategory
| order by FileSizeGB desc
| take 100
// 2.2 - Files Larger Than 1 GB
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileSizeGB >= 1
| summarize
Count = count(),
TotalSizeGB = round(sum(FileSizeGB), 2)
by StorageAccount, FileShare
| order by TotalSizeGB desc
// 2.3 - Storage Distribution by Size Bucket
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
Percentage = round(count() * 100.0 / toscalar(FileInventory_CL | where TimeGenerated > ago(24h) | count()), 2)
by SizeBucket
| order by case(
SizeBucket, "< 1 KB", 1,
SizeBucket, "1 KB - 1 MB", 2,
SizeBucket, "1 MB - 10 MB", 3,
SizeBucket, "10 MB - 100 MB", 4,
SizeBucket, "100 MB - 500 MB", 5,
SizeBucket, "500 MB - 1 GB", 6,
SizeBucket, "1 GB - 5 GB", 7,
SizeBucket, "5 GB - 10 GB", 8,
SizeBucket, "10+ GB", 9,
10) asc
// 2.4 - Size Distribution Chart (for visualization)
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize TotalSizeGB = sum(FileSizeGB) by SizeBucket
| render piechart with (title="Storage by Size Bucket")
// ----------------------------------------------------------------------------
// SECTION 3: FILE AGE & LIFECYCLE ANALYSIS
// ----------------------------------------------------------------------------
// 3.1 - Files by Age Bucket
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
AvgSizeMB = round(avg(FileSizeMB), 2)
by AgeBucket
| order by case(
AgeBucket, "0-7 days", 1,
AgeBucket, "8-30 days", 2,
AgeBucket, "31-90 days", 3,
AgeBucket, "91-180 days", 4,
AgeBucket, "181-365 days", 5,
AgeBucket, "1-2 years", 6,
AgeBucket, "2-5 years", 7,
AgeBucket, "5+ years", 8,
9) asc
// 3.2 - Stale Files (Not Modified in 2+ Years) - Archive Candidates
FileInventory_CL
| where TimeGenerated > ago(24h)
| where AgeInDays > 730
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2)
by StorageAccount, FileShare, FileCategory
| order by TotalSizeGB desc
// 3.3 - Files Not Modified in 5+ Years - Deletion Candidates
FileInventory_CL
| where TimeGenerated > ago(24h)
| where AgeInDays > 1825
| project StorageAccount, FileShare, FilePath, FileName, FileSizeGB, LastModified, AgeInDays
| order by AgeInDays desc
| take 500
// 3.4 - Recent Files (Modified in Last 7 Days)
FileInventory_CL
| where TimeGenerated > ago(24h)
| where AgeInDays <= 7
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2)
by StorageAccount, FileShare
| order by FileCount desc
// 3.5 - File Age Trend Over Time (comparing multiple scans)
FileInventory_CL
| where TimeGenerated > ago(30d)
| summarize
AvgAgeInDays = round(avg(AgeInDays), 0),
StaleFilesCount = countif(AgeInDays > 365)
by bin(TimeGenerated, 1d), StorageAccount
| render timechart with (title="Average File Age Trend")
// ----------------------------------------------------------------------------
// SECTION 4: FILE TYPE ANALYSIS
// ----------------------------------------------------------------------------
// 4.1 - Storage by File Category
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
AvgSizeMB = round(avg(FileSizeMB), 2)
by FileCategory
| order by TotalSizeGB desc
// 4.2 - File Category Distribution Chart
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize TotalSizeGB = sum(FileSizeGB) by FileCategory
| render piechart with (title="Storage by File Category")
// 4.3 - Top File Extensions by Count
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
AvgSizeMB = round(avg(FileSizeMB), 2)
by FileExtension
| order by FileCount desc
| take 20
// 4.4 - Top File Extensions by Storage
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2)
by FileExtension
| order by TotalSizeGB desc
| take 20
// 4.5 - Temporary/Junk Files Analysis
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileCategory == "Temporary" or FileExtension in (".tmp", ".temp", ".bak", ".swp", "~")
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2)
by StorageAccount, FileShare
| order by TotalSizeGB desc
// ----------------------------------------------------------------------------
// SECTION 5: DUPLICATE FILE DETECTION
// ----------------------------------------------------------------------------
// 5.1 - Find Duplicate Files (by Hash)
// Note: Requires hash computation to be enabled during scan
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileHash !in ("SKIPPED", "SKIPPED_TOO_LARGE", "ERROR", "SKIPPED_PERFORMANCE", "")
| summarize
DuplicateCount = count(),
TotalDuplicateSizeGB = round(sum(FileSizeGB), 2),
WastedSpaceGB = round(sum(FileSizeGB) - min(FileSizeGB), 2),
Locations = make_list(strcat(StorageAccount, "/", FileShare, "/", FilePath), 10)
by FileHash, FileName, FileSizeBytes
| where DuplicateCount > 1
| order by WastedSpaceGB desc
| take 100
// 5.2 - Duplicate Files Summary
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileHash !in ("SKIPPED", "SKIPPED_TOO_LARGE", "ERROR", "SKIPPED_PERFORMANCE", "")
| summarize Count = count() by FileHash, FileSizeBytes
| where Count > 1
| summarize
TotalDuplicateGroups = count(),
TotalDuplicateFiles = sum(Count),
EstimatedWastedFiles = sum(Count - 1)
// 5.3 - Potential Duplicates by Name and Size (when hash not available)
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
Count = count(),
Locations = make_list(strcat(StorageAccount, "/", FileShare), 5)
by FileName, FileSizeBytes
| where Count > 1
| order by FileSizeBytes desc
| take 100
// 5.4 - Duplicate Detection Effectiveness Report
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
TotalFiles = count(),
HashesComputed = countif(FileHash !in ("SKIPPED", "SKIPPED_TOO_LARGE", "ERROR", "SKIPPED_PERFORMANCE", "")),
HashesSkipped = countif(FileHash in ("SKIPPED", "SKIPPED_TOO_LARGE", "SKIPPED_PERFORMANCE")),
HashErrors = countif(FileHash == "ERROR")
| extend
HashCoveragePercent = round(HashesComputed * 100.0 / TotalFiles, 2)
// 5.5 - Find Duplicate Records by FileName, Size, and ScanTimestamp
// Identifies records with identical FileName, FileSizeBytes, and ScanTimestamp values
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
DuplicateCount = count(),
FilePaths = make_set(FilePath),
StorageAccounts = make_set(StorageAccount),
FileShares = make_set(FileShare)
by FileName, FileSizeBytes, ScanTimestamp
| where DuplicateCount > 1
| order by DuplicateCount desc, FileSizeBytes desc
// 5.6 - Duplicate Records Detail View
// Shows all individual duplicate records with their full paths
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize Count = count() by FileName, FileSizeBytes, ScanTimestamp
| where Count > 1
| join kind=inner (
FileInventory_CL
| where TimeGenerated > ago(24h)
) on FileName, FileSizeBytes, ScanTimestamp
| project-away FileName1, FileSizeBytes1, ScanTimestamp1, Count
| order by FileName, FileSizeBytes, ScanTimestamp
// ----------------------------------------------------------------------------
// SECTION 6: FOLDER ANALYSIS
// ----------------------------------------------------------------------------
// 6.1 - Top-Level Folder Analysis
FileInventory_CL
| where TimeGenerated > ago(24h)
| extend TopFolder = tostring(split(FilePath, "/")[0])
| where isnotempty(TopFolder)
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
AvgAgeInDays = round(avg(AgeInDays), 0)
by StorageAccount, FileShare, TopFolder
| order by TotalSizeGB desc
| take 50
// 6.2 - Deep Folder Paths (More than 5 levels deep)
FileInventory_CL
| where TimeGenerated > ago(24h)
| extend FolderDepth = array_length(split(FilePath, "/"))
| where FolderDepth > 5
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
MaxDepth = max(FolderDepth)
by StorageAccount, FileShare
| order by MaxDepth desc
// 6.3 - Folder Size Heatmap Data
FileInventory_CL
| where TimeGenerated > ago(24h)
| extend TopFolder = tostring(split(FilePath, "/")[0])
| summarize TotalSizeGB = round(sum(FileSizeGB), 2) by StorageAccount, FileShare, TopFolder
| where TotalSizeGB > 1
| order by TotalSizeGB desc
// ----------------------------------------------------------------------------
// SECTION 7: COST OPTIMIZATION OPPORTUNITIES
// ----------------------------------------------------------------------------
// 7.1 - Archive Tier Candidates (Old + Large files)
FileInventory_CL
| where TimeGenerated > ago(24h)
| where AgeInDays > 365 and FileSizeGB > 0.1
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
EstimatedMonthlySavingsUSD = round(sum(FileSizeGB) * 0.015, 2) // Rough estimate
by StorageAccount, FileShare
| order by TotalSizeGB desc
// 7.2 - Delete Candidates (Very old, small files with no activity)
FileInventory_CL
| where TimeGenerated > ago(24h)
| where AgeInDays > 1095 // 3 years
| where FileSizeMB < 1
| summarize
FileCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2)
by StorageAccount, FileShare, FileCategory
| order by FileCount desc
// 7.3 - Storage Optimization Summary
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
TotalFiles = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
// Files that could be archived (>1 year old)
ArchiveCandidateFiles = countif(AgeInDays > 365),
ArchiveCandidateSizeGB = round(sumif(FileSizeGB, AgeInDays > 365), 2),
// Files that could be deleted (>3 years old)
DeleteCandidateFiles = countif(AgeInDays > 1095),
DeleteCandidateSizeGB = round(sumif(FileSizeGB, AgeInDays > 1095), 2),
// Temporary files
TempFiles = countif(FileCategory == "Temporary"),
TempFilesSizeGB = round(sumif(FileSizeGB, FileCategory == "Temporary"), 2)
by StorageAccount
| extend
ArchiveOpportunityPercent = round(ArchiveCandidateSizeGB * 100.0 / TotalSizeGB, 2),
DeleteOpportunityPercent = round(DeleteCandidateSizeGB * 100.0 / TotalSizeGB, 2)
// ----------------------------------------------------------------------------
// SECTION 8: SECURITY & COMPLIANCE
// ----------------------------------------------------------------------------
// 8.1 - Executable Files Inventory
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileCategory == "Executables"
| project StorageAccount, FileShare, FilePath, FileName, FileSizeGB, LastModified, AgeInDays
| order by LastModified desc
// 8.2 - Files Modified Recently (Potential Changes)
FileInventory_CL
| where TimeGenerated > ago(24h)
| where AgeInDays <= 1
| summarize
RecentlyModifiedCount = count(),
TotalSizeGB = round(sum(FileSizeGB), 2)
by StorageAccount, FileShare, FileCategory
| order by RecentlyModifiedCount desc
// 8.3 - Database Files Audit
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileCategory == "Databases"
| project StorageAccount, FileShare, FilePath, FileName, FileSizeGB, LastModified, AgeInDays
| order by FileSizeGB desc
// 8.4 - Files with Unusual Extensions
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileExtension !in (
".txt", ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx",
".jpg", ".jpeg", ".png", ".gif", ".bmp",
".mp4", ".avi", ".mkv", ".mov",
".mp3", ".wav", ".m4a",
".zip", ".rar", ".7z", ".tar", ".gz",
".exe", ".dll", ".msi",
".json", ".xml", ".yaml", ".yml", ".csv",
".ps1", ".py", ".js", ".ts", ".cs", ".java"
)
| summarize Count = count() by FileExtension
| order by Count desc
| take 30
// ----------------------------------------------------------------------------
// SECTION 9: ALERTS & ANOMALIES
// ----------------------------------------------------------------------------
// 9.1 - Unusually Large Files (Statistical Outliers)
let avgSize = toscalar(
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize avg(FileSizeBytes)
);
let stdDev = toscalar(
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize stdev(FileSizeBytes)
);
FileInventory_CL
| where TimeGenerated > ago(24h)
| where FileSizeBytes > (avgSize + 3 * stdDev)
| project StorageAccount, FileShare, FilePath, FileName, FileSizeGB, LastModified
| order by FileSizeGB desc
// 9.2 - Storage Growth Detection (Compare Scans)
FileInventory_CL
| where TimeGenerated > ago(30d)
| summarize TotalSizeGB = sum(FileSizeGB), FileCount = count() by bin(TimeGenerated, 1d)
| order by TimeGenerated asc
| serialize
| extend
PrevSizeGB = prev(TotalSizeGB),
GrowthGB = TotalSizeGB - prev(TotalSizeGB),
GrowthPercent = round((TotalSizeGB - prev(TotalSizeGB)) * 100.0 / prev(TotalSizeGB), 2)
| where isnotnull(GrowthGB)
// 9.3 - New Files Detection (Files created in last scan)
FileInventory_CL
| where TimeGenerated > ago(24h)
| where Created > ago(7d)
| summarize
NewFilesCount = count(),
NewFilesSizeGB = round(sum(FileSizeGB), 2)
by StorageAccount, FileShare, FileCategory
| order by NewFilesCount desc
// ----------------------------------------------------------------------------
// SECTION 10: REPORTING QUERIES
// ----------------------------------------------------------------------------
// 10.1 - Executive Summary Report
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
TotalFiles = count(),
TotalStorageTB = round(sum(FileSizeGB) / 1024, 3),
UniqueShares = dcount(strcat(StorageAccount, FileShare)),
AvgFileAgeDays = round(avg(AgeInDays), 0),
FilesOlderThan1Year = countif(AgeInDays > 365),
StorageOlderThan1YearGB = round(sumif(FileSizeGB, AgeInDays > 365), 2),
LargeFilesOver1GB = countif(FileSizeGB >= 1),
LargeFilesStorageGB = round(sumif(FileSizeGB, FileSizeGB >= 1), 2)
// 10.2 - Monthly Scan Comparison
FileInventory_CL
| where TimeGenerated > ago(90d)
| summarize
TotalFiles = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
AvgAgeInDays = round(avg(AgeInDays), 0)
by format_datetime(TimeGenerated, "yyyy-MM"), StorageAccount
| order by format_datetime(TimeGenerated, "yyyy-MM") desc
// 10.3 - Share-Level Detail Report
FileInventory_CL
| where TimeGenerated > ago(24h)
| summarize
TotalFiles = count(),
TotalSizeGB = round(sum(FileSizeGB), 2),
AvgSizeMB = round(avg(FileSizeMB), 2),
MedianAgeInDays = percentile(AgeInDays, 50),
OldestFileDays = max(AgeInDays),
NewestFileDays = min(AgeInDays),
UniqueExtensions = dcount(FileExtension),
TopCategory = arg_max(count(), FileCategory)
by StorageAccount, FileShare
| order by TotalSizeGB desc
// 10.4 - Export-Ready Inventory (CSV format)
FileInventory_CL
| where TimeGenerated > ago(24h)
| project
ScanDate = format_datetime(TimeGenerated, "yyyy-MM-dd"),
StorageAccount,
FileShare,
FilePath,
FileName,
FileExtension,
FileSizeBytes,
FileSizeMB,
FileSizeGB,
LastModified = format_datetime(LastModified, "yyyy-MM-dd HH:mm:ss"),
Created = format_datetime(Created, "yyyy-MM-dd HH:mm:ss"),
AgeInDays,
FileCategory,
AgeBucket,
SizeBucket,
FileHash
| order by StorageAccount, FileShare, FilePath