Debian Codegraph @

agent/mibgroup/ucd-snmp/memory_darwin7.c

raw
1 /*
2 * memory_darwin7.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 #include <sys/buf.h>
16 #include <sys/uio.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 #include <sys/stat.h>
24
25 #include <time.h>
26 #include <nlist.h>
27 #include <kvm.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <paths.h>
35 #include <limits.h>
36 #include <mach/mach.h>
37 #include <dirent.h>
38
39 #include <net-snmp/net-snmp-includes.h>
40 #include <net-snmp/agent/net-snmp-agent-includes.h>
41 #include <net-snmp/agent/auto_nlist.h>
42
43 #include "util_funcs/header_generic.h"
44 #include "memory.h"
45 #include "memory_darwin7.h"
46
47 /*
48 * * Swap info
49 * */
50 /*off_t swapTotal;
51 off_t swapUsed;
52 off_t swapFree;
53 */
54
55 /*
56 * Default swap warning limit (kb)
57 */
58 #define DEFAULTMINIMUMSWAP 16000
59
60 /*
61 * Swap warning limit
62 */
63 long minimumswap;
64
65 static FindVarMethod var_extensible_mem;
66
67 void
68 init_memory_darwin7(void)
69 {
70
71 struct variable2 extensible_mem_variables[] = {
72 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
73 var_extensible_mem, 1, {MIBINDEX}},
74 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
75 var_extensible_mem, 1, {ERRORNAME}},
76 {MEMTOTALSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
77 var_extensible_mem, 1, {MEMTOTALSWAP}},
78 {MEMAVAILSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
79 var_extensible_mem, 1, {MEMAVAILSWAP}},
80 {MEMTOTALREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
81 var_extensible_mem, 1, {MEMTOTALREAL}},
82 {MEMAVAILREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
83 var_extensible_mem, 1, {MEMAVAILREAL}},
84 {MEMTOTALSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
85 var_extensible_mem, 1, {MEMTOTALSWAPTXT}},
86 {MEMUSEDSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
87 var_extensible_mem, 1, {MEMUSEDSWAPTXT}},
88 {MEMTOTALREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
89 var_extensible_mem, 1, {MEMTOTALREALTXT}},
90 {MEMUSEDREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
91 var_extensible_mem, 1, {MEMUSEDREALTXT}},
92 {MEMTOTALFREE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
93 var_extensible_mem, 1, {MEMTOTALFREE}},
94 {MEMSWAPMINIMUM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
95 var_extensible_mem, 1, {MEMSWAPMINIMUM}},
96 {MEMSHARED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
97 var_extensible_mem, 1, {MEMSHARED}},
98 {MEMBUFFER, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
99 var_extensible_mem, 1, {MEMBUFFER}},
100 {MEMCACHED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
101 var_extensible_mem, 1, {MEMCACHED}},
102 {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
103 var_extensible_mem, 1, {ERRORFLAG}},
104 {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
105 var_extensible_mem, 1, {ERRORMSG}}
106 };
107
108 /*
109 * Define the OID pointer to the top of the mib tree that we're
110 * registering underneath
111 */
112 oid mem_variables_oid[] = { NETSNMP_UCDAVIS_MIB, NETSNMP_MEMMIBNUM };
113
114 /*
115 * register ourselves with the agent to handle our mib tree
116 */
117 REGISTER_MIB("ucd-snmp/memory", extensible_mem_variables, variable2,
118 mem_variables_oid);
119
120 snmpd_register_config_handler("swap", memory_parse_config,
121 memory_free_config, "min-avail");
122 }
123
124
125 void
126 memory_parse_config(const char *token, char *cptr)
127 {
128 minimumswap = atoi(cptr);
129 }
130
131 void
132 memory_free_config(void)
133 {
134 minimumswap = DEFAULTMINIMUMSWAP;
135 }
136
137 off_t
138 swapsize(void)
139 {
140 int pagesize;
141 int i, n;
142 DIR *dirp;
143 struct dirent *dp;
144 struct stat buf;
145 char errmsg[1024];
146 char full_name[1024];
147 off_t swapSize;
148
149 /* we set the size to -1 if we're not supported */
150 swapSize = -1;
151
152 #if defined(SWAPFILE_DIR) && defined(SWAPFILE_PREFIX)
153 dirp = opendir((const char *) SWAPFILE_DIR);
154 while((dp = readdir(dirp)) != NULL) {
155 /* if the file starts with the same as SWAPFILE_PREFIX
156 * we want to stat the file to get it's size
157 */
158 if(strspn(dp->d_name,(char *) SWAPFILE_PREFIX) == strlen((char *) SWAPFILE_PREFIX)) {
159 sprintf(full_name,"%s/%s",SWAPFILE_DIR,dp->d_name);
160 /* we need to stat each swapfile to get it's size */
161 if(stat(full_name,&buf) != 0) {
162 sprintf(errmsg, "swapsize: can't stat file %s",full_name);
163 snmp_log_perror(errmsg);
164 } else {
165 /* total swap allocated is the size of
166 * all the swapfile's that exist in
167 * the SWAPFILE_DIR dir
168 */
169 swapSize += buf.st_size;
170 }
171 }
172
173 }
174 closedir(dirp);
175 #endif
176
177 return swapSize;
178
179 }
180
181
182 /*
183 * var_extensible_mem(...
184 * Arguments:
185 * vp IN - pointer to variable entry that points here
186 * name IN/OUT - IN/name requested, OUT/name found
187 * length IN/OUT - length of IN/OUT oid's
188 * exact IN - TRUE if an exact match was requested
189 * var_len OUT - length of variable or 0 if function returned
190 * write_method
191 *
192 */
193
194 static unsigned char *
195 var_extensible_mem(struct variable *vp,
196 oid * name,
197 size_t * length,
198 int exact,
199 size_t * var_len, WriteMethod ** write_method)
200 {
201 static long long_ret;
202 static char errmsg[1024];
203 /* the getting used swap routine takes awhile, so we
204 * do not want to run it often, so we use a cache to
205 * keep from updating it too often
206 */
207 static time_t prev_time;
208 time_t cur_time = time((time_t *)NULL);
209
210 int mib[2];
211
212 u_long phys_mem;
213 size_t phys_mem_size = sizeof(phys_mem);
214
215 int pagesize;
216 size_t pagesize_size = sizeof(pagesize);
217
218 u_long pages_used;
219
220 off_t swapFree;
221 static off_t swapUsed;
222 off_t swapSize;
223
224 /* for host_statistics() */
225 vm_statistics_data_t vm_stat;
226 int count = HOST_VM_INFO_COUNT;
227
228 if (header_generic(vp, name, length, exact, var_len, write_method))
229 return (NULL);
230
231 mib[0] = CTL_HW;
232 mib[1] = HW_PHYSMEM;
233
234 /*
235 * Physical memory
236 */
237 if(sysctl(mib, 2, &phys_mem, &phys_mem_size, NULL, 0) == -1)
238 snmp_log_perror("sysctl: phys_mem");
239
240 /*
241 * Pagesize
242 */
243 mib[1] = HW_PAGESIZE;
244 if(sysctl(mib, 2, &pagesize, &pagesize_size, NULL, 0) == -1)
245 snmp_log_perror("sysctl: pagesize");
246 /*
247 * used memory
248 */
249 host_statistics(mach_host_self(),HOST_VM_INFO,&vm_stat,&count);
250 pages_used = vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count;
251 /*
252 * Page-to-kb macro
253 */
254 #define ptok(p) ((p) * (pagesize >> 10))
255
256 /*
257 * swap info
258 */
259
260 swapSize = swapsize();
261 /* if it's been less then 30 seconds since the
262 * last run, don't call the pages_swapped()
263 * routine yet */
264 if(cur_time > prev_time + 30) {
265 swapUsed = (off_t) pages_swapped();
266 prev_time = time((time_t *)NULL);
267 }
268 swapFree = swapSize - (swapUsed * pagesize);
269
270 long_ret = 0; /* set to 0 as default */
271
272 switch (vp->magic) {
273 case MIBINDEX:
274 long_ret = 0;
275 return ((u_char *) (&long_ret));
276 case ERRORNAME: /* dummy name */
277 sprintf(errmsg, "swap");
278 *var_len = strlen(errmsg);
279 return ((u_char *) (errmsg));
280 case MEMTOTALSWAP:
281 long_ret = swapSize >> 10;
282 return ((u_char *) (&long_ret));
283 case MEMAVAILSWAP: /* FREE swap memory */
284 long_ret = swapFree >> 10;
285 return ((u_char *) (&long_ret));
286 case MEMTOTALREAL:
287 long_ret = phys_mem >> 10;
288 return ((u_char *) (&long_ret));
289 case MEMAVAILREAL: /* FREE real memory */
290 long_ret = (phys_mem >> 10) - (ptok(pages_used));
291 return ((u_char *) (&long_ret));
292 case MEMSWAPMINIMUM:
293 long_ret = minimumswap;
294 return ((u_char *) (&long_ret));
295 /*
296 * these are not implemented
297 */
298 case MEMTOTALSWAPTXT:
299 case MEMUSEDSWAPTXT:
300 case MEMTOTALREALTXT:
301 case MEMUSEDREALTXT:
302 case MEMTOTALFREE:
303 case MEMSHARED:
304 case MEMBUFFER:
305 case MEMCACHED:
306 #ifdef NETSNMP_NO_DUMMY_VALUES
307 return NULL;
308 #endif
309 long_ret = -1;
310 return ((u_char *) (&long_ret));
311
312 case ERRORFLAG:
313 long_ret = (swapFree > minimumswap) ? 0 : 1;
314 return ((u_char *) (&long_ret));
315 case ERRORMSG:
316 if (swapFree < minimumswap)
317 sprintf(errmsg, "Running out of swap space (%qd)", swapFree);
318 else
319 errmsg[0] = 0;
320 *var_len = strlen(errmsg);
321 return ((u_char *) (errmsg));
322 }
323 return NULL;
324 }
325
326
327 /* get the number of pages that are swapped out */
328 /* we think this is correct and are valid values */
329 /* but not sure. time will tell if it's correct */
330 /* Note: this routine is _expensive_!!! we run this */
331 /* as little as possible by caching it's return so */
332 /* it's not run on every poll */
333 /* Apple, please give us a better way! :) */
334 int pages_swapped(void) {
335 boolean_t retval;
336 kern_return_t error;
337 processor_set_t *psets, pset;
338 task_t *tasks;
339 unsigned i, j, pcnt, tcnt;
340 int pid;
341 mach_msg_type_number_t count;
342 vm_address_t address;
343 mach_port_t object_name;
344 vm_region_extended_info_data_t info;
345 vm_size_t size;
346 mach_port_t mach_port;
347 int swapped_pages;
348 int swapped_pages_total = 0;
349 char errmsg[1024];
350
351
352 mach_port = mach_host_self();
353 error = host_processor_sets(mach_port, &psets, &pcnt);
354 if (error != KERN_SUCCESS) {
355 sprintf(errmsg, "Error in host_processor_sets(): %s\n", mach_error_string(error));
356 snmp_log_perror(errmsg);
357 return(0);
358 }
359
360 for (i = 0; i < pcnt; i++) {
361 error = host_processor_set_priv(mach_port, psets[i], &pset);
362 if (error != KERN_SUCCESS) {
363 sprintf(errmsg,"Error in host_processor_set_priv(): %s\n", mach_error_string(error));
364 snmp_log_perror(errmsg);
365 return(0);
366 }
367
368 error = processor_set_tasks(pset, &tasks, &tcnt);
369 if (error != KERN_SUCCESS) {
370 sprintf(errmsg,"Error in processor_set_tasks(): %s\n", mach_error_string(error));
371 snmp_log_perror(errmsg);
372 return(0);
373 }
374
375 for (j = 0; j < tcnt; j++) {
376 error = pid_for_task(tasks[j], &pid);
377 if (error != KERN_SUCCESS) {
378 /* Not a process, or the process is gone. */
379 continue;
380 }
381
382 swapped_pages = 0;
383 for (address = 0;; address += size) {
384 kern_return_t ret = KERN_FAILURE;
385
386 /* Get memory region. */
387 count = VM_REGION_EXTENDED_INFO_COUNT;
388 #ifdef HAVE_VM_REGION_64
389 ret = vm_region64(tasks[j], &address, &size,
390 VM_REGION_EXTENDED_INFO,
391 (vm_region_extended_info_t)&info, &count,
392 &object_name);
393 #elif defined(HAVE_VM_REGION)
394 ret = vm_region(tasks[j], &address, &size,
395 VM_REGION_EXTENDED_INFO,
396 (vm_region_extended_info_t)&info, &count,
397 &object_name);
398 #error How to query memory protection information?
399 #endif
400 if (ret != KERN_SUCCESS) {
401 /* No more memory regions. */
402 break;
403 }
404
405 if(info.pages_swapped_out > 0) {
406 swapped_pages += info.pages_swapped_out;
407 }
408 }
409
410 if(swapped_pages > 0) {
411 swapped_pages_total += swapped_pages;
412 }
413
414 if (tasks[j] != mach_task_self()) {
415 mach_port_deallocate(mach_task_self(), tasks[j]);
416 }
417 }
418 }
419
420 return(swapped_pages_total);
421 }