📄 hr_filesys.c
字号:
fsys_type_id[fsys_type_len-1] = 14;#endif#ifdef MNTTYPE_MFS else if (!strcmp( mnt_type, MNTTYPE_MFS)) fsys_type_id[fsys_type_len-1] = 8;#endif#ifdef MNTTYPE_EXT2FS else if (!strcmp( mnt_type, MNTTYPE_EXT2FS)) fsys_type_id[fsys_type_len-1] = 23;#endif#ifdef MNTTYPE_NTFS else if (!strcmp( mnt_type, MNTTYPE_NTFS)) fsys_type_id[fsys_type_len-1] = 9;#endif else fsys_type_id[fsys_type_len-1] = 1; /* Other */#endif /* HAVE_GETFSSTAT */ } *var_len = sizeof(fsys_type_id); return (u_char *)fsys_type_id; case HRFSYS_ACCESS:#if HAVE_GETFSSTAT long_return = HRFS_entry->f_flags & MNT_RDONLY ? 2 : 1;#elif defined(cygwin) long_return = 1;#else if ( hasmntopt( HRFS_entry, "ro" ) != NULL ) long_return = 2; /* Read Only */ else long_return = 1; /* Read-Write */#endif return (u_char *)&long_return; case HRFSYS_BOOT: if ( HRFS_entry->HRFS_mount[0] == '/' && HRFS_entry->HRFS_mount[1] == 0 ) long_return = 1; /* root is probably bootable! */ else long_return = 2; /* others probably aren't */ return (u_char *)&long_return; case HRFSYS_STOREIDX: long_return = fsys_idx; /* Use the same indices */ return (u_char *)&long_return; case HRFSYS_FULLDUMP: return when_dumped( HRFS_entry->HRFS_name, FULL_DUMP, var_len ); case HRFSYS_PARTDUMP: return when_dumped( HRFS_entry->HRFS_name, PART_DUMP, var_len ); default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrfilesys\n", vp->magic)); } return NULL;} /********************* * * Internal implementation functions * *********************/static int HRFS_index;#ifndef HAVE_GETFSSTATstatic FILE *fp;#endifvoidInit_HR_FileSys (void){#if HAVE_GETFSSTAT fscount = getfsstat(NULL, 0, MNT_NOWAIT); if (fsstats) free((char *)fsstats); fsstats = NULL; fsstats = malloc(fscount*sizeof(*fsstats)); getfsstat(fsstats, fscount*sizeof(*fsstats), MNT_NOWAIT); HRFS_index = 0;#else HRFS_index = 1; if ( fp != NULL ) fclose(fp); fp = fopen( ETC_MNTTAB, "r");#endif}const char *HRFS_ignores[] = {#ifdef MNTTYPE_IGNORE MNTTYPE_IGNORE,#endif#ifdef MNTTYPE_SWAP MNTTYPE_SWAP,#endif#ifdef MNTTYPE_PROC MNTTYPE_PROC,#endif#ifdef MNTTYPE_AUTOFS MNTTYPE_AUTOFS,#endif "autofs",#ifdef linux "devpts", "shm",#endif#ifdef solaris2 "fd",#endif 0};intGet_Next_HR_FileSys (void){#if HAVE_GETFSSTAT if (HRFS_index >= fscount) return -1; HRFS_entry = fsstats+HRFS_index; return ++HRFS_index;#else const char **cpp; /* * XXX - According to RFC 1514, hrFSIndex must * "remain constant at least from one re-initialization * of the agent to the next re-initialization." * * This simple-minded counter doesn't handle filesystems * being un-mounted and re-mounted. * Options for fixing this include: * - keeping a history of previous indices used * - calculating the index from filesystem * specific information * * Note: this index is also used as hrStorageIndex * which is assumed to be less than HRS_TYPE_FS_MAX * This assumption may well be broken if the second * option above is followed. Consider indexing the * non-filesystem-based storage entries first in this * case, and assume hrStorageIndex > HRS_TYPE_FS_MIN * (for file-system based storage entries) * * But at least this gets us started. */ if ( fp == NULL ) return -1;#ifdef solaris2 if (getmntent( fp, HRFS_entry) != 0) return -1;#else HRFS_entry = getmntent( fp ); if ( HRFS_entry == NULL ) return -1;#endif /* solaris2 */ for ( cpp = HRFS_ignores ; *cpp != NULL ; ++cpp ) if ( !strcmp( HRFS_entry->HRFS_type, *cpp )) return Get_Next_HR_FileSys(); return HRFS_index++;#endif /* HAVE_GETFSSTAT */}/* * this function checks whether the current file system (info can be found * in HRFS_entry) is a NFS file system * HRFS_entry must be valid prior to calling this function * returns 1 if NFS file system, 0 otherwise */intCheck_HR_FileSys_NFS (void){#if HAVE_GETFSSTAT#if defined(MFSNAMELEN) if (!strcmp(HRFS_entry->HRFS_type, MOUNT_NFS))#else if (HRFS_entry->HRFS_type == MOUNT_NFS)#endif#else /* HAVE_GETFSSTAT */ if ( HRFS_entry->HRFS_type != NULL && (#if defined(MNTTYPE_NFS) !strcmp( HRFS_entry->HRFS_type, MNTTYPE_NFS) ||#else !strcmp( HRFS_entry->HRFS_type, "nfs") ||#endif#if defined(MNTTYPE_NFS3) !strcmp( HRFS_entry->HRFS_type, MNTTYPE_NFS3) ||#endif#if defined(MNTTYPE_LOFS) !strcmp( HRFS_entry->HRFS_type, MNTTYPE_LOFS) ||#endif /* * MVFS is Rational ClearCase's view file system * it is similiar to NFS file systems in that it is mounted * locally or remotely from the ClearCase server */ !strcmp( HRFS_entry->HRFS_type, "mvfs")))#endif /* HAVE_GETFSSTAT */ return 1; /* NFS file system */ return 0; /* no NFS file system */}voidEnd_HR_FileSys (void){#ifdef HAVE_GETFSSTAT if (fsstats) free((char *)fsstats); fsstats = NULL;#else if ( fp != NULL ) fclose(fp); fp = NULL;#endif}static u_char *when_dumped(char *filesys, int level, size_t *length){ time_t dumpdate = 0, tmp; FILE *dump_fp; char line[100]; char *cp1, *cp2, *cp3; /* * Look for the relevent entries in /etc/dumpdates * * This is complicated by the fact that disks are * mounted using block devices, but dumps are * done via the raw character devices. * Thus the device names in /etc/dumpdates and * /etc/mnttab don't match. * These comparisons are therefore made using the * final portion of the device name only. */ if ( *filesys == '\0' ) /* No filesystem name? */ return date_n_time (NULL, length); cp1=strrchr( filesys, '/' ); /* Find the last element of the current FS */ if ( cp1 == NULL ) cp1 = filesys; if ((dump_fp = fopen("/etc/dumpdates", "r")) == NULL ) return date_n_time (NULL, length); while ( fgets( line, sizeof(line), dump_fp ) != NULL ) { cp2=strchr( line, ' ' ); /* Start by looking at the device name only */ if ( cp2!=NULL ) { *cp2 = '\0'; cp3=strrchr( line, '/' ); /* and find the last element */ if ( cp3 == NULL ) cp3 = line; if ( strcmp( cp1, cp3 ) != 0 ) /* Wrong FS */ continue; ++cp2; while (isspace(*cp2)) ++cp2; /* Now find the dump level */ if ( level == FULL_DUMP ) { if ( *(cp2++) != '0' ) continue; /* Not interested in partial dumps */ while (isspace(*cp2)) ++cp2; dumpdate = ctime_to_timet( cp2 ); fclose( dump_fp ); return date_n_time (&dumpdate, length); } else { /* Partial Dump */ if ( *(cp2++) == '0' ) continue; /* Not interested in full dumps */ while (isspace(*cp2)) ++cp2; tmp = ctime_to_timet( cp2 ); if ( tmp > dumpdate ) dumpdate=tmp; /* Remember the 'latest' partial dump */ } } } fclose(dump_fp); return date_n_time (&dumpdate, length);}#define RAW_DEVICE_PREFIX "/dev/rdsk"#define COOKED_DEVICE_PREFIX "/dev/dsk"char *cook_device(char *dev){ static char cooked_dev[MAXPATHLEN]; if ( !strncmp( dev, RAW_DEVICE_PREFIX, strlen(RAW_DEVICE_PREFIX))) { strcpy( cooked_dev, COOKED_DEVICE_PREFIX ); strcat( cooked_dev, dev+strlen(RAW_DEVICE_PREFIX) ); } else strcpy( cooked_dev, dev ); return( cooked_dev );}intGet_FSIndex(char *dev){ int iindex; Init_HR_FileSys(); while ((iindex=Get_Next_HR_FileSys()) != -1 ) if (!strcmp( HRFS_entry->HRFS_name, cook_device(dev))) { End_HR_FileSys(); return iindex; } End_HR_FileSys(); return 0;}longGet_FSSize(char *dev){ struct HRFS_statfs statfs_buf; Init_HR_FileSys(); while (Get_Next_HR_FileSys() != -1 ) if (!strcmp( HRFS_entry->HRFS_name, cook_device(dev))) { End_HR_FileSys(); if (HRFS_statfs( HRFS_entry->HRFS_mount, &statfs_buf) != -1 ) /* * with large file systems the following calculation produces * an overflow: * (statfs_buf.f_blocks*statfs_buf.f_bsize)/1024 * * assumption: f_bsize is either 512 or a multiple of 1024 * in case of 512 (f_blocks/2) is returned * otherwise (f_blocks*(f_bsize/1024)) is returned */ if (statfs_buf.f_bsize == 512) return (statfs_buf.f_blocks/2); else return (statfs_buf.f_blocks*statfs_buf.f_bsize)/1024; else return -1; } End_HR_FileSys(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -