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 #include <net-snmp/net-snmp-includes.h>
47 #include <net-snmp/agent/net-snmp-agent-includes.h>
48 #include <net-snmp/agent/auto_nlist.h>
49 
50 #include "util_funcs/header_generic.h"
51 #include "memory.h"
52 #include "memory_netbsd1.h"
53 
54 /*
55  * Default swap warning limit (kb)
56  */
57 #define DEFAULTMINIMUMSWAP 16000
58 
59 /*
60  * Swap warning limit
61  */
62 long            minimumswap;
63 
64 /*
65  * Swap info
66  */
67 quad_t          swapTotal;
68 quad_t          swapUsed;
69 quad_t          swapFree;
70 
71 static FindVarMethod var_extensible_mem;
72 
73 void
init_memory_netbsd1(void)74 init_memory_netbsd1(void)
75 {
76 
77     struct variable2 extensible_mem_variables[] = {
78         {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
79          var_extensible_mem, 1, {MIBINDEX}},
80         {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
81          var_extensible_mem, 1, {ERRORNAME}},
82         {MEMTOTALSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
83          var_extensible_mem, 1, {MEMTOTALSWAP}},
84         {MEMAVAILSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
85          var_extensible_mem, 1, {MEMAVAILSWAP}},
86         {MEMTOTALREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
87          var_extensible_mem, 1, {MEMTOTALREAL}},
88         {MEMAVAILREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
89          var_extensible_mem, 1, {MEMAVAILREAL}},
90         {MEMTOTALSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
91          var_extensible_mem, 1, {MEMTOTALSWAPTXT}},
92         {MEMUSEDSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
93          var_extensible_mem, 1, {MEMUSEDSWAPTXT}},
94         {MEMTOTALREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
95          var_extensible_mem, 1, {MEMTOTALREALTXT}},
96         {MEMUSEDREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
97          var_extensible_mem, 1, {MEMUSEDREALTXT}},
98         {MEMTOTALFREE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
99          var_extensible_mem, 1, {MEMTOTALFREE}},
100         {MEMSWAPMINIMUM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
101          var_extensible_mem, 1, {MEMSWAPMINIMUM}},
102         {MEMSHARED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
103          var_extensible_mem, 1, {MEMSHARED}},
104         {MEMBUFFER, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
105          var_extensible_mem, 1, {MEMBUFFER}},
106         {MEMCACHED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
107          var_extensible_mem, 1, {MEMCACHED}},
108         {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
109          var_extensible_mem, 1, {ERRORFLAG}},
110         {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
111          var_extensible_mem, 1, {ERRORMSG}}
112     };
113 
114     /*
115      * Define the OID pointer to the top of the mib tree that we're
116      * registering underneath
117      */
118     oid             mem_variables_oid[] = { NETSNMP_UCDAVIS_MIB, NETSNMP_MEMMIBNUM };
119 
120     /*
121      * register ourselves with the agent to handle our mib tree
122      */
123     REGISTER_MIB("ucd-snmp/memory", extensible_mem_variables, variable2,
124                  mem_variables_oid);
125 
126     snmpd_register_config_handler("swap", memory_parse_config,
127                                   memory_free_config, "min-avail");
128 }
129 
130 
131 void
memory_parse_config(const char * token,char * cptr)132 memory_parse_config(const char *token, char *cptr)
133 {
134     minimumswap = atoi(cptr);
135 }
136 
137 void
memory_free_config(void)138 memory_free_config(void)
139 {
140     minimumswap = DEFAULTMINIMUMSWAP;
141 }
142 
143 
144 /*
145  * var_extensible_mem(...
146  * Arguments:
147  * vp     IN      - pointer to variable entry that points here
148  * name    IN/OUT  - IN/name requested, OUT/name found
149  * length  IN/OUT  - length of IN/OUT oid's
150  * exact   IN      - TRUE if an exact match was requested
151  * var_len OUT     - length of variable or 0 if function returned
152  * write_method
153  *
154  */
155 
156 static
157 unsigned char  *
var_extensible_mem(struct variable * vp,oid * name,size_t * length,int exact,size_t * var_len,WriteMethod ** write_method)158 var_extensible_mem(struct variable *vp,
159                    oid * name,
160                    size_t * length,
161                    int exact,
162                    size_t * var_len, WriteMethod ** write_method)
163 {
164     static long     long_ret;
165     static char     errmsg[1024];
166 
167     static struct uvmexp uvmexp;
168     int             uvmexp_size = sizeof(uvmexp);
169     int             uvmexp_mib[] = { CTL_VM, VM_UVMEXP };
170     static struct vmtotal total;
171     size_t          total_size = sizeof(total);
172     int             total_mib[] = { CTL_VM, VM_METER };
173 
174     long            phys_mem;
175     size_t          phys_mem_size = sizeof(phys_mem);
176     int             phys_mem_mib[] = { CTL_HW, HW_PHYSMEM };
177 
178     if (header_generic(vp, name, length, exact, var_len, write_method))
179         return (NULL);
180 
181     /*
182      * Memory info
183      */
184     sysctl(uvmexp_mib, 2, &uvmexp, &uvmexp_size, NULL, 0);
185     sysctl(total_mib, 2, &total, &total_size, NULL, 0);
186 
187     /*
188      * Physical memory
189      */
190     sysctl(phys_mem_mib, 2, &phys_mem, &phys_mem_size, NULL, 0);
191 
192     long_ret = 0;               /* set to 0 as default */
193 
194     /*
195      * Page-to-kb macro
196      */
197 #define ptok(p) ((p) * (uvmexp.pagesize >> 10))
198 
199     switch (vp->magic) {
200     case MIBINDEX:
201         long_ret = 0;
202         return ((u_char *) (&long_ret));
203     case ERRORNAME:            /* dummy name */
204         sprintf(errmsg, "swap");
205         *var_len = strlen(errmsg);
206         return ((u_char *) (errmsg));
207     case MEMTOTALSWAP:
208         long_ret = ptok(uvmexp.swpages);
209         return ((u_char *) (&long_ret));
210     case MEMAVAILSWAP:         /* FREE swap memory */
211         long_ret = ptok(uvmexp.swpages - uvmexp.swpginuse);
212         return ((u_char *) (&long_ret));
213     case MEMTOTALREAL:
214         long_ret = phys_mem >> 10;
215         return ((u_char *) (&long_ret));
216     case MEMAVAILREAL:         /* FREE real memory */
217         long_ret = ptok(uvmexp.free);
218         return ((u_char *) (&long_ret));
219 
220         /*
221          * these are not implemented
222          */
223     case MEMTOTALSWAPTXT:
224     case MEMUSEDSWAPTXT:
225     case MEMTOTALREALTXT:
226     case MEMUSEDREALTXT:
227 #if NETSNMP_NO_DUMMY_VALUES
228         return NULL;
229 #endif
230         long_ret = -1;
231         return ((u_char *) (&long_ret));
232 
233     case MEMTOTALFREE:
234         long_ret = ptok((int) total.t_free);
235         return ((u_char *) (&long_ret));
236     case MEMSWAPMINIMUM:
237         long_ret = minimumswap;
238         return ((u_char *) (&long_ret));
239     case MEMSHARED:
240         return ((u_char *) (&long_ret));
241     case MEMBUFFER:
242         return NULL;
243     case MEMCACHED:
244         return NULL;
245     case ERRORFLAG:
246         long_ret = (swapFree > minimumswap) ? 0 : 1;
247         return ((u_char *) (&long_ret));
248     case ERRORMSG:
249         if (swapFree < minimumswap)
250             sprintf(errmsg, "Running out of swap space (%qd)", swapFree);
251         else
252             errmsg[0] = 0;
253         *var_len = strlen(errmsg);
254         return ((u_char *) (errmsg));
255     }
256     return NULL;
257 }
258