1154133Sharti/*-
2154133Sharti * Copyright (c) 2005-2006 The FreeBSD Project
3154133Sharti * All rights reserved.
4154133Sharti *
5154133Sharti * Author: Victor Cruceru <soc-victor@freebsd.org>
6154133Sharti *
7154133Sharti * Redistribution of this software and documentation and use in source and
8154133Sharti * binary forms, with or without modification, are permitted provided that
9154133Sharti * the following conditions are met:
10154133Sharti *
11154133Sharti * 1. Redistributions of source code or documentation must retain the above
12154133Sharti *    copyright notice, this list of conditions and the following disclaimer.
13154133Sharti * 2. Redistributions in binary form must reproduce the above copyright
14154133Sharti *    notice, this list of conditions and the following disclaimer in the
15154133Sharti *    documentation and/or other materials provided with the distribution.
16154133Sharti *
17154133Sharti * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18154133Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19154133Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20154133Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21154133Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22154133Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23154133Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24154133Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25154133Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26154133Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27154133Sharti * SUCH DAMAGE.
28154133Sharti *
29154133Sharti * This C file contains code developed by Poul-Henning Kamp under the
30154133Sharti * following license:
31154133Sharti *
32154133Sharti * FreeBSD: src/sbin/mdconfig/mdconfig.c,v 1.33.2.1 2004/09/14 03:32:21 jmg Exp
33154133Sharti * ----------------------------------------------------------------------------
34154133Sharti * "THE BEER-WARE LICENSE" (Revision 42):
35154133Sharti * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
36154133Sharti * can do whatever you want with this stuff. If we meet some day, and you think
37154133Sharti * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
38154133Sharti * ----------------------------------------------------------------------------
39154133Sharti *
40154133Sharti * $FreeBSD: stable/11/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c 310899 2016-12-31 10:28:59Z ngie $
41154133Sharti */
42154133Sharti
43154133Sharti/*
44154133Sharti * Host Resources MIB implementation for bsnmpd.
45154133Sharti */
46154133Sharti
47154133Sharti#include <paths.h>
48154133Sharti#include <stdlib.h>
49154133Sharti#include <string.h>
50154133Sharti#include <syslog.h>
51154133Sharti#include <unistd.h>
52154133Sharti
53154133Sharti#include "hostres_snmp.h"
54154133Sharti#include "hostres_oid.h"
55154133Sharti#include "hostres_tree.h"
56154133Sharti
57154133Sharti/* Internal id got after we'll register this module with the agent */
58154133Shartistatic u_int host_registration_id = 0;
59154133Sharti
60154133Sharti/* This our hostres module */
61154133Shartistatic struct lmodule *hostres_module;
62154133Sharti
63154133Sharti/* See the generated file hostres_oid.h */
64154133Shartistatic const struct asn_oid oid_host = OIDX_host;
65154133Sharti
66154133Sharti/* descriptor to access kernel memory */
67154133Shartikvm_t *hr_kd;
68154133Sharti
69154133Sharti/*
70154133Sharti * HOST RESOURCES mib module finalization hook.
71154133Sharti * Returns 0 on success, < 0 on error
72154133Sharti */
73154133Shartistatic int
74154133Shartihostres_fini(void)
75154133Sharti{
76154133Sharti
77154133Sharti	if (hr_kd != NULL)
78154133Sharti		(void)kvm_close(hr_kd);
79154133Sharti
80154133Sharti	fini_storage_tbl();
81154133Sharti	fini_fs_tbl();
82154133Sharti	fini_processor_tbl();
83154133Sharti	fini_disk_storage_tbl();
84154133Sharti	fini_device_tbl();
85154133Sharti	fini_partition_tbl();
86154133Sharti	fini_network_tbl();
87154133Sharti	fini_printer_tbl();
88154133Sharti
89154133Sharti	fini_swrun_tbl();
90154133Sharti	fini_swins_tbl();
91154133Sharti
92154133Sharti	fini_scalars();
93154133Sharti
94154133Sharti	if (host_registration_id > 0)
95154133Sharti		or_unregister(host_registration_id);
96154133Sharti
97154133Sharti	HRDBG("done.");
98154133Sharti	return (0);
99154133Sharti}
100154133Sharti
101154133Sharti/*
102154133Sharti * HOST RESOURCES mib module initialization hook.
103154133Sharti * Returns 0 on success, < 0 on error
104154133Sharti */
105154133Shartistatic int
106154133Shartihostres_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
107154133Sharti{
108154133Sharti
109154133Sharti	hostres_module = mod;
110154133Sharti
111154133Sharti	/*
112154133Sharti	 * NOTE: order of these calls is important here!
113154133Sharti	 */
114154133Sharti	if ((hr_kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY,
115154133Sharti	    "kvm_open")) == NULL) {
116154133Sharti		syslog(LOG_ERR, "kvm_open failed: %m ");
117154133Sharti		return (-1);
118154133Sharti	}
119154133Sharti
120154133Sharti	/*
121154133Sharti	 * The order is relevant here, because some table depend on each other.
122154133Sharti	 */
123154133Sharti	init_device_tbl();
124154133Sharti
125154133Sharti	/* populates partition table too */
126154133Sharti	if (init_disk_storage_tbl()) {
127154133Sharti		hostres_fini();
128154133Sharti		return (-1);
129154133Sharti	}
130154133Sharti	init_processor_tbl();
131154133Sharti	init_printer_tbl();
132154133Sharti
133154133Sharti	/*
134154133Sharti	 * populate storage and FS tables. Must be done after device
135154133Sharti	 * initialisation because the FS refresh code calls into the
136154133Sharti	 * partition refresh code.
137154133Sharti	 */
138154133Sharti	init_storage_tbl();
139154133Sharti
140154133Sharti
141154133Sharti	/* also the hrSWRunPerfTable's support is initialized here */
142154133Sharti	init_swrun_tbl();
143154133Sharti	init_swins_tbl();
144154133Sharti
145154133Sharti	HRDBG("done.");
146154133Sharti
147154133Sharti	return (0);
148154133Sharti}
149154133Sharti
150154133Sharti/*
151154133Sharti * HOST RESOURCES mib module start operation
152154133Sharti * returns nothing
153154133Sharti */
154154133Shartistatic void
155154133Shartihostres_start(void)
156154133Sharti{
157154133Sharti
158154133Sharti	host_registration_id = or_register(&oid_host,
159154133Sharti	    "The MIB module for Host Resource MIB (RFC 2790).",
160154133Sharti	    hostres_module);
161154133Sharti
162154133Sharti	start_device_tbl(hostres_module);
163154133Sharti	start_processor_tbl(hostres_module);
164154133Sharti	start_network_tbl();
165154133Sharti
166310899Sngie	HRDBG("done.");
167154133Sharti}
168154133Sharti
169154133Sharti/* this identifies the HOST RESOURCES mib module */
170154133Sharticonst struct snmp_module config = {
171154133Sharti	"This module implements the host resource mib (rfc 2790)",
172154133Sharti	hostres_init,
173154133Sharti	hostres_fini,
174154133Sharti	NULL,			/* idle function, do not use it */
175154133Sharti	NULL,
176154133Sharti	NULL,
177154133Sharti	hostres_start,
178310899Sngie	NULL,		   /* proxy a PDU */
179310899Sngie	hostres_ctree,	  /* see the generated hostres_tree.h */
180154133Sharti	hostres_CTREE_SIZE,     /* see the generated hostres_tree.h */
181154133Sharti	NULL
182154133Sharti};
183154133Sharti
184154133Sharti/**
185154133Sharti * Make an SNMP DateAndTime from a struct tm. This should be in the library.
186154133Sharti */
187154133Shartiint
188154133Shartimake_date_time(u_char *str, const struct tm *tm, u_int decisecs)
189154133Sharti{
190154133Sharti
191154133Sharti	str[0] = (u_char)((tm->tm_year + 1900) >> 8);
192154133Sharti	str[1] = (u_char)(tm->tm_year + 1900);
193154133Sharti	str[2] = tm->tm_mon + 1;
194154133Sharti	str[3] = tm->tm_mday;
195154133Sharti	str[4] = tm->tm_hour;
196154133Sharti	str[5] = tm->tm_min;
197154133Sharti	str[6] = tm->tm_sec;
198154133Sharti	str[7] = decisecs;
199154133Sharti	if (tm->tm_gmtoff < 0)
200154133Sharti		str[8] = '-';
201154133Sharti	else
202154133Sharti		str[8] = '+';
203154133Sharti
204274900Sdim	str[9] = (u_char)(labs(tm->tm_gmtoff) / 3600);
205274900Sdim	str[10] = (u_char)((labs(tm->tm_gmtoff) % 3600) / 60);
206154133Sharti
207154133Sharti	return (11);
208154133Sharti}
209