and added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / ucd-snmp / memory_netbsd1.c
1 /*
2  * memory_netbsd1.c
3  */
4
5 #include <net-snmp/net-snmp-config.h>
6
7 /*
8  * Ripped from /usr/scr/usr.bin/vmstat/vmstat.c (covering all bases) 
9  */
10 #include <sys/param.h>
11 #include <sys/time.h>
12 #include <sys/proc.h>
13 #include <sys/dkstat.h>
14 #include <sys/buf.h>
15 #include <sys/uio.h>
16 #include <sys/namei.h>
17 #include <sys/malloc.h>
18 #include <sys/signal.h>
19 #include <sys/fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/sysctl.h>
22 #include <sys/vmmeter.h>
23  
24 #if defined(HAVE_UVM_UVM_PARAM_H) && defined(HAVE_UVM_UVM_EXTERN_H)
25 #include <uvm/uvm_param.h>
26 #include <uvm/uvm_extern.h>
27 #elif defined(HAVE_VM_VM_PARAM_H) && defined(HAVE_VM_VM_EXTERN_H)
28 #include <vm/vm_param.h>
29 #include <vm/vm_extern.h>
30 #else
31 #error memory_netbsd1.c: Is this really a NetBSD system?
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_netbsd1.h"
57
58 /*
59  * Default swap warning limit (kb) 
60  */
61 #define DEFAULTMINIMUMSWAP 16000
62
63 /*
64  * Swap warning limit 
65  */
66 long            minimumswap;
67
68 /*
69  * Swap info 
70  */
71 quad_t          swapTotal;
72 quad_t          swapUsed;
73 quad_t          swapFree;
74
75 static FindVarMethod var_extensible_mem;
76
77 void
78 init_memory_netbsd1(void)
79 {
80
81     struct variable2 extensible_mem_variables[] = {
82         {MIBINDEX, ASN_INTEGER, RONLY, var_extensible_mem, 1, {MIBINDEX}},
83         {ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_mem, 1,
84          {ERRORNAME}},
85         {MEMTOTALSWAP, ASN_INTEGER, RONLY, var_extensible_mem, 1,
86          {MEMTOTALSWAP}},
87         {MEMAVAILSWAP, ASN_INTEGER, RONLY, var_extensible_mem, 1,
88          {MEMAVAILSWAP}},
89         {MEMTOTALREAL, ASN_INTEGER, RONLY, var_extensible_mem, 1,
90          {MEMTOTALREAL}},
91         {MEMAVAILREAL, ASN_INTEGER, RONLY, var_extensible_mem, 1,
92          {MEMAVAILREAL}},
93         {MEMTOTALSWAPTXT, ASN_INTEGER, RONLY, var_extensible_mem, 1,
94          {MEMTOTALSWAPTXT}},
95         {MEMUSEDSWAPTXT, ASN_INTEGER, RONLY, var_extensible_mem, 1,
96          {MEMUSEDSWAPTXT}},
97         {MEMTOTALREALTXT, ASN_INTEGER, RONLY, var_extensible_mem, 1,
98          {MEMTOTALREALTXT}},
99         {MEMUSEDREALTXT, ASN_INTEGER, RONLY, var_extensible_mem, 1,
100          {MEMUSEDREALTXT}},
101         {MEMTOTALFREE, ASN_INTEGER, RONLY, var_extensible_mem, 1,
102          {MEMTOTALFREE}},
103         {MEMSWAPMINIMUM, ASN_INTEGER, RONLY, var_extensible_mem, 1,
104          {MEMSWAPMINIMUM}},
105         {MEMSHARED, ASN_INTEGER, RONLY, var_extensible_mem, 1,
106          {MEMSHARED}},
107         {MEMBUFFER, ASN_INTEGER, RONLY, var_extensible_mem, 1,
108          {MEMBUFFER}},
109         {MEMCACHED, ASN_INTEGER, RONLY, var_extensible_mem, 1,
110          {MEMCACHED}},
111         {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_mem, 1,
112          {ERRORFLAG}},
113         {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_mem, 1, {ERRORMSG}}
114     };
115
116     /*
117      * Define the OID pointer to the top of the mib tree that we're
118      * registering underneath 
119      */
120     oid             mem_variables_oid[] = { UCDAVIS_MIB, MEMMIBNUM };
121
122     /*
123      * register ourselves with the agent to handle our mib tree 
124      */
125     REGISTER_MIB("ucd-snmp/memory", extensible_mem_variables, variable2,
126                  mem_variables_oid);
127
128     snmpd_register_config_handler("swap", memory_parse_config,
129                                   memory_free_config, "min-avail");
130 }
131
132
133 void
134 memory_parse_config(const char *token, char *cptr)
135 {
136     minimumswap = atoi(cptr);
137 }
138
139 void
140 memory_free_config(void)
141 {
142     minimumswap = DEFAULTMINIMUMSWAP;
143 }
144
145
146 /*
147  * var_extensible_mem(...
148  * Arguments:
149  * vp     IN      - pointer to variable entry that points here
150  * name    IN/OUT  - IN/name requested, OUT/name found
151  * length  IN/OUT  - length of IN/OUT oid's 
152  * exact   IN      - TRUE if an exact match was requested
153  * var_len OUT     - length of variable or 0 if function returned
154  * write_method
155  * 
156  */
157
158 static
159 unsigned char  *
160 var_extensible_mem(struct variable *vp,
161                    oid * name,
162                    size_t * length,
163                    int exact,
164                    size_t * var_len, WriteMethod ** write_method)
165 {
166     static long     long_ret;
167     static char     errmsg[300];
168
169     static struct uvmexp uvmexp;
170     int             uvmexp_size = sizeof(uvmexp);
171     int             uvmexp_mib[] = { CTL_VM, VM_UVMEXP };
172     static struct vmtotal total;
173     size_t          total_size = sizeof(total);
174     int             total_mib[] = { CTL_VM, VM_METER };
175
176     long            phys_mem;
177     size_t          phys_mem_size = sizeof(phys_mem);
178     int             phys_mem_mib[] = { CTL_HW, HW_USERMEM };
179
180     if (header_generic(vp, name, length, exact, var_len, write_method))
181         return (NULL);
182
183     /*
184      * Memory info 
185      */
186     sysctl(uvmexp_mib, 2, &uvmexp, &uvmexp_size, NULL, 0);
187     sysctl(total_mib, 2, &total, &total_size, NULL, 0);
188
189     /*
190      * Physical memory 
191      */
192     sysctl(phys_mem_mib, 2, &phys_mem, &phys_mem_size, NULL, 0);
193
194     long_ret = 0;               /* set to 0 as default */
195
196     /*
197      * Page-to-kb macro 
198      */
199 #define ptok(p) ((p) * (uvmexp.pagesize >> 10))
200
201     switch (vp->magic) {
202     case MIBINDEX:
203         long_ret = 0;
204         return ((u_char *) (&long_ret));
205     case ERRORNAME:            /* dummy name */
206         sprintf(errmsg, "swap");
207         *var_len = strlen(errmsg);
208         return ((u_char *) (errmsg));
209     case MEMTOTALSWAP:
210         long_ret = ptok(uvmexp.swpages);
211         return ((u_char *) (&long_ret));
212     case MEMAVAILSWAP:         /* FREE swap memory */
213         long_ret = ptok(uvmexp.swpages - uvmexp.swpginuse);
214         return ((u_char *) (&long_ret));
215     case MEMTOTALREAL:
216         long_ret = phys_mem >> 10;
217         return ((u_char *) (&long_ret));
218     case MEMAVAILREAL:         /* FREE real memory */
219         long_ret = ptok(uvmexp.free);
220         return ((u_char *) (&long_ret));
221
222         /*
223          * these are not implemented 
224          */
225     case MEMTOTALSWAPTXT:
226     case MEMUSEDSWAPTXT:
227     case MEMTOTALREALTXT:
228     case MEMUSEDREALTXT:
229 #if NO_DUMMY_VALUES
230         return NULL;
231 #endif
232         long_ret = -1;
233         return ((u_char *) (&long_ret));
234
235     case MEMTOTALFREE:
236         long_ret = ptok((int) total.t_free);
237         return ((u_char *) (&long_ret));
238     case MEMSWAPMINIMUM:
239         long_ret = minimumswap;
240         return ((u_char *) (&long_ret));
241     case MEMSHARED:
242         return ((u_char *) (&long_ret));
243     case MEMBUFFER:
244         return NULL;
245     case MEMCACHED:
246         return NULL;
247     case ERRORFLAG:
248         long_ret = (swapFree > minimumswap) ? 0 : 1;
249         return ((u_char *) (&long_ret));
250     case ERRORMSG:
251         if (swapFree < minimumswap)
252             sprintf(errmsg, "Running out of swap space (%qd)", swapFree);
253         else
254             errmsg[0] = 0;
255         *var_len = strlen(errmsg);
256         return ((u_char *) (errmsg));
257     }
258     return NULL;
259 }