1 /*-
2  * Copyright (c) 2005-2006 The FreeBSD Project
3  * All rights reserved.
4  *
5  * Author: Victor Cruceru <soc-victor@freebsd.org>
6  *
7  * Redistribution of this software and documentation and use in source and
8  * binary forms, with or without modification, are permitted provided that
9  * the following conditions are met:
10  *
11  * 1. Redistributions of source code or documentation must retain the above
12  *    copyright notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * Host Resources MIB for SNMPd. Implementation for hrStorageTable
34  */
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/vmmeter.h>
40 #include <sys/mount.h>
41 
42 #include <vm/vm_param.h>
43 
44 #include <assert.h>
45 #include <err.h>
46 #include <limits.h>
47 #include <memstat.h>
48 #include <paths.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <unistd.h> /* for getpagesize() */
53 #include <sysexits.h>
54 
55 #include "hostres_snmp.h"
56 #include "hostres_oid.h"
57 #include "hostres_tree.h"
58 
59 /* maximum length for descritpion string according to MIB */
60 #define	SE_DESC_MLEN	(255 + 1)
61 
62 /*
63  * This structure is used to hold a SNMP table entry
64  * for HOST-RESOURCES-MIB's hrStorageTable
65  */
66 struct storage_entry {
67 	int32_t		index;
68 	const struct asn_oid *type;
69 	u_char		*descr;
70 	int32_t		allocationUnits;
71 	int32_t		size;
72 	int32_t		used;
73 	uint32_t	allocationFailures;
74 #define	HR_STORAGE_FOUND 0x001
75 	uint32_t	flags;	/* to be used internally*/
76 	TAILQ_ENTRY(storage_entry) link;
77 };
78 TAILQ_HEAD(storage_tbl, storage_entry);
79 
80 /*
81  * Next structure is used to keep o list of mappings from a specific name
82  * (a_name) to an entry in the hrStorageTblEntry. We are trying to keep the
83  * same index for a specific name at least for the duration of one SNMP agent
84  * run.
85  */
86 struct storage_map_entry {
87 	int32_t		hrIndex; /* used for storage_entry::index */
88 
89 	/* map key, also used for storage_entry::descr */
90 	u_char		*a_name;
91 
92 	/*
93 	 * next may be NULL if the respective storage_entry
94 	 * is (temporally) gone
95 	 */
96 	struct storage_entry *entry;
97 	STAILQ_ENTRY(storage_map_entry) link;
98 };
99 STAILQ_HEAD(storage_map, storage_map_entry);
100 
101 /* the head of the list with table's entries */
102 static struct storage_tbl storage_tbl = TAILQ_HEAD_INITIALIZER(storage_tbl);
103 
104 /*for consistent table indexing*/
105 static struct storage_map storage_map =
106     STAILQ_HEAD_INITIALIZER(storage_map);
107 
108 /* last (agent) tick when hrStorageTable was updated */
109 static uint64_t storage_tick;
110 
111 /* maximum number of ticks between two refreshs */
112 uint32_t storage_tbl_refresh = HR_STORAGE_TBL_REFRESH * 100;
113 
114 /* for kvm_getswapinfo, malloc'd */
115 static struct kvm_swap *swap_devs;
116 static size_t swap_devs_len;		/* item count for swap_devs */
117 
118 /* for getfsstat, malloc'd */
119 static struct statfs *fs_buf;
120 static size_t fs_buf_count;		/* item count for fs_buf */
121 
122 static struct vmtotal mem_stats;
123 
124 /* next int available for indexing the hrStorageTable */
125 static uint32_t next_storage_index = 1;
126 
127 /* start of list for memory detailed stats */
128 static struct memory_type_list *mt_list;
129 
130 /* Constants */
131 static const struct asn_oid OIDX_hrStorageRam_c = OIDX_hrStorageRam;
132 static const struct asn_oid OIDX_hrStorageVirtualMemory_c =
133     OIDX_hrStorageVirtualMemory;
134 
135 /**
136  * Create a new entry into the storage table and, if necessary, an
137  * entry into the storage map.
138  */
139 static struct storage_entry *
140 storage_entry_create(const char *name)
141 {
142 	struct storage_entry *entry;
143 	struct storage_map_entry *map;
144 	size_t name_len;
145 
146 	assert(name != NULL);
147 	assert(strlen(name) > 0);
148 
149 	STAILQ_FOREACH(map, &storage_map, link)
150 		if (strcmp(map->a_name, name) == 0)
151 			break;
152 
153 	if (map == NULL) {
154 		/* new object - get a new index */
155 		if (next_storage_index > INT_MAX) {
156 		        syslog(LOG_ERR,
157 			    "%s: hrStorageTable index wrap", __func__);
158 			errx(EX_SOFTWARE, "hrStorageTable index wrap");
159 		}
160 
161 		if ((map = malloc(sizeof(*map))) == NULL) {
162 			syslog(LOG_ERR, "hrStorageTable: %s: %m", __func__ );
163 			return (NULL);
164 		}
165 
166 		name_len = strlen(name) + 1;
167 		if (name_len > SE_DESC_MLEN)
168 			name_len = SE_DESC_MLEN;
169 
170 		if ((map->a_name = malloc(name_len)) == NULL) {
171 			free(map);
172 			return (NULL);
173 		}
174 
175 		strlcpy(map->a_name, name, name_len);
176 		map->hrIndex = next_storage_index++;
177 
178 		STAILQ_INSERT_TAIL(&storage_map, map, link);
179 
180 		HRDBG("%s added into hrStorageMap at index=%d",
181 		    name, map->hrIndex);
182 	} else {
183 		HRDBG("%s exists in hrStorageMap index=%d\n",
184 		    name, map->hrIndex);
185 	}
186 
187 	if ((entry = malloc(sizeof(*entry))) == NULL) {
188 		syslog(LOG_WARNING, "%s: %m", __func__);
189 		return (NULL);
190 	}
191         memset(entry, 0, sizeof(*entry));
192 
193 	entry->index = map->hrIndex;
194 
195 	if ((entry->descr = strdup(map->a_name)) == NULL) {
196 		free(entry);
197 		return (NULL);
198 	}
199 
200 	map->entry = entry;
201 
202 	INSERT_OBJECT_INT(entry, &storage_tbl);
203 
204 	return (entry);
205 }
206 
207 /**
208  * Delete an entry from the storage table.
209  */
210 static void
211 storage_entry_delete(struct storage_entry *entry)
212 {
213 	struct storage_map_entry *map;
214 
215 	assert(entry != NULL);
216 
217 	TAILQ_REMOVE(&storage_tbl, entry, link);
218 	STAILQ_FOREACH(map, &storage_map, link)
219 		if (map->entry == entry) {
220 			map->entry = NULL;
221 			break;
222 		}
223 	free(entry->descr);
224 	free(entry);
225 }
226 
227 /**
228  * Find a table entry by its name.
229  */
230 static struct storage_entry *
231 storage_find_by_name(const char *name)
232 {
233 	struct storage_entry *entry;
234 
235 	TAILQ_FOREACH(entry, &storage_tbl, link)
236 		if (strcmp(entry->descr, name) == 0)
237 			return (entry);
238 
239 	return (NULL);
240 }
241 
242 /*
243  * VM info.
244  */
245 static void
246 storage_OS_get_vm(void)
247 {
248 	int mib[2] = { CTL_VM, VM_TOTAL };
249 	size_t len = sizeof(mem_stats);
250 	int page_size_bytes;
251 	struct storage_entry *entry;
252 
253 	if (sysctl(mib, 2, &mem_stats, &len, NULL, 0) < 0) {
254 		syslog(LOG_ERR,
255 		    "hrStoragetable: %s: sysctl({CTL_VM, VM_METER}) "
256 		    "failed: %m", __func__);
257 		assert(0);
258 		return;
259 	}
260 
261 	page_size_bytes = getpagesize();
262 
263 	/* Real Memory Metrics */
264 	if ((entry = storage_find_by_name("Real Memory Metrics")) == NULL &&
265 	    (entry = storage_entry_create("Real Memory Metrics")) == NULL)
266 		return; /* I'm out of luck now, maybe next time */
267 
268 	entry->flags |= HR_STORAGE_FOUND;
269 	entry->type = &OIDX_hrStorageRam_c;
270 	entry->allocationUnits = page_size_bytes;
271 	entry->size = mem_stats.t_rm;
272 	entry->used = mem_stats.t_arm; /* ACTIVE is not USED - FIXME */
273 	entry->allocationFailures = 0;
274 
275 	/* Shared Real Memory Metrics */
276 	if ((entry = storage_find_by_name("Shared Real Memory Metrics")) ==
277 	    NULL &&
278 	    (entry = storage_entry_create("Shared Real Memory Metrics")) ==
279 	    NULL)
280 		return;
281 
282 	entry->flags |= HR_STORAGE_FOUND;
283 	entry->type = &OIDX_hrStorageRam_c;
284 	entry->allocationUnits = page_size_bytes;
285 	entry->size = mem_stats.t_rmshr;
286 	/* ACTIVE is not USED - FIXME */
287 	entry->used = mem_stats.t_armshr;
288 	entry->allocationFailures = 0;
289 }
290 
291 static void
292 storage_OS_get_memstat(void)
293 {
294 	struct memory_type *mt_item;
295 	struct storage_entry *entry;
296 
297 	if (mt_list == NULL) {
298 		if ((mt_list = memstat_mtl_alloc()) == NULL)
299 			/* again? we have a serious problem */
300 		return;
301 	}
302 
303 	if (memstat_sysctl_all(mt_list, 0) < 0) {
304 		syslog(LOG_ERR, "memstat_sysctl_all failed: %s",
305 		    memstat_strerror(memstat_mtl_geterror(mt_list)) );
306 		return;
307 	}
308 
309 	if ((mt_item = memstat_mtl_first(mt_list)) == NULL) {
310 		/* usually this is not an error, no errno for this failure*/
311 		HRDBG("memstat_mtl_first failed");
312 		return;
313 	}
314 
315 	do {
316 		const char *memstat_name;
317 		uint64_t tmp_size;
318 		int allocator;
319 		char alloc_descr[SE_DESC_MLEN];
320 
321 		memstat_name = memstat_get_name(mt_item);
322 
323 		if (memstat_name == NULL || strlen(memstat_name) == 0)
324 			continue;
325 
326 		switch (allocator = memstat_get_allocator(mt_item)) {
327 
328 		  case ALLOCATOR_MALLOC:
329 			snprintf(alloc_descr, sizeof(alloc_descr),
330 			    "MALLOC: %s", memstat_name);
331 			break;
332 
333 		  case ALLOCATOR_UMA:
334 			snprintf(alloc_descr, sizeof(alloc_descr),
335 			    "UMA: %s", memstat_name);
336 			break;
337 
338 		  default:
339 			snprintf(alloc_descr, sizeof(alloc_descr),
340 			    "UNKNOWN%d: %s", allocator, memstat_name);
341 			break;
342 		}
343 
344 		if ((entry = storage_find_by_name(alloc_descr)) == NULL &&
345 		    (entry = storage_entry_create(alloc_descr)) == NULL)
346 			return;
347 
348 		entry->flags |= HR_STORAGE_FOUND;
349 		entry->type = &OIDX_hrStorageRam_c;
350 
351 		if ((tmp_size = memstat_get_size(mt_item)) == 0)
352 			tmp_size = memstat_get_sizemask(mt_item);
353 		entry->allocationUnits =
354 		    (tmp_size  > INT_MAX ? INT_MAX : (int32_t)tmp_size);
355 
356 		tmp_size  = memstat_get_countlimit(mt_item);
357 		entry->size =
358 		    (tmp_size  > INT_MAX ? INT_MAX : (int32_t)tmp_size);
359 
360 		tmp_size = memstat_get_count(mt_item);
361 		entry->used =
362 		    (tmp_size  > INT_MAX ? INT_MAX : (int32_t)tmp_size);
363 
364 		tmp_size = memstat_get_failures(mt_item);
365 		entry->allocationFailures =
366 		    (tmp_size  > INT_MAX ? INT_MAX : (int32_t)tmp_size);
367 
368 	} while((mt_item = memstat_mtl_next(mt_item)) != NULL);
369 }
370 
371 /**
372  * Get swap info
373  */
374 static void
375 storage_OS_get_swap(void)
376 {
377         int nswapdev = 0;
378 	size_t len = sizeof(nswapdev);
379 	struct storage_entry *entry;
380 	char swap_w_prefix[SE_DESC_MLEN];
381 
382 	if (sysctlbyname("vm.nswapdev", &nswapdev, &len, NULL,0 ) < 0) {
383 		syslog(LOG_ERR,
384 		    "hrStorageTable: sysctlbyname(\"vm.nswapdev\") "
385 		    "failed. %m");
386 		assert(0);
387 		return;
388 	}
389 
390 	if (nswapdev <= 0) {
391 		HRDBG("vm.nswapdev is %d", nswapdev);
392 		return;
393 	}
394 
395 	if (nswapdev + 1 != (int)swap_devs_len || swap_devs == NULL) {
396 		swap_devs_len = nswapdev + 1;
397 		swap_devs = reallocf(swap_devs,
398 		    swap_devs_len * sizeof(struct kvm_swap));
399 
400 		assert(swap_devs != NULL);
401 		if (swap_devs == NULL) {
402 			swap_devs_len = 0;
403 			return;
404 		}
405 	}
406 
407 	nswapdev = kvm_getswapinfo(hr_kd, swap_devs, swap_devs_len, 0);
408 	if (nswapdev < 0) {
409 		syslog(LOG_ERR,
410 		    "hrStorageTable: kvm_getswapinfo failed. %m\n");
411 		assert(0);
412 		return;
413 	}
414 
415 	for (len = 0; len < (size_t)nswapdev; len++) {
416 		memset(&swap_w_prefix[0], '\0', sizeof(swap_w_prefix));
417 		snprintf(swap_w_prefix, sizeof(swap_w_prefix) - 1,
418 		    "Swap:%s%s", _PATH_DEV, swap_devs[len].ksw_devname);
419 
420 		entry = storage_find_by_name(swap_w_prefix);
421 		if (entry == NULL)
422 			entry = storage_entry_create(swap_w_prefix);
423 
424 		assert (entry != NULL);
425 		if (entry == NULL)
426 			return; /* Out of luck */
427 
428 		entry->flags |= HR_STORAGE_FOUND;
429 		entry->type = &OIDX_hrStorageVirtualMemory_c;
430 		entry->allocationUnits = getpagesize();
431 		entry->size = swap_devs[len].ksw_total;
432 		entry->used = swap_devs[len].ksw_used;
433 		entry->allocationFailures = 0;
434 	}
435 }
436 
437 /**
438  * Query the underlaying OS for the mounted file systems
439  * anf fill in the respective lists (for hrStorageTable and for hrFSTable)
440  */
441 static void
442 storage_OS_get_fs(void)
443 {
444 	struct storage_entry *entry;
445 	uint64_t size, used;
446 	int i, mounted_fs_count, units;
447 	char fs_string[SE_DESC_MLEN];
448 
449 	if ((mounted_fs_count = getfsstat(NULL, 0, MNT_NOWAIT)) < 0) {
450 		syslog(LOG_ERR, "hrStorageTable: getfsstat() failed: %m");
451 		return; /* out of luck this time */
452 	}
453 
454 	if (mounted_fs_count != (int)fs_buf_count || fs_buf == NULL) {
455 		fs_buf_count = mounted_fs_count;
456 		fs_buf = reallocf(fs_buf, fs_buf_count * sizeof(struct statfs));
457 		if (fs_buf == NULL) {
458 			fs_buf_count = 0;
459 			assert(0);
460 			return;
461 		}
462 	}
463 
464 	if ((mounted_fs_count = getfsstat(fs_buf,
465 	    fs_buf_count * sizeof(struct statfs), MNT_NOWAIT)) < 0) {
466 		syslog(LOG_ERR, "hrStorageTable: getfsstat() failed: %m");
467 		return; /* out of luck this time */
468 	}
469 
470 	HRDBG("got %d mounted FS", mounted_fs_count);
471 
472 	fs_tbl_pre_refresh();
473 
474 	for (i = 0; i < mounted_fs_count; i++) {
475 		snprintf(fs_string, sizeof(fs_string),
476 		    "%s, type: %s, dev: %s", fs_buf[i].f_mntonname,
477 		    fs_buf[i].f_fstypename, fs_buf[i].f_mntfromname);
478 
479 		entry = storage_find_by_name(fs_string);
480 		if (entry == NULL)
481 			entry = storage_entry_create(fs_string);
482 
483 		assert (entry != NULL);
484 		if (entry == NULL)
485 			return; /* Out of luck */
486 
487 		entry->flags |= HR_STORAGE_FOUND;
488 		entry->type = fs_get_type(&fs_buf[i]); /*XXX - This is wrong*/
489 
490 		units = fs_buf[i].f_bsize;
491 		size = fs_buf[i].f_blocks;
492 		used = fs_buf[i].f_blocks - fs_buf[i].f_bfree;
493 		while (size > INT_MAX) {
494 			units <<= 1;
495 			size >>= 1;
496 			used >>= 1;
497 		}
498 		entry->allocationUnits = units;
499 		entry->size = size;
500 		entry->used = used;
501 
502 		entry->allocationFailures = 0;
503 
504 		/* take care of hrFSTable */
505 		fs_tbl_process_statfs_entry(&fs_buf[i], entry->index);
506 	}
507 
508 	fs_tbl_post_refresh();
509 }
510 
511 /**
512  * Initialize storage table and populate it.
513  */
514 void
515 init_storage_tbl(void)
516 {
517 	if ((mt_list = memstat_mtl_alloc()) == NULL)
518 		syslog(LOG_ERR,
519 		    "hrStorageTable: memstat_mtl_alloc() failed: %m");
520 
521 	refresh_storage_tbl(1);
522 }
523 
524 void
525 fini_storage_tbl(void)
526 {
527 	struct storage_map_entry *n1;
528 
529 	if (swap_devs != NULL) {
530 		free(swap_devs);
531 		swap_devs = NULL;
532 	}
533 	swap_devs_len = 0;
534 
535 	if (fs_buf != NULL) {
536 		free(fs_buf);
537 		fs_buf = NULL;
538 	}
539 	fs_buf_count = 0;
540 
541 	while ((n1 = STAILQ_FIRST(&storage_map)) != NULL) {
542 		STAILQ_REMOVE_HEAD(&storage_map, link);
543 		if (n1->entry != NULL) {
544 			TAILQ_REMOVE(&storage_tbl, n1->entry, link);
545 			free(n1->entry->descr);
546 			free(n1->entry);
547 		}
548 		free(n1->a_name);
549 		free(n1);
550 	}
551 	assert(TAILQ_EMPTY(&storage_tbl));
552 }
553 
554 void
555 refresh_storage_tbl(int force)
556 {
557 	struct storage_entry *entry, *entry_tmp;
558 
559 	if (!force && storage_tick != 0 &&
560 	    this_tick - storage_tick < storage_tbl_refresh) {
561 		HRDBG("no refresh needed");
562 		return;
563 	}
564 
565 	/* mark each entry as missing */
566 	TAILQ_FOREACH(entry, &storage_tbl, link)
567 		entry->flags &= ~HR_STORAGE_FOUND;
568 
569 	storage_OS_get_vm();
570 	storage_OS_get_swap();
571 	storage_OS_get_fs();
572 	storage_OS_get_memstat();
573 
574 	/*
575 	 * Purge items that disappeared
576 	 */
577 	TAILQ_FOREACH_SAFE(entry, &storage_tbl, link, entry_tmp)
578 		if (!(entry->flags & HR_STORAGE_FOUND))
579 			storage_entry_delete(entry);
580 
581 	storage_tick = this_tick;
582 
583 	HRDBG("refresh DONE");
584 }
585 
586 /*
587  * This is the implementation for a generated (by our SNMP tool)
588  * function prototype, see hostres_tree.h
589  * It handles the SNMP operations for hrStorageTable
590  */
591 int
592 op_hrStorageTable(struct snmp_context *ctx __unused, struct snmp_value *value,
593     u_int sub, u_int iidx __unused, enum snmp_op curr_op)
594 {
595 	struct storage_entry *entry;
596 
597 	refresh_storage_tbl(0);
598 
599 	switch (curr_op) {
600 
601 	case SNMP_OP_GETNEXT:
602 		if ((entry = NEXT_OBJECT_INT(&storage_tbl,
603 		    &value->var, sub)) == NULL)
604 			return (SNMP_ERR_NOSUCHNAME);
605 
606 		value->var.len = sub + 1;
607 		value->var.subs[sub] = entry->index;
608 		goto get;
609 
610 	case SNMP_OP_GET:
611 		if ((entry = FIND_OBJECT_INT(&storage_tbl,
612 		    &value->var, sub)) == NULL)
613 			return (SNMP_ERR_NOSUCHNAME);
614 		goto get;
615 
616 	case SNMP_OP_SET:
617 		if ((entry = FIND_OBJECT_INT(&storage_tbl,
618 		    &value->var, sub)) == NULL)
619 			return (SNMP_ERR_NO_CREATION);
620 		return (SNMP_ERR_NOT_WRITEABLE);
621 
622 	case SNMP_OP_ROLLBACK:
623 	case SNMP_OP_COMMIT:
624 		abort();
625 	}
626 	abort();
627 
628   get:
629 	switch (value->var.subs[sub - 1]) {
630 
631 	case LEAF_hrStorageIndex:
632 		value->v.integer = entry->index;
633 		return (SNMP_ERR_NOERROR);
634 
635 	case LEAF_hrStorageType:
636 		assert(entry->type != NULL);
637 		value->v.oid = *entry->type;
638 		return (SNMP_ERR_NOERROR);
639 
640 	case LEAF_hrStorageDescr:
641 		assert(entry->descr != NULL);
642 		return (string_get(value, entry->descr, -1));
643 		break;
644 
645 	case LEAF_hrStorageAllocationUnits:
646 		value->v.integer = entry->allocationUnits;
647 		return (SNMP_ERR_NOERROR);
648 
649 	case LEAF_hrStorageSize:
650 		value->v.integer = entry->size;
651 		return (SNMP_ERR_NOERROR);
652 
653 	case LEAF_hrStorageUsed:
654 		value->v.integer = entry->used;
655 		return (SNMP_ERR_NOERROR);
656 
657 	case LEAF_hrStorageAllocationFailures:
658 		value->v.uint32 = entry->allocationFailures;
659 		return (SNMP_ERR_NOERROR);
660 	}
661 	abort();
662 }
663