Debian Codegraph @

agent/mibgroup/ucd-snmp/vmstat_linux.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-features.h>
3
4 #ifdef HAVE_LIMITS_H
5 #include <limits.h>
6 #endif
7 #ifdef HAVE_STDLIB_H
8 #include <stdlib.h>
9 #endif
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13 #ifdef HAVE_FCNTL_H
14 #include <fcntl.h>
15 #endif
16 #include <ctype.h>
17 #include <signal.h>
18 #ifdef HAVE_MACHINE_PARAM_H
19 #include <machine/param.h>
20 #endif
21 #ifdef HAVE_SYS_VMMETER_H
22 #if !defined(bsdi2) && !defined(netbsd1)
23 #include <sys/vmmeter.h>
24 #endif
25 #endif
26 #ifdef HAVE_SYS_CONF_H
27 #include <sys/conf.h>
28 #endif
29 #ifdef HAVE_SYS_FS_H
30 #include <sys/fs.h>
31 #else
32 #ifdef HAVE_UFS_FS_H
33 #include <ufs/fs.h>
34 #else
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38 #ifdef HAVE_SYS_VNODE_H
39 #include <sys/vnode.h>
40 #endif
41 #ifdef HAVE_UFS_UFS_QUOTA_H
42 #include <ufs/ufs/quota.h>
43 #endif
44 #ifdef HAVE_UFS_UFS_INODE_H
45 #include <ufs/ufs/inode.h>
46 #endif
47 #ifdef HAVE_UFS_FFS_FS_H
48 #include <ufs/ffs/fs.h>
49 #endif
50 #endif
51 #endif
52 #ifdef HAVE_MTAB_H
53 #include <mtab.h>
54 #endif
55 #include <sys/stat.h>
56 #include <errno.h>
57 #ifdef HAVE_FSTAB_H
58 #include <fstab.h>
59 #endif
60 #ifdef HAVE_SYS_STATVFS_H
61 #include <sys/statvfs.h>
62 #endif
63 #ifdef HAVE_SYS_VFS_H
64 #include <sys/vfs.h>
65 #endif
66 #if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)
67 #ifdef HAVE_SYS_PARAM_H
68 #include <sys/param.h>
69 #endif
70 #ifdef HAVE_SYS_MOUNT_H
71 #include <sys/mount.h>
72 #endif
73 #ifdef HAVE_SYS_SYSCTL_H
74 #include <sys/sysctl.h>
75 #endif
76 #define statvfs statfs
77 #endif
78 #ifdef HAVE_VM_SWAP_PAGER_H
79 #include <vm/swap_pager.h>
80 #endif
81 #ifdef HAVE_SYS_FIXPOINT_H
82 #include <sys/fixpoint.h>
83 #endif
84 #ifdef HAVE_MALLOC_H
85 #include <malloc.h>
86 #endif
87 #ifdef HAVE_STRING_H
88 #include <string.h>
89 #else
90 #include <strings.h>
91 #endif
92 #ifdef TIME_WITH_SYS_TIME
93 # include <sys/time.h>
94 # include <time.h>
95 #else
96 # ifdef HAVE_SYS_TIME_H
97 # include <sys/time.h>
98 # else
99 # include <time.h>
100 # endif
101 #endif
102
103 #include <sys/utsname.h>
104
105 #include <net-snmp/net-snmp-includes.h>
106 #include <net-snmp/agent/net-snmp-agent-includes.h>
107 #include <net-snmp/agent/auto_nlist.h>
108 #include <net-snmp/agent/hardware/cpu.h>
109
110 #include "mibdefs.h"
111 #include "struct.h"
112 #include "util_funcs/header_generic.h"
113 #include "vmstat.h"
114
115 netsnmp_feature_require(hardware_cpu_load);
116
117
118 FindVarMethod var_extensible_vmstat;
119
120 static int has_vmstat = 1;
121 static int has_cpu_26 = 1;
122 static time_t cache_time;
123 #define CACHE_TIMEOUT 5
124 #define MAX_INT32 0x7fffffff
125 #define MAX_COUNTER 0xffffffff
126
127 #define STAT_FILE "/proc/stat"
128 #define VMSTAT_FILE "/proc/vmstat"
129
130
131 void
132 init_vmstat(void)
133 {
134 struct variable2 extensible_vmstat_variables[] = {
135 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
136 var_extensible_vmstat, 1, {MIBINDEX}},
137 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
138 var_extensible_vmstat, 1, {ERRORNAME}},
139 {SWAPIN, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
140 var_extensible_vmstat, 1, {SWAPIN}},
141 {SWAPOUT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
142 var_extensible_vmstat, 1, {SWAPOUT}},
143 {RAWSWAPIN, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
144 var_extensible_vmstat, 1, {RAWSWAPIN}},
145 {RAWSWAPOUT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
146 var_extensible_vmstat, 1, {RAWSWAPOUT}},
147 {IOSENT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
148 var_extensible_vmstat, 1, {IOSENT}},
149 {IORECEIVE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
150 var_extensible_vmstat, 1, {IORECEIVE}},
151 {IORAWSENT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
152 var_extensible_vmstat, 1, {IORAWSENT}},
153 {IORAWRECEIVE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
154 var_extensible_vmstat, 1, {IORAWRECEIVE}},
155 {SYSINTERRUPTS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
156 var_extensible_vmstat, 1, {SYSINTERRUPTS}},
157 {SYSCONTEXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
158 var_extensible_vmstat, 1, {SYSCONTEXT}},
159 {CPUUSER, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
160 var_extensible_vmstat, 1, {CPUUSER}},
161 {CPUSYSTEM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
162 var_extensible_vmstat, 1, {CPUSYSTEM}},
163 {CPUIDLE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
164 var_extensible_vmstat, 1, {CPUIDLE}},
165 {CPURAWUSER, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
166 var_extensible_vmstat, 1, {CPURAWUSER}},
167 {CPURAWNICE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
168 var_extensible_vmstat, 1, {CPURAWNICE}},
169 {CPURAWSYSTEM, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
170 var_extensible_vmstat, 1, {CPURAWSYSTEM}},
171 {CPURAWKERNEL, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
172 var_extensible_vmstat, 1, {CPURAWKERNEL}},
173 {CPURAWIDLE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
174 var_extensible_vmstat, 1, {CPURAWIDLE}},
175 {SYSRAWINTERRUPTS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
176 var_extensible_vmstat, 1, {SYSRAWINTERRUPTS}},
177 {SYSRAWCONTEXT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
178 var_extensible_vmstat, 1, {SYSRAWCONTEXT}},
179 {CPURAWWAIT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
180 var_extensible_vmstat, 1, {CPURAWWAIT}},
181 {CPURAWINTR, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
182 var_extensible_vmstat, 1, {CPURAWINTR}},
183 {CPURAWSOFTIRQ, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
184 var_extensible_vmstat, 1, {CPURAWSOFTIRQ}},
185 /*
186 * Future use:
187 */
188 /*
189 * {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
190 * var_extensible_vmstat, 1, {ERRORFLAG }},
191 * {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
192 * var_extensible_vmstat, 1, {ERRORMSG }}
193 */
194 };
195
196 /*
197 * Define the OID pointer to the top of the mib tree that we're
198 * registering underneath
199 */
200 oid vmstat_variables_oid[] = { NETSNMP_UCDAVIS_MIB, 11 };
201
202 /*
203 * register ourselves with the agent to handle our mib tree
204 */
205 REGISTER_MIB("ucd-snmp/vmstat", extensible_vmstat_variables, variable2,
206 vmstat_variables_oid);
207 }
208
209
210 static void
211 getstat(unsigned long *cuse, unsigned long *cice, unsigned long *csys,
212 unsigned long *cide, unsigned *pin, unsigned *pout,
213 unsigned *swpin, unsigned *swpout, unsigned *itot, unsigned *i1,
214 unsigned *ct, unsigned long *ciow, unsigned long *cirq,
215 unsigned long *csoft)
216 {
217 int statfd, vmstatfd;
218 static int first = 1;
219 static char *buff = NULL, *vmbuff = NULL;
220 static int bsize = 0, vmbsize = 0;
221 char *b, *c;
222 time_t now;
223 unsigned long long cpunum;
224 unsigned long long cusell = 0, cicell = 0, csysll = 0, cidell = 0,
225 ciowll = 0, cirqll = 0, csoftll = 0, ctll = 0, itotll = 0, i1ll = 0;
226
227 time(&now);
228 if (cache_time + CACHE_TIMEOUT < now) {
229 if ((statfd = open(STAT_FILE, O_RDONLY, 0)) == -1) {
230 snmp_log_perror(STAT_FILE);
231 return;
232 }
233 if (bsize == 0) {
234 bsize = 256;
235 buff = malloc(bsize);
236 }
237 while (read(statfd, buff, bsize) == bsize) {
238 bsize += 256;
239 buff = realloc(buff, bsize);
240 DEBUGMSGTL(("vmstat", "/proc/stat buffer increased to %d\n", bsize));
241 close(statfd);
242 statfd = open(STAT_FILE, O_RDONLY, 0);
243 }
244 close(statfd);
245
246 if (has_vmstat) {
247 vmstatfd = open(VMSTAT_FILE, O_RDONLY, 0);
248 if (vmstatfd == -1) {
249 snmp_log(LOG_ERR, "cannot open %s\n", VMSTAT_FILE);
250 has_vmstat = 0;
251 } else {
252 if (vmbsize == 0) {
253 vmbsize = 256;
254 vmbuff = malloc(vmbsize);
255 }
256 while (read(vmstatfd, vmbuff, vmbsize) == vmbsize) {
257 vmbsize += 256;
258 vmbuff = realloc(vmbuff, vmbsize);
259 close(vmstatfd);
260 vmstatfd = open(VMSTAT_FILE, O_RDONLY, 0);
261 }
262 close(vmstatfd);
263 }
264 }
265 cache_time = now;
266 }
267
268 *itot = 0;
269 *i1 = 1;
270 c = buff;
271 while ((c = strstr(c+1, "cpu")) != NULL)
272 b = c;
273 sscanf(b, "cpu%llu", &cpunum);
274 cpunum++;
275 b = strstr(buff, "cpu ");
276 if (b) {
277 if (!has_cpu_26 ||
278 sscanf(b, "cpu %llu %llu %llu %llu %llu %llu %llu", &cusell,
279 &cicell, &csysll, &cidell, &ciowll, &cirqll, &csoftll) != 7) {
280 has_cpu_26 = 0;
281 sscanf(b, "cpu %llu %llu %llu %llu", &cusell, &cicell, &csysll,
282 &cidell);
283 *ciow = *cirq = *csoft = 0;
284 } else {
285 *ciow = (unsigned long)(ciowll/cpunum);
286 *cirq = (unsigned long)(cirqll/cpunum);
287 *csoft = (unsigned long)(csoftll/cpunum);
288 }
289 *cuse = (unsigned long)(cusell/cpunum);
290 *cice = (unsigned long)(cicell/cpunum);
291 *csys = (unsigned long)(csysll/cpunum);
292 *cide = (unsigned long)(cidell/cpunum);
293
294 }
295 else {
296 if (first)
297 snmp_log(LOG_ERR, "No cpu line in %s\n", STAT_FILE);
298 *cuse = *cice = *csys = *cide = *ciow = *cirq = *csoft = 0;
299 }
300 if (has_vmstat) {
301 b = strstr(vmbuff, "pgpgin ");
302 if (b)
303 sscanf(b, "pgpgin %u", pin);
304 else {
305 if (first)
306 snmp_log(LOG_ERR, "No pgpgin line in %s\n", VMSTAT_FILE);
307 *pin = 0;
308 }
309 b = strstr(vmbuff, "pgpgout ");
310 if (b)
311 sscanf(b, "pgpgout %u", pout);
312 else {
313 if (first)
314 snmp_log(LOG_ERR, "No pgpgout line in %s\n", VMSTAT_FILE);
315 *pout = 0;
316 }
317 b = strstr(vmbuff, "pswpin ");
318 if (b)
319 sscanf(b, "pswpin %u", swpin);
320 else {
321 if (first)
322 snmp_log(LOG_ERR, "No pswpin line in %s\n", VMSTAT_FILE);
323 *swpin = 0;
324 }
325 b = strstr(vmbuff, "pswpout ");
326 if (b)
327 sscanf(b, "pswpout %u", swpout);
328 else {
329 if (first)
330 snmp_log(LOG_ERR, "No pswpout line in %s\n", VMSTAT_FILE);
331 *swpout = 0;
332 }
333 }
334 else {
335 b = strstr(buff, "page ");
336 if (b)
337 sscanf(b, "page %u %u", pin, pout);
338 else {
339 if (first)
340 snmp_log(LOG_ERR, "No page line in %s\n", STAT_FILE);
341 *pin = *pout = 0;
342 }
343 b = strstr(buff, "swap ");
344 if (b)
345 sscanf(b, "swap %u %u", swpin, swpout);
346 else {
347 if (first)
348 snmp_log(LOG_ERR, "No swap line in %s\n", STAT_FILE);
349 *swpin = *swpout = 0;
350 }
351 }
352 b = strstr(buff, "intr ");
353 if (b) {
354 sscanf(b, "intr %llu %llu", &itotll, &i1ll);
355 *itot = (unsigned)itotll;
356 *i1 = (unsigned)i1ll;
357 }
358 else {
359 if (first)
360 snmp_log(LOG_ERR, "No intr line in %s\n", STAT_FILE);
361 *itot = 0;
362 }
363 b = strstr(buff, "ctxt ");
364 if (b) {
365 sscanf(b, "ctxt %llu", &ctll);
366 *ct = (unsigned long)ctll;
367 }
368 else {
369 if (first)
370 snmp_log(LOG_ERR, "No ctxt line in %s\n", STAT_FILE);
371 *ct = 0;
372 }
373 first = 0;
374 }
375
376 enum vmstat_index { swapin = 0, swapout,
377 rawswapin, rawswapout,
378 iosent, ioreceive,
379 rawiosent, rawioreceive,
380 sysinterrupts, syscontext,
381 cpuuser, cpusystem, cpuidle,
382 cpurawuser, cpurawnice,
383 cpurawsystem, cpurawidle,
384 cpurawinter, cpurawsoft, cpurawwait,
385 rawinterrupts, rawcontext
386 };
387
388 static unsigned
389 vmstat(int iindex)
390 {
391 double duse, dsys, didl, ddiv, divo2;
392 double druse, drnic, drsys, dridl;
393 unsigned int hertz;
394 double ddiv2;
395
396 netsnmp_cpu_info *cpu;
397 netsnmp_cpu_load();
398 cpu = netsnmp_cpu_get_byIdx( -1, 0 );
399
400 duse = cpu->user_ticks + cpu->nice_ticks;
401 dsys = cpu->sys_ticks;
402 didl = cpu->idle_ticks;
403 ddiv = duse + dsys + didl;
404 hertz = sysconf(_SC_CLK_TCK); /* get ticks/s from system */
405 divo2 = ddiv / 2;
406 druse = cpu->user_ticks;
407 drnic = cpu->nice_ticks;
408 drsys = cpu->sys_ticks;
409 dridl = cpu->idle_ticks;
410
411 ddiv2 = ddiv + cpu->wait_ticks
412 + cpu->intrpt_ticks
413 + cpu->sirq_ticks;
414 if (cpu->history) {
415 duse -= (cpu->history[0].user_hist + cpu->history[0].nice_hist);
416 dsys -= cpu->history[0].sys_hist;
417 didl -= cpu->history[0].idle_hist;
418 ddiv2 -= cpu->history[0].total_hist;
419 }
420 if (!ddiv) ddiv=1; /* Protect against division-by-0 */
421
422 switch (iindex) {
423 case swapin:
424 return (cpu->swapIn * 4 * hertz + divo2) / ddiv;
425 case swapout:
426 return (cpu->swapOut * 4 * hertz + divo2) / ddiv;
427 case iosent:
428 return (cpu->pageIn * hertz + divo2) / ddiv;
429 case ioreceive:
430 return (cpu->pageOut * hertz + divo2) / ddiv;
431 case sysinterrupts:
432 return (cpu->nInterrupts * hertz + divo2) / ddiv;
433 case syscontext:
434 return (cpu->nCtxSwitches * hertz + divo2) / ddiv;
435 case cpuuser:
436 return (ddiv2 ? 100 * duse / ddiv2 : 0);
437 case cpusystem:
438 return (ddiv2 ? 100 * dsys / ddiv2 : 0);
439 case cpuidle:
440 return (ddiv2 ? 100 * didl / ddiv2 : 0);
441 case cpurawuser:
442 return druse;
443 case cpurawnice:
444 return drnic;
445 case cpurawsystem:
446 return drsys;
447 case cpurawidle:
448 return dridl;
449 case rawinterrupts:
450 return cpu->nInterrupts;
451 case rawcontext:
452 return cpu->nCtxSwitches;
453 case cpurawwait:
454 return cpu->wait_ticks;
455 case cpurawinter:
456 return cpu->intrpt_ticks;
457 case cpurawsoft:
458 return cpu->sirq_ticks;
459 case rawiosent:
460 return cpu->pageOut*2;
461 case rawioreceive:
462 return cpu->pageIn*2;
463 case rawswapin:
464 return cpu->swapIn;
465 case rawswapout:
466 return cpu->swapOut;
467 default:
468 return -1;
469 }
470 }
471
472 unsigned char *
473 var_extensible_vmstat(struct variable *vp,
474 oid * name,
475 size_t * length,
476 int exact,
477 size_t * var_len, WriteMethod ** write_method)
478 {
479
480 static long long_ret;
481 static char errmsg[300];
482
483 long_ret = 0; /* set to 0 as default */
484
485 if (header_generic(vp, name, length, exact, var_len, write_method))
486 return (NULL);
487 switch (vp->magic) {
488 case MIBINDEX:
489 long_ret = 1;
490 return ((u_char *) (&long_ret));
491 case ERRORNAME: /* dummy name */
492 sprintf(errmsg, "systemStats");
493 *var_len = strlen(errmsg);
494 return ((u_char *) (errmsg));
495 case SWAPIN:
496 long_ret = vmstat(swapin) & MAX_INT32;
497 return ((u_char *) (&long_ret));
498 case SWAPOUT:
499 long_ret = vmstat(swapout) & MAX_INT32;
500 return ((u_char *) (&long_ret));
501 case RAWSWAPIN:
502 long_ret = vmstat(rawswapin) & MAX_COUNTER;
503 return ((u_char *) (&long_ret));
504 case RAWSWAPOUT:
505 long_ret = vmstat(rawswapout) & MAX_COUNTER;
506 return ((u_char *) (&long_ret));
507 case IOSENT:
508 long_ret = vmstat(iosent) & MAX_INT32;
509 return ((u_char *) (&long_ret));
510 case IORECEIVE:
511 long_ret = vmstat(ioreceive) & MAX_INT32;
512 return ((u_char *) (&long_ret));
513 case IORAWSENT:
514 long_ret = vmstat(rawiosent) & MAX_COUNTER;
515 return ((u_char *) (&long_ret));
516 case IORAWRECEIVE:
517 long_ret = vmstat(rawioreceive) & MAX_COUNTER;
518 return ((u_char *) (&long_ret));
519 case SYSINTERRUPTS:
520 long_ret = vmstat(sysinterrupts) & MAX_INT32;
521 return ((u_char *) (&long_ret));
522 case SYSCONTEXT:
523 long_ret = vmstat(syscontext) & MAX_INT32;
524 return ((u_char *) (&long_ret));
525 case CPUUSER:
526 long_ret = vmstat(cpuuser) & MAX_INT32;
527 return ((u_char *) (&long_ret));
528 case CPUSYSTEM:
529 long_ret = vmstat(cpusystem) & MAX_INT32;
530 return ((u_char *) (&long_ret));
531 case CPUIDLE:
532 long_ret = vmstat(cpuidle) & MAX_INT32;
533 return ((u_char *) (&long_ret));
534 case CPURAWUSER:
535 long_ret = vmstat(cpurawuser) & MAX_COUNTER;
536 return ((u_char *) (&long_ret));
537 case CPURAWNICE:
538 long_ret = vmstat(cpurawnice) & MAX_COUNTER;
539 return ((u_char *) (&long_ret));
540 case CPURAWSYSTEM:
541 long_ret = (vmstat(cpurawsystem)+vmstat(cpurawinter)+vmstat(cpurawsoft)) & MAX_COUNTER;
542 return ((u_char *) (&long_ret));
543 case CPURAWKERNEL:
544 long_ret = vmstat(cpurawsystem) & MAX_COUNTER;
545 return ((u_char *) (&long_ret));
546 case CPURAWIDLE:
547 long_ret = vmstat(cpurawidle) & MAX_COUNTER;
548 return ((u_char *) (&long_ret));
549 case SYSRAWINTERRUPTS:
550 long_ret = vmstat(rawinterrupts) & MAX_COUNTER;
551 return (u_char *)&long_ret;
552 case SYSRAWCONTEXT:
553 long_ret = vmstat(rawcontext) & MAX_COUNTER;
554 return (u_char *)&long_ret;
555 case CPURAWWAIT:
556 if (!has_cpu_26) return NULL;
557 long_ret = vmstat(cpurawwait) & MAX_COUNTER;
558 return ((u_char *) (&long_ret));
559 case CPURAWINTR:
560 if (!has_cpu_26) return NULL;
561 long_ret = vmstat(cpurawinter) & MAX_COUNTER;
562 return ((u_char *) (&long_ret));
563 case CPURAWSOFTIRQ:
564 if (!has_cpu_26) return NULL;
565 long_ret = vmstat(cpurawsoft) & MAX_COUNTER;
566 return ((u_char *) (&long_ret));
567
568 /*
569 * reserved for future use
570 */
571 /*
572 * case ERRORFLAG:
573 * return((u_char *) (&long_ret));
574 * case ERRORMSG:
575 * return((u_char *) (&long_ret));
576 */
577 default:
578 snmp_log(LOG_ERR, "vmstat.c: don't know how to handle %d request\n",
579 vp->magic);
580 }
581 return NULL;
582 }