2 * Copyright (c) 2005-2006 The FreeBSD Project
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
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:
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.
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
33 * Host Resources MIB scalars implementation for SNMPd.
36 #include <sys/types.h>
37 #include <sys/sysctl.h>
46 #include "hostres_snmp.h"
47 #include "hostres_oid.h"
48 #include "hostres_tree.h"
50 /* boot timestamp in centi-seconds */
51 static uint64_t kernel_boot;
53 /* physical memory size in Kb */
54 static uint64_t phys_mem_size;
56 /* boot line (malloced) */
57 static u_char *boot_line;
59 /* maximum number of processes */
60 static uint32_t max_proc;
63 * Free all static data
73 * Get system uptime in hundredths of seconds since the epoch
74 * Returns 0 in case of an error
77 OS_getSystemUptime(uint32_t *ut)
79 struct timeval right_now;
82 if (kernel_boot == 0) {
83 /* first time, do the sysctl */
84 struct timeval kernel_boot_timestamp;
85 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
86 size_t len = sizeof(kernel_boot_timestamp);
88 if (sysctl(mib, 2, &kernel_boot_timestamp,
89 &len, NULL, 0) == -1) {
90 syslog(LOG_ERR, "sysctl KERN_BOOTTIME failed: %m");
91 return (SNMP_ERR_GENERR);
94 HRDBG("boot timestamp from kernel: {%lld, %ld}",
95 (long long)kernel_boot_timestamp.tv_sec,
96 (long)kernel_boot_timestamp.tv_usec);
98 kernel_boot = ((uint64_t)kernel_boot_timestamp.tv_sec * 100) +
99 (kernel_boot_timestamp.tv_usec / 10000);
102 if (gettimeofday(&right_now, NULL) < 0) {
103 syslog(LOG_ERR, "gettimeofday failed: %m");
104 return (SNMP_ERR_GENERR);
106 now = ((uint64_t)right_now.tv_sec * 100) + (right_now.tv_usec / 10000);
108 if (now - kernel_boot > UINT32_MAX)
111 *ut = now - kernel_boot;
113 return (SNMP_ERR_NOERROR);
117 * Get system local date and time in a foramt suitable for DateAndTime TC:
118 * field octets contents range
119 * ----- ------ -------- -----
120 * 1 1-2 year* 0..65536
126 * (use 60 for leap-second)
127 * 7 8 deci-seconds 0..9
128 * 8 9 direction from UTC '+' / '-'
129 * 9 10 hours from UTC* 0..13
130 * 10 11 minutes from UTC 0..59
133 * - the value of year is in network-byte order
134 * - daylight saving time in New Zealand is +13
136 * For example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be
139 * 1992-5-26,13:30:15.0,-4:0
141 * Returns -1 in case of an error or the length of the string (8 or 11)
142 * Actually returns always 11 on freebsd
145 OS_getSystemDate(struct snmp_value *value)
147 u_char s_date_time[11];
150 struct timeval right_now;
153 if (gettimeofday(&right_now, NULL) < 0) {
154 syslog(LOG_ERR, "gettimeofday failed: %m");
155 return (SNMP_ERR_GENERR);
158 tloc_time_t = right_now.tv_sec;
160 if (localtime_r(&tloc_time_t, &tloc_tm) == NULL) {
161 syslog(LOG_ERR, "localtime_r() failed: %m ");
162 return (SNMP_ERR_GENERR);
165 string_len = make_date_time(s_date_time, &tloc_tm,
166 right_now.tv_usec / 100000);
168 return (string_get(value, s_date_time, string_len));
172 * Get kernel boot path. For FreeBSD it seems that no arguments are
173 * present. Returns NULL if an error occured. The returned data is a
174 * pointer to a global strorage.
177 OS_getSystemInitialLoadParameters(u_char **params)
180 if (boot_line == NULL) {
181 int mib[2] = { CTL_KERN, KERN_BOOTFILE };
185 /* get the needed buffer len */
186 if (sysctl(mib, 2, NULL, &buf_len, NULL, 0) != 0) {
188 "sysctl({CTL_KERN,KERN_BOOTFILE}) failed: %m");
189 return (SNMP_ERR_GENERR);
192 if ((buf = malloc(buf_len)) == NULL) {
193 syslog(LOG_ERR, "malloc failed");
194 return (SNMP_ERR_GENERR);
196 if (sysctl(mib, 2, buf, &buf_len, NULL, 0)) {
198 "sysctl({CTL_KERN,KERN_BOOTFILE}) failed: %m");
200 return (SNMP_ERR_GENERR);
204 HRDBG("kernel boot file: %s", boot_line);
208 return (SNMP_ERR_NOERROR);
212 * Get number of current users which are logged in
215 OS_getSystemNumUsers(uint32_t *nu)
221 while ((utmp = getutxent()) != NULL) {
222 if (utmp->ut_type == USER_PROCESS)
227 return (SNMP_ERR_NOERROR);
231 * Get number of current processes existing into the system
234 OS_getSystemProcesses(uint32_t *proc_count)
239 return (SNMP_ERR_GENERR);
241 if (kvm_getprocs(hr_kd, KERN_PROC_PROC, 0, &pc) == NULL) {
242 syslog(LOG_ERR, "kvm_getprocs failed: %m");
243 return (SNMP_ERR_GENERR);
247 return (SNMP_ERR_NOERROR);
251 * Get maximum number of processes allowed on this system
254 OS_getSystemMaxProcesses(uint32_t *mproc)
258 int mib[2] = { CTL_KERN, KERN_MAXPROC };
260 size_t len = sizeof(mp);
262 if (sysctl(mib, 2, &mp, &len, NULL, 0) == -1) {
263 syslog(LOG_ERR, "sysctl KERN_MAXPROC failed: %m");
264 return (SNMP_ERR_GENERR);
270 return (SNMP_ERR_NOERROR);
274 * Get the physical memeory size in Kbytes.
275 * Returns SNMP error code.
278 OS_getMemorySize(uint32_t *ms)
281 if (phys_mem_size == 0) {
282 int mib[2] = { CTL_HW, HW_PHYSMEM };
284 size_t len = sizeof(physmem);
286 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == -1) {
288 "sysctl({ CTL_HW, HW_PHYSMEM }) failed: %m");
289 return (SNMP_ERR_GENERR);
292 phys_mem_size = physmem / 1024;
295 if (phys_mem_size > UINT32_MAX)
299 return (SNMP_ERR_NOERROR);
303 * Try to use the s_date_time parameter as a DateAndTime TC to fill in
304 * the second parameter.
305 * Returns 0 on succes and -1 for an error.
306 * Bug: time zone info is not used
308 static struct timeval *
309 OS_checkSystemDateInput(const u_char *str, u_int len)
315 if (len != 8 && len != 11)
318 if (str[2] == 0 || str[2] > 12 ||
319 str[3] == 0 || str[3] > 31 ||
320 str[4] > 23 || str[5] > 59 || str[6] > 60 || str[7] > 9)
323 tm_to_set.tm_year = ((str[0] << 8) + str[1]) - 1900;
324 tm_to_set.tm_mon = str[2] - 1;
325 tm_to_set.tm_mday = str[3];
326 tm_to_set.tm_hour = str[4];
327 tm_to_set.tm_min = str[5];
328 tm_to_set.tm_sec = str[6];
329 tm_to_set.tm_isdst = 0;
331 /* now make UTC from it */
332 if ((t = timegm(&tm_to_set)) == (time_t)-1)
335 /* now apply timezone if specified */
337 if (str[9] > 13 || str[10] > 59)
340 t += 3600 * str[9] + 60 * str[10];
342 t -= 3600 * str[9] + 60 * str[10];
345 if ((tv = malloc(sizeof(*tv))) == NULL)
349 tv->tv_usec = (int32_t)str[7] * 100000;
355 * Set system date and time. Timezone is not changed
358 OS_setSystemDate(const struct timeval *timeval_to_set)
360 if (settimeofday(timeval_to_set, NULL) == -1) {
361 syslog(LOG_ERR, "settimeofday failed: %m");
362 return (SNMP_ERR_GENERR);
364 return (SNMP_ERR_NOERROR);
368 * prototype of this function was genrated by gensnmptree tool in header file
370 * Returns SNMP_ERR_NOERROR on success
373 op_hrSystem(struct snmp_context *ctx, struct snmp_value *value,
374 u_int sub, u_int iidx __unused, enum snmp_op curr_op)
382 switch (value->var.subs[sub - 1]) {
384 case LEAF_hrSystemUptime:
385 return (OS_getSystemUptime(&value->v.uint32));
387 case LEAF_hrSystemDate:
388 return (OS_getSystemDate(value));
390 case LEAF_hrSystemInitialLoadDevice:
391 value->v.uint32 = 0; /* FIXME */
392 return (SNMP_ERR_NOERROR);
394 case LEAF_hrSystemInitialLoadParameters:
395 if ((err = OS_getSystemInitialLoadParameters(&str)) !=
398 return (string_get(value, str, -1));
400 case LEAF_hrSystemNumUsers:
401 return (OS_getSystemNumUsers(&value->v.uint32));
403 case LEAF_hrSystemProcesses:
404 return (OS_getSystemProcesses(&value->v.uint32));
406 case LEAF_hrSystemMaxProcesses:
407 return (OS_getSystemMaxProcesses(&value->v.uint32));
412 switch (value->var.subs[sub - 1]) {
414 case LEAF_hrSystemDate:
415 if ((ctx->scratch->ptr1 =
416 OS_checkSystemDateInput(value->v.octetstring.octets,
417 value->v.octetstring.len)) == NULL)
418 return (SNMP_ERR_WRONG_VALUE);
420 return (SNMP_ERR_NOERROR);
422 case LEAF_hrSystemInitialLoadDevice:
423 case LEAF_hrSystemInitialLoadParameters:
424 return (SNMP_ERR_NOT_WRITEABLE);
429 case SNMP_OP_ROLLBACK:
430 switch (value->var.subs[sub - 1]) {
432 case LEAF_hrSystemDate:
433 free(ctx->scratch->ptr1);
434 return (SNMP_ERR_NOERROR);
436 case LEAF_hrSystemInitialLoadDevice:
437 case LEAF_hrSystemInitialLoadParameters:
443 switch (value->var.subs[sub - 1]) {
445 case LEAF_hrSystemDate:
446 (void)OS_setSystemDate(ctx->scratch->ptr1);
447 free(ctx->scratch->ptr1);
448 return (SNMP_ERR_NOERROR);
450 case LEAF_hrSystemInitialLoadDevice:
451 case LEAF_hrSystemInitialLoadParameters:
456 case SNMP_OP_GETNEXT:
463 * prototype of this function was genrated by gensnmptree tool
464 * in the header file hostres_tree.h
465 * Returns SNMP_ERR_NOERROR on success
468 op_hrStorage(struct snmp_context *ctx __unused, struct snmp_value *value,
469 u_int sub, u_int iidx __unused, enum snmp_op curr_op)
472 /* only GET is possible */
476 switch (value->var.subs[sub - 1]) {
478 case LEAF_hrMemorySize:
479 return (OS_getMemorySize(&value->v.uint32));
484 return (SNMP_ERR_NOT_WRITEABLE);
486 case SNMP_OP_ROLLBACK:
488 case SNMP_OP_GETNEXT: