📄 hr_disk.c
字号:
newname[HRDISK_ENTRY_NAME_LENGTH] = LowIndex; memcpy( (char *)name,(char *)newname, ((int)vp->namelen + 1) * sizeof(oid)); *length = vp->namelen + 1; *write_method = 0; *var_len = sizeof(long); /* default to 'long' results */ DEBUGMSGTL(("host/hr_disk", "... get disk stats ")); DEBUGMSGOID(("host/hr_disk", name, *length)); DEBUGMSG(("host/hr_disk","\n")); return LowIndex;} /********************* * * System specific implementation functions * *********************/u_char *var_hrdisk(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ int disk_idx; disk_idx = header_hrdisk(vp, name, length, exact, var_len, write_method); if ( disk_idx == MATCH_FAILED ) return NULL; switch (vp->magic){ case HRDISK_ACCESS: long_return = Is_It_Writeable(); return (u_char *)&long_return; case HRDISK_MEDIA: long_return = What_Type_Disk(); return (u_char *)&long_return; case HRDISK_REMOVEABLE: long_return = Is_It_Removeable(); return (u_char *)&long_return; case HRDISK_CAPACITY: long_return = HRD_savedCapacity; return (u_char *)&long_return; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrdisk\n", vp->magic)); } return NULL;} /********************* * * Internal implementation functions * *********************/#define MAX_NUMBER_DISK_TYPES 16 /* probably should be a variable */#define MAX_DISKS_PER_TYPE 15 /* SCSI disks - not a hard limit */#define HRDISK_TYPE_SHIFT 4 /* log2 (MAX_DISKS_PER_TYPE+1) */typedef struct { const char *disk_devpart_string; /* printf() format disk part. name */ short disk_controller; /* controller id or -1 if NA */ short disk_device_first; /* first device id */ short disk_device_last; /* last device id */ const char *disk_devfull_string; /* printf() format full disk name */ short disk_partition_first; /* first partition id */ short disk_partition_last; /* last partition id */} HRD_disk_t;static HRD_disk_t disk_devices[ MAX_NUMBER_DISK_TYPES ];static int HR_number_disk_types = 0;static voidAdd_HR_Disk_entry (const char *devpart_string, int first_ctl, int last_ctl, int first_dev, int last_dev, const char *devfull_string, int first_partn, int last_partn){ while ( first_ctl <= last_ctl ) { disk_devices[ HR_number_disk_types ].disk_devpart_string = devpart_string; disk_devices[ HR_number_disk_types ].disk_controller = first_ctl; disk_devices[ HR_number_disk_types ].disk_device_first = first_dev; disk_devices[ HR_number_disk_types ].disk_device_last = last_dev; disk_devices[ HR_number_disk_types ].disk_devfull_string = devfull_string; disk_devices[ HR_number_disk_types ].disk_partition_first = first_partn; disk_devices[ HR_number_disk_types ].disk_partition_last = last_partn; /* * Split long runs of disks into separate "types" */ while ( last_dev - first_dev > MAX_DISKS_PER_TYPE ) { first_dev = first_dev + MAX_DISKS_PER_TYPE; disk_devices[ HR_number_disk_types ].disk_device_last = first_dev - 1; HR_number_disk_types++; disk_devices[ HR_number_disk_types ].disk_devpart_string = devpart_string; disk_devices[ HR_number_disk_types ].disk_controller = first_ctl; disk_devices[ HR_number_disk_types ].disk_device_first = first_dev; disk_devices[ HR_number_disk_types ].disk_device_last = last_dev; disk_devices[ HR_number_disk_types ].disk_devfull_string = devfull_string; disk_devices[ HR_number_disk_types ].disk_partition_first = first_partn; disk_devices[ HR_number_disk_types ].disk_partition_last = last_partn; } first_ctl++; HR_number_disk_types++; }}voidInit_HR_Disk(void){ HRD_type_index = 0; HRD_index = -1; DEBUGMSGTL(("host/hr_disk","Init_Disk\n"));}intGet_Next_HR_Disk (void){ char string[100]; int fd, result; int iindex; int max_disks; time_t now; HRD_index++; (void*) time( &now ); DEBUGMSGTL(("host/hr_disk","Next_Disk type %d of %d\n", HRD_type_index, HR_number_disk_types)); while ( HRD_type_index < HR_number_disk_types ) { max_disks = disk_devices[ HRD_type_index ].disk_device_last - disk_devices[ HRD_type_index ].disk_device_first + 1; DEBUGMSGTL(("host/hr_disk","Next_Disk max %d of type %d\n", max_disks, HRD_type_index )); while ( HRD_index < max_disks ) { iindex = (HRD_type_index << HRDISK_TYPE_SHIFT) + HRD_index; /* * Check to see whether this device * has been probed for 'recently' * and skip if so. * This has a *major* impact on run * times (by a factor of 10!) */ if (( HRD_history[ iindex ] > 0 ) && (( now - HRD_history[ iindex ]) < 60 )) { HRD_index++; continue; } /* Construct the full device name in "string" */ if ( disk_devices[ HRD_type_index ].disk_controller != -1 ) { sprintf( string, disk_devices[ HRD_type_index ].disk_devfull_string, disk_devices[ HRD_type_index ].disk_controller, disk_devices[ HRD_type_index ].disk_device_first + HRD_index); } else { sprintf( string, disk_devices[ HRD_type_index ].disk_devfull_string, disk_devices[ HRD_type_index ].disk_device_first + HRD_index); } DEBUGMSGTL(("host/hr_disk", "Get_Next_HR_Disk: %s (%d/%d)\n", string, HRD_type_index, HRD_index )); if ( HRD_history[ iindex ] == -1 ) { /* * check whether this device is in the "ignoredisk" list in * the config file. if yes this device will be marked as * invalid for the future, i.e. it won't ever be checked * again. */ if (match_disk_config(string)) { /* device name matches entry in ignoredisk list */ DEBUGMSGTL(("host/hr_disk", "Get_Next_HR_Disk: %s ignored\n", string )); HRD_history[ iindex ] = LONG_MAX; HRD_index++; continue; } } /* use O_NDELAY to avoid CDROM spin-up and media detection * (too slow) --okir */ /* at least with HP-UX 11.0 this doesn't seem to work properly * when accessing an empty CDROM device --jsf */#ifdef O_NDELAY /* I'm sure everything has it, but just in case... --Wes */ fd = open( string, O_RDONLY | O_NDELAY );#else fd = open( string, O_RDONLY );#endif if (fd != -1 ) { result = Query_Disk( fd, string ); close(fd); if ( result != -1 ) { HRD_history[ iindex ] = 0; return ((HRDEV_DISK << HRDEV_TYPE_SHIFT) + iindex ); } } HRD_history[ iindex ] = now; HRD_index++; } HRD_type_index++; HRD_index = 0; } HRD_index = -1; return -1;}intGet_Next_HR_Disk_Partition (char *string, int HRP_index){ DEBUGMSGTL(("host/hr_disk","Next_Partition type %d/%d:%d\n", HRD_type_index, HRD_type_index, HRP_index)); /* no more partition names => return -1 */ if ( disk_devices[ HRD_type_index ].disk_partition_last - disk_devices[ HRD_type_index ].disk_partition_first + 1 <= HRP_index) { return -1; } /* Construct the partition name in "string" */ if ( disk_devices[ HRD_type_index ].disk_controller != -1 ) { sprintf( string, disk_devices[ HRD_type_index ].disk_devpart_string, disk_devices[ HRD_type_index ].disk_controller, disk_devices[ HRD_type_index ].disk_device_first + HRD_index, disk_devices[ HRD_type_index ].disk_partition_first + HRP_index); } else { sprintf( string, disk_devices[ HRD_type_index ].disk_devpart_string, disk_devices[ HRD_type_index ].disk_device_first + HRD_index, disk_devices[ HRD_type_index ].disk_partition_first + HRP_index); } DEBUGMSGTL(("host/hr_disk", "Get_Next_HR_Disk_Partition: %s (%d/%d:%d)\n", string, HRD_type_index, HRD_index, HRP_index )); return 0;}static voidSave_HR_Disk_Specific (void){#ifdef DIOC_DESCRIBE HRD_savedIntf_type = HRD_info.intf_type; HRD_savedDev_type = HRD_info.dev_type; HRD_savedFlags = HRD_info.flags; HRD_savedCapacity = HRD_cap.lba / 2;#endif#ifdef DKIOCINFO HRD_savedCtrl_type = HRD_info.dki_ctype; HRD_savedFlags = HRD_info.dki_flags; HRD_savedCapacity = HRD_cap.dkg_ncyl* HRD_cap.dkg_nhead* HRD_cap.dkg_nsect / 2; /* ??? */#endif#ifdef HAVE_LINUX_HDREG_H HRD_savedCapacity = HRD_info.lba_capacity / 2 ; HRD_savedFlags = HRD_info.config;#endif#ifdef DIOCGDINFO HRD_savedCapacity = HRD_info.d_secperunit / 2;#endif}static voidSave_HR_Disk_General (void){#ifdef DIOC_DESCRIBE strcpy( HRD_savedModel, HRD_info.model_num ); #endif#ifdef DKIOCINFO strcpy( HRD_savedModel, HRD_info.dki_dname ); #endif#ifdef HAVE_LINUX_HDREG_H strcpy( HRD_savedModel, (const char *)HRD_info.model ); #endif#ifdef DIOCGDINFO strcpy( HRD_savedModel, dktypenames[HRD_info.d_type]);#endif}static const char *describe_disk(int idx){ if ( HRD_savedModel[0] == '\0' ) return( "some sort of disk"); else return( HRD_savedModel );}static intQuery_Disk(int fd, const char *devfull){ int result = -1;#ifdef DIOC_DESCRIBE result = ioctl( fd, DIOC_DESCRIBE, &HRD_info ); if ( result != -1 ) result = ioctl( fd, DIOC_CAPACITY, &HRD_cap );#endif#ifdef DKIOCINFO result = ioctl( fd, DKIOCINFO, &HRD_info ); if ( result != -1 ) result = ioctl( fd, DKIOCGGEOM, &HRD_cap );#endif#ifdef HAVE_LINUX_HDREG_H if ( HRD_type_index == 0 ) /* IDE hard disk */ result = ioctl( fd, HDIO_GET_IDENTITY, &HRD_info ); else if ( HRD_type_index <= 2 ) { /* SCSI hard disk and md devices */ long h; result = ioctl( fd, BLKGETSIZE, &h); if ( result != -1 && HRD_type_index == 2 && h == 0L) result = -1; /* ignore empty md devices */ if ( result != -1 ) { HRD_info.lba_capacity = h; if (HRD_type_index == 1) sprintf( HRD_info.model, "SCSI disk (%s)", devfull ); else sprintf( HRD_info.model, "RAID disk (%s)", devfull ); HRD_info.config = 0; } }#endif#ifdef DIOCGDINFO result = ioctl(fd, DIOCGDINFO, &HRD_info);#endif return( result );}static intIs_It_Writeable(void){#ifdef DIOC_DESCRIBE if (( HRD_savedFlags & WRITE_PROTECT_FLAG ) || ( HRD_savedDev_type == CDROM_DEV_TYPE )) return(2); /* read only */#endif#ifdef DKIOCINFO if ( HRD_savedCtrl_type == DKC_CDROM ) return(2); /* read only */#endif return(1); /* read-write */}static intWhat_Type_Disk(void){#ifdef DIOC_DESCRIBE switch ( HRD_savedDev_type ) { case DISK_DEV_TYPE: if ( HRD_savedIntf_type == PC_FDC_INTF ) return(4); /* Floppy Disk */ else return(3); /* Hard Disk */ break; case CDROM_DEV_TYPE: return(5); /* Optical RO */ break; case WORM_DEV_TYPE: return(6); /* Optical WORM */ break; case MO_DEV_TYPE: return(7); /* Optical R/W */ break; default: return(2); /* Unknown */ break; }#endif#ifdef DKIOCINFO switch ( HRD_savedCtrl_type ) { case DKC_WDC2880: case DKC_DSD5215:#ifdef DKC_XY450 case DKC_XY450:#endif case DKC_ACB4000: case DKC_MD21:#ifdef DKC_XD7053 case DKC_XD7053:#endif case DKC_SCSI_CCS:#ifdef DKC_PANTHER case DKC_PANTHER:#endif#ifdef DKC_CDC_9057 case DKC_CDC_9057:#endif#ifdef DKC_FJ_M1060 case DKC_FJ_M1060:#endif case DKC_DIRECT: case DKC_PCMCIA_ATA: return(3); /* Hard Disk */ break; case DKC_NCRFLOPPY: case DKC_SMSFLOPPY: case DKC_INTEL82077: return(4); /* Floppy Disk */ break; case DKC_CDROM: return(5); /* Optical RO */ break; case DKC_PCMCIA_MEM: return(8); /* RAM disk */ break; case DKC_MD: /* "meta-disk" driver */ return(1); /* Other */ break; }#endif return(2); /* Unknown */}static intIs_It_Removeable(void){#ifdef DIOC_DESCRIBE if (( HRD_savedIntf_type == PC_FDC_INTF ) || ( HRD_savedDev_type == WORM_DEV_TYPE ) || ( HRD_savedDev_type == MO_DEV_TYPE ) || ( HRD_savedDev_type == CDROM_DEV_TYPE )) return(1); /* true */#endif#ifdef DKIOCINFO if (( HRD_savedCtrl_type == DKC_CDROM ) || ( HRD_savedCtrl_type == DKC_NCRFLOPPY ) || ( HRD_savedCtrl_type == DKC_SMSFLOPPY ) || ( HRD_savedCtrl_type == DKC_INTEL82077 ) || ( HRD_savedCtrl_type == DKC_PCMCIA_MEM ) || ( HRD_savedCtrl_type == DKC_PCMCIA_ATA )) return(1); /* true */#endif#ifdef HAVE_LINUX_HDREG_H if ( HRD_savedFlags & 0x80 ) return(1); /* true */#endif return(2); /* false */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -