Path: blob/main/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h
108394 views
/*1* Copyright (c) 2005-2006 The FreeBSD Project2* All rights reserved.3*4* Author: Victor Cruceru <[email protected]>5*6* Redistribution of this software and documentation and use in source and7* binary forms, with or without modification, are permitted provided that8* the following conditions are met:9*10* 1. Redistributions of source code or documentation must retain the above11* copyright notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*28* Host Resources MIB for SNMPd.29*/3031#ifndef HOSTRES_SNMP_H_113224501732#define HOSTRES_SNMP_H_11322450173334#include <sys/types.h>35#include <sys/queue.h>3637#include <stdio.h>38#include <fcntl.h>39#include <kvm.h>40#include <devinfo.h>4142#include <bsnmp/asn1.h>43#include <bsnmp/snmp.h>4445#include <bsnmp/snmpmod.h>4647/*48* Default package directory for hrSWInstalledTable. Can be overridden49* via SNMP or configuration file.50*/51#define PATH_PKGDIR "/var/db/pkg"5253/*54* These are the default maximum caching intervals for the various tables55* in seconds. They can be overridden from the configuration file.56*/57#define HR_STORAGE_TBL_REFRESH 758#define HR_FS_TBL_REFRESH 759#define HR_DISK_TBL_REFRESH 760#define HR_NETWORK_TBL_REFRESH 761#define HR_SWINS_TBL_REFRESH 12062#define HR_SWRUN_TBL_REFRESH 36364struct tm;65struct statfs;6667/* a debug macro */68#ifndef NDEBUG6970#define HRDBG(...) do { \71fprintf(stderr, "HRDEBUG: %s: ", __func__); \72fprintf(stderr, __VA_ARGS__); \73fprintf(stderr, "\n"); \74} while (0)7576#else7778#define HRDBG(...) do { } while (0)7980#endif /*NDEBUG*/8182/* path to devd(8) output pipe */83#define PATH_DEVD_PIPE "/var/run/devd.pipe"8485#define IS_KERNPROC(kp) (((kp)->ki_flag & P_KPROC) == P_KPROC)8687enum snmpTCTruthValue {88SNMP_TRUE = 1,89SNMP_FALSE= 290};9192/* The number of CPU load samples per one minute, per each CPU */93#define MAX_CPU_SAMPLES 4949596/*97* max len (including '\0'), for device_entry::descr field below,98* according to MIB99*/100#define DEV_DESCR_MLEN (64 + 1)101102/*103* max len (including '\0'), for device_entry::name and104* device_map_entry::name_key fields below, according to MIB105*/106#define DEV_NAME_MLEN (32 + 1)107108/*109* max len (including '\0'), for device_entry::location and110* device_map_entry::location_key fields below, according to MIB111*/112#define DEV_LOC_MLEN (128 + 1)113114/*115* This structure is used to hold a SNMP table entry116* for HOST-RESOURCES-MIB's hrDeviceTable117*/118struct device_entry {119int32_t index;120const struct asn_oid *type;121u_char *descr;122const struct asn_oid *id; /* only oid_zeroDotZero as (*id) value*/123int32_t status; /* enum DeviceStatus */124uint32_t errors;125126#define HR_DEVICE_FOUND 0x001127/* not detected by libdevice, so don't try to refresh it*/128#define HR_DEVICE_IMMUTABLE 0x002129130/* next 3 are not from the SNMP mib table, only to be used internally */131uint32_t flags;132133u_char *name;134u_char *location;135TAILQ_ENTRY(device_entry) link;136};137138/*139* Next structure is used to keep o list of mappings from a specific140* name (a_name) to an entry in the hrFSTblEntry;141* We are trying to keep the same index for a specific name at least142* for the duration of one SNMP agent run.143*/144struct device_map_entry {145int32_t hrIndex; /* used for hrDeviceTblEntry::index */146147/* map key is the pair (name_key, location_key) */148u_char *name_key; /* copy of device name */149u_char *location_key;150151/*152* Next may be NULL if the respective hrDeviceTblEntry153* is (temporally) gone.154*/155struct device_entry *entry_p;156STAILQ_ENTRY(device_map_entry) link;157};158STAILQ_HEAD(device_map, device_map_entry);159160/* descriptor to access kernel memory */161extern kvm_t *hr_kd;162163/* Table used for consistent device table indexing. */164extern struct device_map device_map;165166/* Maximum number of ticks between two updates for hrStorageTable */167extern uint32_t storage_tbl_refresh;168169/* Maximum number of ticks between updated of FS table */170extern uint32_t fs_tbl_refresh;171172/* maximum number of ticks between updates of SWRun and SWRunPerf table */173extern uint32_t swrun_tbl_refresh;174175/* Maximum number of ticks between device table refreshs. */176extern uint32_t device_tbl_refresh;177178/* maximum number of ticks between refreshs */179extern uint32_t disk_storage_tbl_refresh;180181/* maximum number of ticks between updates of network table */182extern uint32_t swins_tbl_refresh;183184/* maximum number of ticks between updates of network table */185extern uint32_t network_tbl_refresh;186187/* package directory */188extern u_char *pkg_dir;189190/* Initialize and populate storage table */191void init_storage_tbl(void);192193/* Finalization routine for hrStorageTable. */194void fini_storage_tbl(void);195196/* Refresh routine for hrStorageTable. */197void refresh_storage_tbl(int);198199/*200* Get the type of filesystem referenced in a struct statfs * -201* used by FSTbl and StorageTbl functions.202*/203const struct asn_oid *fs_get_type(const struct statfs *);204205/*206* Because hrFSTable depends to hrStorageTable we are207* refreshing hrFSTable by refreshing hrStorageTable.208* When one entry "of type" fs from hrStorageTable is refreshed209* then the corresponding entry from hrFSTable is refreshed210* FS_tbl_pre_refresh_v() is called before refreshing fs part of hrStorageTable211*/212void fs_tbl_pre_refresh(void);213void fs_tbl_process_statfs_entry(const struct statfs *, int32_t);214215/* Called after refreshing fs part of hrStorageTable */216void fs_tbl_post_refresh(void);217218/* Refresh the FS table if necessary. */219void refresh_fs_tbl(void);220221/* Finalization routine for hrFSTable. */222void fini_fs_tbl(void);223224/* Init the things for both of hrSWRunTable and hrSWRunPerfTable */225void init_swrun_tbl(void);226227/* Finalization routine for both of hrSWRunTable and hrSWRunPerfTable */228void fini_swrun_tbl(void);229230/* Init and populate hrDeviceTable */231void init_device_tbl(void);232233/* start devd monitoring */234void start_device_tbl(struct lmodule *);235236/* Finalization routine for hrDeviceTable */237void fini_device_tbl(void);238239/* Refresh routine for hrDeviceTable. */240void refresh_device_tbl(int);241242/* Find an item in hrDeviceTbl by its entry->index. */243struct device_entry *device_find_by_index(int32_t);244245/* Find an item in hrDeviceTbl by name. */246struct device_entry *device_find_by_name(const char *);247248/* Create a new entry out of thin air. */249struct device_entry *device_entry_create(const char *, const char *,250const char *);251252/* Delete an entry from hrDeviceTbl */253void device_entry_delete(struct device_entry *entry);254255/* Init the things for hrProcessorTable. */256void init_processor_tbl(void);257258/* Finalization routine for hrProcessorTable. */259void fini_processor_tbl(void);260261/* Start the processor table CPU load collector. */262void start_processor_tbl(struct lmodule *);263264/* Init the things for hrDiskStorageTable */265int init_disk_storage_tbl(void);266267/* Finalization routine for hrDiskStorageTable. */268void fini_disk_storage_tbl(void);269270/* Refresh routine for hrDiskStorageTable. */271void refresh_disk_storage_tbl(int);272273/* Finalization routine for hrPartitionTable. */274void fini_partition_tbl(void);275276/* Finalization routine for hrNetworkTable. */277void fini_network_tbl(void);278279/* populate network table */280void start_network_tbl(void);281282/* initialize installed software table */283void init_swins_tbl(void);284285/* finalize installed software table */286void fini_swins_tbl(void);287288/* refresh the hrSWInstalledTable if necessary */289void refresh_swins_tbl(void);290291/* Init the things for hrPrinterTable */292void init_printer_tbl(void);293294/* Finalization routine for hrPrinterTable. */295void fini_printer_tbl(void);296297/* Refresh printer table */298void refresh_printer_tbl(void);299300/* get boot command line */301int OS_getSystemInitialLoadParameters(u_char **);302303/* Start refreshing the partition table */304void partition_tbl_post_refresh(void);305306/* Handle refresh for the given disk */307void partition_tbl_handle_disk(int32_t, const char *);308309/* Finish refreshing the partition table. */310void partition_tbl_pre_refresh(void);311312/* Set the FS index in a partition table entry */313void handle_partition_fs_index(const char *, int32_t);314315/* Make an SNMP DateAndTime from a struct tm. */316int make_date_time(u_char *, const struct tm *, u_int);317318/* Free all static data */319void fini_scalars(void);320321#endif /* HOSTRES_SNMP_H_1132245017 */322323324