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 * Host Resources MIB for SNMPd.
30 *
31 * $FreeBSD$
32 */
33
34#ifndef HOSTRES_SNMP_H_1132245017
35#define	HOSTRES_SNMP_H_1132245017
36
37#include <sys/types.h>
38#include <sys/queue.h>
39
40#include <stdio.h>
41#include <fcntl.h>
42#include <kvm.h>
43#include <devinfo.h>
44
45#include <bsnmp/asn1.h>
46#include <bsnmp/snmp.h>
47
48#include <bsnmp/snmpmod.h>
49
50/*
51 * Default package directory for hrSWInstalledTable. Can be overridden
52 * via SNMP or configuration file.
53 */
54#define	PATH_PKGDIR     "/var/db/pkg"
55
56/*
57 * These are the default maximum caching intervals for the various tables
58 * in seconds. They can be overridden from the configuration file.
59 */
60#define	HR_STORAGE_TBL_REFRESH	7
61#define	HR_FS_TBL_REFRESH	7
62#define	HR_DISK_TBL_REFRESH	7
63#define	HR_NETWORK_TBL_REFRESH	7
64#define	HR_SWINS_TBL_REFRESH	120
65#define	HR_SWRUN_TBL_REFRESH	3
66
67struct tm;
68struct statfs;
69
70/* a debug macro */
71#ifndef NDEBUG
72
73#define	HRDBG(...) do {							\
74	fprintf(stderr, "HRDEBUG: %s: ", __func__);			\
75	fprintf(stderr, __VA_ARGS__);					\
76	fprintf(stderr, "\n");						\
77   } while (0)
78
79#else
80
81#define	HRDBG(...) do { } while (0)
82
83#endif /*NDEBUG*/
84
85/* path to devd(8) output pipe */
86#define	PATH_DEVD_PIPE	"/var/run/devd.pipe"
87
88#define	IS_KERNPROC(kp)	(((kp)->ki_flag & P_KTHREAD) == P_KTHREAD)
89
90enum snmpTCTruthValue {
91	SNMP_TRUE = 1,
92	SNMP_FALSE= 2
93};
94
95/* The number of CPU load samples per one minute, per each CPU */
96#define	MAX_CPU_SAMPLES 4
97
98
99/*
100 * max len (including '\0'), for device_entry::descr field below,
101 * according to MIB
102 */
103#define	DEV_DESCR_MLEN	(64 + 1)
104
105/*
106 * max len (including '\0'), for device_entry::name and
107 * device_map_entry::name_key fields below, according to MIB
108 */
109#define	DEV_NAME_MLEN	(32 + 1)
110
111/*
112 * max len (including '\0'), for device_entry::location and
113 * device_map_entry::location_key fields below, according to MIB
114 */
115#define	DEV_LOC_MLEN	(128 + 1)
116
117/*
118 * This structure is used to hold a SNMP table entry
119 * for HOST-RESOURCES-MIB's hrDeviceTable
120 */
121struct device_entry {
122	int32_t		index;
123	const struct asn_oid *type;
124	u_char		*descr;
125	const struct asn_oid *id;	/* only oid_zeroDotZero as (*id) value*/
126	int32_t		status;		/* enum DeviceStatus */
127	uint32_t	errors;
128
129#define	HR_DEVICE_FOUND		0x001
130	/* not dectected by libdevice, so don't try to refresh it*/
131#define	HR_DEVICE_IMMUTABLE	0x002
132
133	/* next 3 are not from the SNMP mib table, only to be used internally */
134	uint32_t	flags;
135
136	u_char		*name;
137	u_char		*location;
138	TAILQ_ENTRY(device_entry) link;
139};
140
141/*
142 * Next structure is used to keep o list of mappings from a specific
143 * name (a_name) to an entry in the hrFSTblEntry;
144 * We are trying to keep the same index for a specific name at least
145 * for the duration of one SNMP agent run.
146 */
147struct device_map_entry {
148	int32_t		hrIndex;	/* used for hrDeviceTblEntry::index */
149
150	/* map key is the pair (name_key, location_key) */
151	u_char		*name_key;	/* copy of device name */
152	u_char		*location_key;
153
154	/*
155	 * Next may be NULL if the respective hrDeviceTblEntry
156	 * is (temporally) gone.
157	 */
158	struct device_entry *entry_p;
159	STAILQ_ENTRY(device_map_entry) link;
160};
161STAILQ_HEAD(device_map, device_map_entry);
162
163/* descriptor to access kernel memory */
164extern kvm_t *hr_kd;
165
166/* Table used for consistent device table indexing. */
167extern struct device_map device_map;
168
169/* Maximum number of ticks between two updates for hrStorageTable */
170extern uint32_t storage_tbl_refresh;
171
172/* Maximum number of ticks between updated of FS table */
173extern uint32_t fs_tbl_refresh;
174
175/* maximum number of ticks between updates of SWRun and SWRunPerf table */
176extern uint32_t swrun_tbl_refresh;
177
178/* Maximum number of ticks between device table refreshs. */
179extern uint32_t device_tbl_refresh;
180
181/* maximum number of ticks between refreshs */
182extern uint32_t disk_storage_tbl_refresh;
183
184/* maximum number of ticks between updates of network table */
185extern uint32_t swins_tbl_refresh;
186
187/* maximum number of ticks between updates of network table */
188extern uint32_t network_tbl_refresh;
189
190/* package directory */
191extern u_char *pkg_dir;
192
193/* Initialize and populate storage table */
194void init_storage_tbl(void);
195
196/* Finalization routine for hrStorageTable. */
197void fini_storage_tbl(void);
198
199/* Refresh routine for hrStorageTable. */
200void refresh_storage_tbl(int);
201
202/*
203 * Get the type of filesystem referenced in a struct statfs * -
204 * used by FSTbl and StorageTbl functions.
205 */
206const struct asn_oid *fs_get_type(const struct statfs *);
207
208/*
209 * Because hrFSTable depends to hrStorageTable we are
210 * refreshing hrFSTable by refreshing hrStorageTable.
211 * When one entry "of type" fs from hrStorageTable is refreshed
212 * then the corresponding entry from hrFSTable is refreshed
213 * FS_tbl_pre_refresh_v() is called  before refeshing fs part of hrStorageTable
214 */
215void fs_tbl_pre_refresh(void);
216void fs_tbl_process_statfs_entry(const struct statfs *, int32_t);
217
218/* Called after refreshing fs part of hrStorageTable */
219void fs_tbl_post_refresh(void);
220
221/* Refresh the FS table if neccessary. */
222void refresh_fs_tbl(void);
223
224/* Finalization routine for hrFSTable. */
225void fini_fs_tbl(void);
226
227/* Init the things for both of hrSWRunTable and hrSWRunPerfTable */
228void init_swrun_tbl(void);
229
230/* Finalization routine for both of hrSWRunTable and hrSWRunPerfTable */
231void fini_swrun_tbl(void);
232
233/* Init and populate hrDeviceTable */
234void init_device_tbl(void);
235
236/* start devd monitoring */
237void start_device_tbl(struct lmodule *);
238
239/* Finalization routine for hrDeviceTable */
240void fini_device_tbl(void);
241
242/* Refresh routine for hrDeviceTable. */
243void refresh_device_tbl(int);
244
245/* Find an item in hrDeviceTbl by its entry->index. */
246struct device_entry *device_find_by_index(int32_t);
247
248/* Find an item in hrDeviceTbl by name. */
249struct device_entry *device_find_by_name(const char *);
250
251/* Create a new entry out of thin air. */
252struct device_entry *device_entry_create(const char *, const char *,
253    const char *);
254
255/* Delete an entry from hrDeviceTbl */
256void device_entry_delete(struct device_entry *entry);
257
258/* Init the things for hrProcessorTable. */
259void init_processor_tbl(void);
260
261/* Finalization routine for hrProcessorTable. */
262void fini_processor_tbl(void);
263
264/* Start the processor table CPU load collector. */
265void start_processor_tbl(struct lmodule *);
266
267/* Init the things for hrDiskStorageTable */
268int init_disk_storage_tbl(void);
269
270/* Finalization routine for hrDiskStorageTable. */
271void fini_disk_storage_tbl(void);
272
273/* Refresh routine for hrDiskStorageTable. */
274void refresh_disk_storage_tbl(int);
275
276/* Finalization routine for hrPartitionTable. */
277void fini_partition_tbl(void);
278
279/* Finalization routine for hrNetworkTable. */
280void fini_network_tbl(void);
281
282/* populate network table */
283void start_network_tbl(void);
284
285/* initialize installed software table */
286void init_swins_tbl(void);
287
288/* finalize installed software table */
289void fini_swins_tbl(void);
290
291/* refresh the hrSWInstalledTable if necessary */
292void refresh_swins_tbl(void);
293
294/* Init the things for hrPrinterTable */
295void init_printer_tbl(void);
296
297/* Finalization routine for hrPrinterTable. */
298void fini_printer_tbl(void);
299
300/* Refresh printer table */
301void refresh_printer_tbl(void);
302
303/* get boot command line */
304int OS_getSystemInitialLoadParameters(u_char **);
305
306/* Start refreshing the partition table */
307void partition_tbl_post_refresh(void);
308
309/* Handle refresh for the given disk */
310void partition_tbl_handle_disk(int32_t, const char *);
311
312/* Finish refreshing the partition table. */
313void partition_tbl_pre_refresh(void);
314
315/* Set the FS index in a partition table entry */
316void handle_partition_fs_index(const char *, int32_t);
317
318/* Make an SNMP DateAndTime from a struct tm. */
319int make_date_time(u_char *, const struct tm *, u_int);
320
321/* Free all static data */
322void fini_scalars(void);
323
324#endif /* HOSTRES_SNMP_H_1132245017 */
325