Debian Codegraph @

agent/mibgroup/ucd-snmp/memory_netbsd1.c

raw
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
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
132 memory_parse_config(const char *token, char *cptr)
133 {
134 minimumswap = atoi(cptr);
135 }
136
137 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 *
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 size_t 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 if (sysctl(uvmexp_mib, 2, &uvmexp, &uvmexp_size, NULL, 0) == -1) {
185 snmp_log(LOG_ERR, "sysctl VM_UVMEXP failed (errno %d)\n", errno);
186 return NULL;
187 }
188 if (sysctl(total_mib, 2, &total, &total_size, NULL, 0) == -1) {
189 snmp_log(LOG_ERR, "sysctl VM_METER failed (errno %d)\n", errno);
190 return NULL;
191 }
192
193 /*
194 * Physical memory
195 */
196 if (sysctl(phys_mem_mib, 2, &phys_mem, &phys_mem_size, NULL, 0) == -1) {
197 snmp_log(LOG_ERR, "sysctl HW_USERMEM failed (errno %d)\n", errno);
198 return NULL;
199 }
200
201 long_ret = 0; /* set to 0 as default */
202
203 /*
204 * Page-to-kb macro
205 */
206 #define ptok(p) ((p) * (uvmexp.pagesize >> 10))
207
208 switch (vp->magic) {
209 case MIBINDEX:
210 long_ret = 0;
211 return ((u_char *) (&long_ret));
212 case ERRORNAME: /* dummy name */
213 sprintf(errmsg, "swap");
214 *var_len = strlen(errmsg);
215 return ((u_char *) (errmsg));
216 case MEMTOTALSWAP:
217 long_ret = ptok(uvmexp.swpages);
218 return ((u_char *) (&long_ret));
219 case MEMAVAILSWAP: /* FREE swap memory */
220 long_ret = ptok(uvmexp.swpages - uvmexp.swpginuse);
221 return ((u_char *) (&long_ret));
222 case MEMTOTALREAL:
223 long_ret = phys_mem >> 10;
224 return ((u_char *) (&long_ret));
225 case MEMAVAILREAL: /* FREE real memory */
226 long_ret = ptok(uvmexp.free);
227 return ((u_char *) (&long_ret));
228
229 /*
230 * these are not implemented
231 */
232 case MEMTOTALSWAPTXT:
233 case MEMUSEDSWAPTXT:
234 case MEMTOTALREALTXT:
235 case MEMUSEDREALTXT:
236 #ifdef NETSNMP_NO_DUMMY_VALUES
237 return NULL;
238 #endif
239 long_ret = -1;
240 return ((u_char *) (&long_ret));
241
242 case MEMTOTALFREE:
243 long_ret = ptok((int) total.t_free);
244 return ((u_char *) (&long_ret));
245 case MEMSWAPMINIMUM:
246 long_ret = minimumswap;
247 return ((u_char *) (&long_ret));
248 case MEMSHARED:
249 return ((u_char *) (&long_ret));
250 case MEMBUFFER:
251 return NULL;
252 case MEMCACHED:
253 return NULL;
254 case ERRORFLAG:
255 long_ret = (swapFree > minimumswap) ? 0 : 1;
256 return ((u_char *) (&long_ret));
257 case ERRORMSG:
258 if (swapFree < minimumswap)
259 sprintf(errmsg, "Running out of swap space (%qd)", swapFree);
260 else
261 errmsg[0] = 0;
262 *var_len = strlen(errmsg);
263 return ((u_char *) (errmsg));
264 }
265 return NULL;
266 }