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.75 by ats, Sat Nov 6 15:32:33 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 37 | Line 37
37   #include <sys/mnttab.h>
38   #include <sys/statvfs.h>
39   #include <kstat.h>
40 < #define VALID_FS_TYPES {"ufs", "tmpfs"}
40 > #define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs"}
41   #endif
42  
43   #if defined(LINUX) || defined(CYGWIN)
# 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 + #ifdef HPUX
80 + #include <sys/param.h>
81 + #include <sys/pstat.h>
82 + #include <sys/types.h>
83 + #include <sys/stat.h>
84 + #include <sys/vfs.h>
85 + #include <mntent.h>
86 + #include <dirent.h>
87 + #include <stdio.h>
88 + #include <time.h>
89 + #define VALID_FS_TYPES {"vxfs", "hfs"}
90 + #define DISK_BATCH 30
91 + #endif
92 +
93   static void disk_stat_init(sg_fs_stats *d) {
94          d->device_name = NULL;
95          d->fs_type = NULL;
# Line 102 | Line 116 | static int is_valid_fs_type(const char *type) {
116  
117   sg_fs_stats *sg_get_fs_stats(int *entries){
118          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
119 <                              disk_stat_init, disk_stat_destroy);
119 >                              disk_stat_init, disk_stat_destroy);
120  
121          int valid_type;
122          int num_disks=0;
123 < #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
123 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX)
124          FILE *f;
125   #endif
126  
# Line 116 | Line 130 | sg_fs_stats *sg_get_fs_stats(int *entries){
130          struct mnttab mp;
131          struct statvfs fs;
132   #endif
133 < #if defined(LINUX) || defined(CYGWIN)
133 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
134          struct mntent *mp;
135          struct statfs fs;
136   #endif
137   #ifdef ALLBSD
138          int nummnt;
139 + #ifdef HAVE_STATVFS
140 +        struct statvfs *mp;
141 + #else
142          struct statfs *mp;
143   #endif
144 + #endif
145  
146   #ifdef ALLBSD
147          nummnt=getmntinfo(&mp , MNT_LOCAL);
148          if (nummnt<=0){
149 +                sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL);
150                  return NULL;
151          }
152          for(;nummnt--; mp++){
153                  valid_type = is_valid_fs_type(mp->f_fstypename);
154   #endif
155  
156 < #if defined(LINUX) || defined(CYGWIN)
156 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
157 > #ifdef MNT_MNTTAB
158 >        if ((f=setmntent(MNT_MNTTAB, "r" ))==NULL){
159 > #else
160          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
161 + #endif
162 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
163                  return NULL;
164          }
165  
# Line 149 | Line 173 | sg_fs_stats *sg_get_fs_stats(int *entries){
173  
174   #ifdef SOLARIS
175          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
176 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/etc/mnttab");
177                  return NULL;
178          }
179          while((getmntent(f, &mp)) == 0){
# Line 184 | Line 209 | sg_fs_stats *sg_get_fs_stats(int *entries){
209                          /* Freebsd doesn't have a "available" inodes */
210                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
211   #endif
212 < #if defined(LINUX) || defined(CYGWIN)
212 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
213                          if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
214                                  return NULL;
215                          }
# Line 216 | Line 241 | sg_fs_stats *sg_get_fs_stats(int *entries){
241                          }
242  
243                          if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
244 <                                return NULL;
245 <                        }
244 >                                return NULL;
245 >                        }
246          
247                          if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
248 <                                return NULL;
249 <                        }
248 >                                return NULL;
249 >                        }
250                          
251                          disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
252                          disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
# Line 238 | Line 263 | sg_fs_stats *sg_get_fs_stats(int *entries){
263          *entries=num_disks;    
264  
265          /* If this fails, there is very little i can do about it, so
266 <           I'll ignore it :) */
267 < #if defined(LINUX) || defined(CYGWIN)
266 >           I'll ignore it :) */
267 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
268          endmntent(f);
269   #endif
270   #if defined(SOLARIS)
# Line 250 | Line 275 | sg_fs_stats *sg_get_fs_stats(int *entries){
275  
276   }
277  
278 + int sg_fs_compare_device_name(const void *va, const void *vb) {
279 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
280 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
281 +
282 +        return strcmp(a->device_name, b->device_name);
283 + }
284 +
285 + int sg_fs_compare_mnt_point(const void *va, const void *vb) {
286 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
287 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
288 +
289 +        return strcmp(a->mnt_point, b->mnt_point);
290 + }
291 +
292   static void diskio_stat_init(sg_disk_io_stats *d) {
293          d->disk_name = NULL;
294   }
# Line 259 | Line 298 | static void diskio_stat_destroy(sg_disk_io_stats *d) {
298   }
299  
300   VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
301 <                      diskio_stat_init, diskio_stat_destroy);
301 >                      diskio_stat_init, diskio_stat_destroy);
302  
303   #ifdef LINUX
304   typedef struct {
# Line 274 | Line 313 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
313          sg_disk_io_stats *diskio_stats_ptr;
314   #endif
315  
316 + #ifdef HPUX
317 +        long long rbytes = 0, wbytes = 0;
318 +        struct dirent *dinfo = NULL;
319 +        struct stat lstatinfo;
320 +        struct pst_diskinfo pstat_diskinfo[DISK_BATCH];
321 +        char fullpathbuf[1024] = {0};
322 +        dev_t diskid;
323 +        DIR *dh = NULL;
324 +        int diskidx = 0;
325 +        int num, i;
326 + #endif
327   #ifdef SOLARIS
328 <        kstat_ctl_t *kc;
329 <        kstat_t *ksp;
328 >        kstat_ctl_t *kc;
329 >        kstat_t *ksp;
330          kstat_io_t kios;
331   #endif
332   #ifdef LINUX
# Line 321 | Line 371 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
371  
372          num_diskio=0;
373  
374 + #ifdef HPUX
375 +        while (1) {
376 +                num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
377 +                                    DISK_BATCH, diskidx);
378 +                if (num == -1) {
379 +                        sg_set_error_with_errno(SG_ERROR_PSTAT,
380 +                                                "pstat_getdisk");
381 +                        return NULL;
382 +                } else if (num == 0) {
383 +                        break;
384 +                }
385 +
386 +                for (i = 0; i < num; i++) {
387 +                        struct pst_diskinfo *di = &pstat_diskinfo[i];
388 +
389 +                        /* Skip "disabled" disks. */
390 +                        if (di->psd_status == 0) {
391 +                                continue;
392 +                        }
393 +        
394 +                        /* We can't seperate the reads from the writes, we'll
395 +                           just give the same to each. */
396 +                        rbytes = wbytes = (di->psd_dkwds * 64);
397 +        
398 +                        /* Skip unused disks. */
399 +                        if (rbytes == 0 && wbytes == 0) {
400 +                                continue;
401 +                        }
402 +        
403 +                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
404 +                                return NULL;
405 +                        }
406 +        
407 +                        diskio_stats_ptr = diskio_stats + num_diskio;
408 +        
409 +                        diskio_stats_ptr->read_bytes = rbytes;
410 +                        diskio_stats_ptr->write_bytes = wbytes;
411 +        
412 +                        diskio_stats_ptr->systime = time(NULL);
413 +        
414 +                        num_diskio++;
415 +        
416 +                        if (diskio_stats_ptr->disk_name == NULL) {
417 +                                dh = opendir("/dev/dsk");
418 +                                if (dh == NULL) {
419 +                                        continue;
420 +                                }
421 +        
422 +                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
423 +                                while (1) {
424 +                                        dinfo = readdir(dh);
425 +                                        if (dinfo == NULL) {
426 +                                                break;
427 +                                        }
428 +                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
429 +                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
430 +                                                continue;
431 +                                        }
432 +        
433 +                                        if (lstatinfo.st_rdev == diskid) {
434 +                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
435 +                                                        return NULL;
436 +                                                }
437 +                                                break;
438 +                                        }
439 +                                }
440 +                                closedir(dh);
441 +        
442 +                                if (diskio_stats_ptr->disk_name == NULL) {
443 +                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
444 +                                                return NULL;
445 +                                        }
446 +                                }
447 +                        }
448 +                }
449 +                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
450 +        }
451 + #endif
452   #ifdef OPENBSD
453          mib[0] = CTL_HW;
454          mib[1] = HW_DISKCOUNT;
455  
456          size = sizeof(diskcount);
457          if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
458 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
459                  return NULL;
460          }
461  
# Line 334 | Line 463 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
463          mib[1] = HW_DISKNAMES;
464  
465          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
466 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
467                  return NULL;
468          }
469  
470 <        disknames = malloc(size);
470 >        disknames = sg_malloc(size);
471          if (disknames == NULL) {
472                  return NULL;
473          }
474  
475          if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
476 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
477                  return NULL;
478          }
479  
480 <        dk_name = calloc(diskcount, sizeof(char *));
480 >        dk_name = sg_malloc(diskcount * sizeof(char *));
481          bufpp = disknames;
482          for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
483                  dk_name[i] = name;
# Line 361 | Line 492 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
492   #endif
493  
494          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
495 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
496                  return NULL;
497          }
498  
# Line 370 | Line 502 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
502          num_disks = size / sizeof(struct diskstats);
503   #endif
504  
505 <        stats = malloc(size);
505 >        stats = sg_malloc(size);
506          if (stats == NULL) {
507                  return NULL;
508          }
509  
510          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
511 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
512                  return NULL;
513          }
514  
# Line 388 | Line 521 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
521                  rbytes = stats[i].dk_rbytes;
522                  wbytes = stats[i].dk_wbytes;
523   #else
524 <                /* Before 1.7, NetBSD merged reads and writes. */
524 >                /* Before 2.0, NetBSD merged reads and writes. */
525                  rbytes = wbytes = stats[i].dk_bytes;
526   #endif
527   #else
528 + #ifdef HAVE_DS_RBYTES
529 +                rbytes = stats[i].ds_rbytes;
530 +                wbytes = stats[i].ds_wbytes;
531 + #else
532 +                /* Before 3.5, OpenBSD merged reads and writes */
533                  rbytes = wbytes = stats[i].ds_bytes;
534   #endif
535 + #endif
536  
537                  /* Don't keep stats for disks that have never been used. */
538                  if (rbytes == 0 && wbytes == 0) {
# Line 429 | Line 568 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
568  
569   #if defined(FREEBSD) || defined(DFBSD)
570          if (!stats_init) {
571 <                stats.dinfo=malloc(sizeof(struct devinfo));
571 >                stats.dinfo=sg_malloc(sizeof(struct devinfo));
572                  if(stats.dinfo==NULL) return NULL;
573                  bzero(stats.dinfo, sizeof(struct devinfo));
574                  stats_init = 1;
575          }
576   #ifdef FREEBSD5
577 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
577 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
578 >                /* FIXME devstat functions return a string error in
579 >                   devstat_errbuf */
580 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
581 >                return NULL;
582 >        }
583          /* Not aware of a get all devices, so i said 999. If we ever
584           * find a machine with more than 999 disks, then i'll change
585           * this number :)
586           */
587 <        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;
587 >        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) {
588 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
589 >                return NULL;
590 >        }
591   #else
592 <        if ((getdevs(&stats)) < 0) return NULL;
592 >        if ((getdevs(&stats)) < 0) {
593 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
594 >                return NULL;
595 >        }
596          /* Not aware of a get all devices, so i said 999. If we ever
597           * find a machine with more than 999 disks, then i'll change
598           * this number :)
599           */
600 <        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;
600 >        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) {
601 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
602 >                return NULL;
603 >        }
604   #endif
605  
606          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 477 | Line 630 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
630   #endif
631                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
632                  if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
633 +                        sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL);
634                          return NULL;
635                  }
636                  diskio_stats_ptr->systime=time(NULL);
# Line 488 | Line 642 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
642   #endif
643   #ifdef SOLARIS
644          if ((kc = kstat_open()) == NULL) {
645 <                return NULL;
646 <        }
645 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
646 >                return NULL;
647 >        }
648  
649          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
650 <                if (!strcmp(ksp->ks_class, "disk")) {
650 >                if (!strcmp(ksp->ks_class, "disk")) {
651  
652                          if(ksp->ks_type != KSTAT_TYPE_IO) continue;
653                          /* We dont want metadevices appearins as num_diskio */
654                          if(strcmp(ksp->ks_module, "md")==0) continue;
655 <                        if((kstat_read(kc, ksp, &kios))==-1){  
655 >                        if((kstat_read(kc, ksp, &kios))==-1){  
656                          }
657                          
658                          if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
# Line 509 | Line 664 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
664                          diskio_stats_ptr->read_bytes=kios.nread;
665                          diskio_stats_ptr->write_bytes=kios.nwritten;
666                          if (sg_update_string(&diskio_stats_ptr->disk_name,
667 <                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
667 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
668                                  return NULL;
669                          }
670                          diskio_stats_ptr->systime=time(NULL);
# Line 656 | Line 811 | out:
811   #endif
812  
813   #ifdef CYGWIN
814 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
815          return NULL;
816   #endif
817  
# Line 666 | Line 822 | out:
822  
823   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
824          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
825 <                              diskio_stat_init, diskio_stat_destroy);
825 >                              diskio_stat_init, diskio_stat_destroy);
826          sg_disk_io_stats *src = NULL, *dest;
827          int i, j, diff_count, new_count;
828  
# Line 726 | Line 882 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
882  
883          *entries = diff_count;
884          return diff;
885 + }
886 +
887 + int sg_disk_io_compare_name(const void *va, const void *vb) {
888 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
889 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
890 +
891 +        return strcmp(a->disk_name, b->disk_name);
892   }
893  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines