Path: blob/main/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c
107769 views
/*-1* SPDX-License-Identifier: (BSD-2-Clause AND Beerware)2*3* Copyright (c) 2005-2006 The FreeBSD Project4* All rights reserved.5*6* Author: Victor Cruceru <[email protected]>7*8* Redistribution of this software and documentation and use in source and9* binary forms, with or without modification, are permitted provided that10* the following conditions are met:11*12* 1. Redistributions of source code or documentation must retain the above13* copyright notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*30* This C file contains code developed by Poul-Henning Kamp under the31* following license:32*33* FreeBSD: src/sbin/mdconfig/mdconfig.c,v 1.33.2.1 2004/09/14 03:32:21 jmg Exp34* ----------------------------------------------------------------------------35* "THE BEER-WARE LICENSE" (Revision 42):36* <[email protected]> wrote this file. As long as you retain this notice you37* can do whatever you want with this stuff. If we meet some day, and you think38* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp39* ----------------------------------------------------------------------------40*/4142/*43* Host Resources MIB implementation for bsnmpd.44*/4546#include <paths.h>47#include <stdlib.h>48#include <string.h>49#include <syslog.h>50#include <unistd.h>5152#include "hostres_snmp.h"53#include "hostres_oid.h"54#include "hostres_tree.h"5556/* Internal id got after we'll register this module with the agent */57static u_int host_registration_id = 0;5859/* This our hostres module */60static struct lmodule *hostres_module;6162/* See the generated file hostres_oid.h */63static const struct asn_oid oid_host = OIDX_host;6465/* descriptor to access kernel memory */66kvm_t *hr_kd;6768/*69* HOST RESOURCES mib module finalization hook.70* Returns 0 on success, < 0 on error71*/72static int73hostres_fini(void)74{7576if (hr_kd != NULL)77(void)kvm_close(hr_kd);7879fini_storage_tbl();80fini_fs_tbl();81fini_processor_tbl();82fini_disk_storage_tbl();83fini_device_tbl();84fini_partition_tbl();85fini_network_tbl();86fini_printer_tbl();8788fini_swrun_tbl();89fini_swins_tbl();9091fini_scalars();9293if (host_registration_id > 0)94or_unregister(host_registration_id);9596HRDBG("done.");97return (0);98}99100/*101* HOST RESOURCES mib module initialization hook.102* Returns 0 on success, < 0 on error103*/104static int105hostres_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)106{107108hostres_module = mod;109110/*111* NOTE: order of these calls is important here!112*/113if ((hr_kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY,114"kvm_open")) == NULL) {115syslog(LOG_ERR, "kvm_open failed: %m ");116return (-1);117}118119/*120* The order is relevant here, because some table depend on each other.121*/122init_device_tbl();123124/* populates partition table too */125if (init_disk_storage_tbl()) {126hostres_fini();127return (-1);128}129init_processor_tbl();130init_printer_tbl();131132/*133* populate storage and FS tables. Must be done after device134* initialisation because the FS refresh code calls into the135* partition refresh code.136*/137init_storage_tbl();138139140/* also the hrSWRunPerfTable's support is initialized here */141init_swrun_tbl();142init_swins_tbl();143144HRDBG("done.");145146return (0);147}148149/*150* HOST RESOURCES mib module start operation151* returns nothing152*/153static void154hostres_start(void)155{156157host_registration_id = or_register(&oid_host,158"The MIB module for Host Resource MIB (RFC 2790).",159hostres_module);160161start_device_tbl(hostres_module);162start_processor_tbl(hostres_module);163start_network_tbl();164165HRDBG("done.");166}167168/* this identifies the HOST RESOURCES mib module */169const struct snmp_module config = {170"This module implements the host resource mib (rfc 2790)",171hostres_init,172hostres_fini,173NULL, /* idle function, do not use it */174NULL,175NULL,176hostres_start,177NULL, /* proxy a PDU */178hostres_ctree, /* see the generated hostres_tree.h */179hostres_CTREE_SIZE, /* see the generated hostres_tree.h */180NULL181};182183/**184* Make an SNMP DateAndTime from a struct tm. This should be in the library.185*/186int187make_date_time(u_char *str, const struct tm *tm, u_int decisecs)188{189190str[0] = (u_char)((tm->tm_year + 1900) >> 8);191str[1] = (u_char)(tm->tm_year + 1900);192str[2] = tm->tm_mon + 1;193str[3] = tm->tm_mday;194str[4] = tm->tm_hour;195str[5] = tm->tm_min;196str[6] = tm->tm_sec;197str[7] = decisecs;198if (tm->tm_gmtoff < 0)199str[8] = '-';200else201str[8] = '+';202203str[9] = (u_char)(labs(tm->tm_gmtoff) / 3600);204str[10] = (u_char)((labs(tm->tm_gmtoff) % 3600) / 60);205206return (11);207}208209210