| 1 |
/* |
| 2 |
* vmstat_solaris2.c |
| 3 |
* UCD SNMP module for systemStats section of UCD-SNMP-MIB for SunOS/Solaris |
| 4 |
* Jochen Kmietsch <[email protected]> |
| 5 |
* with fixes and additions from the UCD-SNMP community |
| 6 |
* Uses some ideas from xosview and top |
| 7 |
* Some comments paraphrased from the SUN man pages |
| 8 |
* Version 0.1 initial release (Dec 1999) |
| 9 |
* Version 0.2 added support for multiprocessor machines (Jan 2000) |
| 10 |
* Version 0.3 some reliability enhancements and compile time fixes (Feb 2000) |
| 11 |
* Version 0.4 portability issue and raw cpu value support (Jun 2000) |
| 12 |
* Version 0.5 64-bit Solaris support and new data gathering routine (Aug 2000) |
| 13 |
* Version 0.6 Memory savings, overroll precautions and lint checks (Aug 2000) |
| 14 |
* Version 0.7 More raw counters and some cosmetic changes (Jan 2001) |
| 15 |
* |
| 16 |
*/ |
| 17 |
|
| 18 |
/* |
| 19 |
* To make lint skip the debug code and stop complaining |
| 20 |
*/ |
| 21 |
#ifdef __lint |
| 22 |
#define NETSNMP_NO_DEBUGGING 1 |
| 23 |
#endif |
| 24 |
|
| 25 |
/* |
| 26 |
* Includes start here |
| 27 |
*/ |
| 28 |
|
| 29 |
/* |
| 30 |
* Standard includes |
| 31 |
*/ |
| 32 |
#include <stdlib.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/time.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
/* |
| 39 |
* kstat and sysinfo structs |
| 40 |
*/ |
| 41 |
#include <kstat.h> |
| 42 |
#include <sys/sysinfo.h> |
| 43 |
|
| 44 |
#include <net-snmp/net-snmp-config.h> |
| 45 |
#include <net-snmp/net-snmp-includes.h> |
| 46 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 47 |
|
| 48 |
#include "mibdefs.h" |
| 49 |
#include "util_funcs/header_generic.h" |
| 50 |
|
| 51 |
/* |
| 52 |
* Header file for this module |
| 53 |
*/ |
| 54 |
#include "vmstat.h" |
| 55 |
#include "vmstat_solaris2.h" |
| 56 |
|
| 57 |
/* |
| 58 |
* Includes end here |
| 59 |
*/ |
| 60 |
|
| 61 |
|
| 62 |
/* |
| 63 |
* Global structures start here |
| 64 |
*/ |
| 65 |
|
| 66 |
/* |
| 67 |
* A structure to save data gathered from the kernel kstat interface to. |
| 68 |
* We used to have the sys/sysinfo.h cpu_stat_t here but we did not need |
| 69 |
* all of it, some in a different size and some additional ones so we build |
| 70 |
* our own |
| 71 |
*/ |
| 72 |
struct cpu_stat_snapshot { |
| 73 |
hrtime_t css_time; |
| 74 |
unsigned int css_cpus; |
| 75 |
unsigned long long css_swapin; |
| 76 |
unsigned long long css_swapout; |
| 77 |
unsigned long long css_blocks_read; |
| 78 |
unsigned long long css_blocks_write; |
| 79 |
unsigned long long css_interrupts; |
| 80 |
unsigned long long css_context_sw; |
| 81 |
unsigned long long css_cpu[CPU_STATES]; |
| 82 |
}; |
| 83 |
|
| 84 |
/* |
| 85 |
* Global structures end here |
| 86 |
*/ |
| 87 |
|
| 88 |
|
| 89 |
/* |
| 90 |
* Global variables start here |
| 91 |
*/ |
| 92 |
|
| 93 |
/* |
| 94 |
* From kstat.h: |
| 95 |
* Provides access to the kernel statistics library by |
| 96 |
* initializing a kstat control structure and returning a pointer |
| 97 |
* to this structure. This pointer must be used as the kc argument in |
| 98 |
* following function calls from libkstat (here kc is called kstat_fd). |
| 99 |
* Pointer to structure to be opened with kstat_open in main procedure. |
| 100 |
* We share this one with memory_solaris2 and kernel_sunos5, where it's |
| 101 |
* defined. |
| 102 |
*/ |
| 103 |
extern kstat_ctl_t *kstat_fd; |
| 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 ulong swapin; |
| 110 |
static ulong swapout; |
| 111 |
static ulong blocks_read; |
| 112 |
static ulong blocks_write; |
| 113 |
static ulong interrupts; |
| 114 |
static ulong context_sw; |
| 115 |
|
| 116 |
/* |
| 117 |
* Since MIB wants CPU_SYSTEM, which is CPU_KERNEL + CPU_WAIT |
| 118 |
*/ |
| 119 |
static long cpu_perc[CPU_STATES + 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_solaris2 starts here |
| 157 |
* Init function for this module, from prototype |
| 158 |
* Defines variables handled by this module, defines root OID for |
| 159 |
* this module and registers it with the agent |
| 160 |
*/ |
| 161 |
|
| 162 |
FindVarMethod var_extensible_vmstat; |
| 163 |
|
| 164 |
void |
| 165 |
init_vmstat_solaris2(void) |
| 166 |
{ |
| 167 |
|
| 168 |
/* |
| 169 |
* Which variables do we service ? |
| 170 |
*/ |
| 171 |
struct variable2 extensible_vmstat_variables[] = { |
| 172 |
{MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 173 |
var_extensible_vmstat, 1, {MIBINDEX}}, |
| 174 |
{ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 175 |
var_extensible_vmstat, 1, {ERRORNAME}}, |
| 176 |
{SWAPIN, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 177 |
var_extensible_vmstat, 1, {SWAPIN}}, |
| 178 |
{SWAPOUT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 179 |
var_extensible_vmstat, 1, {SWAPOUT}}, |
| 180 |
{IOSENT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 181 |
var_extensible_vmstat, 1, {IOSENT}}, |
| 182 |
{IORECEIVE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 183 |
var_extensible_vmstat, 1, {IORECEIVE}}, |
| 184 |
{SYSINTERRUPTS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 185 |
var_extensible_vmstat, 1, {SYSINTERRUPTS}}, |
| 186 |
{SYSCONTEXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 187 |
var_extensible_vmstat, 1, {SYSCONTEXT}}, |
| 188 |
{CPUUSER, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 189 |
var_extensible_vmstat, 1, {CPUUSER}}, |
| 190 |
{CPUSYSTEM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 191 |
var_extensible_vmstat, 1, {CPUSYSTEM}}, |
| 192 |
{CPUIDLE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 193 |
var_extensible_vmstat, 1, {CPUIDLE}}, |
| 194 |
{CPURAWUSER, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 195 |
var_extensible_vmstat, 1, {CPURAWUSER}}, |
| 196 |
{CPURAWSYSTEM, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 197 |
var_extensible_vmstat, 1, {CPURAWSYSTEM}}, |
| 198 |
{CPURAWIDLE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 199 |
var_extensible_vmstat, 1, {CPURAWIDLE}}, |
| 200 |
{CPURAWWAIT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 201 |
var_extensible_vmstat, 1, {CPURAWWAIT}}, |
| 202 |
{CPURAWKERNEL, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 203 |
var_extensible_vmstat, 1, {CPURAWKERNEL}}, |
| 204 |
{IORAWSENT, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 205 |
var_extensible_vmstat, 1, {IORAWSENT}}, |
| 206 |
{IORAWRECEIVE, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 207 |
var_extensible_vmstat, 1, {IORAWRECEIVE}}, |
| 208 |
/* |
| 209 |
* Future use: |
| 210 |
* {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 211 |
* var_extensible_vmstat, 1, {ERRORFLAG }}, |
| 212 |
* {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 213 |
* var_extensible_vmstat, 1, {ERRORMSG }} |
| 214 |
*/ |
| 215 |
}; |
| 216 |
|
| 217 |
/* |
| 218 |
* Define the OID pointer to the top of the mib tree that we're |
| 219 |
* registering underneath |
| 220 |
*/ |
| 221 |
oid vmstat_variables_oid[] = { NETSNMP_UCDAVIS_MIB, 11 }; |
| 222 |
|
| 223 |
/* |
| 224 |
* register ourselves with the agent to handle our mib tree |
| 225 |
* LINTED Trust me, I know what I'm doing |
| 226 |
*/ |
| 227 |
REGISTER_MIB("ucd-snmp/vmstat", extensible_vmstat_variables, variable2, |
| 228 |
vmstat_variables_oid); |
| 229 |
|
| 230 |
/* |
| 231 |
* First check whether shared kstat contol is NULL, if so, try to open our |
| 232 |
* own. |
| 233 |
*/ |
| 234 |
if (kstat_fd == NULL) { |
| 235 |
kstat_fd = kstat_open(); |
| 236 |
} |
| 237 |
|
| 238 |
/* |
| 239 |
* Then check whether either shared kstat was found or we succeeded in |
| 240 |
* opening our own. |
| 241 |
*/ |
| 242 |
if (kstat_fd == NULL) { |
| 243 |
snmp_log(LOG_ERR, |
| 244 |
"vmstat_solaris2 (init): kstat_open() failed and no shared kstat control found.\n"); |
| 245 |
} |
| 246 |
|
| 247 |
/* |
| 248 |
* Start with some useful data |
| 249 |
*/ |
| 250 |
update_stats(0, NULL); |
| 251 |
|
| 252 |
/* |
| 253 |
* update_stats is run every POLL_INTERVAL seconds using this routine |
| 254 |
* (see 'man snmp_alarm') |
| 255 |
* This is only executed once to get some useful data in the beginning |
| 256 |
*/ |
| 257 |
if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) { |
| 258 |
snmp_log(LOG_WARNING, |
| 259 |
"vmstat_solaris2 (init): snmp_alarm_register failed.\n"); |
| 260 |
} |
| 261 |
/* |
| 262 |
* This is the one that runs update_stats every POLL_INTERVAL seconds |
| 263 |
*/ |
| 264 |
if (snmp_alarm_register(POLL_INTERVAL, SA_REPEAT, update_stats, NULL) |
| 265 |
== 0) { |
| 266 |
snmp_log(LOG_ERR, |
| 267 |
"vmstat_solaris2 (init): snmp_alarm_register failed, cannot service requests.\n"); |
| 268 |
} |
| 269 |
|
| 270 |
} /* init_vmstat_solaris2 ends here */ |
| 271 |
|
| 272 |
/* |
| 273 |
* Data collection function take_snapshot starts here |
| 274 |
* Get data from kernel and save into the snapshot strutcs |
| 275 |
* Argument is the snapshot struct to save to. Global anyway, but looks nicer |
| 276 |
*/ |
| 277 |
static int |
| 278 |
take_snapshot(struct cpu_stat_snapshot *css) |
| 279 |
{ |
| 280 |
/* |
| 281 |
* Variables start here |
| 282 |
*/ |
| 283 |
|
| 284 |
/* |
| 285 |
* From sys/kstat.h (included from kstat.h): |
| 286 |
* Pointer to current kstat |
| 287 |
*/ |
| 288 |
kstat_t *ksp; |
| 289 |
|
| 290 |
/* |
| 291 |
* Counters |
| 292 |
*/ |
| 293 |
unsigned int cpu_num = 0; |
| 294 |
|
| 295 |
/* |
| 296 |
* High resolution time counter |
| 297 |
*/ |
| 298 |
hrtime_t current_time; |
| 299 |
|
| 300 |
/* |
| 301 |
* see sys/sysinfo.h, holds CPU data |
| 302 |
*/ |
| 303 |
cpu_stat_t cs; |
| 304 |
|
| 305 |
/* |
| 306 |
* The usual stuff to count on, err, by |
| 307 |
*/ |
| 308 |
int i; |
| 309 |
|
| 310 |
/* |
| 311 |
* Variables end here |
| 312 |
*/ |
| 313 |
|
| 314 |
/* |
| 315 |
* Function starts here |
| 316 |
*/ |
| 317 |
|
| 318 |
/* |
| 319 |
* Get time |
| 320 |
*/ |
| 321 |
current_time = gethrtime(); |
| 322 |
|
| 323 |
/* |
| 324 |
* If we have just gotten the data, return the values from last run (skip if-clause) |
| 325 |
* This happens on a snmpwalk request. No need to read the kstat again |
| 326 |
* if we just did it less than 2 seconds ago |
| 327 |
* Jumps into if-clause either when snapshot is empty or when too old |
| 328 |
*/ |
| 329 |
|
| 330 |
if ((css->css_time == 0) |
| 331 |
|| (current_time > css->css_time + 2000000000)) { |
| 332 |
/* |
| 333 |
* Make sure we clean up before we put new data into snapshot |
| 334 |
*/ |
| 335 |
memset(css, 0, sizeof *css); |
| 336 |
|
| 337 |
/* |
| 338 |
* Update timer |
| 339 |
*/ |
| 340 |
css->css_time = current_time; |
| 341 |
|
| 342 |
/* |
| 343 |
* Look thru all the cpu slots on the machine whether they holds a CPU |
| 344 |
* and if so, get the data from that CPU |
| 345 |
* We walk through the whole kstat chain and sum up all the found cpu_stat kstats, |
| 346 |
* there's one for every CPU in a machine |
| 347 |
*/ |
| 348 |
for (ksp = kstat_fd->kc_chain; ksp != NULL; ksp = ksp->ks_next) { |
| 349 |
/* |
| 350 |
* If we encounter an invalid kstat, skip it and continue with next one |
| 351 |
*/ |
| 352 |
if (ksp->ks_flags & KSTAT_FLAG_INVALID) { |
| 353 |
continue; |
| 354 |
} |
| 355 |
|
| 356 |
if (strcmp(ksp->ks_module, "cpu_stat") == 0) { |
| 357 |
/* |
| 358 |
* Yeah, we found a CPU. |
| 359 |
*/ |
| 360 |
cpu_num++; |
| 361 |
|
| 362 |
/* |
| 363 |
* Read data from kstat into cs structure |
| 364 |
* kstat_fd is the control structure, ksp the kstat we are reading |
| 365 |
* and cs the buffer we are writing to. |
| 366 |
*/ |
| 367 |
if ((ksp->ks_type != KSTAT_TYPE_RAW) || |
| 368 |
(ksp->ks_data_size != sizeof cs) || |
| 369 |
(kstat_read(kstat_fd, ksp, &cs) == -1)) { |
| 370 |
snmp_log(LOG_ERR, |
| 371 |
"vmstat_solaris2 (take_snapshot): could not read cs structure.\n"); |
| 372 |
return (-1); |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
* Get the data from the cs structure and sum it up in our own structure |
| 377 |
*/ |
| 378 |
css->css_swapin += |
| 379 |
(unsigned long long) cs.cpu_vminfo.swapin; |
| 380 |
css->css_swapout += |
| 381 |
(unsigned long long) cs.cpu_vminfo.swapout; |
| 382 |
css->css_blocks_read += |
| 383 |
(unsigned long long) cs.cpu_sysinfo.bread; |
| 384 |
css->css_blocks_write += |
| 385 |
(unsigned long long) cs.cpu_sysinfo.bwrite; |
| 386 |
css->css_interrupts += |
| 387 |
(unsigned long long) cs.cpu_sysinfo.intr; |
| 388 |
css->css_context_sw += |
| 389 |
(unsigned long long) cs.cpu_sysinfo.pswitch; |
| 390 |
|
| 391 |
/* |
| 392 |
* We need a for-loop for the CPU values |
| 393 |
*/ |
| 394 |
cs.cpu_sysinfo.cpu[CPU_WAIT] = cs.cpu_sysinfo.wait[W_IO] + |
| 395 |
cs.cpu_sysinfo.wait[W_PIO]; |
| 396 |
for (i = 0; i < CPU_STATES; i++) { |
| 397 |
css->css_cpu[i] += |
| 398 |
(unsigned long long) cs.cpu_sysinfo.cpu[i]; |
| 399 |
} /* end for */ |
| 400 |
} /* end if */ |
| 401 |
} /* end for */ |
| 402 |
|
| 403 |
/* |
| 404 |
* Increment number of CPUs we gathered data from, for future use |
| 405 |
*/ |
| 406 |
css->css_cpus = cpu_num; |
| 407 |
} |
| 408 |
|
| 409 |
/* |
| 410 |
* All engines running at warp speed, no problems (if there are any engines, that is) |
| 411 |
*/ |
| 412 |
return (cpu_num > 0 ? 0 : -1); |
| 413 |
} /* take_snapshot ends here */ |
| 414 |
|
| 415 |
/* |
| 416 |
* This gets called every POLL_INTERVAL seconds to update the snapshots. It takes a new snapshot and |
| 417 |
* 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 |
| 419 |
* The alarm timer is in the init function of this module (snmp_alarm_register) |
| 420 |
* ARGSUSED0 |
| 421 |
*/ |
| 422 |
static void |
| 423 |
update_stats(unsigned int registrationNumber, void *clientarg) |
| 424 |
{ |
| 425 |
/* |
| 426 |
* The time between the samples we compare |
| 427 |
*/ |
| 428 |
hrtime_t time_diff; |
| 429 |
|
| 430 |
/* |
| 431 |
* Easier to use these than the snapshots, short hand pointers |
| 432 |
*/ |
| 433 |
struct cpu_stat_snapshot *css_old, *css_new; |
| 434 |
|
| 435 |
/* |
| 436 |
* The usual stuff to count on, err, by |
| 437 |
*/ |
| 438 |
int i; |
| 439 |
|
| 440 |
/* |
| 441 |
* Kstat chain id, to check whether kstat chain changed |
| 442 |
*/ |
| 443 |
kid_t kid; |
| 444 |
|
| 445 |
/* |
| 446 |
* The sum of the CPU ticks that have passed on the different CPU states, so we can calculate |
| 447 |
* the percentages of each state |
| 448 |
*/ |
| 449 |
unsigned long long cpu_sum = 0; |
| 450 |
|
| 451 |
DEBUGMSGTL(("ucd-snmp/vmstat_solaris2.c:update_stats", |
| 452 |
"updating stats\n")); |
| 453 |
|
| 454 |
/* |
| 455 |
* Just in case someone added (or removed) some CPUs during operation (or other kstat chain changes) |
| 456 |
*/ |
| 457 |
kid = kstat_chain_update(kstat_fd); |
| 458 |
if (kid != 0) { |
| 459 |
if (kid == -1) { |
| 460 |
snmp_log(LOG_WARNING, |
| 461 |
"vmstat_solaris2 (update_stats): Could not update kstat chain.\n"); |
| 462 |
} else { |
| 463 |
/* |
| 464 |
* On some machines this floods the logfile, thus commented out |
| 465 |
* snmp_log(LOG_INFO, "vmstat_solaris2 (update_stats): Kstat chain changed."); |
| 466 |
*/ |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/* |
| 471 |
* Take the current snapshot |
| 472 |
*/ |
| 473 |
if (take_snapshot(&snapshot[0]) == -1) { |
| 474 |
snmp_log(LOG_WARNING, |
| 475 |
"vmstat_solaris2 (update_stats): Something went wrong with take_snapshot.\n"); |
| 476 |
return; |
| 477 |
} |
| 478 |
|
| 479 |
/* |
| 480 |
* Do we have some data we can use ? An issue right after the start of the agent |
| 481 |
*/ |
| 482 |
if (number_of_snapshots > 0) { |
| 483 |
/* |
| 484 |
* Huh, the number of CPUs changed during run time. That is indeed s.th. worth noting, we |
| 485 |
* output a humorous (more or less) syslog message and need to retake the snapshots |
| 486 |
*/ |
| 487 |
if (snapshot[0].css_cpus != snapshot[1].css_cpus) { |
| 488 |
if (snapshot[0].css_cpus > snapshot[1].css_cpus) { |
| 489 |
snmp_log(LOG_NOTICE, |
| 490 |
"vmstat_solaris2 (update_stats): Cool ! Number of CPUs increased, must be hot-pluggable.\n"); |
| 491 |
} else { |
| 492 |
snmp_log(LOG_NOTICE, |
| 493 |
"vmstat_solaris2 (update_stats): Lost at least one CPU, RIP.\n"); |
| 494 |
} |
| 495 |
/* |
| 496 |
* Make all snapshots but the current one invalid |
| 497 |
*/ |
| 498 |
number_of_snapshots = 1; |
| 499 |
/* |
| 500 |
* Move the current one in the "first" [1] slot |
| 501 |
*/ |
| 502 |
memmove(&snapshot[1], &snapshot[0], sizeof snapshot[0]); |
| 503 |
/* |
| 504 |
* Erase the current one |
| 505 |
*/ |
| 506 |
memset(&snapshot[0], 0, sizeof snapshot[0]); |
| 507 |
/* |
| 508 |
* Try to get a new snapshot in five seconds so we can return s.th. useful |
| 509 |
*/ |
| 510 |
if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) { |
| 511 |
snmp_log(LOG_WARNING, |
| 512 |
"vmstat_solaris2 (update_stats): snmp_alarm_register failed.\n"); |
| 513 |
} |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
/* |
| 518 |
* Short hand pointers |
| 519 |
*/ |
| 520 |
css_new = &snapshot[0]; |
| 521 |
css_old = &snapshot[number_of_snapshots]; |
| 522 |
|
| 523 |
/* |
| 524 |
* How much time has passed between the snapshots we get the values from ? |
| 525 |
* Time is in nanoseconds so a few zeros here to juggle with |
| 526 |
* But the hrtime is not subject to change (s.b. setting the clock), unlike the normal time |
| 527 |
*/ |
| 528 |
time_diff = |
| 529 |
(snapshot[0].css_time - |
| 530 |
snapshot[number_of_snapshots].css_time) / 1000000; |
| 531 |
if ( time_diff == 0 ) { time_diff = 1; } /* Protect against division-by-zero */ |
| 532 |
|
| 533 |
DEBUGMSGTL(("ucd-snmp/vmstat_solaris2.c:update_stats", |
| 534 |
"time_diff: %lld\n", time_diff)); |
| 535 |
|
| 536 |
/* |
| 537 |
* swapin and swapout are in pages, MIB wants kB/s,so we just need to get kB and seconds |
| 538 |
* For the others we need to get value per second |
| 539 |
* getpagesize() returns pagesize in bytes |
| 540 |
* decided to use sysconf(_SC_PAGESIZE) instead to get around an #ifndef (I don't like those) |
| 541 |
* that was needed b/c some old Solaris versions don't have getpagesize() |
| 542 |
*/ |
| 543 |
/* |
| 544 |
* LINTED cast needed, really |
| 545 |
*/ |
| 546 |
swapin = |
| 547 |
(uint_t) ((css_new->css_swapin - |
| 548 |
css_old->css_swapin) * (hrtime_t) 1000 * |
| 549 |
sysconf(_SC_PAGESIZE) / 1024 / time_diff); |
| 550 |
/* |
| 551 |
* LINTED cast needed, really |
| 552 |
*/ |
| 553 |
swapout = |
| 554 |
(uint_t) ((css_new->css_swapout - |
| 555 |
css_old->css_swapout) * (hrtime_t) 1000 * |
| 556 |
sysconf(_SC_PAGESIZE) / 1024 / time_diff); |
| 557 |
/* |
| 558 |
* LINTED cast needed, really |
| 559 |
*/ |
| 560 |
blocks_read = |
| 561 |
(uint_t) ((css_new->css_blocks_read - |
| 562 |
css_old->css_blocks_read) * (hrtime_t) 1000 / |
| 563 |
time_diff); |
| 564 |
/* |
| 565 |
* LINTED cast needed, really |
| 566 |
*/ |
| 567 |
blocks_write = |
| 568 |
(uint_t) ((css_new->css_blocks_write - |
| 569 |
css_old->css_blocks_write) * (hrtime_t) 1000 / |
| 570 |
time_diff); |
| 571 |
/* |
| 572 |
* LINTED cast needed, really |
| 573 |
*/ |
| 574 |
interrupts = |
| 575 |
(uint_t) ((css_new->css_interrupts - |
| 576 |
css_old->css_interrupts) * (hrtime_t) 1000 / |
| 577 |
time_diff); |
| 578 |
/* |
| 579 |
* LINTED cast needed, really |
| 580 |
*/ |
| 581 |
context_sw = |
| 582 |
(uint_t) ((css_new->css_context_sw - |
| 583 |
css_old->css_context_sw) * (hrtime_t) 1000 / |
| 584 |
time_diff); |
| 585 |
|
| 586 |
/* |
| 587 |
* Loop thru all the CPU_STATES and get the differences |
| 588 |
*/ |
| 589 |
for (i = 0; i < CPU_STATES; i++) { |
| 590 |
cpu_sum += (css_new->css_cpu[i] - css_old->css_cpu[i]); |
| 591 |
} |
| 592 |
|
| 593 |
/* |
| 594 |
* Now calculate the absolute percentage values |
| 595 |
* Looks somewhat complicated sometimes but tries to get around using floats to increase speed |
| 596 |
*/ |
| 597 |
for (i = 0; i < CPU_STATES; i++) { |
| 598 |
/* |
| 599 |
* Since we don't return fractions we use + 0.5 to get between 99 and 101 percent adding the values |
| 600 |
* together, otherwise we would get less than 100 most of the time |
| 601 |
*/ |
| 602 |
/* |
| 603 |
* LINTED has to be 'long' |
| 604 |
*/ |
| 605 |
cpu_perc[i] = |
| 606 |
(long) (((css_new->css_cpu[i] - |
| 607 |
css_old->css_cpu[i]) * 100 + |
| 608 |
(cpu_sum / 2)) / cpu_sum); |
| 609 |
} |
| 610 |
|
| 611 |
/* |
| 612 |
* As said before, MIB wants CPU_SYSTEM which is CPU_KERNEL + CPU_WAIT |
| 613 |
*/ |
| 614 |
/* |
| 615 |
* LINTED has to be 'long' |
| 616 |
*/ |
| 617 |
cpu_perc[CPU_SYSTEM] = |
| 618 |
(long) ((((css_new->css_cpu[CPU_KERNEL] - |
| 619 |
css_old->css_cpu[CPU_KERNEL]) |
| 620 |
+ (css_new->css_cpu[CPU_WAIT] - |
| 621 |
css_old->css_cpu[CPU_WAIT])) |
| 622 |
* 100 + (cpu_sum / 2)) / cpu_sum); |
| 623 |
} |
| 624 |
|
| 625 |
/* |
| 626 |
* Make the current one the first one and move the whole thing one place down |
| 627 |
*/ |
| 628 |
memmove(&snapshot[1], &snapshot[0], |
| 629 |
(size_t) (((char *) &snapshot[POLL_VALUES]) - |
| 630 |
((char *) &snapshot[0]))); |
| 631 |
|
| 632 |
/* |
| 633 |
* Erase the current one |
| 634 |
*/ |
| 635 |
memset(&snapshot[0], 0, sizeof snapshot[0]); |
| 636 |
|
| 637 |
/* |
| 638 |
* Only important on start up, we keep track of how many snapshots we have taken so far |
| 639 |
*/ |
| 640 |
if (number_of_snapshots < POLL_VALUES) { |
| 641 |
number_of_snapshots++; |
| 642 |
} |
| 643 |
} /* update_stats ends here */ |
| 644 |
|
| 645 |
/* |
| 646 |
* *var_extensible_vmstat starts here |
| 647 |
* The guts of the module, this routine gets called to service a request |
| 648 |
*/ |
| 649 |
unsigned char * |
| 650 |
var_extensible_vmstat(struct variable *vp, |
| 651 |
oid * name, |
| 652 |
size_t * length, |
| 653 |
int exact, |
| 654 |
size_t * var_len, WriteMethod ** write_method) |
| 655 |
{ |
| 656 |
/* |
| 657 |
* Needed for returning the values |
| 658 |
*/ |
| 659 |
static long long_ret; |
| 660 |
static char errmsg[300]; |
| 661 |
|
| 662 |
/* |
| 663 |
* set to 0 as default |
| 664 |
*/ |
| 665 |
long_ret = 0; |
| 666 |
|
| 667 |
/* |
| 668 |
* generic check whether the options passed make sense and whether the |
| 669 |
* right variable is requested |
| 670 |
*/ |
| 671 |
if (header_generic(vp, name, length, exact, var_len, write_method) != |
| 672 |
MATCH_SUCCEEDED) { |
| 673 |
return (NULL); |
| 674 |
} |
| 675 |
|
| 676 |
/* |
| 677 |
* The function that actually returns s.th. |
| 678 |
*/ |
| 679 |
switch (vp->magic) { |
| 680 |
case MIBINDEX: |
| 681 |
long_ret = 1; |
| 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)); |
| 687 |
case SWAPIN: |
| 688 |
return ((u_char *) (&swapin)); |
| 689 |
case SWAPOUT: |
| 690 |
return ((u_char *) (&swapout)); |
| 691 |
case IOSENT: |
| 692 |
return ((u_char *) (&blocks_write)); |
| 693 |
case IORECEIVE: |
| 694 |
return ((u_char *) (&blocks_read)); |
| 695 |
case SYSINTERRUPTS: |
| 696 |
return ((u_char *) (&interrupts)); |
| 697 |
case SYSCONTEXT: |
| 698 |
return ((u_char *) (&context_sw)); |
| 699 |
case CPUUSER: |
| 700 |
return ((u_char *) (&cpu_perc[CPU_USER])); |
| 701 |
case CPUSYSTEM: |
| 702 |
return ((u_char *) (&cpu_perc[CPU_SYSTEM])); |
| 703 |
case CPUIDLE: |
| 704 |
return ((u_char *) (&cpu_perc[CPU_IDLE])); |
| 705 |
case CPURAWUSER: |
| 706 |
take_snapshot(&raw_values); |
| 707 |
/* |
| 708 |
* LINTED has to be 'long' |
| 709 |
*/ |
| 710 |
long_ret = |
| 711 |
(long) (raw_values.css_cpu[CPU_USER] / raw_values.css_cpus); |
| 712 |
return ((u_char *) (&long_ret)); |
| 713 |
/* |
| 714 |
* We are missing CPURAWNICE, Solaris does not account for this in the kernel so this OID can not |
| 715 |
* be returned. Also, these values will roll over sooner or later and then return inaccurate data |
| 716 |
* but the MIB wants Integer32 so we cannot put a counter here |
| 717 |
* (Has been changed to Counter32 in the latest MIB version!) |
| 718 |
*/ |
| 719 |
case CPURAWSYSTEM: |
| 720 |
take_snapshot(&raw_values); |
| 721 |
/* |
| 722 |
* LINTED has to be 'long' |
| 723 |
*/ |
| 724 |
long_ret = |
| 725 |
(long) ((raw_values.css_cpu[CPU_KERNEL] + |
| 726 |
raw_values.css_cpu[CPU_WAIT]) / raw_values.css_cpus); |
| 727 |
return ((u_char *) (&long_ret)); |
| 728 |
case CPURAWIDLE: |
| 729 |
take_snapshot(&raw_values); |
| 730 |
/* |
| 731 |
* LINTED has to be 'long' |
| 732 |
*/ |
| 733 |
long_ret = |
| 734 |
(long) (raw_values.css_cpu[CPU_IDLE] / raw_values.css_cpus); |
| 735 |
return ((u_char *) (&long_ret)); |
| 736 |
case CPURAWWAIT: |
| 737 |
take_snapshot(&raw_values); |
| 738 |
/* |
| 739 |
* LINTED has to be 'long' |
| 740 |
*/ |
| 741 |
long_ret = |
| 742 |
(long) (raw_values.css_cpu[CPU_WAIT] / raw_values.css_cpus); |
| 743 |
return ((u_char *) (&long_ret)); |
| 744 |
case CPURAWKERNEL: |
| 745 |
take_snapshot(&raw_values); |
| 746 |
/* |
| 747 |
* LINTED has to be 'long' |
| 748 |
*/ |
| 749 |
long_ret = |
| 750 |
(long) (raw_values.css_cpu[CPU_KERNEL] / raw_values.css_cpus); |
| 751 |
return ((u_char *) (&long_ret)); |
| 752 |
case IORAWSENT: |
| 753 |
long_ret = (long) (raw_values.css_blocks_write); |
| 754 |
return ((u_char *) (&long_ret)); |
| 755 |
case IORAWRECEIVE: |
| 756 |
long_ret = (long) (raw_values.css_blocks_read); |
| 757 |
return ((u_char *) (&long_ret)); |
| 758 |
|
| 759 |
/* |
| 760 |
* reserved for future use |
| 761 |
*/ |
| 762 |
/* |
| 763 |
* case ERRORFLAG: |
| 764 |
* return((u_char *) (&long_ret)); |
| 765 |
* case ERRORMSG: |
| 766 |
* return((u_char *) (&long_ret)); |
| 767 |
*/ |
| 768 |
default: |
| 769 |
snmp_log(LOG_ERR, |
| 770 |
"vmstat_solaris2: Error in request, no match for %d.\n", |
| 771 |
vp->magic); |
| 772 |
} |
| 773 |
return (NULL); |
| 774 |
} /* *var_extensible_vmstat ends here */ |
| 775 |
|
| 776 |
/* |
| 777 |
* Functions end here |
| 778 |
*/ |
| 779 |
|
| 780 |
/* |
| 781 |
* Program ends here |
| 782 |
*/ |