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 * This C file contains code developed by Poul-Henning Kamp under the 30 * following license: 31 * 32 * FreeBSD: src/sbin/mdconfig/mdconfig.c,v 1.33.2.1 2004/09/14 03:32:21 jmg Exp 33 * ---------------------------------------------------------------------------- 34 * "THE BEER-WARE LICENSE" (Revision 42): 35 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 36 * can do whatever you want with this stuff. If we meet some day, and you think 37 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 38 * ---------------------------------------------------------------------------- 39 * 40 * $FreeBSD$ 41 */ 42 43 /* 44 * Host Resources MIB implementation for bsnmpd. 45 */ 46 47 #include <paths.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <syslog.h> 51 #include <unistd.h> 52 #include <utmp.h> 53 54 #include "hostres_snmp.h" 55 #include "hostres_oid.h" 56 #include "hostres_tree.h" 57 58 /* Internal id got after we'll register this module with the agent */ 59 static u_int host_registration_id = 0; 60 61 /* This our hostres module */ 62 static struct lmodule *hostres_module; 63 64 /* See the generated file hostres_oid.h */ 65 static const struct asn_oid oid_host = OIDX_host; 66 67 /* descriptor to access kernel memory */ 68 kvm_t *hr_kd; 69 70 /* 71 * HOST RESOURCES mib module finalization hook. 72 * Returns 0 on success, < 0 on error 73 */ 74 static int 75 hostres_fini(void) 76 { 77 78 if (hr_kd != NULL) 79 (void)kvm_close(hr_kd); 80 81 fini_storage_tbl(); 82 fini_fs_tbl(); 83 fini_processor_tbl(); 84 fini_disk_storage_tbl(); 85 fini_device_tbl(); 86 fini_partition_tbl(); 87 fini_network_tbl(); 88 fini_printer_tbl(); 89 90 fini_swrun_tbl(); 91 fini_swins_tbl(); 92 93 fini_scalars(); 94 95 if (host_registration_id > 0) 96 or_unregister(host_registration_id); 97 98 HRDBG("done."); 99 return (0); 100 } 101 102 /* 103 * HOST RESOURCES mib module initialization hook. 104 * Returns 0 on success, < 0 on error 105 */ 106 static int 107 hostres_init(struct lmodule *mod, int argc __unused, char *argv[] __unused) 108 { 109 110 hostres_module = mod; 111 112 /* 113 * NOTE: order of these calls is important here! 114 */ 115 if ((hr_kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, 116 "kvm_open")) == NULL) { 117 syslog(LOG_ERR, "kvm_open failed: %m "); 118 return (-1); 119 } 120 121 /* 122 * The order is relevant here, because some table depend on each other. 123 */ 124 init_device_tbl(); 125 126 /* populates partition table too */ 127 if (init_disk_storage_tbl()) { 128 hostres_fini(); 129 return (-1); 130 } 131 init_processor_tbl(); 132 init_printer_tbl(); 133 134 /* 135 * populate storage and FS tables. Must be done after device 136 * initialisation because the FS refresh code calls into the 137 * partition refresh code. 138 */ 139 init_storage_tbl(); 140 141 142 /* also the hrSWRunPerfTable's support is initialized here */ 143 init_swrun_tbl(); 144 init_swins_tbl(); 145 146 HRDBG("done."); 147 148 return (0); 149 } 150 151 /* 152 * HOST RESOURCES mib module start operation 153 * returns nothing 154 */ 155 static void 156 hostres_start(void) 157 { 158 159 host_registration_id = or_register(&oid_host, 160 "The MIB module for Host Resource MIB (RFC 2790).", 161 hostres_module); 162 163 start_device_tbl(hostres_module); 164 start_processor_tbl(hostres_module); 165 start_network_tbl(); 166 167 HRDBG("done."); 168 } 169 170 /* this identifies the HOST RESOURCES mib module */ 171 const struct snmp_module config = { 172 "This module implements the host resource mib (rfc 2790)", 173 hostres_init, 174 hostres_fini, 175 NULL, /* idle function, do not use it */ 176 NULL, 177 NULL, 178 hostres_start, 179 NULL, /* proxy a PDU */ 180 hostres_ctree, /* see the generated hostres_tree.h */ 181 hostres_CTREE_SIZE, /* see the generated hostres_tree.h */ 182 NULL 183 }; 184 185 /** 186 * Make an SNMP DateAndTime from a struct tm. This should be in the library. 187 */ 188 int 189 make_date_time(u_char *str, const struct tm *tm, u_int decisecs) 190 { 191 192 str[0] = (u_char)((tm->tm_year + 1900) >> 8); 193 str[1] = (u_char)(tm->tm_year + 1900); 194 str[2] = tm->tm_mon + 1; 195 str[3] = tm->tm_mday; 196 str[4] = tm->tm_hour; 197 str[5] = tm->tm_min; 198 str[6] = tm->tm_sec; 199 str[7] = decisecs; 200 if (tm->tm_gmtoff < 0) 201 str[8] = '-'; 202 else 203 str[8] = '+'; 204 205 str[9] = (u_char)(abs(tm->tm_gmtoff) / 3600); 206 str[10] = (u_char)((abs(tm->tm_gmtoff) % 3600) / 60); 207 208 return (11); 209 } 210