-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowsUpdateCheck.cs
74 lines (60 loc) · 2.39 KB
/
WindowsUpdateCheck.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tether.Plugins;
using WUApiLib;
namespace Tether.WindowsUpdates
{
public class WindowsUpdateCheck : ILongRunningMetricProvider
{
private static ISearchResult CheckForUpdates(UpdateSession session)
{
ISearchResult uResult;
session = new UpdateSession();
var uSearcher = session.CreateUpdateSearcher();
uResult = uSearcher.Search("IsInstalled = 0 and Type='Software'");
return uResult;
}
public List<Metric> GetMetrics()
{
var session = new UpdateSession();
ISearchResult uResult;
var updateChecker = new AutomaticUpdatesClass();
var results = new List<Metric>();
results.Add(new Metric("windows.updates.enabled", updateChecker.ServiceEnabled ? 1 : 0));
if (!updateChecker.ServiceEnabled)
{
return results;
}
int totalUpdates = 0;
int criticalUpdates = 0;
int updatesNeedingReboot = 0;
uResult = CheckForUpdates(session);
if (uResult != null)
{
foreach (IUpdate5 uResultUpdate in uResult.Updates)
{
if (uResultUpdate == null)
{
continue;
}
if (!String.IsNullOrWhiteSpace(uResultUpdate.MsrcSeverity) && uResultUpdate.MsrcSeverity.Contains("Critical"))
{
criticalUpdates++;
}
totalUpdates++;
if (uResultUpdate.RebootRequired)
{
updatesNeedingReboot++;
}
}
}
results.Add(new Metric("windows.updates.count", totalUpdates, tags:new Dictionary<string, string>{{"severity","all"}}));
results.Add(new Metric("windows.updates.count", criticalUpdates, tags:new Dictionary<string, string>{{"severity","critical"}}));
results.Add(new Metric("windows.updates.rebootcount", updatesNeedingReboot, tags:new Dictionary<string, string>{{"severity","all"}}));
return results;
}
public TimeSpan CacheDuration => TimeSpan.FromHours(12);
}
}