1
#include <net-snmp/net-snmp-config.h>
17
#if HAVE_MACHINE_PARAM_H
18
#include <machine/param.h>
20
#if HAVE_SYS_VMMETER_H
21
#if !defined(bsdi2) && !defined(netbsd1)
22
#include <sys/vmmeter.h>
34
#ifdef HAVE_SYS_STAT_H
37
#ifdef HAVE_SYS_VNODE_H
38
#include <sys/vnode.h>
40
#ifdef HAVE_UFS_UFS_QUOTA_H
41
#include <ufs/ufs/quota.h>
43
#ifdef HAVE_UFS_UFS_INODE_H
44
#include <ufs/ufs/inode.h>
47
#include <ufs/ffs/fs.h>
59
#if HAVE_SYS_STATVFS_H
60
#include <sys/statvfs.h>
65
#if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)
67
#include <sys/param.h>
70
#include <sys/mount.h>
73
#include <sys/sysctl.h>
75
#define statvfs statfs
77
#if HAVE_VM_SWAP_PAGER_H
78
#include <vm/swap_pager.h>
80
#if HAVE_SYS_FIXPOINT_H
81
#include <sys/fixpoint.h>
91
#if TIME_WITH_SYS_TIME
92
# include <sys/time.h>
96
# include <sys/time.h>
102
#include <net-snmp/net-snmp-includes.h>
103
#include <net-snmp/agent/net-snmp-agent-includes.h>
104
#include <net-snmp/agent/auto_nlist.h>
108
#include "util_funcs.h"
111
FindVarMethod var_extensible_vmstat;
116
struct variable2 extensible_vmstat_variables[] = {
117
{MIBINDEX, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
119
{ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1,
121
{SWAPIN, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPIN}},
122
{SWAPOUT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPOUT}},
123
{IOSENT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {IOSENT}},
124
{IORECEIVE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
126
{SYSINTERRUPTS, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
128
{SYSCONTEXT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
130
{CPUUSER, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUUSER}},
131
{CPUSYSTEM, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,
133
{CPUIDLE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUIDLE}},
134
{CPURAWUSER, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
136
{CPURAWNICE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
138
{CPURAWSYSTEM, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
140
{CPURAWIDLE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,
146
* {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {ERRORFLAG }},
147
* {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1, {ERRORMSG }}
152
* Define the OID pointer to the top of the mib tree that we're
153
* registering underneath
155
oid vmstat_variables_oid[] = { UCDAVIS_MIB, 11 };
158
* register ourselves with the agent to handle our mib tree
160
REGISTER_MIB("ucd-snmp/vmstat", extensible_vmstat_variables, variable2,
161
vmstat_variables_oid);
167
#define VMSTAT_FILE "/proc/stat"
170
getstat(unsigned long *cuse, unsigned long *cice, unsigned long *csys,
171
unsigned long *cide, unsigned *pin, unsigned *pout,
172
unsigned *swpin, unsigned *swpout, unsigned *itot, unsigned *i1,
177
static char *buff = NULL;
178
static int bsize = 0;
180
if ((statfd = open(VMSTAT_FILE, O_RDONLY, 0)) != -1) {
184
buff = malloc(bsize);
186
while (read(statfd, buff, bsize) == bsize) {
188
buff = realloc(buff, bsize);
189
DEBUGMSGTL(("vmstat", "/proc/stat buffer increased to %d\n", bsize));
191
statfd = open(VMSTAT_FILE, O_RDONLY, 0);
195
*i1 = 1; /* ensure assert below will fail if the sscanf bombs */
196
b = strstr(buff, "cpu ");
198
sscanf(b, "cpu %lu %lu %lu %lu", cuse, cice, csys, cide);
201
snmp_log(LOG_ERR, "No cpu line in /proc/stat\n");
202
*cuse = *cice = *csys = *cide = 0;
204
b = strstr(buff, "page ");
206
sscanf(b, "page %u %u", pin, pout);
209
snmp_log(LOG_ERR, "No page line in /proc/stat\n");
212
b = strstr(buff, "swap ");
214
sscanf(b, "swap %u %u", swpin, swpout);
217
snmp_log(LOG_ERR, "No swap line in /proc/stat\n");
218
*swpin = *swpout = 0;
220
b = strstr(buff, "intr ");
222
sscanf(b, "intr %u %u", itot, i1);
225
snmp_log(LOG_ERR, "No intr line in /proc/stat\n");
228
b = strstr(buff, "ctxt ");
230
sscanf(b, "ctxt %u", ct);
233
snmp_log(LOG_ERR, "No ctxt line in /proc/stat\n");
238
snmp_log_perror(VMSTAT_FILE);
242
enum vmstat_index { swapin = 0, swapout,
244
sysinterrupts, syscontext,
245
cpuuser, cpusystem, cpuidle,
246
cpurawuser, cpurawnice,
247
cpurawsystem, cpurawidle
253
unsigned long cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2];
254
double duse, dsys, didl, ddiv, divo2;
255
double druse, drnic, drsys, dridl;
256
unsigned int pgpgin[2], pgpgout[2], pswpin[2], pswpout[2];
257
unsigned int inter[2], ticks[2], ctxt[2];
260
getstat(cpu_use, cpu_nic, cpu_sys, cpu_idl,
261
pgpgin, pgpgout, pswpin, pswpout, inter, ticks, ctxt);
262
duse = *(cpu_use) + *(cpu_nic);
265
ddiv = (duse + dsys + didl);
266
hertz = sysconf(_SC_CLK_TCK); /* get ticks/s from system */
271
dridl = (*(cpu_idl));
275
return (*(pswpin) * 4 * hertz + divo2) / ddiv;
277
return (*(pswpout) * 4 * hertz + divo2) / ddiv;
279
return (*(pgpgin) * hertz + divo2) / ddiv;
281
return (*(pgpgout) * hertz + divo2) / ddiv;
283
return (*(inter) * hertz + divo2) / ddiv;
285
return (*(ctxt) * hertz + divo2) / ddiv;
287
return (100 * duse / ddiv);
289
return (100 * dsys / ddiv);
291
return (100 * didl / ddiv);
306
var_extensible_vmstat(struct variable *vp,
310
size_t * var_len, WriteMethod ** write_method)
313
static long long_ret;
314
static char errmsg[300];
316
struct vmtotal total;
319
long_ret = 0; /* set to 0 as default */
321
if (header_generic(vp, name, length, exact, var_len, write_method))
326
return ((u_char *) (&long_ret));
327
case ERRORNAME: /* dummy name */
328
sprintf(errmsg, "systemStats");
329
*var_len = strlen(errmsg);
330
return ((u_char *) (errmsg));
333
long_ret = vmstat(swapin);
335
return ((u_char *) (&long_ret));
338
long_ret = vmstat(swapout);
340
return ((u_char *) (&long_ret));
343
long_ret = vmstat(iosent);
345
return ((u_char *) (&long_ret));
348
long_ret = vmstat(ioreceive);
350
return ((u_char *) (&long_ret));
353
long_ret = vmstat(sysinterrupts);
355
return ((u_char *) (&long_ret));
358
long_ret = vmstat(syscontext);
360
return ((u_char *) (&long_ret));
363
long_ret = vmstat(cpuuser);
365
return ((u_char *) (&long_ret));
368
long_ret = vmstat(cpusystem);
370
return ((u_char *) (&long_ret));
373
long_ret = vmstat(cpuidle);
375
return ((u_char *) (&long_ret));
378
long_ret = vmstat(cpurawuser);
380
return ((u_char *) (&long_ret));
383
long_ret = vmstat(cpurawnice);
385
return ((u_char *) (&long_ret));
388
long_ret = vmstat(cpurawsystem);
390
return ((u_char *) (&long_ret));
393
long_ret = vmstat(cpurawidle);
395
return ((u_char *) (&long_ret));
397
* reserved for future use
401
* return((u_char *) (&long_ret));
403
* return((u_char *) (&long_ret));