3 * UCD SNMP module for systemStats section of UCD-SNMP-MIB for HPUX 10.x/11.x
7 * To make lint skip the debug code and stop complaining
10 #define SNMP_NO_DEBUGGING 1
18 * UCD-SNMP config details
20 #include <net-snmp/net-snmp-config.h>
27 #include <sys/types.h>
32 * pstat and sysinfo structs
34 #include <sys/pstat.h>
39 * Includes needed for all modules
41 #include <net-snmp/net-snmp-includes.h>
42 #include <net-snmp/agent/net-snmp-agent-includes.h>
46 * Utility functions for UCD-SNMP
48 #include "util_funcs.h"
51 * Header file for this module
54 #include "vmstat_hpux.h"
62 * Global structures start here
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
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
80 struct cpu_stat_snapshot {
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];
93 * Define a structure to hold kernel static information
95 struct pst_static pst;
98 * Global structures end here
102 * Global variables start here
106 * Variables for the calculated values, filled in update_stats
109 * Need to be global since we need them in more than one function
111 static unsigned long swapin;
112 static unsigned long swapout;
113 static unsigned long blocks_read;
114 static unsigned long blocks_write;
115 static unsigned long interrupts;
116 static unsigned long context_sw;
119 * Since MIB wants CPU_SYSTEM, which is CP_SYS + CP_WAIT (see sys/dk.h)
121 static long cpu_perc[CPUSTATES + 1];
124 * How many snapshots we have already taken, needed for the first
127 * POLL_INTERVAL * POLL_VALUES seconds of agent running
129 static unsigned int number_of_snapshots;
132 * The place to store the snapshots of system data in
134 static struct cpu_stat_snapshot snapshot[POLL_VALUES + 1];
137 * And one for the raw counters, which we fill when the raw values are
140 * requested, as opposed to the absolute values, which are taken every
143 * POLL_INTERVAL seconds and calculated over POLL_INTERVAL * POLL_VALUES time
145 static struct cpu_stat_snapshot raw_values;
148 * Global variables end here
153 * Functions start here
159 static void update_stats(unsigned int registrationNumber,
161 static int take_snapshot(struct cpu_stat_snapshot *css);
164 * init_vmstat_hpux starts here
167 * Init function for this module, from prototype
170 * Defines variables handled by this module, defines root OID for
173 * this module and registers it with the agent
176 FindVarMethod var_extensible_vmstat;
179 init_vmstat_hpux(void)
183 * Which variables do we service ?
185 struct variable2 extensible_vmstat_variables[] = {
186 {MIBINDEX, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
188 {ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1,
190 {SWAPIN, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPIN}},
191 {SWAPOUT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPOUT}},
192 {IOSENT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {IOSENT}},
193 {IORECEIVE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
195 {SYSINTERRUPTS, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
197 {SYSCONTEXT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
199 {CPUUSER, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUUSER}},
200 {CPUSYSTEM, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
202 {CPUIDLE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUIDLE}},
203 {CPURAWUSER, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
205 {CPURAWNICE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
207 {CPURAWSYSTEM, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
209 {CPURAWIDLE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
211 {CPURAWWAIT, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
213 {CPURAWKERNEL, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
215 {IORAWSENT, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
217 {IORAWRECEIVE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
223 * {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {ERRORFLAG }},
224 * {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1, {ERRORMSG }}
229 * Define the OID pointer to the top of the mib tree that we're
232 * registering underneath
234 oid vmstat_variables_oid[] = { UCDAVIS_MIB, 11 };
237 * register ourselves with the agent to handle our mib tree
240 * LINTED Trust me, I know what I'm doing
242 REGISTER_MIB("ucd-snmp/vmstat", extensible_vmstat_variables, variable2,
243 vmstat_variables_oid);
246 * Start with some useful data
248 update_stats(0, NULL);
251 * update_stats is run every POLL_INTERVAL seconds using this routine
254 * (see 'man snmp_alarm')
257 * This is only executed once to get some useful data in the beginning
259 if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {
260 snmp_log(LOG_WARNING,
261 "vmstat_hpux (init): snmp_alarm_register failed.\n");
264 * This is the one that runs update_stats every POLL_INTERVAL seconds
266 if (snmp_alarm_register(POLL_INTERVAL, SA_REPEAT, update_stats, NULL)
269 "vmstat_hpux (init): snmp_alarm_register failed, cannot service requests.\n");
272 } /* init_vmstat_hpux ends here */
275 * Data collection function take_snapshot starts here
278 * Get data from kernel and save into the snapshot strutcs
281 * Argument is the snapshot struct to save to. Global anyway, but looks nicer
284 take_snapshot(struct cpu_stat_snapshot *css)
287 * Variables start here
290 struct pst_dynamic psd;
291 struct pst_processor *psp;
292 struct pst_vminfo psv;
300 * The usual stuff to count on, err, by
309 * Function starts here
318 * If we have just gotten the data, return the values from last run (skip if-clause)
321 * This happens on a snmpwalk request. No need to read the pstat again
324 * if we just did it less than 2 seconds ago
327 * Jumps into if-clause either when snapshot is empty or when too old
330 if ((css->css_time == 0) || (current_time > css->css_time + 2)) {
332 * Make sure we clean up before we put new data into snapshot
334 memset(css, 0, sizeof *css);
339 css->css_time = current_time;
341 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) != -1) {
342 css->css_cpus = psd.psd_proc_cnt;
343 DEBUGMSGTL(("take_snapshot", "*** Number of CPUs: %d\n",
347 * We need a for-loop for the CPU values
349 for (i = 0; i < CPUSTATES; i++) {
350 css->css_cpu[i] = (unsigned long long) psd.psd_cpu_time[i];
351 DEBUGMSGTL(("take_snapshot",
352 "*** Time for CPU state %d: %d\n", i,
353 psd.psd_cpu_time[i]));
356 (struct pst_processor *) malloc(css->css_cpus *
358 if (pstat_getprocessor(psp, sizeof(*psp), css->css_cpus, 0) !=
361 for (i = 0; i < css->css_cpus; i++) {
362 css->css_blocks_read = psp[i].psp_phread;
363 css->css_blocks_write = psp[i].psp_phwrite;
367 "vmstat_hpux (take_snapshot): pstat_getprocessor failed!\n");
370 "vmstat_hpux (take_snapshot): pstat_getdynamic failed!\n");
372 if (pstat_getvminfo(&psv, sizeof(psv), (size_t) 1, 0) != -1) {
373 css->css_swapin = psv.psv_sswpin;
374 css->css_swapout = psv.psv_sswpout;
375 css->css_interrupts = psv.psv_sintr;
376 css->css_context_sw = psv.psv_sswtch;
379 "vmstat_hpux (take_snapshot): pstat_getvminfo failed!\n");
384 * All engines running at warp speed, no problems (if there are any engines, that is)
386 return (css->css_cpus > 0 ? 0 : -1);
387 } /* take_snapshot ends here */
390 * This gets called every POLL_INTERVAL seconds to update the snapshots. It takes a new snapshot and
393 * drops the oldest one. This way we move the time window so we always take the values over
396 * POLL_INTERVAL * POLL_VALUES seconds and update the data used every POLL_INTERVAL seconds
399 * The alarm timer is in the init function of this module (snmp_alarm_register)
405 update_stats(unsigned int registrationNumber, void *clientarg)
408 * The time between the samples we compare
413 * Easier to use these than the snapshots, short hand pointers
415 struct cpu_stat_snapshot *css_old, *css_new;
418 * The usual stuff to count on, err, by
423 * The sum of the CPU ticks that have passed on the different CPU states, so we can calculate
426 * the percentages of each state
428 unsigned long long cpu_sum = 0;
430 DEBUGMSGTL(("ucd-snmp/vmstat_hpux.c:update_stats",
431 "updating stats\n"));
434 * Take the current snapshot
436 if (take_snapshot(&snapshot[0]) == -1) {
437 snmp_log(LOG_WARNING,
438 "vmstat_hpux (update_stats): Something went wrong with take_snapshot.\n");
443 * Do we have some data we can use ? An issue right after the start of the agent
445 if (number_of_snapshots > 0) {
447 * Huh, the number of CPUs changed during run time. That is indeed s.th. worth noting, we
450 * output a humorous (more or less) syslog message and need to retake the snapshots
452 if (snapshot[0].css_cpus != snapshot[1].css_cpus) {
453 if (snapshot[0].css_cpus > snapshot[1].css_cpus) {
455 "vmstat_hpux (update_stats): Cool ! Number of CPUs increased, must be hot-pluggable.\n");
458 "vmstat_hpux (update_stats): Lost at least one CPU, RIP.\n");
461 * Make all snapshots but the current one invalid
463 number_of_snapshots = 1;
465 * Move the current one in the "first" [1] slot
467 memmove(&snapshot[1], &snapshot[0], sizeof snapshot[0]);
469 * Erase the current one
471 memset(&snapshot[0], 0, sizeof snapshot[0]);
473 * Try to get a new snapshot in five seconds so we can return s.th. useful
475 if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {
476 snmp_log(LOG_WARNING,
477 "vmstat_hpux (update_stats): snmp_alarm_register failed.\n");
483 * Short hand pointers
485 css_new = &snapshot[0];
486 css_old = &snapshot[number_of_snapshots];
489 * How much time has passed between the snapshots we get the values from ?
492 (snapshot[0].css_time -
493 snapshot[number_of_snapshots].css_time);
495 DEBUGMSGTL(("ucd-snmp/vmstat_hpux.c:update_stats",
496 "time_diff: %lld\n", time_diff));
499 * swapin and swapout are in pages, MIB wants kB/s,so we just need to get kB and seconds
502 * For the others we need to get value per second
505 * Retreive static information to obtain memory page_size
507 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
508 snmp_log(LOG_ERR, "vmstat_hpux: pstat_getstatic failed!\n");
512 * LINTED cast needed, really
515 (unsigned int) ((css_new->css_swapin - css_old->css_swapin) *
516 pst.page_size / 1024 / time_diff);
518 * LINTED cast needed, really
521 (unsigned int) ((css_new->css_swapout - css_old->css_swapout) *
522 pst.page_size / 1024 / time_diff);
524 * LINTED cast needed, really
528 int) ((css_new->css_blocks_read -
529 css_old->css_blocks_read) / time_diff);
531 * LINTED cast needed, really
535 int) ((css_new->css_blocks_write -
536 css_old->css_blocks_write) / time_diff);
538 * LINTED cast needed, really
542 int) ((css_new->css_interrupts -
543 css_old->css_interrupts) / time_diff);
545 * LINTED cast needed, really
549 int) ((css_new->css_context_sw -
550 css_old->css_context_sw) / time_diff);
553 * Loop thru all the CPUSTATES and get the differences
555 for (i = 0; i < CPUSTATES; i++) {
556 cpu_sum += (css_new->css_cpu[i] - css_old->css_cpu[i]);
560 * Now calculate the absolute percentage values
563 * Looks somewhat complicated sometimes but tries to get around using floats to increase speed
565 for (i = 0; i < CPUSTATES; i++) {
567 * Since we don't return fractions we use + 0.5 to get between 99 and 101 percent adding the values
570 * together, otherwise we would get less than 100 most of the time
573 * LINTED has to be 'long'
576 (long) (((css_new->css_cpu[i] -
577 css_old->css_cpu[i]) * 100 +
578 (cpu_sum / 2)) / cpu_sum);
582 * As said before, MIB wants CPU_SYSTEM which is CP_SYS + CP_WAIT
585 * LINTED has to be 'long'
587 cpu_perc[CPU_SYSTEM] =
588 (long) ((((css_new->css_cpu[CP_SYS] - css_old->css_cpu[CP_SYS])
589 + (css_new->css_cpu[CP_WAIT] -
590 css_old->css_cpu[CP_WAIT]))
591 * 100 + (cpu_sum / 2)) / cpu_sum);
595 * Make the current one the first one and move the whole thing one place down
597 memmove(&snapshot[1], &snapshot[0],
598 (size_t) (((char *) &snapshot[POLL_VALUES]) -
599 ((char *) &snapshot[0])));
602 * Erase the current one
604 memset(&snapshot[0], 0, sizeof snapshot[0]);
607 * Only important on start up, we keep track of how many snapshots we have taken so far
609 if (number_of_snapshots < POLL_VALUES) {
610 number_of_snapshots++;
612 } /* update_stats ends here */
615 * *var_extensible_vmstat starts here
618 * The guts of the module, this routine gets called to service a request
621 var_extensible_vmstat(struct variable *vp,
625 size_t * var_len, WriteMethod ** write_method)
628 * Needed for returning the values
630 static long long_ret;
631 static char errmsg[300];
634 * set to 0 as default
639 * generic check whether the options passed make sense and whether the
642 * right variable is requested
644 if (header_generic(vp, name, length, exact, var_len, write_method) !=
650 * The function that actually returns s.th.
655 return ((u_char *) (&long_ret));
656 case ERRORNAME: /* dummy name */
657 sprintf(errmsg, "systemStats");
658 *var_len = strlen(errmsg);
659 return ((u_char *) (errmsg));
661 return ((u_char *) (&swapin));
663 return ((u_char *) (&swapout));
665 return ((u_char *) (&blocks_write));
667 return ((u_char *) (&blocks_read));
669 return ((u_char *) (&interrupts));
671 return ((u_char *) (&context_sw));
673 return ((u_char *) (&cpu_perc[CP_USER]));
675 return ((u_char *) (&cpu_perc[CPU_SYSTEM]));
677 return ((u_char *) (&cpu_perc[CP_IDLE]));
679 take_snapshot(&raw_values);
681 * LINTED has to be 'long'
684 (long) (raw_values.css_cpu[CP_USER] / raw_values.css_cpus);
685 return ((u_char *) (&long_ret));
687 take_snapshot(&raw_values);
689 * LINTED has to be 'long'
692 (long) (raw_values.css_cpu[CP_NICE] / raw_values.css_cpus);
693 return ((u_char *) (&long_ret));
695 take_snapshot(&raw_values);
697 * LINTED has to be 'long'
700 (long) ((raw_values.css_cpu[CP_SYS] +
701 raw_values.css_cpu[CP_WAIT]) / raw_values.css_cpus);
702 return ((u_char *) (&long_ret));
704 take_snapshot(&raw_values);
706 * LINTED has to be 'long'
709 (long) (raw_values.css_cpu[CP_IDLE] / raw_values.css_cpus);
710 return ((u_char *) (&long_ret));
712 take_snapshot(&raw_values);
714 * LINTED has to be 'long'
717 (long) (raw_values.css_cpu[CP_WAIT] / raw_values.css_cpus);
718 return ((u_char *) (&long_ret));
720 take_snapshot(&raw_values);
722 * LINTED has to be 'long'
725 (long) (raw_values.css_cpu[CP_SYS] / raw_values.css_cpus);
726 return ((u_char *) (&long_ret));
728 take_snapshot(&raw_values);
730 * LINTED has to be 'long'
732 long_ret = (long) (raw_values.css_blocks_write);
733 return ((u_char *) (&long_ret));
735 take_snapshot(&raw_values);
737 * LINTED has to be 'long'
739 long_ret = (long) (raw_values.css_blocks_read);
740 return ((u_char *) (&long_ret));
743 * reserved for future use
747 * return((u_char *) (&long_ret));
749 * return((u_char *) (&long_ret));
753 "vmstat_hpux: Error in request, no match found.\n");
756 } /* *var_extensible_vmstat ends here */