-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwmi.c
203 lines (185 loc) · 5.52 KB
/
wmi.c
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
#include "mrbig.h"
#include "disphelper.h"
#include <wchar.h>
#define HR_TRY(func) if (FAILED(func)) { printf("\n## Fatal error on line %d.\n", __LINE__); goto cleanup; }
#define FIELDS_MAX 100
static struct fields {
char field[100];
char op1[100];
char op2[100];
} fields[FIELDS_MAX];
static int nfields;
/* **************************************************************************
* getWmiStr:
* Helper function to create wmi moniker incorporating computer name.
*
============================================================================ */
static LPWSTR getWmiStr(LPCWSTR szComputer)
{
static WCHAR szWmiStr[256];
wcscpy(szWmiStr, L"winmgmts:{impersonationLevel=impersonate}!\\\\");
if (szComputer) wcsncat(szWmiStr, szComputer, 128);
else wcscat (szWmiStr, L".");
wcscat(szWmiStr, L"\\root\\cimv2");
return szWmiStr;
}
/*
Configuration for this module appears under the [wmi] header.
Example with explanations:
[wmi]
begin wmi1 -- a test page with the name "wmi1" will be produced
text This is a wmi test page -- this page will appear on the report
query select * from Win32_foo -- any wql query
field xxx -- this field will appear on the report, value is ignored
field yyy > 0 -- this field will also appear, value must be > 0
go -- run the query
text Another query on the same page
query select * from Win32_bar
field aaa = "Ok" -- this field must have the string value Ok
field bbb
go
end -- page is finished, send the report
begin wmi2 -- another test page
always green -- the test will have green status regardless of individual fields
text Another wmi test page
query select * from Win32_baz
field qwe
field asd = 0 -- status will be indicated by red or green bullet
go
end
*/
void wmi(void)
{
LPCWSTR szComputer = L"."; // local computer
DISPATCH_OBJ(wmiSvc);
DISPATCH_OBJ(colWmiTest);
LPWSTR szWmiItem;
wchar_t query[1000];
int i, n, f;
char b[1000], key[1000], value[1000];
char *green = "green", *red = "red", *blue = "blue", *clear = "clear";
char always[100], *color = green, *bullet;
char page[100];
char report[10000];
query[0] = L'\0';
report[0] = '\0';
always[0] = '\0';
page[0] = '\0';
nfields = 0;
if (debug) mrlog("wmi()");
if (get_option("no_wmi", 0)) {
/* no mrsend with clear status because we don't know
the names of the tests
*/
return;
}
dhInitialize(TRUE);
for (i = 0; get_cfg("wmi", b, sizeof b, i); i++) {
if (debug) mrlog("wmi: %s", b);
if (b[0] == '#') continue;
key[0] = value[0] = '\0';
n = sscanf(b, "%s %[^\n]", key, value);
if (n < 1) continue;
if (!strcmp(key, "always")) {
strcpy(always, value);
} else if (!strcmp(key, "begin")) {
strcpy(page, value);
always[0] = '\0';
report[0] = '\0';
color = green;
} else if (!strcmp(key, "end")) {
if (!page[0]) {
mrlog("wmi end without begin");
continue;
}
if (always[0]) color = always;
mrsend(mrmachine, page, color, report);
} else if (!strcmp(key, "field")) {
fields[nfields].field[0] = '\0';
fields[nfields].op1[0] = '\0';
fields[nfields].op2[0] = '\0';
n = sscanf(value, "%s %s %[^\n]",
fields[nfields].field,
fields[nfields].op1,
fields[nfields].op2);
if (n < 1) continue;
nfields++;
} else if (!strcmp(key, "go")) {
if (!query[0]) {
mrlog("no wmi query");
continue;
}
HR_TRY(dhGetObject(getWmiStr(szComputer), NULL, &wmiSvc));
HR_TRY(dhGetValue(L"%o", &colWmiTest, wmiSvc, L".ExecQuery(%S)", query));
FOR_EACH(objWmiTest, colWmiTest, NULL) {
wchar_t item[1000];
for (f = 0; f < nfields; f++) {
char *op1 = fields[f].op1;
char *op2 = fields[f].op2;
_snwprintf(item, sizeof item, L".%hs", fields[f].field);
ZeroMemory(&szWmiItem, sizeof szWmiItem);
dhGetValue(L"%S", &szWmiItem, objWmiTest, item);
bullet = "red";
if (szWmiItem) {
char item[1000];
snprintf(item, sizeof item, "%ls", szWmiItem);
if (op1[0] && op2[0]) {
if (isdigit(op2[0])) {
int a1 = atoi(item), a2 = atoi(op2);
if ((!strcmp(op1, "=") && (a1 == a2)) ||
(!strcmp(op1, "<") && (a1 < a2)) ||
(!strcmp(op1, "<=") && (a1 <= a2)) ||
(!strcmp(op1, ">") && (a1 > a2)) ||
(!strcmp(op1, ">=") && (a1 >= a2))) {
bullet = green;
}
} else {
int a = strcmp(item, op2);
if ((!strcmp(op1, "=") && (a == 0)) ||
(!strcmp(op1, "<") && (a < 0)) ||
(!strcmp(op1, "<=") && (a <= 0)) ||
(!strcmp(op1, ">") && (a > 0)) ||
(!strcmp(op1, ">=") && (a >= 0)) ||
(!strcmp(op1, "contains") && strstr(item, op2))) {
bullet = green;
}
}
} else {
bullet = blue;
}
} else {
if (op1[0]) {
bullet = red;
} else {
bullet = clear;
}
}
if (bullet == red) color = bullet;
#if 1 /* this messes up the ncv rrd stuff */
snprcat(report, sizeof report, "&%s %s: %ls\n",
bullet, fields[f].field,
szWmiItem?szWmiItem:L"");
#else
snprcat(report, sizeof report, "%s: %ls &%s\n",
fields[f].field, szWmiItem?szWmiItem:L"", bullet);
#endif
dhFreeString(szWmiItem);
}
snprcat(report, sizeof report, "\n");
} NEXT(objWmiTest);
cleanup:
SAFE_RELEASE(colWmiTest);
SAFE_RELEASE(wmiSvc);
} else if (!strcmp(key, "query")) {
_snwprintf(query, sizeof query, L"%hs", value);
nfields = 0;
} else if (!strcmp(key, "text")) {
snprcat(report, sizeof report, "%s\n", value);
} else {
mrlog("Unknown key '%s'", key);
}
}
if (debug) mrlog("wmi done");
dhUninitialize(TRUE);
return;
}