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 * Host Resources MIB for SNMPd.
30154133Sharti *
31154133Sharti * $FreeBSD$
32154133Sharti */
33154133Sharti
34154133Sharti#ifndef HOSTRES_SNMP_H_1132245017
35160341Sharti#define	HOSTRES_SNMP_H_1132245017
36154133Sharti
37154133Sharti#include <sys/types.h>
38154133Sharti#include <sys/queue.h>
39154133Sharti
40154133Sharti#include <stdio.h>
41154133Sharti#include <fcntl.h>
42154133Sharti#include <kvm.h>
43154133Sharti#include <devinfo.h>
44154133Sharti
45154133Sharti#include <bsnmp/asn1.h>
46154133Sharti#include <bsnmp/snmp.h>
47154133Sharti
48154133Sharti#include <bsnmp/snmpmod.h>
49154133Sharti
50154133Sharti/*
51154133Sharti * Default package directory for hrSWInstalledTable. Can be overridden
52154133Sharti * via SNMP or configuration file.
53154133Sharti */
54160341Sharti#define	PATH_PKGDIR     "/var/db/pkg"
55154133Sharti
56154133Sharti/*
57154133Sharti * These are the default maximum caching intervals for the various tables
58154133Sharti * in seconds. They can be overridden from the configuration file.
59154133Sharti */
60160341Sharti#define	HR_STORAGE_TBL_REFRESH	7
61160341Sharti#define	HR_FS_TBL_REFRESH	7
62160341Sharti#define	HR_DISK_TBL_REFRESH	7
63160341Sharti#define	HR_NETWORK_TBL_REFRESH	7
64160341Sharti#define	HR_SWINS_TBL_REFRESH	120
65160341Sharti#define	HR_SWRUN_TBL_REFRESH	3
66154133Sharti
67154133Shartistruct tm;
68154133Shartistruct statfs;
69154133Sharti
70154133Sharti/* a debug macro */
71154133Sharti#ifndef NDEBUG
72154133Sharti
73154133Sharti#define	HRDBG(...) do {							\
74154133Sharti	fprintf(stderr, "HRDEBUG: %s: ", __func__);			\
75154133Sharti	fprintf(stderr, __VA_ARGS__);					\
76154133Sharti	fprintf(stderr, "\n");						\
77154133Sharti   } while (0)
78154133Sharti
79154133Sharti#else
80154133Sharti
81154133Sharti#define	HRDBG(...) do { } while (0)
82154133Sharti
83154133Sharti#endif /*NDEBUG*/
84154133Sharti
85154133Sharti/* path to devd(8) output pipe */
86154133Sharti#define	PATH_DEVD_PIPE	"/var/run/devd.pipe"
87154133Sharti
88154133Sharti#define	IS_KERNPROC(kp)	(((kp)->ki_flag & P_KTHREAD) == P_KTHREAD)
89154133Sharti
90154133Shartienum snmpTCTruthValue {
91154133Sharti	SNMP_TRUE = 1,
92154133Sharti	SNMP_FALSE= 2
93154133Sharti};
94154133Sharti
95154133Sharti/* The number of CPU load samples per one minute, per each CPU */
96160341Sharti#define	MAX_CPU_SAMPLES 4
97154133Sharti
98160341Sharti
99154133Sharti/*
100160341Sharti * max len (including '\0'), for device_entry::descr field below,
101160341Sharti * according to MIB
102160341Sharti */
103160341Sharti#define	DEV_DESCR_MLEN	(64 + 1)
104160341Sharti
105160341Sharti/*
106160341Sharti * max len (including '\0'), for device_entry::name and
107160341Sharti * device_map_entry::name_key fields below, according to MIB
108160341Sharti */
109160341Sharti#define	DEV_NAME_MLEN	(32 + 1)
110160341Sharti
111160341Sharti/*
112160341Sharti * max len (including '\0'), for device_entry::location and
113160341Sharti * device_map_entry::location_key fields below, according to MIB
114160341Sharti */
115160341Sharti#define	DEV_LOC_MLEN	(128 + 1)
116160341Sharti
117160341Sharti/*
118154133Sharti * This structure is used to hold a SNMP table entry
119154133Sharti * for HOST-RESOURCES-MIB's hrDeviceTable
120154133Sharti */
121154133Shartistruct device_entry {
122154133Sharti	int32_t		index;
123160341Sharti	const struct asn_oid *type;
124160341Sharti	u_char		*descr;
125160341Sharti	const struct asn_oid *id;	/* only oid_zeroDotZero as (*id) value*/
126160341Sharti	int32_t		status;		/* enum DeviceStatus */
127154133Sharti	uint32_t	errors;
128154133Sharti
129160341Sharti#define	HR_DEVICE_FOUND		0x001
130154133Sharti	/* not dectected by libdevice, so don't try to refresh it*/
131160341Sharti#define	HR_DEVICE_IMMUTABLE	0x002
132154133Sharti
133154133Sharti	/* next 3 are not from the SNMP mib table, only to be used internally */
134154133Sharti	uint32_t	flags;
135160341Sharti
136160341Sharti	u_char		*name;
137160341Sharti	u_char		*location;
138154133Sharti	TAILQ_ENTRY(device_entry) link;
139154133Sharti};
140154133Sharti
141154133Sharti/*
142154133Sharti * Next structure is used to keep o list of mappings from a specific
143154133Sharti * name (a_name) to an entry in the hrFSTblEntry;
144154133Sharti * We are trying to keep the same index for a specific name at least
145154133Sharti * for the duration of one SNMP agent run.
146154133Sharti */
147154133Shartistruct device_map_entry {
148154133Sharti	int32_t		hrIndex;	/* used for hrDeviceTblEntry::index */
149154133Sharti
150154133Sharti	/* map key is the pair (name_key, location_key) */
151160341Sharti	u_char		*name_key;	/* copy of device name */
152160341Sharti	u_char		*location_key;
153154133Sharti
154154133Sharti	/*
155154133Sharti	 * Next may be NULL if the respective hrDeviceTblEntry
156154133Sharti	 * is (temporally) gone.
157154133Sharti	 */
158154133Sharti	struct device_entry *entry_p;
159154133Sharti	STAILQ_ENTRY(device_map_entry) link;
160154133Sharti};
161154133ShartiSTAILQ_HEAD(device_map, device_map_entry);
162154133Sharti
163154133Sharti/* descriptor to access kernel memory */
164154133Shartiextern kvm_t *hr_kd;
165154133Sharti
166154133Sharti/* Table used for consistent device table indexing. */
167154133Shartiextern struct device_map device_map;
168154133Sharti
169154133Sharti/* Maximum number of ticks between two updates for hrStorageTable */
170154133Shartiextern uint32_t storage_tbl_refresh;
171154133Sharti
172154133Sharti/* Maximum number of ticks between updated of FS table */
173154133Shartiextern uint32_t fs_tbl_refresh;
174154133Sharti
175154133Sharti/* maximum number of ticks between updates of SWRun and SWRunPerf table */
176154133Shartiextern uint32_t swrun_tbl_refresh;
177154133Sharti
178154133Sharti/* Maximum number of ticks between device table refreshs. */
179154133Shartiextern uint32_t device_tbl_refresh;
180154133Sharti
181154133Sharti/* maximum number of ticks between refreshs */
182154133Shartiextern uint32_t disk_storage_tbl_refresh;
183154133Sharti
184154133Sharti/* maximum number of ticks between updates of network table */
185154133Shartiextern uint32_t swins_tbl_refresh;
186154133Sharti
187154133Sharti/* maximum number of ticks between updates of network table */
188154133Shartiextern uint32_t network_tbl_refresh;
189154133Sharti
190154133Sharti/* package directory */
191154133Shartiextern u_char *pkg_dir;
192154133Sharti
193154133Sharti/* Initialize and populate storage table */
194154133Shartivoid init_storage_tbl(void);
195154133Sharti
196154133Sharti/* Finalization routine for hrStorageTable. */
197154133Shartivoid fini_storage_tbl(void);
198154133Sharti
199154133Sharti/* Refresh routine for hrStorageTable. */
200154133Shartivoid refresh_storage_tbl(int);
201154133Sharti
202154133Sharti/*
203154133Sharti * Get the type of filesystem referenced in a struct statfs * -
204154133Sharti * used by FSTbl and StorageTbl functions.
205154133Sharti */
206154133Sharticonst struct asn_oid *fs_get_type(const struct statfs *);
207154133Sharti
208154133Sharti/*
209154133Sharti * Because hrFSTable depends to hrStorageTable we are
210154133Sharti * refreshing hrFSTable by refreshing hrStorageTable.
211154133Sharti * When one entry "of type" fs from hrStorageTable is refreshed
212154133Sharti * then the corresponding entry from hrFSTable is refreshed
213154133Sharti * FS_tbl_pre_refresh_v() is called  before refeshing fs part of hrStorageTable
214154133Sharti */
215154133Shartivoid fs_tbl_pre_refresh(void);
216154133Shartivoid fs_tbl_process_statfs_entry(const struct statfs *, int32_t);
217154133Sharti
218154133Sharti/* Called after refreshing fs part of hrStorageTable */
219154133Shartivoid fs_tbl_post_refresh(void);
220154133Sharti
221154133Sharti/* Refresh the FS table if neccessary. */
222154133Shartivoid refresh_fs_tbl(void);
223154133Sharti
224154133Sharti/* Finalization routine for hrFSTable. */
225154133Shartivoid fini_fs_tbl(void);
226154133Sharti
227154133Sharti/* Init the things for both of hrSWRunTable and hrSWRunPerfTable */
228154133Shartivoid init_swrun_tbl(void);
229154133Sharti
230154133Sharti/* Finalization routine for both of hrSWRunTable and hrSWRunPerfTable */
231154133Shartivoid fini_swrun_tbl(void);
232154133Sharti
233154133Sharti/* Init and populate hrDeviceTable */
234154133Shartivoid init_device_tbl(void);
235154133Sharti
236154133Sharti/* start devd monitoring */
237154133Shartivoid start_device_tbl(struct lmodule *);
238154133Sharti
239154133Sharti/* Finalization routine for hrDeviceTable */
240154133Shartivoid fini_device_tbl(void);
241154133Sharti
242154133Sharti/* Refresh routine for hrDeviceTable. */
243154133Shartivoid refresh_device_tbl(int);
244154133Sharti
245154133Sharti/* Find an item in hrDeviceTbl by its entry->index. */
246154133Shartistruct device_entry *device_find_by_index(int32_t);
247154133Sharti
248154133Sharti/* Find an item in hrDeviceTbl by name. */
249154133Shartistruct device_entry *device_find_by_name(const char *);
250154133Sharti
251154133Sharti/* Create a new entry out of thin air. */
252154133Shartistruct device_entry *device_entry_create(const char *, const char *,
253154133Sharti    const char *);
254154133Sharti
255160341Sharti/* Delete an entry from hrDeviceTbl */
256160341Shartivoid device_entry_delete(struct device_entry *entry);
257160341Sharti
258154133Sharti/* Init the things for hrProcessorTable. */
259154133Shartivoid init_processor_tbl(void);
260154133Sharti
261154133Sharti/* Finalization routine for hrProcessorTable. */
262154133Shartivoid fini_processor_tbl(void);
263154133Sharti
264154133Sharti/* Start the processor table CPU load collector. */
265154133Shartivoid start_processor_tbl(struct lmodule *);
266154133Sharti
267154133Sharti/* Init the things for hrDiskStorageTable */
268154133Shartiint init_disk_storage_tbl(void);
269154133Sharti
270154133Sharti/* Finalization routine for hrDiskStorageTable. */
271154133Shartivoid fini_disk_storage_tbl(void);
272154133Sharti
273154133Sharti/* Refresh routine for hrDiskStorageTable. */
274154133Shartivoid refresh_disk_storage_tbl(int);
275154133Sharti
276154133Sharti/* Finalization routine for hrPartitionTable. */
277154133Shartivoid fini_partition_tbl(void);
278154133Sharti
279154133Sharti/* Finalization routine for hrNetworkTable. */
280154133Shartivoid fini_network_tbl(void);
281154133Sharti
282154133Sharti/* populate network table */
283154133Shartivoid start_network_tbl(void);
284154133Sharti
285154133Sharti/* initialize installed software table */
286154133Shartivoid init_swins_tbl(void);
287154133Sharti
288154133Sharti/* finalize installed software table */
289154133Shartivoid fini_swins_tbl(void);
290154133Sharti
291154133Sharti/* refresh the hrSWInstalledTable if necessary */
292154133Shartivoid refresh_swins_tbl(void);
293154133Sharti
294154133Sharti/* Init the things for hrPrinterTable */
295154133Shartivoid init_printer_tbl(void);
296154133Sharti
297154133Sharti/* Finalization routine for hrPrinterTable. */
298154133Shartivoid fini_printer_tbl(void);
299154133Sharti
300154133Sharti/* Refresh printer table */
301154133Shartivoid refresh_printer_tbl(void);
302154133Sharti
303154133Sharti/* get boot command line */
304154133Shartiint OS_getSystemInitialLoadParameters(u_char **);
305154133Sharti
306154133Sharti/* Start refreshing the partition table */
307154133Shartivoid partition_tbl_post_refresh(void);
308154133Sharti
309154133Sharti/* Handle refresh for the given disk */
310154133Shartivoid partition_tbl_handle_disk(int32_t, const char *);
311154133Sharti
312154133Sharti/* Finish refreshing the partition table. */
313154133Shartivoid partition_tbl_pre_refresh(void);
314154133Sharti
315154133Sharti/* Set the FS index in a partition table entry */
316154133Shartivoid handle_partition_fs_index(const char *, int32_t);
317154133Sharti
318154133Sharti/* Make an SNMP DateAndTime from a struct tm. */
319154133Shartiint make_date_time(u_char *, const struct tm *, u_int);
320154133Sharti
321154133Sharti/* Free all static data */
322154133Shartivoid fini_scalars(void);
323154133Sharti
324154133Sharti#endif /* HOSTRES_SNMP_H_1132245017 */
325