3 * UCD SNMP module for systemStats section of UCD-SNMP-MIB for Dynix
4 * Patrick Hess <phess@phess.best.vwh.net>
6 * This is just a port of the vmstat_solaris2 code Version 0.7
11 * To make lint skip the debug code and stop complaining
14 #define SNMP_NO_DEBUGGING 1
17 #define __NO_ASM_MACRO 1
22 #include <net-snmp/net-snmp-config.h>
27 #include <sys/tmp_ctl.h>
28 #include <sys/sysperf.h>
29 #include <sys/vmmeter.h>
33 #include <net-snmp/net-snmp-includes.h>
34 #include <net-snmp/agent/net-snmp-agent-includes.h>
37 #include "util_funcs.h"
40 * Header file for this module
43 #include "vmstat_dynix.h"
51 * Global structures start here
55 * A structure to save data gathered from the kernel kstat interface to.
58 * We used to have the sys/sysinfo.h cpu_stat_t here but we did not need
61 * all of it, some in a different size and some additional ones so we build
66 struct cpu_stat_snapshot {
68 unsigned int css_cpus;
69 unsigned long long css_swapin;
70 unsigned long long css_swapout;
71 unsigned long long css_blocks_read;
72 unsigned long long css_blocks_write;
73 unsigned long long css_interrupts;
74 unsigned long long css_context_sw;
75 unsigned long long css_cpu[V_CPU_STATES];
79 * Global structures end here
84 * Global variables start here
88 * Variables for the calculated values, filled in update_stats
91 * Need to be global since we need them in more than one function
95 static ulong blocks_read;
96 static ulong blocks_write;
97 static ulong interrupts;
98 static ulong context_sw;
101 * Since MIB wants V_CPU_SYSTEM, which is V_CPU_KERNEL + V_CPU_STREAM
103 static long cpu_perc[V_CPU_STATES + 1];
106 * How many snapshots we have already taken, needed for the first
109 * POLL_INTERVAL * POLL_VALUES seconds of agent running
111 static unsigned int number_of_snapshots;
114 * The place to store the snapshots of system data in
116 static struct cpu_stat_snapshot snapshot[POLL_VALUES + 1];
119 * And one for the raw counters, which we fill when the raw values are
122 * requested, as opposed to the absolute values, which are taken every
125 * POLL_INTERVAL seconds and calculated over POLL_INTERVAL * POLL_VALUES time
127 static struct cpu_stat_snapshot raw_values;
130 * Global variables end here
135 * Functions start here
141 static void update_stats(unsigned int registrationNumber,
143 static int take_snapshot(struct cpu_stat_snapshot *css);
146 * init_vmstat_dynix starts here
149 * Init function for this module, from prototype
152 * Defines variables handled by this module, defines root OID for
155 * this module and registers it with the agent
158 FindVarMethod var_extensible_vmstat;
161 init_vmstat_dynix(void)
165 * Which variables do we service ?
167 struct variable2 extensible_vmstat_variables[] = {
168 {MIBINDEX, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
170 {ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1,
172 {SWAPIN, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPIN}},
173 {SWAPOUT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPOUT}},
174 {IOSENT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {IOSENT}},
175 {IORECEIVE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
177 {SYSINTERRUPTS, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
179 {SYSCONTEXT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
181 {CPUUSER, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUUSER}},
182 {CPUSYSTEM, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
184 {CPUIDLE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUIDLE}},
185 {CPURAWUSER, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
187 {CPURAWSYSTEM, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
189 {CPURAWIDLE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
191 {CPURAWWAIT, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
193 {CPURAWKERNEL, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
195 {IORAWSENT, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
197 {IORAWRECEIVE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
203 * {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {ERRORFLAG }},
204 * {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1, {ERRORMSG }}
209 * Define the OID pointer to the top of the mib tree that we're
212 * registering underneath
214 oid vmstat_variables_oid[] = { UCDAVIS_MIB, 11 };
217 * register ourselves with the agent to handle our mib tree
220 * LINTED Trust me, I know what I'm doing
222 REGISTER_MIB("ucd-snmp/vmstat", extensible_vmstat_variables, variable2,
223 vmstat_variables_oid);
226 * Start with some useful data
228 update_stats(0, NULL);
231 * update_stats is run every POLL_INTERVAL seconds using this routine
234 * (see 'man snmp_alarm')
237 * This is only executed once to get some useful data in the beginning
239 if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {
240 snmp_log(LOG_WARNING,
241 "vmstat_dynix (init): snmp_alarm_register failed.\n");
244 * This is the one that runs update_stats every POLL_INTERVAL seconds
246 if (snmp_alarm_register(POLL_INTERVAL, SA_REPEAT, update_stats, NULL)
249 "vmstat_dynix (init): snmp_alarm_register failed, cannot service requests.\n");
252 } /* init_vmstat_dynix ends here */
255 * Data collection function take_snapshot starts here
258 * Get data from kernel and save into the snapshot strutcs
261 * Argument is the snapshot struct to save to. Global anyway, but looks nicer
264 take_snapshot(struct cpu_stat_snapshot *css)
267 * Variables start here
273 unsigned int cpu_num = 0;
276 * Low resolution time counter
281 * see sys/sysperf.h, holds CPU data
283 exp_vmmeter_t *cs, *origcs = 0;
286 * size of the cs struct
291 * The usual stuff to count on, err, by
301 * Function starts here
307 current_time = time(0);
310 * If we have just gotten the data, return the values from last run (skip if-clause)
313 * This happens on a snmpwalk request.
316 * if we just did it less than 2 seconds ago
319 * Jumps into if-clause either when snapshot is empty or when too old
322 if ((css->css_time == 0) || (current_time > css->css_time + 2)) {
324 * Make sure we clean up before we put new data into snapshot
326 memset(css, 0, sizeof *css);
329 * Get the number of CPUs we gather data from
331 if ((cpu_num = tmp_ctl(TMP_NENG, 0)) < 0) {
333 "vmstat_dynix: (take snapshot) bad tmp_ctl return\n");
336 css->css_cpus = cpu_num;
338 vminfo_size = cpu_num * sizeof(exp_vmmeter_t);
340 if (!(cs = (exp_vmmeter_t *) malloc(vminfo_size))) {
342 "vmstat_dynix: (take_snapshot) bad malloc return\n");
350 css->css_time = current_time;
353 * Read data from kernel into cs structure
356 * cs is the buffer we are writing to and
359 * vminfo_size is the size of the cs struct
361 if ((getkerndata(VMMETER_DATAID, cs, vminfo_size)) < 0) {
363 "vmstat_dynix (take_snapshot): getkerndata failure.");
368 * Get the data from each CPU
371 * We walk through the whole vmmeter struct and sum up all the found stats,
374 * there's one for every CPU in a machine
377 * Okay... you can't laugh at this! I'm a C-hack, not a C-coder. :)
379 while (engnum < cpu_num) {
382 * Get the data from the cs structure and sum it up in our own structure
384 css->css_swapin += (unsigned long long) cs->v_swpin;
385 css->css_swapout += (unsigned long long) cs->v_swpout;
386 css->css_blocks_read += (unsigned long long) cs->v_phread;
387 css->css_blocks_write += (unsigned long long) cs->v_phwrite;
388 css->css_interrupts += (unsigned long long) cs->v_intr;
389 css->css_context_sw += (unsigned long long) cs->v_swtch;
392 * We need a for-loop for the CPU STATE values
394 for (i = 0; i < V_CPU_STATES; i++) {
395 css->css_cpu[i] += (unsigned long long) cs->v_time[i];
403 free((void *) origcs);
406 * All engines running at warp speed, no problems (if there are any engines, that is)
408 return (cpu_num > 0 ? 0 : -1);
409 } /* take_snapshot ends here */
412 * This gets called every POLL_INTERVAL seconds to update the snapshots. It takes a new snapshot and
415 * drops the oldest one. This way we move the time window so we always take the values over
418 * POLL_INTERVAL * POLL_VALUES seconds and update the data used every POLL_INTERVAL seconds
421 * The alarm timer is in the init function of this module (snmp_alarm_register)
427 update_stats(unsigned int registrationNumber, void *clientarg)
430 * The time between the samples we compare
435 * Easier to use these than the snapshots, short hand pointers
437 struct cpu_stat_snapshot *css_old, *css_new;
440 * The usual stuff to count on, err, by
445 * The sum of the CPU ticks that have passed on the different CPU states, so we can calculate
448 * the percentages of each state
450 unsigned long long cpu_sum = 0;
452 DEBUGMSGTL(("ucd-snmp/vmstat_dynix.c:update_stats",
453 "updating stats\n"));
456 * Take the current snapshot
458 if (take_snapshot(&snapshot[0]) == -1) {
459 snmp_log(LOG_WARNING,
460 "vmstat_dynix (update_stats): Something went wrong with take_snapshot.");
465 * Do we have some data we can use ? An issue right after the start of the agent
467 if (number_of_snapshots > 0) {
469 * Huh, the number of CPUs changed during run time. That is indeed s.th. worth noting, we
472 * output a humorous (more or less) syslog message and need to retake the snapshots
474 if (snapshot[0].css_cpus != snapshot[1].css_cpus) {
475 if (snapshot[0].css_cpus > snapshot[1].css_cpus) {
477 "vmstat_dynix (update_stats): Cool ! Number of CPUs increased, must be hot-pluggable.");
480 "vmstat_dynix (update_stats): Lost at least one CPU, RIP.");
483 * Make all snapshots but the current one invalid
485 number_of_snapshots = 1;
487 * Move the current one in the "first" [1] slot
489 memmove(&snapshot[1], &snapshot[0], sizeof snapshot[0]);
491 * Erase the current one
493 memset(&snapshot[0], 0, sizeof snapshot[0]);
495 * Try to get a new snapshot in five seconds so we can return s.th. useful
497 if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {
498 snmp_log(LOG_WARNING,
499 "vmstat_dynix (update_stats): snmp_alarm_register failed.\n");
505 * Short hand pointers
507 css_new = &snapshot[0];
508 css_old = &snapshot[number_of_snapshots];
511 * How much time has passed between the snapshots we get the values from ?
517 snapshot[0].css_time - snapshot[number_of_snapshots].css_time;
520 DEBUGMSGTL(("ucd-snmp/vmstat_dynix.c:update_stats",
521 "time_diff is ZERO... watch for the segfault\n"));
523 DEBUGMSGTL(("ucd-snmp/vmstat_dynix.c:update_stats",
524 "time_diff: %lld\n", time_diff));
527 * swapin and swapout are in pages, MIB wants kB/s,so we just need to get kB and seconds
530 * For the others we need to get value per second
533 * decided to use sysconf(_SC_PAGESIZE) instead to get around an #ifndef (I don't like those)
536 * LINTED cast needed, really
539 (uint_t) ((css_new->css_swapin -
540 css_old->css_swapin) * (time_t) 1000 *
541 sysconf(_SC_PAGESIZE) / 1024 / time_diff);
543 * LINTED cast needed, really
546 (uint_t) ((css_new->css_swapout -
547 css_old->css_swapout) * (time_t) 1000 *
548 sysconf(_SC_PAGESIZE) / 1024 / time_diff);
550 * LINTED cast needed, really
553 (uint_t) ((css_new->css_blocks_read -
554 css_old->css_blocks_read) * (time_t) 1000 /
557 * LINTED cast needed, really
560 (uint_t) ((css_new->css_blocks_write -
561 css_old->css_blocks_write) * (time_t) 1000 /
564 * LINTED cast needed, really
567 (uint_t) ((css_new->css_interrupts -
568 css_old->css_interrupts) * (time_t) 1000 /
571 * LINTED cast needed, really
574 (uint_t) ((css_new->css_context_sw -
575 css_old->css_context_sw) * (time_t) 1000 /
579 * Loop thru all the V_CPU_STATES and get the differences
581 for (i = 0; i < V_CPU_STATES; i++) {
582 cpu_sum += (css_new->css_cpu[i] - css_old->css_cpu[i]);
586 * Now calculate the absolute percentage values
589 * Looks somewhat complicated sometimes but tries to get around using floats to increase speed
591 for (i = 0; i < V_CPU_STATES; i++) {
593 * Since we don't return fractions we use + 0.5 to get between 99 and 101 percent adding the values
596 * together, otherwise we would get less than 100 most of the time
599 * LINTED has to be 'long'
602 (long) (((css_new->css_cpu[i] -
603 css_old->css_cpu[i]) * 100 +
604 (cpu_sum / 2)) / cpu_sum);
608 * As said before, MIB wants V_CPU_SYSTEM which is V_CPU_KERNEL + V_CPU_STREAM
611 * LINTED has to be 'long'
613 cpu_perc[V_CPU_SYSTEM] =
614 (long) ((((css_new->css_cpu[V_CPU_KERNEL] -
615 css_old->css_cpu[V_CPU_KERNEL])
616 + (css_new->css_cpu[V_CPU_STREAM] -
617 css_old->css_cpu[V_CPU_STREAM]))
618 * 100 + (cpu_sum / 2)) / cpu_sum);
622 * Make the current one the first one and move the whole thing one place down
624 memmove(&snapshot[1], &snapshot[0],
625 (size_t) (((char *) &snapshot[POLL_VALUES]) -
626 ((char *) &snapshot[0])));
629 * Erase the current one
631 memset(&snapshot[0], 0, sizeof snapshot[0]);
634 * Only important on start up, we keep track of how many snapshots we have taken so far
636 if (number_of_snapshots < POLL_VALUES) {
637 number_of_snapshots++;
639 } /* update_stats ends here */
642 * *var_extensible_vmstat starts here
645 * The guts of the module, this routine gets called to service a request
648 var_extensible_vmstat(struct variable *vp,
652 size_t * var_len, WriteMethod ** write_method)
655 * Needed for returning the values
657 static long long_ret;
658 static char errmsg[300];
661 * set to 0 as default
666 * generic check whether the options passed make sense and whether the
669 * right variable is requested
671 if (header_generic(vp, name, length, exact, var_len, write_method) !=
677 * The function that actually returns s.th.
682 return ((u_char *) (&long_ret));
683 case ERRORNAME: /* dummy name */
684 sprintf(errmsg, "systemStats");
685 *var_len = strlen(errmsg);
686 return ((u_char *) (errmsg));
688 return ((u_char *) (&swapin));
690 return ((u_char *) (&swapout));
692 return ((u_char *) (&blocks_write));
694 return ((u_char *) (&blocks_read));
696 return ((u_char *) (&interrupts));
698 return ((u_char *) (&context_sw));
700 return ((u_char *) (&cpu_perc[V_CPU_USER]));
702 return ((u_char *) (&cpu_perc[V_CPU_SYSTEM]));
704 return ((u_char *) (&cpu_perc[V_CPU_IDLE]));
706 take_snapshot(&raw_values);
708 * LINTED has to be 'long'
711 (long) (raw_values.css_cpu[V_CPU_USER] / raw_values.css_cpus);
712 return ((u_char *) (&long_ret));
714 * We are missing CPURAWNICE, Dynix does not account for this in the kernel so this OID can not
717 * be returned. Also, these values will roll over sooner or later and then return inaccurate data
720 * but the MIB wants Integer32 so we cannot put a counter here
723 * (Has been changed to Counter32 in the latest MIB version!)
726 take_snapshot(&raw_values);
728 * LINTED has to be 'long'
731 (long) ((raw_values.css_cpu[V_CPU_KERNEL] +
732 raw_values.css_cpu[V_CPU_STREAM]) /
733 raw_values.css_cpus);
734 return ((u_char *) (&long_ret));
736 take_snapshot(&raw_values);
738 * LINTED has to be 'long'
741 (long) (raw_values.css_cpu[V_CPU_IDLE] / raw_values.css_cpus);
742 return ((u_char *) (&long_ret));
744 take_snapshot(&raw_values);
746 * LINTED has to be 'long'
749 (long) (raw_values.css_cpu[V_CPU_STREAM] /
750 raw_values.css_cpus);
751 return ((u_char *) (&long_ret));
753 take_snapshot(&raw_values);
755 * LINTED has to be 'long'
758 (long) (raw_values.css_cpu[V_CPU_KERNEL] /
759 raw_values.css_cpus);
760 return ((u_char *) (&long_ret));
762 long_ret = (long) (raw_values.css_blocks_write);
763 return ((u_char *) (&long_ret));
765 long_ret = (long) (raw_values.css_blocks_read);
766 return ((u_char *) (&long_ret));
769 * reserved for future use
773 * return((u_char *) (&long_ret));
775 * return((u_char *) (&long_ret));
779 "vmstat_dynix: Error in request, no match found.\n");
782 } /* *var_extensible_vmstat ends here */