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