summaryrefslogtreecommitdiff
path: root/src/pmdas/windows/instance.c
blob: fbbb08313c6a218047f5e4a2cd8cf557d0532623 (plain)
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
/*
 * Copyright (c) 2008-2010 Aconex.  All Rights Reserved.
 * Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
 * Parts of this file contributed by Ken McDonell
 * (kenj At internode DoT on DoT net)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include "hypnotoad.h"
#include <ctype.h>

int
windows_indom_fixed(int serial)
{
    return (serial != PROCESS_INDOM && serial != THREAD_INDOM);
}

void
windows_instance_refresh(pmInDom indom)
{
    int			i, index, setup;

    index = pmInDom_serial(indom);
    windows_indom_reset[index] = 0;
    setup = windows_indom_setup[index];

    for (i = 0; i < metricdesc_sz; i++) {
	pdh_metric_t *mp = &metricdesc[i];

	if (indom != mp->desc.indom || mp->pat[0] != '\\')
	    continue;
	if (!setup || (mp->flags & M_REDO))
	    windows_visit_metric(mp, NULL);
	break;
    }

    /* Do we want to persist this instance domain to disk? */
    if (windows_indom_reset[index] && windows_indom_fixed(index))
	pmdaCacheOp(indom, PMDA_CACHE_SAVE);
}

int
windows_lookup_instance(char *path, pdh_metric_t *mp)
{
    __pmInDom_int	*ip;
    static void		*seen = (void *)0xfeedbabe;
    void		*sp;
    char		*p, *q, *name = NULL;
    int			sts, ok = 0;

    if (mp->desc.indom == PM_INDOM_NULL)
	return PM_IN_NULL;

    ip = (__pmInDom_int *)&mp->desc.indom;
    switch (ip->serial) {
	/*
	 * Examples:
	 * \\WINBUILD\PhysicalDisk(0 C:)\Disk Reads/sec
	 * \\SOMEHOST\PhysicalDisk(0 C: D: E:)\Disk Transfers/sec
	 * \\WINBUILD\PhysicalDisk(_Total)\Disk Write Bytes/sec
	 */
	case DISK_INDOM:
	    p = strchr(path, '(');	// skip hostname and metric name
	    if (p != NULL) {
		p++;
		if (strncmp(p, "_Total)", 7) == 0) {
		    /*
		     * The totals get enumerated in the per disk path
		     * expansion, just skip 'em here
		     */
		    return -1;
		}
		while (isascii((int)*p) && isdigit((int)*p)) p++;
		if (*p == ' ') {
		    p++;
		    q = strchr(p, ')');
		    if (q != NULL) {
			name = (char *)malloc(q - p + 1);
			if (name != NULL) {
			    strncpy(name, p, q - p);
			    name[q - p] = '\0';
			    ok = 1;
			}
			else {
			    __pmNotifyErr(LOG_ERR, "windows_check_instance: "
					"Error: DISK_INDOM malloc[%d] failed "
					"path=%s\n", q - p + 1, path);
			    return -1;
			}
			/*
			 * If more than one drive letter maps to the same
			 * logical disk (e.g. mirrored root),, the name
			 * contains spaces, e.g.
			 * "C: D:" ... replace ' ' by '_' to play by the
			 * PCP rules for instance names
			 */
			for (p = name; *p; p++) {
			    if (*p == ' ') *p = '_';
			}
		    }
		}
	    }
	    /*
	     * expecting something like ...\PhysicalDisk(N name)...
	     * we will just have to ignore this one!  (might be due
	     * to: http://support.microsoft.com/kb/974878 - fail as
	     * entries like "17" and "17#1" are not useable anyway)
	     */
	    if (!ok) {
		if (pmDebug & DBG_TRACE_LIBPMDA)
		    __pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				"unrecognized disk instance: %s\n", path);
		free(name);
		return -1;
	    }
	    break;

	/*
	 * Examples:
	 * \\WINBUILD\Processor(0)\% User Time
	 * \\WINBUILD\Processor(_Total)\Interrupts/sec
	 */
	case CPU_INDOM:
	    p = strchr(path, '(');	// skip hostname and metric name
	    if (p != NULL) {
		p++;
		if (strncmp(p, "_Total)", 7) == 0) {
		    /*
		     * The totals get enumerated in the per cpu path
		     * expansion, just skip 'em here
		     */
		    return -1;
		}
		int inst = atoi(p);
		name = (char *)malloc(8);	// "cpuNNNN"
		if (name != NULL)
		    sprintf(name, "cpu%d", inst);
		ok = 1;
	    }
	    /*
	     * expecting something like ...\Processor(N)...
	     * don't know what to do with this one!
	     */
	    if (!ok) {
		__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				"unrecognized cpu instance: %s\n", path);
		free(name);
		return -1;
	    }
	    break;

	/*
	 * Examples:
	 * \\WINNT\Network Interface(MS TCP Loopback interface)\Bytes Total/sec
	 */
	case NETIF_INDOM:
	    p = strchr(path, '(');	// skip hostname and metric name
	    if (p != NULL) {
		p++;
		q = strchr(p, ')');
		if (q != NULL) {
		    name = (char *)malloc(q - p + 1);
		    if (name != NULL) {
			strncpy(name, p, q - p);
			name[q - p] = '\0';
			ok = 1;
		    }
		    else {
			__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
					"malloc[%d] failed for NETIF_INDOM "
					"path=%s\n", q - p + 1, path);
			return -1;
		    }
		    /*
		     * The network interface names have many spaces and are
		     * not unique up to the first space by any means.  So,
		     * replace ' 's to play by the PCP instance name rules.
		     */
		    for (p = name; *p; p++) {
			if (*p == ' ') *p = '_';
		    }
		}
	    }
	    /*
	     * expecting something like ...\Network Interface(...)...
	     * don't know what to do with this one!
	     */
	    if (!ok) {
		__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
			"unrecognized network interface instance: %s\n", path);
		free(name);
		return -1;
	    }
	    break;

	/*
	 * Examples:
	 * \\TOWER\LogicalDisk(C:)\% Free Space
	 */
	case FILESYS_INDOM:
	    p = strchr(path, '(');	// skip hostname and metric name
	    if (p != NULL) {
		p++;
		if (strncmp(p, "_Total)", 7) == 0) {
		    /*
		     * The totals value makes no semantic sense,
		     * just skip it here
		     */
		    return -1;
		}
		while (isascii((int)*p) && isdigit((int)*p))
		    p++;
		if (*p == ' ')
		    p++;
		q = strchr(p, ')');
		if (q != NULL) {
		    name = (char *)malloc(q - p + 1);
		    if (name != NULL) {
			strncpy(name, p, q - p);
			name[q - p] = '\0';
			ok = 1;
		    }
		    else {
			__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				"malloc[%d] failed for LDISK_INDOM path=%s\n",
				q - p + 1, path);
			return -1;
		    }
		}
	    }
	    /*
	     * expecting something like ...\LogicalDisk(C:)...
	     * don't know what to do with this one!
	     */
	    if (!ok) {
		__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
			"unrecognized logical disk instance: %s\n", path);
		free(name);
		return -1;
	    }
	    break;

	/*
	 * SQLServer instance domains all have similar syntax
	 *
	 * Examples:
	 * \\TOWER\SQLServer:Locks(Table)\Average Wait Time (ms)
	 * \\TOWER\SQLServer:Cache Manager(Cursors)\Cache Hit Ratio
	 * \\TOWER\SQLServer:Databases(ACONEX_SYS)\Transactions/sec
	 */
	case SQL_LOCK_INDOM:
	case SQL_CACHE_INDOM:
	case SQL_DB_INDOM:
	case SQL_USER_INDOM:
	    p = strchr(path, '(');	// skip hostname and metric name
	    if (p != NULL) {
		p++;
		if (strncmp(p, "_Total)", 7) == 0) {
		    /*
		     * The totals are done as independent metrics,
		     * just skip them here
		     */
		    return -1;
		}
		q = strchr(p, ')');
		if (q != NULL) {
		    name = (char *)malloc(q - p + 1);
		    if (name != NULL) {
			strncpy(name, p, q - p);
			name[q - p] = '\0';
			ok = 1;
		    }
		    else {
			__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				"malloc[%d] failed, SQL_INDOM path=%s\n",
				q - p + 1, path);
			return -1;
		    }

		    /*
		     * The user counter names have many spaces and are
		     * not unique up to the first space by any means.  So,
		     * replace ' 's to play by the PCP instance name rules.
		     */
		    if (ip->serial == SQL_USER_INDOM) {
			for (p = name; *p; p++)
			    if (*p == ' ') *p = '_';
		    }
		}
	    }
	    /*
	     * expecting something like ... \SQLServer:...(...)\...
	     * don't know what to do with this one!
	     */
	    if (!ok) {
		__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				"unrecognized SQLServer instance: %s\n", path);
		free(name);
		return -1;
	    }
	    break;

	/*
	 * Per-process and per-thread instance domain
	 *
	 * Examples:
	 * \\TOWER\Process(svchost#6)\% Processor Time
	 * \\TOWER\Thread(svchost/1#1)\% Processor Time
	 * \\TOWER\Thread(Idle/0)\ID Process
	 * \\TOWER\Thread(Idle/0)\ID Thread
	 */
	case PROCESS_INDOM:
	case THREAD_INDOM:
	    p = strchr(path, '(');	// skip hostname and Process/Thread
	    if (p != NULL) {
		p++;
		if ((strncmp(p, "_Total)", 7) == 0) ||
		    (strncmp(p, "_Total/", 7) == 0)) {
		    /*
		     * The totals are done as independent metrics,
		     * just skip them here
		     */
		    return -1;
		}
		q = strchr(p, ')');
		if (q != NULL) {
		    name = (char *)malloc(q - p + 1);
		    if (name != NULL) {
			strncpy(name, p, q - p);
			name[q - p] = '\0';
			ok = 1;
		    }
		    else {
			__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				"malloc[%d] failed, process/thread path=%s\n",
				q - p + 1, path);
			return -1;
		    }
		}
	    }
	    /*
	     * expecting something like ... \Process(...)\...
	     * don't know what to do with this one!
	     */
	    if (!ok) {
		__pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				"unrecognized process/thread name: %s\n", path);
		free(name);
		return -1;
	    }
	    break;

	default:
	    __pmNotifyErr(LOG_ERR, "windows_check_instance: Error: "
				   "pmInDom %s is unknown for metric %s\n",
			pmInDomStr(mp->desc.indom), pmIDStr(mp->desc.pmid));
	    return -1;
    }

    sts = pmdaCacheLookupName(mp->desc.indom, name, &ok, &sp);
    if (sts != PMDA_CACHE_ACTIVE) {
	if (sp != seen)	/* new instance, never seen before, mark it */
	    windows_indom_reset[pmInDom_serial(mp->desc.indom)] = 1;
	ok = pmdaCacheStore(mp->desc.indom, PMDA_CACHE_ADD, name, seen);
    }
    free(name);
    return ok;
}