Debian Codegraph @

agent/mibgroup/ucd-snmp/loadave.c

raw
1 #include <net-snmp/net-snmp-config.h>
2
3 #ifdef HAVE_STDLIB_H
4 #include <stdlib.h>
5 #endif
6 #ifdef HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
9 #ifdef HAVE_FCNTL_H
10 #include <fcntl.h>
11 #endif
12 #include <signal.h>
13 #ifdef HAVE_MACHINE_PARAM_H
14 #include <machine/param.h>
15 #endif
16 #ifdef HAVE_SYS_PARAM_H
17 #include <sys/param.h>
18 #endif
19 #ifdef HAVE_SYS_VMMETER_H
20 #if !(defined(bsdi2) || defined(netbsd1))
21 #include <sys/vmmeter.h>
22 #endif
23 #endif
24 #ifdef HAVE_SYS_CONF_H
25 #include <sys/conf.h>
26 #endif
27 #ifdef HAVE_ASM_PAGE_H
28 #include <asm/page.h>
29 #endif
30 #ifdef HAVE_SYS_SWAP_H
31 #include <sys/swap.h>
32 #endif
33 #ifdef HAVE_SYS_FS_H
34 #include <sys/fs.h>
35 #else
36 #ifdef HAVE_UFS_FS_H
37 #include <ufs/fs.h>
38 #else
39 #ifdef HAVE_SYS_STAT_H
40 #include <sys/stat.h>
41 #endif
42 #if !defined(dragonfly)
43 #ifdef HAVE_SYS_VNODE_H
44 #include <sys/vnode.h>
45 #endif
46 #endif
47 #ifdef HAVE_UFS_UFS_QUOTA_H
48 #include <ufs/ufs/quota.h>
49 #endif
50 #ifdef HAVE_UFS_UFS_INODE_H
51 #include <ufs/ufs/inode.h>
52 #endif
53 #ifdef HAVE_UFS_FFS_FS_H
54 #include <ufs/ffs/fs.h>
55 #endif
56 #endif
57 #endif
58 #ifdef HAVE_MTAB_H
59 #include <mtab.h>
60 #endif
61 #include <errno.h>
62 #ifdef HAVE_FSTAB_H
63 #include <fstab.h>
64 #endif
65 #ifdef HAVE_SYS_STATFS_H
66 #include <sys/statfs.h>
67 #endif
68 #ifdef HAVE_SYS_STATVFS_H
69 #include <sys/statvfs.h>
70 #endif
71 #ifdef HAVE_SYS_VFS_H
72 #include <sys/vfs.h>
73 #endif
74 #if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)
75 #ifdef HAVE_SYS_MOUNT_H
76 #include <sys/mount.h>
77 #endif
78 #ifdef HAVE_SYS_SYSCTL_H
79 #include <sys/sysctl.h>
80 #endif
81 #define statvfs statfs
82 #endif
83 #ifdef HAVE_VM_VM_H
84 #include <vm/vm.h>
85 #endif
86 #ifdef HAVE_VM_SWAP_PAGER_H
87 #include <vm/swap_pager.h>
88 #endif
89 #ifdef HAVE_SYS_FIXPOINT_H
90 #include <sys/fixpoint.h>
91 #endif
92 #ifdef HAVE_SYS_LOADAVG_H
93 #include <sys/loadavg.h>
94 #endif
95 #ifdef HAVE_MALLOC_H
96 #include <malloc.h>
97 #endif
98 #ifdef HAVE_STRING_H
99 #include <string.h>
100 #endif
101 #ifdef TIME_WITH_SYS_TIME
102 # include <sys/time.h>
103 # include <time.h>
104 #else
105 # ifdef HAVE_SYS_TIME_H
106 # include <sys/time.h>
107 # else
108 # include <time.h>
109 # endif
110 #endif
111 #ifdef dynix
112 #include <sys/mc_vmparam.h>
113 #endif
114 #if defined(hpux10) || defined(hpux11)
115 #include <sys/pstat.h>
116 #endif
117 #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
118 #ifdef HAVE_SYS_PROTOSW_H
119 #include <sys/protosw.h>
120 #endif
121 #include <libperfstat.h>
122 #endif
123 #ifdef HAVE_SYS_SYSGET_H
124 #include <sys/sysget.h>
125 #endif
126
127 #include <net-snmp/net-snmp-includes.h>
128 #include <net-snmp/agent/net-snmp-agent-includes.h>
129 #include <net-snmp/agent/auto_nlist.h>
130
131 #include "struct.h"
132 #include "loadave.h"
133 #include "util_funcs/header_simple_table.h"
134 #include "kernel.h"
135
136 static double maxload[3];
137 static int laConfigSet = 0;
138
139 static int
140 loadave_store_config(int a, int b, void *c, void *d)
141 {
142 char line[SNMP_MAXBUF_SMALL];
143 if (laConfigSet > 0) {
144 snprintf(line, SNMP_MAXBUF_SMALL, "pload %.02f %.02f %.02f", maxload[0], maxload[1], maxload[2]);
145 snmpd_store_config(line);
146 }
147 return SNMPERR_SUCCESS;
148 }
149
150 void
151 init_loadave(void)
152 {
153
154 /*
155 * define the structure we're going to ask the agent to register our
156 * information at
157 */
158 struct variable2 extensible_loadave_variables[] = {
159 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
160 var_extensible_loadave, 1, {MIBINDEX}},
161 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
162 var_extensible_loadave, 1, {ERRORNAME}},
163 {LOADAVE, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
164 var_extensible_loadave, 1, {LOADAVE}},
165 {LOADMAXVAL, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
166 var_extensible_loadave, 1, {LOADMAXVAL}},
167 {LOADAVEINT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
168 var_extensible_loadave, 1, {LOADAVEINT}},
169 #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
170 {LOADAVEFLOAT, ASN_OPAQUE_FLOAT, NETSNMP_OLDAPI_RONLY,
171 var_extensible_loadave, 1, {LOADAVEFLOAT}},
172 #endif
173 {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
174 var_extensible_loadave, 1, {ERRORFLAG}},
175 {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
176 var_extensible_loadave, 1, {ERRORMSG}}
177 };
178
179 /*
180 * Define the OID pointer to the top of the mib tree that we're
181 * registering underneath
182 */
183 oid loadave_variables_oid[] =
184 { NETSNMP_UCDAVIS_MIB, NETSNMP_LOADAVEMIBNUM, 1 };
185
186 /*
187 * register ourselves with the agent to handle our mib tree
188 */
189 REGISTER_MIB("ucd-snmp/loadave", extensible_loadave_variables,
190 variable2, loadave_variables_oid);
191
192 laConfigSet = 0;
193
194 snmpd_register_config_handler("load", loadave_parse_config,
195 loadave_free_config,
196 "max1 [max5] [max15]");
197
198 snmpd_register_config_handler("pload",
199 loadave_parse_config, NULL, NULL);
200
201
202 /*
203 * we need to be called back later
204 */
205 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
206 loadave_store_config, NULL);
207
208 }
209
210 void
211 loadave_parse_config(const char *token, char *cptr)
212 {
213 int i;
214
215 if (strcmp(token, "pload") == 0) {
216 if (laConfigSet < 0) {
217 snmp_log(LOG_WARNING,
218 "ignoring attempted override of read-only load\n");
219 return;
220 } else {
221 laConfigSet++;
222 }
223 } else {
224 if (laConfigSet > 0) {
225 snmp_log(LOG_WARNING,
226 "ignoring attempted override of read-only load\n");
227 /*
228 * Fall through and copy in this value.
229 */
230 }
231 laConfigSet = -1;
232 }
233
234 for (i = 0; i <= 2; i++) {
235 if (cptr != NULL)
236 maxload[i] = atof(cptr);
237 else
238 if (i > 0)
239 maxload[i] = maxload[i - 1];
240 else
241 maxload[i] = NETSNMP_DEFMAXLOADAVE;
242 cptr = skip_not_white(cptr);
243 cptr = skip_white(cptr);
244 }
245 }
246
247 void
248 loadave_free_config(void)
249 {
250 int i;
251
252 for (i = 0; i <= 2; i++)
253 maxload[i] = NETSNMP_DEFMAXLOADAVE;
254 }
255
256 /*
257 * try to get load average
258 * Inputs: pointer to array of doubles, number of elements in array
259 * Returns: 0=array has values, -1=error occurred.
260 */
261 int
262 try_getloadavg(double *r_ave, size_t s_ave)
263 {
264 #ifndef HAVE_GETLOADAVG
265 #ifdef HAVE_SYS_FIXPOINT_H
266 fix favenrun[3];
267 #endif
268 #if (defined(ultrix) || defined(sun) || defined(__alpha) || defined(dynix))
269 int i;
270 #if (defined(sun) || defined(__alpha) || defined(dynix))
271 long favenrun[3];
272 if (s_ave > 3) /* bounds check */
273 return (-1);
274 #define FIX_TO_DBL(_IN) (((double) _IN)/((double) FSCALE))
275 #endif
276 #endif
277 #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
278 perfstat_cpu_total_t cs;
279 #endif
280 #if defined(hpux10) || defined(hpux11)
281 struct pst_dynamic pst_buf;
282 #endif
283 #ifdef irix6
284 int i, favenrun[3];
285 sgt_cookie_t cookie;
286 #endif
287 #endif /* !HAVE_GETLOADAVG */
288
289 #ifdef HAVE_GETLOADAVG
290 if (getloadavg(r_ave, s_ave) == -1)
291 return (-1);
292 #elif defined(linux)
293 {
294 FILE *in = fopen("/proc/loadavg", "r");
295 if (!in) {
296 NETSNMP_LOGONCE((LOG_ERR, "snmpd: cannot open /proc/loadavg\n"));
297 return (-1);
298 }
299 fscanf(in, "%lf %lf %lf", r_ave, (r_ave + 1), (r_ave + 2));
300 fclose(in);
301 }
302 #elif (defined(ultrix) || defined(sun) || defined(__alpha) || defined(dynix))
303 if (auto_nlist(LOADAVE_SYMBOL, (char *) favenrun, sizeof(favenrun)) ==
304 0)
305 return (-1);
306 for (i = 0; i < s_ave; i++)
307 *(r_ave + i) = FIX_TO_DBL(favenrun[i]);
308 #elif defined(hpux10) || defined(hpux11)
309 if (pstat_getdynamic(&pst_buf, sizeof(struct pst_dynamic), 1, 0) < 0)
310 return(-1);
311 r_ave[0] = pst_buf.psd_avg_1_min;
312 r_ave[1] = pst_buf.psd_avg_5_min;
313 r_ave[2] = pst_buf.psd_avg_15_min;
314 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
315 if(perfstat_cpu_total((perfstat_id_t *)NULL, &cs, sizeof(perfstat_cpu_total_t), 1) > 0) {
316 r_ave[0] = cs.loadavg[0] / 65536.0;
317 r_ave[1] = cs.loadavg[1] / 65536.0;
318 r_ave[2] = cs.loadavg[2] / 65536.0;
319 }
320 #elif defined(irix6)
321 SGT_COOKIE_INIT(&cookie);
322 SGT_COOKIE_SET_KSYM(&cookie, "avenrun");
323 sysget(SGT_KSYM, (char*)favenrun, sizeof(favenrun), SGT_READ, &cookie);
324 for (i = 0; i < s_ave; i++)
325 r_ave[i] = favenrun[i] / 1000.0;
326 DEBUGMSGTL(("ucd-snmp/loadave", "irix6: %d %d %d\n", favenrun[0], favenrun[1], favenrun[2]));
327 #elif !defined(cygwin)
328 #if defined(NETSNMP_CAN_USE_NLIST) && defined(LOADAVE_SYMBOL)
329 if (auto_nlist(LOADAVE_SYMBOL, (char *) r_ave, sizeof(double) * s_ave)
330 == 0)
331 #endif
332 return (-1);
333 #endif
334 /*
335 * XXX
336 * To calculate this, we need to compare
337 * successive values of the kernel array
338 * '_cp_times', and calculate the resulting
339 * percentage changes.
340 * This calculation needs to be performed
341 * regularly - perhaps as a background process.
342 *
343 * See the source to 'top' for full details.
344 *
345 * The linux SNMP HostRes implementation
346 * uses 'avenrun[0]*100' as an approximation.
347 * This is less than accurate, but has the
348 * advantage of being simple to implement!
349 *
350 * I'm also assuming a single processor
351 */
352 return 0;
353 }
354
355 static int
356 write_laConfig(int action,
357 u_char * var_val,
358 u_char var_val_type,
359 size_t var_val_len,
360 u_char * statP, oid * name, size_t name_len)
361 {
362 static double laConfig = 0;
363
364 switch (action) {
365 case RESERVE1: /* Check values for acceptability */
366 if (var_val_type != ASN_OCTET_STR) {
367 DEBUGMSGTL(("ucd-snmp/loadave",
368 "write to laConfig not ASN_OCTET_STR\n"));
369 return SNMP_ERR_WRONGTYPE;
370 }
371 if (var_val_len > 8 || var_val_len <= 0) {
372 DEBUGMSGTL(("ucd-snmp/loadave",
373 "write to laConfig: bad length\n"));
374 return SNMP_ERR_WRONGLENGTH;
375 }
376
377 if (laConfigSet < 0) {
378 /*
379 * The object is set in a read-only configuration file.
380 */
381 return SNMP_ERR_NOTWRITABLE;
382 }
383 break;
384
385 case RESERVE2: /* Allocate memory and similar resources */
386 {
387 char buf[8];
388 int old_errno = errno;
389 double val;
390 char *endp;
391
392 sprintf(buf, "%.*s", (int) var_val_len, (char *)var_val);
393 val = strtod(buf, &endp);
394
395 if (errno == ERANGE || *endp != '\0' || val < 0 || val > 65536.00) {
396 errno = old_errno;
397 DEBUGMSGTL(("ucd-snmp/loadave",
398 "write to laConfig: invalid value\n"));
399 return SNMP_ERR_WRONGVALUE;
400 }
401
402 errno = old_errno;
403
404 laConfig = val;
405 }
406 break;
407
408 case COMMIT:
409 {
410 int idx = name[name_len - 1] - 1;
411 maxload[idx] = laConfig;
412 laConfigSet = 1;
413 }
414 }
415
416 return SNMP_ERR_NOERROR;
417 }
418
419 u_char *
420 var_extensible_loadave(struct variable * vp,
421 oid * name,
422 size_t * length,
423 int exact,
424 size_t * var_len, WriteMethod ** write_method)
425 {
426
427 static long long_ret;
428 static float float_ret;
429 static char errmsg[300];
430 double avenrun[3];
431 if (header_simple_table
432 (vp, name, length, exact, var_len, write_method, 3))
433 return (NULL);
434 switch (vp->magic) {
435 case MIBINDEX:
436 long_ret = name[*length - 1];
437 return ((u_char *) (&long_ret));
438 case LOADMAXVAL:
439 /* setup write method, but don't return yet */
440 *write_method = write_laConfig;
441 break;
442 case ERRORNAME:
443 sprintf(errmsg, "Load-%d", ((name[*length - 1] == 1) ? 1 :
444 ((name[*length - 1] == 2) ? 5 : 15)));
445 *var_len = strlen(errmsg);
446 return ((u_char *) (errmsg));
447 }
448 if (try_getloadavg(&avenrun[0], sizeof(avenrun) / sizeof(avenrun[0]))
449 == -1)
450 return NULL;
451 switch (vp->magic) {
452 case LOADAVE:
453
454 sprintf(errmsg, "%.2f", avenrun[name[*length - 1] - 1]);
455 *var_len = strlen(errmsg);
456 return ((u_char *) (errmsg));
457 case LOADMAXVAL:
458 sprintf(errmsg, "%.2f", maxload[name[*length - 1] - 1]);
459 *var_len = strlen(errmsg);
460 return ((u_char *) (errmsg));
461 case LOADAVEINT:
462 long_ret = (u_long) (avenrun[name[*length - 1] - 1] * 100);
463 return ((u_char *) (&long_ret));
464 #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
465 case LOADAVEFLOAT:
466 float_ret = (float) avenrun[name[*length - 1] - 1];
467 *var_len = sizeof(float_ret);
468 return ((u_char *) (&float_ret));
469 #endif
470 case ERRORFLAG:
471 long_ret = (maxload[name[*length - 1] - 1] != 0 &&
472 avenrun[name[*length - 1] - 1] >=
473 maxload[name[*length - 1] - 1]) ? 1 : 0;
474 return ((u_char *) (&long_ret));
475 case ERRORMSG:
476 if (maxload[name[*length - 1] - 1] != 0 &&
477 avenrun[name[*length - 1] - 1] >=
478 maxload[name[*length - 1] - 1]) {
479 snprintf(errmsg, sizeof(errmsg),
480 "%d min Load Average too high (= %.2f)",
481 name[*length - 1] == 1 ? 1 :
482 name[*length - 1] == 2 ? 5 : 15,
483 avenrun[name[*length - 1] - 1]);
484 } else {
485 errmsg[0] = 0;
486 }
487 *var_len = strlen(errmsg);
488 return ((u_char *) errmsg);
489 }
490 return NULL;
491 }