Debian Codegraph @

agent/mibgroup/ucd-snmp/vmstat_hpux.c

raw
1 /*
2 * vmstat_hpux.c
3 * UCD SNMP module for systemStats section of UCD-SNMP-MIB for HPUX 10.x/11.x
4 */
5
6 /*
7 * To make lint skip the debug code and stop complaining
8 */
9 #ifdef __lint
10 #define NETSNMP_NO_DEBUGGING 1
11 #endif
12
13 /*
14 * Includes start here
15 */
16
17 /*
18 * UCD-SNMP config details
19 */
20 #include <net-snmp/net-snmp-config.h>
21
22 /*
23 * Standard includes
24 */
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <string.h>
30
31 /*
32 * pstat and sysinfo structs
33 */
34 #include <sys/pstat.h>
35 #include <sys/dk.h>
36
37
38 /*
39 * Includes needed for all modules
40 */
41 #include <net-snmp/net-snmp-includes.h>
42 #include <net-snmp/agent/net-snmp-agent-includes.h>
43 #include "mibdefs.h"
44
45 /*
46 * Utility functions for UCD-SNMP
47 */
48 #include "util_funcs/header_generic.h"
49
50 /*
51 * Header file for this module
52 */
53 #include "vmstat.h"
54 #include "vmstat_hpux.h"
55
56 /*
57 * Includes end here
58 */
59
60
61 /*
62 * Global structures start here
63 */
64
65 /*
66 * A structure to save data gathered from the kernel pstat interface to.
67 * CPUSTATES are defined as (see sys/dk.h):
68 * #define CPUSTATES 9 -- number of CPU states
69 * #define CP_USER 0 -- user mode of USER process
70 * #define CP_NICE 1 -- user mode of USER process at nice priority
71 * #define CP_SYS 2 -- kernel mode of USER process
72 * #define CP_IDLE 3 -- IDLE mode
73 * #define CP_WAIT 4
74 * #define CP_BLOCK 5 -- time blocked on a spinlock
75 * #define CP_SWAIT 6 -- time blocked on the kernel semaphore
76 * #define CP_INTR 7 -- INTERRUPT mode
77 * #define CP_SSYS 8 -- kernel mode of KERNEL process
78 */
79
80 struct cpu_stat_snapshot {
81 time_t css_time;
82 unsigned int css_cpus;
83 unsigned long long css_swapin;
84 unsigned long long css_swapout;
85 unsigned long long css_blocks_read;
86 unsigned long long css_blocks_write;
87 unsigned long long css_interrupts;
88 unsigned long long css_context_sw;
89 unsigned long long css_cpu[CPUSTATES];
90 };
91
92 /*
93 * Define a structure to hold kernel static information
94 */
95 struct pst_static pst;
96
97 /*
98 * Global structures end here
99 */
100
101 /*
102 * Global variables start here
103 */
104
105 /*
106 * Variables for the calculated values, filled in update_stats
107 * Need to be global since we need them in more than one function
108 */
109 static unsigned long swapin;
110 static unsigned long swapout;
111 static unsigned long blocks_read;
112 static unsigned long blocks_write;
113 static unsigned long interrupts;
114 static unsigned long context_sw;
115
116 /*
117 * Since MIB wants CPU_SYSTEM, which is CP_SYS + CP_WAIT (see sys/dk.h)
118 */
119 static long cpu_perc[CPUSTATES + 1];
120
121 /*
122 * How many snapshots we have already taken, needed for the first
123 * POLL_INTERVAL * POLL_VALUES seconds of agent running
124 */
125 static unsigned int number_of_snapshots;
126
127 /*
128 * The place to store the snapshots of system data in
129 */
130 static struct cpu_stat_snapshot snapshot[POLL_VALUES + 1];
131
132 /*
133 * And one for the raw counters, which we fill when the raw values are
134 * requested, as opposed to the absolute values, which are taken every
135 * POLL_INTERVAL seconds and calculated over POLL_INTERVAL * POLL_VALUES time
136 */
137 static struct cpu_stat_snapshot raw_values;
138
139 /*
140 * Global variables end here
141 */
142
143
144 /*
145 * Functions start here
146 */
147
148 /*
149 * Function prototype
150 */
151 static void update_stats(unsigned int registrationNumber,
152 void *clientarg);
153 static int take_snapshot(struct cpu_stat_snapshot *css);
154
155 /*
156 * init_vmstat_hpux starts here
157 */
158 /*
159 * Init function for this module, from prototype
160 * Defines variables handled by this module, defines root OID for
161 * this module and registers it with the agent
162 */
163
164 FindVarMethod var_extensible_vmstat;
165
166 void
167 init_vmstat_hpux(void)
168 {
169
170 /*
171 * Which variables do we service ?
172 */
173 struct variable2 extensible_vmstat_variables[] = {
174 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
175 var_extensible_vmstat, 1, {MIBINDEX}},
176 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
177 var_extensible_vmstat, 1, {ERRORNAME}},
178 {SWAPIN, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
179 var_extensible_vmstat, 1, {SWAPIN}},
180 {SWAPOUT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
181 var_extensible_vmstat, 1, {SWAPOUT}},
182 {IOSENT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
183 var_extensible_vmstat, 1, {IOSENT}},
184 {IORECEIVE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
185 var_extensible_vmstat, 1, {IORECEIVE}},
186 {SYSINTERRUPTS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
187 var_extensible_vmstat, 1, {SYSINTERRUPTS}},
188 {SYSCONTEXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
189 var_extensible_vmstat, 1, {SYSCONTEXT}},
190 {CPUUSER, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
191 var_extensible_vmstat, 1, {CPUUSER}},
192 {CPUSYSTEM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
193 var_extensible_vmstat, 1, {CPUSYSTEM}},
194 {CPUIDLE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
195 var_extensible_vmstat, 1, {CPUIDLE}},
196 {CPURAWUSER, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
197 var_extensible_vmstat, 1, {CPURAWUSER}},
198 {CPURAWNICE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
199 var_extensible_vmstat, 1, {CPURAWNICE}},
200 {CPURAWSYSTEM, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
201 var_extensible_vmstat, 1, {CPURAWSYSTEM}},
202 {CPURAWIDLE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
203 var_extensible_vmstat, 1, {CPURAWIDLE}},
204 {CPURAWWAIT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
205 var_extensible_vmstat, 1, {CPURAWWAIT}},
206 {CPURAWKERNEL, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
207 var_extensible_vmstat, 1, {CPURAWKERNEL}},
208 {IORAWSENT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
209 var_extensible_vmstat, 1, {IORAWSENT}},
210 {IORAWRECEIVE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
211 var_extensible_vmstat, 1, {IORAWRECEIVE}},
212 /*
213 * Future use:
214 */
215 /*
216 * {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
217 * var_extensible_vmstat, 1, {ERRORFLAG }},
218 * {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
219 * var_extensible_vmstat, 1, {ERRORMSG }}
220 */
221 };
222
223 /*
224 * Define the OID pointer to the top of the mib tree that we're
225 * registering underneath
226 */
227 oid vmstat_variables_oid[] = { NETSNMP_UCDAVIS_MIB, 11 };
228
229 /*
230 * register ourselves with the agent to handle our mib tree
231 */
232 /*
233 * LINTED Trust me, I know what I'm doing
234 */
235 REGISTER_MIB("ucd-snmp/vmstat", extensible_vmstat_variables, variable2,
236 vmstat_variables_oid);
237
238 /*
239 * Start with some useful data
240 */
241 update_stats(0, NULL);
242
243 /*
244 * update_stats is run every POLL_INTERVAL seconds using this routine
245 * (see 'man snmp_alarm')
246 * This is only executed once to get some useful data in the beginning
247 */
248 if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {
249 snmp_log(LOG_WARNING,
250 "vmstat_hpux (init): snmp_alarm_register failed.\n");
251 }
252 /*
253 * This is the one that runs update_stats every POLL_INTERVAL seconds
254 */
255 if (snmp_alarm_register(POLL_INTERVAL, SA_REPEAT, update_stats, NULL)
256 == 0) {
257 snmp_log(LOG_ERR,
258 "vmstat_hpux (init): snmp_alarm_register failed, cannot service requests.\n");
259 }
260
261 } /* init_vmstat_hpux ends here */
262
263 /*
264 * Data collection function take_snapshot starts here
265 * Get data from kernel and save into the snapshot strutcs
266 * Argument is the snapshot struct to save to. Global anyway, but looks nicer
267 */
268 static int
269 take_snapshot(struct cpu_stat_snapshot *css)
270 {
271 /*
272 * Variables start here
273 */
274
275 struct pst_dynamic psd;
276 struct pst_processor *psp;
277 struct pst_vminfo psv;
278
279 /*
280 * time counter
281 */
282 time_t current_time;
283
284 /*
285 * The usual stuff to count on, err, by
286 */
287 int i;
288
289 /*
290 * Variables end here
291 */
292
293 /*
294 * Function starts here
295 */
296
297 /*
298 * Get time
299 */
300 time(&current_time);
301
302 /*
303 * If we have just gotten the data, return the values from last run (skip if-clause)
304 * This happens on a snmpwalk request. No need to read the pstat again
305 * if we just did it less than 2 seconds ago
306 * Jumps into if-clause either when snapshot is empty or when too old
307 */
308
309 if ((css->css_time == 0) || (current_time > css->css_time + 2)) {
310 /*
311 * Make sure we clean up before we put new data into snapshot
312 */
313 memset(css, 0, sizeof *css);
314
315 /*
316 * Update timer
317 */
318 css->css_time = current_time;
319
320 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) != -1) {
321 css->css_cpus = psd.psd_proc_cnt;
322 DEBUGMSGTL(("take_snapshot", "*** Number of CPUs: %d\n",
323 css->css_cpus));
324
325 /*
326 * We need a for-loop for the CPU values
327 */
328 for (i = 0; i < CPUSTATES; i++) {
329 css->css_cpu[i] = (unsigned long long) psd.psd_cpu_time[i];
330 DEBUGMSGTL(("take_snapshot",
331 "*** Time for CPU state %d: %d\n", i,
332 psd.psd_cpu_time[i]));
333 }
334 psp =
335 (struct pst_processor *) malloc(css->css_cpus *
336 sizeof(*psp));
337 if (pstat_getprocessor(psp, sizeof(*psp), css->css_cpus, 0) !=
338 -1) {
339 int i;
340 for (i = 0; i < css->css_cpus; i++) {
341 css->css_blocks_read = psp[i].psp_phread;
342 css->css_blocks_write = psp[i].psp_phwrite;
343 }
344 } else
345 snmp_log(LOG_ERR,
346 "vmstat_hpux (take_snapshot): pstat_getprocessor failed!\n");
347 } else
348 snmp_log(LOG_ERR,
349 "vmstat_hpux (take_snapshot): pstat_getdynamic failed!\n");
350
351 if (pstat_getvminfo(&psv, sizeof(psv), (size_t) 1, 0) != -1) {
352 css->css_swapin = psv.psv_sswpin;
353 css->css_swapout = psv.psv_sswpout;
354 css->css_interrupts = psv.psv_sintr;
355 css->css_context_sw = psv.psv_sswtch;
356 } else
357 snmp_log(LOG_ERR,
358 "vmstat_hpux (take_snapshot): pstat_getvminfo failed!\n");
359
360 }
361
362 /*
363 * All engines running at warp speed, no problems (if there are any engines, that is)
364 */
365 return (css->css_cpus > 0 ? 0 : -1);
366 } /* take_snapshot ends here */
367
368 /*
369 * This gets called every POLL_INTERVAL seconds to update the snapshots. It takes a new snapshot and
370 * drops the oldest one. This way we move the time window so we always take the values over
371 * POLL_INTERVAL * POLL_VALUES seconds and update the data used every POLL_INTERVAL seconds
372 * The alarm timer is in the init function of this module (snmp_alarm_register)
373 */
374 /*
375 * ARGSUSED0
376 */
377 static void
378 update_stats(unsigned int registrationNumber, void *clientarg)
379 {
380 /*
381 * The time between the samples we compare
382 */
383 time_t time_diff;
384
385 /*
386 * Easier to use these than the snapshots, short hand pointers
387 */
388 struct cpu_stat_snapshot *css_old, *css_new;
389
390 /*
391 * The usual stuff to count on, err, by
392 */
393 int i;
394
395 /*
396 * The sum of the CPU ticks that have passed on the different CPU states, so we can calculate
397 * the percentages of each state
398 */
399 unsigned long long cpu_sum = 0;
400
401 DEBUGMSGTL(("ucd-snmp/vmstat_hpux.c:update_stats",
402 "updating stats\n"));
403
404 /*
405 * Take the current snapshot
406 */
407 if (take_snapshot(&snapshot[0]) == -1) {
408 snmp_log(LOG_WARNING,
409 "vmstat_hpux (update_stats): Something went wrong with take_snapshot.\n");
410 return;
411 }
412
413 /*
414 * Do we have some data we can use ? An issue right after the start of the agent
415 */
416 if (number_of_snapshots > 0) {
417 /*
418 * Huh, the number of CPUs changed during run time. That is indeed s.th. worth noting, we
419 * output a humorous (more or less) syslog message and need to retake the snapshots
420 */
421 if (snapshot[0].css_cpus != snapshot[1].css_cpus) {
422 if (snapshot[0].css_cpus > snapshot[1].css_cpus) {
423 snmp_log(LOG_NOTICE,
424 "vmstat_hpux (update_stats): Cool ! Number of CPUs increased, must be hot-pluggable.\n");
425 } else {
426 snmp_log(LOG_NOTICE,
427 "vmstat_hpux (update_stats): Lost at least one CPU, RIP.\n");
428 }
429 /*
430 * Make all snapshots but the current one invalid
431 */
432 number_of_snapshots = 1;
433 /*
434 * Move the current one in the "first" [1] slot
435 */
436 memmove(&snapshot[1], &snapshot[0], sizeof snapshot[0]);
437 /*
438 * Erase the current one
439 */
440 memset(&snapshot[0], 0, sizeof snapshot[0]);
441 /*
442 * Try to get a new snapshot in five seconds so we can return s.th. useful
443 */
444 if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {
445 snmp_log(LOG_WARNING,
446 "vmstat_hpux (update_stats): snmp_alarm_register failed.\n");
447 }
448 return;
449 }
450
451 /*
452 * Short hand pointers
453 */
454 css_new = &snapshot[0];
455 css_old = &snapshot[number_of_snapshots];
456
457 /*
458 * How much time has passed between the snapshots we get the values from ?
459 */
460 time_diff =
461 (snapshot[0].css_time -
462 snapshot[number_of_snapshots].css_time);
463
464 DEBUGMSGTL(("ucd-snmp/vmstat_hpux.c:update_stats",
465 "time_diff: %lld\n", time_diff));
466
467 /*
468 * swapin and swapout are in pages, MIB wants kB/s,so we just need to get kB and seconds
469 * For the others we need to get value per second
470 * Retreive static information to obtain memory page_size
471 */
472 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
473 snmp_log(LOG_ERR, "vmstat_hpux: pstat_getstatic failed!\n");
474 }
475
476 /*
477 * LINTED cast needed, really
478 */
479 swapin =
480 (unsigned int) ((css_new->css_swapin - css_old->css_swapin) *
481 pst.page_size / 1024 / time_diff);
482 /*
483 * LINTED cast needed, really
484 */
485 swapout =
486 (unsigned int) ((css_new->css_swapout - css_old->css_swapout) *
487 pst.page_size / 1024 / time_diff);
488 /*
489 * LINTED cast needed, really
490 */
491 blocks_read =
492 (unsigned
493 int) ((css_new->css_blocks_read -
494 css_old->css_blocks_read) / time_diff);
495 /*
496 * LINTED cast needed, really
497 */
498 blocks_write =
499 (unsigned
500 int) ((css_new->css_blocks_write -
501 css_old->css_blocks_write) / time_diff);
502 /*
503 * LINTED cast needed, really
504 */
505 interrupts =
506 (unsigned
507 int) ((css_new->css_interrupts -
508 css_old->css_interrupts) / time_diff);
509 /*
510 * LINTED cast needed, really
511 */
512 context_sw =
513 (unsigned
514 int) ((css_new->css_context_sw -
515 css_old->css_context_sw) / time_diff);
516
517 /*
518 * Loop thru all the CPUSTATES and get the differences
519 */
520 for (i = 0; i < CPUSTATES; i++) {
521 cpu_sum += (css_new->css_cpu[i] - css_old->css_cpu[i]);
522 }
523
524 /*
525 * Now calculate the absolute percentage values
526 * Looks somewhat complicated sometimes but tries to get around using floats to increase speed
527 */
528 for (i = 0; i < CPUSTATES; i++) {
529 /*
530 * Since we don't return fractions we use + 0.5 to get between 99 and 101 percent adding the values
531 * together, otherwise we would get less than 100 most of the time
532 */
533 /*
534 * LINTED has to be 'long'
535 */
536 cpu_perc[i] =
537 (long) (((css_new->css_cpu[i] -
538 css_old->css_cpu[i]) * 100 +
539 (cpu_sum / 2)) / cpu_sum);
540 }
541
542 /*
543 * As said before, MIB wants CPU_SYSTEM which is CP_SYS + CP_WAIT
544 */
545 /*
546 * LINTED has to be 'long'
547 */
548 cpu_perc[CPU_SYSTEM] =
549 (long) ((((css_new->css_cpu[CP_SYS] - css_old->css_cpu[CP_SYS])
550 + (css_new->css_cpu[CP_WAIT] -
551 css_old->css_cpu[CP_WAIT]))
552 * 100 + (cpu_sum / 2)) / cpu_sum);
553 }
554
555 /*
556 * Make the current one the first one and move the whole thing one place down
557 */
558 memmove(&snapshot[1], &snapshot[0],
559 (size_t) (((char *) &snapshot[POLL_VALUES]) -
560 ((char *) &snapshot[0])));
561
562 /*
563 * Erase the current one
564 */
565 memset(&snapshot[0], 0, sizeof snapshot[0]);
566
567 /*
568 * Only important on start up, we keep track of how many snapshots we have taken so far
569 */
570 if (number_of_snapshots < POLL_VALUES) {
571 number_of_snapshots++;
572 }
573 } /* update_stats ends here */
574
575 /*
576 * *var_extensible_vmstat starts here
577 * The guts of the module, this routine gets called to service a request
578 */
579 unsigned char *
580 var_extensible_vmstat(struct variable *vp,
581 oid * name,
582 size_t * length,
583 int exact,
584 size_t * var_len, WriteMethod ** write_method)
585 {
586 /*
587 * Needed for returning the values
588 */
589 static long long_ret;
590 static char errmsg[300];
591
592 /*
593 * set to 0 as default
594 */
595 long_ret = 0;
596
597 /*
598 * generic check whether the options passed make sense and whether the
599 * right variable is requested
600 */
601 if (header_generic(vp, name, length, exact, var_len, write_method) !=
602 MATCH_SUCCEEDED) {
603 return (NULL);
604 }
605
606 /*
607 * The function that actually returns s.th.
608 */
609 switch (vp->magic) {
610 case MIBINDEX:
611 long_ret = 1;
612 return ((u_char *) (&long_ret));
613 case ERRORNAME: /* dummy name */
614 sprintf(errmsg, "systemStats");
615 *var_len = strlen(errmsg);
616 return ((u_char *) (errmsg));
617 case SWAPIN:
618 return ((u_char *) (&swapin));
619 case SWAPOUT:
620 return ((u_char *) (&swapout));
621 case IOSENT:
622 return ((u_char *) (&blocks_write));
623 case IORECEIVE:
624 return ((u_char *) (&blocks_read));
625 case SYSINTERRUPTS:
626 return ((u_char *) (&interrupts));
627 case SYSCONTEXT:
628 return ((u_char *) (&context_sw));
629 case CPUUSER:
630 return ((u_char *) (&cpu_perc[CP_USER]));
631 case CPUSYSTEM:
632 return ((u_char *) (&cpu_perc[CPU_SYSTEM]));
633 case CPUIDLE:
634 return ((u_char *) (&cpu_perc[CP_IDLE]));
635 case CPURAWUSER:
636 take_snapshot(&raw_values);
637 /*
638 * LINTED has to be 'long'
639 */
640 long_ret =
641 (long) (raw_values.css_cpu[CP_USER] / raw_values.css_cpus);
642 return ((u_char *) (&long_ret));
643 case CPURAWNICE:
644 take_snapshot(&raw_values);
645 /*
646 * LINTED has to be 'long'
647 */
648 long_ret =
649 (long) (raw_values.css_cpu[CP_NICE] / raw_values.css_cpus);
650 return ((u_char *) (&long_ret));
651 case CPURAWSYSTEM:
652 take_snapshot(&raw_values);
653 /*
654 * LINTED has to be 'long'
655 */
656 long_ret =
657 (long) ((raw_values.css_cpu[CP_SYS] +
658 raw_values.css_cpu[CP_WAIT]) / raw_values.css_cpus);
659 return ((u_char *) (&long_ret));
660 case CPURAWIDLE:
661 take_snapshot(&raw_values);
662 /*
663 * LINTED has to be 'long'
664 */
665 long_ret =
666 (long) (raw_values.css_cpu[CP_IDLE] / raw_values.css_cpus);
667 return ((u_char *) (&long_ret));
668 case CPURAWWAIT:
669 take_snapshot(&raw_values);
670 /*
671 * LINTED has to be 'long'
672 */
673 long_ret =
674 (long) (raw_values.css_cpu[CP_WAIT] / raw_values.css_cpus);
675 return ((u_char *) (&long_ret));
676 case CPURAWKERNEL:
677 take_snapshot(&raw_values);
678 /*
679 * LINTED has to be 'long'
680 */
681 long_ret =
682 (long) (raw_values.css_cpu[CP_SYS] / raw_values.css_cpus);
683 return ((u_char *) (&long_ret));
684 case IORAWSENT:
685 take_snapshot(&raw_values);
686 /*
687 * LINTED has to be 'long'
688 */
689 long_ret = (long) (raw_values.css_blocks_write);
690 return ((u_char *) (&long_ret));
691 case IORAWRECEIVE:
692 take_snapshot(&raw_values);
693 /*
694 * LINTED has to be 'long'
695 */
696 long_ret = (long) (raw_values.css_blocks_read);
697 return ((u_char *) (&long_ret));
698
699 /*
700 * reserved for future use
701 */
702 /*
703 * case ERRORFLAG:
704 * return((u_char *) (&long_ret));
705 * case ERRORMSG:
706 * return((u_char *) (&long_ret));
707 */
708 default:
709 snmp_log(LOG_ERR,
710 "vmstat_hpux: Error in request, no match found.\n");
711 }
712 return (NULL);
713 } /* *var_extensible_vmstat ends here */
714
715 /*
716 * Functions end here
717 */
718
719 /*
720 * Program ends here
721 */