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