ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/disk_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/disk_stats.c (file contents):
Revision 1.62 by ats, Mon Apr 5 18:10:11 2004 UTC vs.
Revision 1.70 by tdb, Sun Jun 20 20:32:24 2004 UTC

# Line 1 | Line 1
1   /*
2 < * i-scream central monitoring system
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4   * Copyright (C) 2000-2004 i-scream
5   *
# Line 47 | Line 47
47  
48   #ifdef LINUX
49   #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
50 <                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
51 <                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
52 <                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
53 <                        "umsdos", "vfat", "xfs", "jfs"}
50 >                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
51 >                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
52 >                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
53 >                        "umsdos", "vfat", "xfs", "jfs"}
54   #endif
55  
56   #ifdef CYGWIN
# Line 66 | Line 66
66   #include <sys/dkstat.h>
67   #include <devstat.h>
68   #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
69 <                        "ufs", "mfs"}
69 >                        "ufs", "mfs"}
70   #endif
71   #if defined(NETBSD) || defined(OPENBSD)
72   #include <sys/param.h>
73   #include <sys/sysctl.h>
74   #include <sys/disk.h>
75   #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
76 <                        "ntfs"}
76 >                        "ntfs"}
77   #endif
78  
79   static void disk_stat_init(sg_fs_stats *d) {
# Line 102 | Line 102 | static int is_valid_fs_type(const char *type) {
102  
103   sg_fs_stats *sg_get_fs_stats(int *entries){
104          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
105 <                              disk_stat_init, disk_stat_destroy);
105 >                              disk_stat_init, disk_stat_destroy);
106  
107          int valid_type;
108          int num_disks=0;
# Line 122 | Line 122 | sg_fs_stats *sg_get_fs_stats(int *entries){
122   #endif
123   #ifdef ALLBSD
124          int nummnt;
125 + #ifdef HAVE_STATVFS
126 +        struct statvfs *mp;
127 + #else
128          struct statfs *mp;
129   #endif
130 + #endif
131  
132   #ifdef ALLBSD
133          nummnt=getmntinfo(&mp , MNT_LOCAL);
134          if (nummnt<=0){
135 +                sg_set_error(SG_ERROR_GETMNTINFO, NULL);
136                  return NULL;
137          }
138          for(;nummnt--; mp++){
# Line 136 | Line 141 | sg_fs_stats *sg_get_fs_stats(int *entries){
141  
142   #if defined(LINUX) || defined(CYGWIN)
143          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
144 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
145                  return NULL;
146          }
147  
# Line 149 | Line 155 | sg_fs_stats *sg_get_fs_stats(int *entries){
155  
156   #ifdef SOLARIS
157          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
158 +                sg_set_error(SG_ERROR_OPEN, "/etc/mnttab");
159                  return NULL;
160          }
161          while((getmntent(f, &mp)) == 0){
# Line 216 | Line 223 | sg_fs_stats *sg_get_fs_stats(int *entries){
223                          }
224  
225                          if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
226 <                                return NULL;
227 <                        }
226 >                                return NULL;
227 >                        }
228          
229                          if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
230 <                                return NULL;
231 <                        }
230 >                                return NULL;
231 >                        }
232                          
233                          disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
234                          disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
# Line 238 | Line 245 | sg_fs_stats *sg_get_fs_stats(int *entries){
245          *entries=num_disks;    
246  
247          /* If this fails, there is very little i can do about it, so
248 <           I'll ignore it :) */
248 >           I'll ignore it :) */
249   #if defined(LINUX) || defined(CYGWIN)
250          endmntent(f);
251   #endif
# Line 250 | Line 257 | sg_fs_stats *sg_get_fs_stats(int *entries){
257  
258   }
259  
260 + int sg_fs_compare_device_name(const void *va, const void *vb) {
261 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
262 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
263 +
264 +        return strcmp(a->device_name, b->device_name);
265 + }
266 +
267 + int sg_fs_compare_mnt_point(const void *va, const void *vb) {
268 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
269 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
270 +
271 +        return strcmp(a->mnt_point, b->mnt_point);
272 + }
273 +
274   static void diskio_stat_init(sg_disk_io_stats *d) {
275          d->disk_name = NULL;
276   }
# Line 259 | Line 280 | static void diskio_stat_destroy(sg_disk_io_stats *d) {
280   }
281  
282   VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
283 <                      diskio_stat_init, diskio_stat_destroy);
283 >                      diskio_stat_init, diskio_stat_destroy);
284  
285   #ifdef LINUX
286   typedef struct {
# Line 275 | Line 296 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
296   #endif
297  
298   #ifdef SOLARIS
299 <        kstat_ctl_t *kc;
300 <        kstat_t *ksp;
299 >        kstat_ctl_t *kc;
300 >        kstat_t *ksp;
301          kstat_io_t kios;
302   #endif
303   #ifdef LINUX
# Line 327 | Line 348 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
348  
349          size = sizeof(diskcount);
350          if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
351 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
352                  return NULL;
353          }
354  
# Line 334 | Line 356 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
356          mib[1] = HW_DISKNAMES;
357  
358          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
359 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
360                  return NULL;
361          }
362  
363 <        disknames = malloc(size);
363 >        disknames = sg_malloc(size);
364          if (disknames == NULL) {
365                  return NULL;
366          }
367  
368          if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
369 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
370                  return NULL;
371          }
372  
373 <        dk_name = calloc(diskcount, sizeof(char *));
373 >        dk_name = sg_malloc(diskcount * sizeof(char *));
374          bufpp = disknames;
375          for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
376                  dk_name[i] = name;
# Line 361 | Line 385 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
385   #endif
386  
387          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
388 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
389                  return NULL;
390          }
391  
# Line 370 | Line 395 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
395          num_disks = size / sizeof(struct diskstats);
396   #endif
397  
398 <        stats = malloc(size);
398 >        stats = sg_malloc(size);
399          if (stats == NULL) {
400                  return NULL;
401          }
402  
403          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
404 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
405                  return NULL;
406          }
407  
# Line 388 | Line 414 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
414                  rbytes = stats[i].dk_rbytes;
415                  wbytes = stats[i].dk_wbytes;
416   #else
417 <                /* Before 1.7, NetBSD merged reads and writes. */
417 >                /* Before 2.0, NetBSD merged reads and writes. */
418                  rbytes = wbytes = stats[i].dk_bytes;
419   #endif
420   #else
421 + #ifdef HAVE_DS_RBYTES
422 +                rbytes = stats[i].ds_rbytes;
423 +                wbytes = stats[i].ds_wbytes;
424 + #else
425 +                /* Before 3.5, OpenBSD merged reads and writes */
426                  rbytes = wbytes = stats[i].ds_bytes;
427   #endif
428 + #endif
429  
430                  /* Don't keep stats for disks that have never been used. */
431                  if (rbytes == 0 && wbytes == 0) {
# Line 429 | Line 461 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
461  
462   #if defined(FREEBSD) || defined(DFBSD)
463          if (!stats_init) {
464 <                stats.dinfo=malloc(sizeof(struct devinfo));
464 >                stats.dinfo=sg_malloc(sizeof(struct devinfo));
465                  if(stats.dinfo==NULL) return NULL;
466                  bzero(stats.dinfo, sizeof(struct devinfo));
467                  stats_init = 1;
468          }
469   #ifdef FREEBSD5
470 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
470 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
471 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
472 >                return NULL;
473 >        }
474          /* Not aware of a get all devices, so i said 999. If we ever
475           * find a machine with more than 999 disks, then i'll change
476           * this number :)
477           */
478 <        if (devstat_selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL;
478 >        if (devstat_selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) {
479 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
480 >                return NULL;
481 >        }
482   #else
483 <        if ((getdevs(&stats)) < 0) return NULL;
483 >        if ((getdevs(&stats)) < 0) {
484 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
485 >                return NULL;
486 >        }
487          /* Not aware of a get all devices, so i said 999. If we ever
488           * find a machine with more than 999 disks, then i'll change
489           * this number :)
490           */
491 <        if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL;
491 >        if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) {
492 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
493 >                return NULL;
494 >        }
495   #endif
496  
497          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 477 | Line 521 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
521   #endif
522                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
523                  if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
524 +                        sg_set_error(SG_ERROR_ASPRINTF, NULL);
525                          return NULL;
526                  }
527                  diskio_stats_ptr->systime=time(NULL);
# Line 488 | Line 533 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
533   #endif
534   #ifdef SOLARIS
535          if ((kc = kstat_open()) == NULL) {
536 <                return NULL;
537 <        }
536 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
537 >                return NULL;
538 >        }
539  
540          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
541 <                if (!strcmp(ksp->ks_class, "disk")) {
541 >                if (!strcmp(ksp->ks_class, "disk")) {
542  
543                          if(ksp->ks_type != KSTAT_TYPE_IO) continue;
544                          /* We dont want metadevices appearins as num_diskio */
545                          if(strcmp(ksp->ks_module, "md")==0) continue;
546 <                        if((kstat_read(kc, ksp, &kios))==-1){  
546 >                        if((kstat_read(kc, ksp, &kios))==-1){  
547                          }
548                          
549                          if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
# Line 509 | Line 555 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
555                          diskio_stats_ptr->read_bytes=kios.nread;
556                          diskio_stats_ptr->write_bytes=kios.nwritten;
557                          if (sg_update_string(&diskio_stats_ptr->disk_name,
558 <                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
558 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
559                                  return NULL;
560                          }
561                          diskio_stats_ptr->systime=time(NULL);
# Line 656 | Line 702 | out:
702   #endif
703  
704   #ifdef CYGWIN
705 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
706          return NULL;
707   #endif
708  
# Line 666 | Line 713 | out:
713  
714   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
715          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
716 <                              diskio_stat_init, diskio_stat_destroy);
716 >                              diskio_stat_init, diskio_stat_destroy);
717          sg_disk_io_stats *src = NULL, *dest;
718          int i, j, diff_count, new_count;
719  
# Line 726 | Line 773 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
773  
774          *entries = diff_count;
775          return diff;
776 + }
777 +
778 + int sg_disk_io_compare_name(const void *va, const void *vb) {
779 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
780 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
781 +
782 +        return strcmp(a->disk_name, b->disk_name);
783   }
784  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines