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 67 struct tm; 68 struct 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 90 enum 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 * This structure is used to hold a SNMP table entry 100 * for HOST-RESOURCES-MIB's hrDeviceTable 101 */ 102 struct device_entry { 103 int32_t index; 104 struct asn_oid type; 105 u_char descr[64 + 1]; 106 struct asn_oid id; 107 int32_t status; /* enum DeviceStatus */ 108 uint32_t errors; 109 110 #define HR_DEVICE_FOUND 0x001 111 /* not dectected by libdevice, so don't try to refresh it*/ 112 #define HR_DEVICE_IMMUTABLE 0x002 113 114 /* next 3 are not from the SNMP mib table, only to be used internally */ 115 uint32_t flags; 116 u_char name[32 + 1]; 117 u_char location[128 + 1]; 118 TAILQ_ENTRY(device_entry) link; 119 }; 120 121 /* 122 * Next structure is used to keep o list of mappings from a specific 123 * name (a_name) to an entry in the hrFSTblEntry; 124 * We are trying to keep the same index for a specific name at least 125 * for the duration of one SNMP agent run. 126 */ 127 struct device_map_entry { 128 int32_t hrIndex; /* used for hrDeviceTblEntry::index */ 129 130 /* map key is the pair (name_key, location_key) */ 131 u_char name_key[32 + 1]; /* copy of device name */ 132 u_char location_key[128 + 1]; 133 134 /* 135 * Next may be NULL if the respective hrDeviceTblEntry 136 * is (temporally) gone. 137 */ 138 struct device_entry *entry_p; 139 STAILQ_ENTRY(device_map_entry) link; 140 }; 141 STAILQ_HEAD(device_map, device_map_entry); 142 143 /* descriptor to access kernel memory */ 144 extern kvm_t *hr_kd; 145 146 /* Table used for consistent device table indexing. */ 147 extern struct device_map device_map; 148 149 /* Maximum number of ticks between two updates for hrStorageTable */ 150 extern uint32_t storage_tbl_refresh; 151 152 /* Maximum number of ticks between updated of FS table */ 153 extern uint32_t fs_tbl_refresh; 154 155 /* maximum number of ticks between updates of SWRun and SWRunPerf table */ 156 extern uint32_t swrun_tbl_refresh; 157 158 /* Maximum number of ticks between device table refreshs. */ 159 extern uint32_t device_tbl_refresh; 160 161 /* maximum number of ticks between refreshs */ 162 extern uint32_t disk_storage_tbl_refresh; 163 164 /* maximum number of ticks between updates of network table */ 165 extern uint32_t swins_tbl_refresh; 166 167 /* maximum number of ticks between updates of network table */ 168 extern uint32_t network_tbl_refresh; 169 170 /* package directory */ 171 extern u_char *pkg_dir; 172 173 /* Initialize and populate storage table */ 174 void init_storage_tbl(void); 175 176 /* Finalization routine for hrStorageTable. */ 177 void fini_storage_tbl(void); 178 179 /* Refresh routine for hrStorageTable. */ 180 void refresh_storage_tbl(int); 181 182 /* 183 * Get the type of filesystem referenced in a struct statfs * - 184 * used by FSTbl and StorageTbl functions. 185 */ 186 const struct asn_oid *fs_get_type(const struct statfs *); 187 188 /* 189 * Because hrFSTable depends to hrStorageTable we are 190 * refreshing hrFSTable by refreshing hrStorageTable. 191 * When one entry "of type" fs from hrStorageTable is refreshed 192 * then the corresponding entry from hrFSTable is refreshed 193 * FS_tbl_pre_refresh_v() is called before refeshing fs part of hrStorageTable 194 */ 195 void fs_tbl_pre_refresh(void); 196 void fs_tbl_process_statfs_entry(const struct statfs *, int32_t); 197 198 /* Called after refreshing fs part of hrStorageTable */ 199 void fs_tbl_post_refresh(void); 200 201 /* Refresh the FS table if neccessary. */ 202 void refresh_fs_tbl(void); 203 204 /* Finalization routine for hrFSTable. */ 205 void fini_fs_tbl(void); 206 207 /* Init the things for both of hrSWRunTable and hrSWRunPerfTable */ 208 void init_swrun_tbl(void); 209 210 /* Finalization routine for both of hrSWRunTable and hrSWRunPerfTable */ 211 void fini_swrun_tbl(void); 212 213 /* Init and populate hrDeviceTable */ 214 void init_device_tbl(void); 215 216 /* start devd monitoring */ 217 void start_device_tbl(struct lmodule *); 218 219 /* Finalization routine for hrDeviceTable */ 220 void fini_device_tbl(void); 221 222 /* Refresh routine for hrDeviceTable. */ 223 void refresh_device_tbl(int); 224 225 /* Find an item in hrDeviceTbl by its entry->index. */ 226 struct device_entry *device_find_by_index(int32_t); 227 228 /* Find an item in hrDeviceTbl by name. */ 229 struct device_entry *device_find_by_name(const char *); 230 231 /* Create a new entry out of thin air. */ 232 struct device_entry *device_entry_create(const char *, const char *, 233 const char *); 234 235 /* Init the things for hrProcessorTable. */ 236 void init_processor_tbl(void); 237 238 /* Finalization routine for hrProcessorTable. */ 239 void fini_processor_tbl(void); 240 241 /* Start the processor table CPU load collector. */ 242 void start_processor_tbl(struct lmodule *); 243 244 /* Init the things for hrDiskStorageTable */ 245 int init_disk_storage_tbl(void); 246 247 /* Finalization routine for hrDiskStorageTable. */ 248 void fini_disk_storage_tbl(void); 249 250 /* Refresh routine for hrDiskStorageTable. */ 251 void refresh_disk_storage_tbl(int); 252 253 /* Finalization routine for hrPartitionTable. */ 254 void fini_partition_tbl(void); 255 256 /* Finalization routine for hrNetworkTable. */ 257 void fini_network_tbl(void); 258 259 /* populate network table */ 260 void start_network_tbl(void); 261 262 /* initialize installed software table */ 263 void init_swins_tbl(void); 264 265 /* finalize installed software table */ 266 void fini_swins_tbl(void); 267 268 /* refresh the hrSWInstalledTable if necessary */ 269 void refresh_swins_tbl(void); 270 271 /* Init the things for hrPrinterTable */ 272 void init_printer_tbl(void); 273 274 /* Finalization routine for hrPrinterTable. */ 275 void fini_printer_tbl(void); 276 277 /* Refresh printer table */ 278 void refresh_printer_tbl(void); 279 280 /* get boot command line */ 281 int OS_getSystemInitialLoadParameters(u_char **); 282 283 /* Start refreshing the partition table */ 284 void partition_tbl_post_refresh(void); 285 286 /* Handle refresh for the given disk */ 287 void partition_tbl_handle_disk(int32_t, const char *); 288 289 /* Finish refreshing the partition table. */ 290 void partition_tbl_pre_refresh(void); 291 292 /* Set the FS index in a partition table entry */ 293 void handle_partition_fs_index(const char *, int32_t); 294 295 /* Make an SNMP DateAndTime from a struct tm. */ 296 int make_date_time(u_char *, const struct tm *, u_int); 297 298 /* Free all static data */ 299 void fini_scalars(void); 300 301 #endif /* HOSTRES_SNMP_H_1132245017 */ 302