Debian Codegraph @

agent/mibgroup/ucd-snmp/memory_freebsd2.c

raw
1 /*
2 * memory_freebsd2.c
3 */
4
5 #include <net-snmp/net-snmp-config.h>
6
7
8 /*
9 * Ripped from /usr/scr/usr.bin/vmstat/vmstat.c (covering all bases)
10 */
11 #include <sys/param.h>
12 #include <sys/time.h>
13 #ifdef dragonfly
14 #include <sys/user.h>
15 #else
16 #include <sys/proc.h>
17 #include <sys/dkstat.h>
18 #endif
19 #ifdef freebsd5
20 #include <sys/bio.h>
21 #endif
22 #include <sys/buf.h>
23 #include <sys/uio.h>
24 #include <sys/namei.h>
25 #include <sys/malloc.h>
26 #include <sys/signal.h>
27 #include <sys/fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/sysctl.h>
30 #include <sys/vmmeter.h>
31
32 #ifdef HAVE_SYS_VMPARAM_H
33 #include <sys/vmparam.h>
34 #else
35 #include <vm/vm_param.h>
36 #endif
37
38 #include <time.h>
39 #include <nlist.h>
40 #include <kvm.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <paths.h>
48 #include <limits.h>
49
50 #include <net-snmp/net-snmp-includes.h>
51 #include <net-snmp/agent/net-snmp-agent-includes.h>
52 #include <net-snmp/agent/auto_nlist.h>
53
54 #include "util_funcs.h"
55 #include "memory.h"
56 #include "memory_freebsd2.h"
57
58 /*
59 * nlist symbols
60 */
61 #define SUM_SYMBOL "cnt"
62 #ifndef openbsd2
63 #define BUFSPACE_SYMBOL "bufspace"
64 #endif
65
66 /*
67 * Default swap warning limit (kb)
68 */
69 #define DEFAULTMINIMUMSWAP 16000
70
71 /*
72 * Swap warning limit
73 */
74 long minimumswap;
75
76 /*
77 * Swap info
78 */
79 quad_t swapTotal;
80 quad_t swapUsed;
81 quad_t swapFree;
82
83 static FindVarMethod var_extensible_mem;
84
85 void
86 init_memory_freebsd2(void)
87 {
88
89 struct variable2 extensible_mem_variables[] = {
90 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
91 var_extensible_mem, 1, {MIBINDEX}},
92 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
93 var_extensible_mem, 1, {ERRORNAME}},
94 {MEMTOTALSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
95 var_extensible_mem, 1, {MEMTOTALSWAP}},
96 {MEMAVAILSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
97 var_extensible_mem, 1, {MEMAVAILSWAP}},
98 {MEMTOTALREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
99 var_extensible_mem, 1, {MEMTOTALREAL}},
100 {MEMAVAILREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
101 var_extensible_mem, 1, {MEMAVAILREAL}},
102 {MEMTOTALSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
103 var_extensible_mem, 1, {MEMTOTALSWAPTXT}},
104 {MEMUSEDSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
105 var_extensible_mem, 1, {MEMUSEDSWAPTXT}},
106 {MEMTOTALREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
107 var_extensible_mem, 1, {MEMTOTALREALTXT}},
108 {MEMUSEDREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
109 var_extensible_mem, 1, {MEMUSEDREALTXT}},
110 {MEMTOTALFREE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
111 var_extensible_mem, 1, {MEMTOTALFREE}},
112 {MEMSWAPMINIMUM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
113 var_extensible_mem, 1, {MEMSWAPMINIMUM}},
114 {MEMSHARED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
115 var_extensible_mem, 1, {MEMSHARED}},
116 {MEMBUFFER, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
117 var_extensible_mem, 1, {MEMBUFFER}},
118 {MEMCACHED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
119 var_extensible_mem, 1, {MEMCACHED}},
120 {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
121 var_extensible_mem, 1, {ERRORFLAG}},
122 {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
123 var_extensible_mem, 1, {ERRORMSG}}
124 };
125
126 /*
127 * Define the OID pointer to the top of the mib tree that we're
128 * registering underneath
129 */
130 oid mem_variables_oid[] = { NETSNMP_UCDAVIS_MIB, NETSNMP_MEMMIBNUM };
131
132 /*
133 * register ourselves with the agent to handle our mib tree
134 */
135 REGISTER_MIB("ucd-snmp/memory", extensible_mem_variables, variable2,
136 mem_variables_oid);
137
138 snmpd_register_config_handler("swap", memory_parse_config,
139 memory_free_config, "min-avail");
140 }
141
142
143 void
144 memory_parse_config(const char *token, char *cptr)
145 {
146 minimumswap = atoi(cptr);
147 }
148
149 void
150 memory_free_config(void)
151 {
152 minimumswap = DEFAULTMINIMUMSWAP;
153 }
154
155 #ifndef freebsd4
156 /*
157 * Executes swapinfo and parses last line
158 */
159 /*
160 * This is just way too ugly ;)
161 */
162
163 void
164 swapmode(void)
165 {
166 struct extensible ext;
167 int fd;
168 FILE *file;
169
170 strcpy(ext.command, "/usr/sbin/swapinfo -k");
171
172 if ((fd = get_exec_output(&ext)) != -1) {
173 file = fdopen(fd, "r");
174
175 while (fgets(ext.output, sizeof(ext.output), file) != NULL);
176
177 fclose(file);
178 wait_on_exec(&ext);
179
180 sscanf(ext.output, "%*s%*d%qd%qd", &swapUsed, &swapFree);
181
182 swapTotal = swapUsed + swapFree;
183 }
184 }
185 #else
186 /*
187 * swapmode is based on a program called swapinfo written
188 * by Kevin Lahey <[email protected]>.
189 */
190
191 #include <sys/conf.h>
192
193 void
194 swapmode(void)
195 {
196 int pagesize;
197 int i, n;
198 static kvm_t *kd = NULL;
199 struct kvm_swap kswap[16];
200
201 if (kd == NULL)
202 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
203
204 n = kvm_getswapinfo(kd, kswap, sizeof(kswap) / sizeof(kswap[0]), 0);
205
206 swapUsed = swapTotal = swapFree = 0;
207 /*
208 * Count up free swap space.
209 */
210 for (i = 0; i < n; ++i)
211 swapFree += kswap[i].ksw_total - kswap[i].ksw_used;
212
213 /*
214 * Count up total swap space
215 */
216 for (i = 0; i < n; i++)
217 swapTotal += kswap[i].ksw_total;
218
219 /*
220 * Calculate used swap space
221 */
222 swapUsed = swapTotal - swapFree;
223
224 /*
225 * Convert to kb
226 */
227 pagesize = getpagesize() / 1024;
228
229 swapTotal *= pagesize;
230 swapUsed *= pagesize;
231 swapFree *= pagesize;
232 }
233 #endif
234
235
236 /*
237 * var_extensible_mem(...
238 * Arguments:
239 * vp IN - pointer to variable entry that points here
240 * name IN/OUT - IN/name requested, OUT/name found
241 * length IN/OUT - length of IN/OUT oid's
242 * exact IN - TRUE if an exact match was requested
243 * var_len OUT - length of variable or 0 if function returned
244 * write_method
245 *
246 */
247
248 static unsigned char *
249 var_extensible_mem(struct variable *vp,
250 oid * name,
251 size_t * length,
252 int exact,
253 size_t * var_len, WriteMethod ** write_method)
254 {
255 static long long_ret;
256 static char errmsg[1024];
257
258 #ifdef dragonfly
259 static struct vmstats mem;
260 size_t vmstats_size = sizeof(mem);
261 #endif
262 static struct vmmeter mem;
263 #endif
264 static struct vmtotal total;
265
266 size_t total_size = sizeof(total);
267 int total_mib[] = { CTL_VM, VM_METER };
268
269 u_long phys_mem;
270 size_t phys_mem_size = sizeof(phys_mem);
271 int phys_mem_mib[] = { CTL_HW, HW_PHYSMEM };
272
273 #ifdef BUFSPACE_SYMBOL
274 long bufspace;
275 #endif
276
277 if (header_generic(vp, name, length, exact, var_len, write_method))
278 return (NULL);
279
280 /*
281 * Memory info
282 */
283 #ifdef dragonfly
284 sysctlbyname("vm.vmstats", &vmstats, &vmstats_size, NULL, 0);
285 #else
286 auto_nlist(SUM_SYMBOL, (char *) &mem, sizeof(mem));
287 #endif
288 sysctl(total_mib, 2, &total, &total_size, NULL, 0);
289
290 /*
291 * Swap info
292 */
293 swapmode();
294 /*
295 * getSwap();
296 */
297
298 /*
299 * Physical memory
300 */
301 sysctl(phys_mem_mib, 2, &phys_mem, &phys_mem_size, NULL, 0);
302
303 #ifdef BUFSPACE_SYMBOL
304 /*
305 * Buffer space
306 */
307 auto_nlist(BUFSPACE_SYMBOL, (char *) &bufspace, sizeof(bufspace));
308 #endif
309
310 long_ret = 0; /* set to 0 as default */
311
312 /*
313 * Page-to-kb macro
314 */
315 #define ptok(p) ((p) * (mem.v_page_size >> 10))
316
317 switch (vp->magic) {
318 case MIBINDEX:
319 long_ret = 0;
320 return ((u_char *) (&long_ret));
321 case ERRORNAME: /* dummy name */
322 sprintf(errmsg, "swap");
323 *var_len = strlen(errmsg);
324 return ((u_char *) (errmsg));
325 case MEMTOTALSWAP:
326 long_ret = swapTotal;
327 return ((u_char *) (&long_ret));
328 case MEMAVAILSWAP: /* FREE swap memory */
329 long_ret = swapFree;
330 return ((u_char *) (&long_ret));
331 case MEMTOTALREAL:
332 long_ret = phys_mem >> 10;
333 return ((u_char *) (&long_ret));
334 case MEMAVAILREAL: /* FREE real memory */
335 long_ret = ptok(mem.v_free_count);
336 return ((u_char *) (&long_ret));
337
338 /*
339 * these are not implemented
340 */
341 case MEMTOTALSWAPTXT:
342 case MEMUSEDSWAPTXT:
343 case MEMTOTALREALTXT:
344 case MEMUSEDREALTXT:
345 #ifdef NETSNMP_NO_DUMMY_VALUES
346 return NULL;
347 #endif
348 long_ret = -1;
349 return ((u_char *) (&long_ret));
350
351 case MEMTOTALFREE:
352 long_ret = ptok((int) total.t_free);
353 return ((u_char *) (&long_ret));
354 case MEMSWAPMINIMUM:
355 long_ret = minimumswap;
356 return ((u_char *) (&long_ret));
357 case MEMSHARED:
358 long_ret = ptok(total.t_vmshr +
359 total.t_avmshr + total.t_rmshr + total.t_armshr);
360 return ((u_char *) (&long_ret));
361 #ifdef BUFSPACE_SYMBOL
362 case MEMBUFFER:
363 long_ret = bufspace >> 10;
364 return ((u_char *) (&long_ret));
365 #endif
366 #ifndef openbsd2
367 case MEMCACHED:
368 #ifdef darwin
369 long_ret = ptok(mem.v_lookups);
370 #elif defined(dragonfly)
371 long_ret = ptok(mem.v_cache_count);
372 #else
373 long_ret = ptok(mem.v_cache_count) + ptok(mem.v_inactive_count);
374 #endif
375 return ((u_char *) (&long_ret));
376 #endif
377 case ERRORFLAG:
378 long_ret = (swapFree > minimumswap) ? 0 : 1;
379 return ((u_char *) (&long_ret));
380 case ERRORMSG:
381 if (swapFree < minimumswap)
382 sprintf(errmsg, "Running out of swap space (%qd)", swapFree);
383 else
384 errmsg[0] = 0;
385 *var_len = strlen(errmsg);
386 return ((u_char *) (errmsg));
387 }
388 return NULL;
389 }