⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 disk.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 2 页
字号:
   * to the caller    */#if HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS   #if HAVE_GETMNTENT#if HAVE_SETMNTENT  mntfp = setmntent(ETC_MNTTAB, "r");  while (NULL != (mntent = getmntent(mntfp))) {    add_device(mntent->mnt_dir, mntent->mnt_fsname, -1, minpercent, 0);    dummy = 1;  }  if (mntfp)    endmntent(mntfp);  if(dummy != 0) {    /*     * dummy clause for else below     */  }#else                           /* getmentent but not setmntent */  mntfp = fopen(ETC_MNTTAB, "r");  while ((i = getmntent(mntfp, &mnttab)) == 0) {    add_device(mnttab.mnt_mountp, mnttab.mnt_special, -1, minpercent, 0);    dummy = 1;  }  fclose(mntfp);  if(dummy != 0) {    /*     * dummy clause for else below     */  }#endif /* HAVE_SETMNTENT */#elif HAVE_FSTAB_H  setfsent();			/* open /etc/fstab */  while((fstab1 = getfsent()) != NULL) {    add_device(fstab1->fs_file, fstab1->fs_spec, -1, minpercent, 0);    dummy = 1;  }  endfsent();			/* close /etc/fstab */  if(dummy != 0) {    /*     * dummy clause for else below     */  }#elif HAVE_STATFS  /*   * since there is no way to get all the mounted systems with just   * statfs we default to the root partition "/"   */  if (statfs("/", &statf) == 0) {    add_device("/", statf.f_mntfromname, -1, minpercent, 0);  }#endif  else {    if (numdisks == MAXDISKS) {      return;    }    snprintf(tmpbuf, sizeof(tmpbuf),             "Couldn't find device for disk %s",             disks[numdisks].path);    tmpbuf[ sizeof(tmpbuf)-1 ] = 0;    config_pwarn(tmpbuf);    disks[numdisks].minimumspace = -1;    disks[numdisks].minpercent = -1;    disks[numdisks].path[0] = 0;  }#else  config_perror("'disk' checks not supported on this architecture.");#endif                   /* HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS */   }static u_char *find_device(char *path){#if HAVE_GETMNTENT#if HAVE_SYS_MNTTAB_H  struct mnttab   mnttab;#else  struct mntent  *mntent;#endif  FILE           *mntfp;#elif HAVE_FSTAB_H  struct fstab   *fstab;  struct stat     stat1;#elif HAVE_STATFS  struct statfs   statf;#endif  char            tmpbuf[1024];  static char     device[STRMAX];#if defined(HAVE_GETMNTENT) && !defined(HAVE_SETMNTENT)  int             i;#endif  device[0] = '\0';		/* null terminate the device */  /* find the device for the path and copy the device into the   * string declared above and at the end of the routine return it   * to the caller    */#if HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS   #if HAVE_GETMNTENT#if HAVE_SETMNTENT  mntfp = setmntent(ETC_MNTTAB, "r");  while (NULL != (mntent = getmntent(mntfp)))    if (strcmp(path, mntent->mnt_dir) == 0) {      copy_nword(mntent->mnt_fsname, device,		 sizeof(device));      DEBUGMSGTL(("ucd-snmp/disk", "Disk:  %s\n",		  mntent->mnt_fsname));      break;    } else {      DEBUGMSGTL(("ucd-snmp/disk", "  %s != %s\n",		  path, mntent->mnt_dir));    }  if (mntfp)    endmntent(mntfp);#else                           /* getmentent but not setmntent */  mntfp = fopen(ETC_MNTTAB, "r");  while ((i = getmntent(mntfp, &mnttab)) == 0)    if (strcmp(path, mnttab.mnt_mountp) == 0)      break;    else {      DEBUGMSGTL(("ucd-snmp/disk", "  %s != %s\n",		  path, mnttab.mnt_mountp));    }  fclose(mntfp);  if (i == 0) {    copy_nword(mnttab.mnt_special, device,	       sizeof(device));  }#endif /* HAVE_SETMNTENT */#elif HAVE_FSTAB_H  stat(path, &stat1);  setfsent();  if ((fstab = getfsfile(path))) {    copy_nword(fstab->fs_spec, device,	       sizeof(device));  }  endfsent();  if (device[0] != '\0') {     /*      * dummy clause for else below      */   }#elif HAVE_STATFS  if (statfs(path, &statf) == 0) {    copy_word(statf.f_mntfromname, device);    DEBUGMSGTL(("ucd-snmp/disk", "Disk:  %s\n",		statf.f_mntfromname));  }#endif  else {    sprintf(tmpbuf, "Couldn't find device for disk %s",	    path);    config_pwarn(tmpbuf);  }#else  config_perror("'disk' checks not supported on this architecture.");#endif                   /* HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS */    return device;}/* * var_extensible_disk(... * Arguments: * vp     IN      - pointer to variable entry that points here * name    IN/OUT  - IN/name requested, OUT/name found * length  IN/OUT  - length of IN/OUT oid's  * exact   IN      - TRUE if an exact match was requested * var_len OUT     - length of variable or 0 if function returned * write_method *  */u_char         *var_extensible_disk(struct variable *vp,                    oid * name,                    size_t * length,                    int exact,                    size_t * var_len, WriteMethod ** write_method){    int             percent, iserror, disknum = 0;#if !defined(HAVE_SYS_STATVFS_H) && !defined(HAVE_STATFS)    double          totalblks, free, used, avail, availblks;#else    static long     avail;#if defined(STRUCT_STATVFS_HAS_F_FILES) || defined(STRUCT_STATFS_HAS_F_FILES)    int             percent_inode;#endif#endif    static long     long_ret;    static char     errmsg[300];    float           multiplier;#if defined(HAVE_STATVFS) || defined(HAVE_STATFS)#ifdef STAT_STATFS_FS_DATA    struct fs_data  fsd;    struct {        u_int           f_blocks, f_bfree, f_bavail, f_bsize;    } vfs;#else    struct statvfs  vfs;#endif#else#if HAVE_FSTAB_H    int             file;    union {        struct fs       iu_fs;        char            dummy[SBSIZE];    } sb;#define filesys sb.iu_fs#endif#endif    if (header_simple_table        (vp, name, length, exact, var_len, write_method, numdisks))        return (NULL);    disknum = name[*length - 1] - 1;    switch (vp->magic) {    case MIBINDEX:        long_ret = disknum + 1;        return ((u_char *) (&long_ret));    case ERRORNAME:            /* DISKPATH */        *var_len = strlen(disks[disknum].path);        return ((u_char *) disks[disknum].path);    case DISKDEVICE:        *var_len = strlen(disks[disknum].device);        return ((u_char *) disks[disknum].device);    case DISKMINIMUM:        long_ret = disks[disknum].minimumspace;        return ((u_char *) (&long_ret));    case DISKMINPERCENT:        long_ret = disks[disknum].minpercent;        return ((u_char *) (&long_ret));    }#if defined(HAVE_STATVFS) || defined(HAVE_STATFS)#ifdef STAT_STATFS_FS_DATA    if (statvfs(disks[disknum].path, &fsd) == -1)#else    if (statvfs(disks[disknum].path, &vfs) == -1)#endif    {        snmp_log(LOG_ERR, "Couldn't open device %s\n",                 disks[disknum].device);        setPerrorstatus("statvfs dev/disk");        return NULL;    }#ifdef STAT_STATFS_FS_DATA    vfs.f_blocks = fsd.fd_btot;    vfs.f_bfree = fsd.fd_bfree;    vfs.f_bavail = fsd.fd_bfreen;    vfs.f_bsize = 1024;         /*  Ultrix f_bsize is a VM parameter apparently.  */#endif#if defined(HAVE_ODS)    vfs.f_blocks = vfs.f_spare[0];    vfs.f_bfree = vfs.f_spare[1];    vfs.f_bavail = vfs.f_spare[2];#endif    percent = vfs.f_bavail <= 0 ? 100 :        (int) ((double) (vfs.f_blocks - vfs.f_bfree) /               (double) (vfs.f_blocks -                         (vfs.f_bfree - vfs.f_bavail)) * 100.0 + 0.5);    multiplier = (float)vfs.f_bsize / (float)1024.0;#ifdef STRUCT_STATVFS_HAS_F_FRSIZE    if (vfs.f_frsize > 255)        multiplier = (float)vfs.f_frsize / (float)1024.0;#endif    avail = (long)(vfs.f_bavail * multiplier);    iserror = (disks[disknum].minimumspace >= 0 ?               avail < disks[disknum].minimumspace :               100 - percent <= disks[disknum].minpercent) ? 1 : 0;#if defined(STRUCT_STATVFS_HAS_F_FILES) || defined STRUCT_STATFS_HAS_F_FAVAIL    percent_inode = vfs.f_favail <= 0 ? 100 :        (int) ((double) (vfs.f_files - vfs.f_ffree) /               (double) (vfs.f_files -                         (vfs.f_ffree - vfs.f_favail)) * 100.0 + 0.5);#else#if defined(STRUCT_STATFS_HAS_F_FILES) && defined(STRUCT_STATFS_HAS_F_FFREE)   percent_inode = vfs.f_files == 0 ? 100.0 :      (int) ((double) (vfs.f_files - vfs.f_ffree) /	          (double) (vfs.f_files) * 100.0 + 0.5);#endif #endif /* defined(STRUCT_STATVFS_HAS_F_FILES) */     switch (vp->magic) {    case DISKTOTAL:        long_ret = (long)(vfs.f_blocks * multiplier);        return ((u_char *) (&long_ret));    case DISKAVAIL:        return ((u_char *) (&avail));    case DISKUSED:        long_ret = (long)((vfs.f_blocks - vfs.f_bfree) * multiplier);        return ((u_char *) (&long_ret));    case DISKPERCENT:        long_ret = percent;        return ((u_char *) (&long_ret));#if defined(STRUCT_STATVFS_HAS_F_FILES) || defined (STRUCT_STATFS_HAS_F_FILES)    case DISKPERCENTNODE:        long_ret = percent_inode;        return ((u_char *) (&long_ret));#endif    case ERRORFLAG:        long_ret = iserror;        return ((u_char *) (&long_ret));    case ERRORMSG:        if (iserror) {            if (disks[disknum].minimumspace >= 0)                snprintf(errmsg, sizeof(errmsg),                        "%s: less than %d free (= %d)",                        disks[disknum].path, disks[disknum].minimumspace,                        (int) avail);            else                snprintf(errmsg, sizeof(errmsg),                        "%s: less than %d%% free (= %d%%)",                        disks[disknum].path, disks[disknum].minpercent,                        percent);            errmsg[ sizeof(errmsg)-1 ] = 0;        } else            errmsg[0] = 0;        *var_len = strlen(errmsg);        return ((u_char *) (errmsg));    }#else#if HAVE_FSTAB_H    /*     * read the disk information      */    if ((file = open(disks[disknum].device, 0)) < 0) {        snmp_log(LOG_ERR, "Couldn't open device %s\n",                 disks[disknum].device);        setPerrorstatus("open dev/disk");        return (NULL);    }    lseek(file, (long) (SBLOCK * DEV_BSIZE), 0);    if (read(file, (char *) &filesys, SBSIZE) != SBSIZE) {        setPerrorstatus("open dev/disk");        snmp_log(LOG_ERR, "Error reading device %s\n",                 disks[disknum].device);        close(file);        return (NULL);    }    close(file);    totalblks = filesys.fs_dsize;    free = filesys.fs_cstotal.cs_nbfree * filesys.fs_frag +        filesys.fs_cstotal.cs_nffree;    used = totalblks - free;    availblks = totalblks * (100 - filesys.fs_minfree) / 100;    avail = availblks > used ? availblks - used : 0;    percent =        availblks ==        0 ? 100 : (int) ((double) used / (double) totalblks * 100.0 + 0.5);    multiplier = (float)filesys.fs_fsize / (float)1024.0;    iserror =        (disks[disknum].minimumspace >= 0            ? avail * multiplier < disks[disknum].minimumspace            : 100 - percent <= disks[disknum].minpercent) ? 1 : 0;    switch (vp->magic) {    case DISKTOTAL:        long_ret = (long)(totalblks * multiplier);        return ((u_char *) (&long_ret));    case DISKAVAIL:        long_ret = (long)(avail * multiplier);        return ((u_char *) (&long_ret));    case DISKUSED:        long_ret = (long)(used * multiplier);        return ((u_char *) (&long_ret));    case DISKPERCENT:        long_ret = percent;        return ((u_char *) (&long_ret));    case ERRORFLAG:        long_ret = iserror;        return ((u_char *) (&long_ret));    case ERRORMSG:        if (iserror) {            if (disks[disknum].minimumspace >= 0)                snprintf(errmsg, sizeof(errmsg),                        "%s: less than %d free (= %d)",                        disks[disknum].path, disks[disknum].minimumspace,                        avail * filesys.fs_fsize / 1024);            else                snprintf(errmsg, sizeof(errmsg),                        "%s: less than %d%% free (= %d%%)",                        disks[disknum].path, disks[disknum].minpercent,                        percent);            errmsg[ sizeof(errmsg)-1 ] = 0;        } else            errmsg[0] = 0;        *var_len = strlen(errmsg);        return ((u_char *) (errmsg));    }#endif#endif    return NULL;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -