📄 disk.c
字号:
/* * disk.c */#include <net-snmp/net-snmp-config.h>#include <stdio.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_FCNTL_H#include <fcntl.h>#endif#include <signal.h>#if HAVE_MACHINE_PARAM_H#include <machine/param.h>#endif#if HAVE_SYS_VMMETER_H#if !(defined(bsdi2) || defined(netbsd1))#include <sys/vmmeter.h>#endif#endif#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#if HAVE_SYS_CONF_H#include <sys/conf.h>#endif#if HAVE_ASM_PAGE_H#include <asm/page.h>#endif#if HAVE_SYS_SWAP_H#include <sys/swap.h>#endif#if HAVE_SYS_FS_H#include <sys/fs.h>#else#if HAVE_UFS_FS_H#include <ufs/fs.h>#else#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#ifdef HAVE_SYS_VNODE_H#include <sys/vnode.h>#endif#ifdef HAVE_UFS_UFS_QUOTA_H#include <ufs/ufs/quota.h>#endif#ifdef HAVE_UFS_UFS_INODE_H#include <ufs/ufs/inode.h>#endif#if HAVE_UFS_FFS_FS_H#include <ufs/ffs/fs.h>#endif#endif#endif#if HAVE_MTAB_H#include <mtab.h>#endif#include <sys/stat.h>#include <errno.h>#if HAVE_SYS_STATFS_H#include <sys/statfs.h>#endif#if HAVE_SYS_STATVFS_H#include <sys/statvfs.h>#endif#if HAVE_SYS_VFS_H#include <sys/vfs.h>#endif#if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)#if HAVE_SYS_MOUNT_H#include <sys/mount.h>#endif#if HAVE_SYS_SYSCTL_H#include <sys/sysctl.h>#endif#define statvfs statfs#endif#if HAVE_VM_VM_H#include <vm/vm.h>#endif#if HAVE_VM_SWAP_PAGER_H#include <vm/swap_pager.h>#endif#if HAVE_SYS_FIXPOINT_H#include <sys/fixpoint.h>#endif#if HAVE_MALLOC_H#include <malloc.h>#endif#if HAVE_STRING_H#include <string.h>#endif#if HAVE_FSTAB_H#include <fstab.h>#endif#if HAVE_MNTENT_H#include <mntent.h>#endif#if HAVE_SYS_MNTTAB_H#include <sys/mnttab.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/agent/auto_nlist.h>#include "struct.h"#include "disk.h"#include "util_funcs.h"#if USING_UCD_SNMP_ERRORMIB_MODULE#include "errormib.h"#else#define setPerrorstatus(x) snmp_log_perror(x)#endif/* * * config file parsing routines * */static void disk_free_config(void);static void disk_parse_config(const char *, char *);static void disk_parse_config_all(const char *, char *);static void find_and_add_allDisks(int minpercent);static void add_device(char *path, char *device, int minspace, int minpercent, int override);static void modify_disk_parameters(int index, int minspace, int minpercent);static int disk_exists(char *path);static u_char *find_device(char *path);struct diskpart { char device[STRMAX]; char path[STRMAX]; int minimumspace; int minpercent;};int numdisks;int allDisksIncluded = 0;struct diskpart disks[MAXDISKS];struct variable2 extensible_disk_variables[] = { {MIBINDEX, ASN_INTEGER, RONLY, var_extensible_disk, 1, {MIBINDEX}}, {ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_disk, 1, {ERRORNAME}}, {DISKDEVICE, ASN_OCTET_STR, RONLY, var_extensible_disk, 1, {DISKDEVICE}}, {DISKMINIMUM, ASN_INTEGER, RONLY, var_extensible_disk, 1, {DISKMINIMUM}}, {DISKMINPERCENT, ASN_INTEGER, RONLY, var_extensible_disk, 1, {DISKMINPERCENT}}, {DISKTOTAL, ASN_INTEGER, RONLY, var_extensible_disk, 1, {DISKTOTAL}}, {DISKAVAIL, ASN_INTEGER, RONLY, var_extensible_disk, 1, {DISKAVAIL}}, {DISKUSED, ASN_INTEGER, RONLY, var_extensible_disk, 1, {DISKUSED}}, {DISKPERCENT, ASN_INTEGER, RONLY, var_extensible_disk, 1, {DISKPERCENT}}, {DISKPERCENTNODE, ASN_INTEGER, RONLY, var_extensible_disk, 1, {DISKPERCENTNODE}}, {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_disk, 1, {ERRORFLAG}}, {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_disk, 1, {ERRORMSG}}};/* * Define the OID pointer to the top of the mib tree that we're * registering underneath */oid disk_variables_oid[] = { UCDAVIS_MIB, DISKMIBNUM, 1 };voidinit_disk(void){ /* * register ourselves with the agent to handle our mib tree */ REGISTER_MIB("ucd-snmp/disk", extensible_disk_variables, variable2, disk_variables_oid); snmpd_register_config_handler("disk", disk_parse_config, disk_free_config, "path [ minspace | minpercent% ]"); snmpd_register_config_handler("includeAllDisks", disk_parse_config_all, disk_free_config, "minpercent%"); allDisksIncluded = 0;}static voiddisk_free_config(void){ int i; numdisks = 0; for (i = 0; i < MAXDISKS; i++) { /* init/erase disk db */ disks[i].device[0] = 0; disks[i].path[0] = 0; disks[i].minimumspace = -1; disks[i].minpercent = -1; }}static void disk_parse_config(const char *token, char *cptr){#if HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS char tmpbuf[1024]; char path[STRMAX]; int minpercent; int minspace; if (numdisks == MAXDISKS) { config_perror("Too many disks specified."); snprintf(tmpbuf, sizeof(tmpbuf), "\tignoring: %s", cptr); tmpbuf[ sizeof(tmpbuf)-1 ] = 0; config_perror(tmpbuf); } else { /* * read disk path (eg, /1 or /usr) */ copy_nword(cptr, path, sizeof(path)); cptr = skip_not_white(cptr); cptr = skip_white(cptr); /* * read optional minimum disk usage spec */ if(cptr != NULL) { if(strchr(cptr, '%') == 0) { minspace = atoi(cptr); minpercent = -1; } else { minspace = -1; minpercent = atoi(cptr); } } else { minspace = DEFDISKMINIMUMSPACE; minpercent = -1; } /* * check if the disk already exists, if so then modify its * parameters. if it does not exist then add it */ add_device(path, find_device(path), minspace, minpercent, 1); }#endif /* HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS */}static void disk_parse_config_all(const char *token, char *cptr){#if HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS char tmpbuf[1024]; int minpercent = DISKMINPERCENT; if (numdisks == MAXDISKS) { config_perror("Too many disks specified."); sprintf(tmpbuf, "\tignoring: %s", cptr); config_perror(tmpbuf); } else { /* * read the minimum disk usage percent */ if(cptr != NULL) { if(strchr(cptr, '%') != 0) { minpercent = atoi(cptr); } } /* * if we have already seen the "includeAllDisks" directive * then search for the disk in the "disks" array and modify * the values. if we havent seen the "includeAllDisks" * directive then include this disk */ if(allDisksIncluded) { config_perror("includeAllDisks already specified."); sprintf(tmpbuf, "\tignoring: includeAllDisks %s", cptr); config_perror(tmpbuf); } else { allDisksIncluded = 1; find_and_add_allDisks(minpercent); } }#endif /* HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS */}static voidadd_device(char *path, char *device, int minspace, int minpercent, int override) { int index; if (numdisks == MAXDISKS) { char tmpbuf[1024]; config_perror("Too many disks specified."); snprintf(tmpbuf, sizeof(tmpbuf), "\tignoring: %s", device); tmpbuf[ sizeof(tmpbuf)-1 ] = 0; config_perror(tmpbuf); return; } index = disk_exists(path); if((index != -1) && (index < MAXDISKS) && (override==1)) { modify_disk_parameters(index, minspace, minpercent); } else if(index == -1){ /* add if and only if the device was found */ if(device[0] != 0) { copy_nword(path, disks[numdisks].path, sizeof(disks[numdisks].path)); copy_nword(device, disks[numdisks].device, sizeof(disks[numdisks].device)); disks[numdisks].minimumspace = minspace; disks[numdisks].minpercent = minpercent; numdisks++; } else { disks[numdisks].minimumspace = -1; disks[numdisks].minpercent = -1; disks[numdisks].path[0] = 0; disks[numdisks].device[0] = 0; } }}voidmodify_disk_parameters(int index, int minspace, int minpercent){ disks[index].minimumspace = minspace; disks[index].minpercent = minpercent;}int disk_exists(char *path) { int index; for(index = 0; index < numdisks; index++) { DEBUGMSGTL(("ucd-snmp/disk:", "Checking for %s. Found %s at %d ", path, disks[index].path, index)); if(strcmp(path, disks[index].path) == 0) { return index; } } return -1;}static void find_and_add_allDisks(int minpercent){#if HAVE_GETMNTENT#if HAVE_SYS_MNTTAB_H struct mnttab mnttab;#else struct mntent *mntent;#endif FILE *mntfp;#elif HAVE_FSTAB_H struct fstab *fstab1;#elif HAVE_STATFS struct statfs statf;#endif#if defined(HAVE_GETMNTENT) && !defined(HAVE_SETMNTENT) int i;#endif int dummy = 0; char tmpbuf[1024]; /* * find the device for the path and copy the device into the * string declared above and at the end of the routine return it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -