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.28 by pajs, Thu Oct 9 15:22:59 2003 UTC vs.
Revision 1.48 by tdb, Fri Jan 16 15:54:54 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 > * 02111-1307 USA
20   */
21  
22   #ifdef HAVE_CONFIG_H
# Line 25 | Line 26
26   #include <stdio.h>
27   #include <stdlib.h>
28   #include <string.h>
29 + #include <time.h>
30   #include "statgrab.h"
31  
32   #ifdef SOLARIS
# Line 34 | Line 36
36   #define VALID_FS_TYPES {"ufs", "tmpfs"}
37   #endif
38  
39 < #ifdef LINUX
38 < #include <sys/vfs.h>
39 > #if defined(LINUX) || defined(CYGWIN)
40   #include <mntent.h>
41 + #include <sys/vfs.h>
42   #include "tools.h"
41 #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
43   #endif
44  
45 < #ifdef FREEBSD
45 > #ifdef LINUX
46 > #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
47 >                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
48 >                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
49 >                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
50 >                        "umsdos", "vfat", "xfs", "jfs"}
51 > #endif
52 >
53 > #ifdef CYGWIN
54 > #define VALID_FS_TYPES {"user"}
55 > #endif
56 >
57 > #ifdef ALLBSD
58   #include <sys/param.h>
59   #include <sys/ucred.h>
60   #include <sys/mount.h>
61 + #endif
62 + #ifdef FREEBSD
63   #include <sys/dkstat.h>
64   #include <devstat.h>
65 < #define VALID_FS_TYPES {"ufs", "mfs"}
65 > #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
66 >                        "ufs", "mfs"}
67   #endif
68 + #ifdef NETBSD
69 + #include <sys/param.h>
70 + #include <sys/sysctl.h>
71 + #include <sys/disk.h>
72 + #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
73 +                        "ntfs"}
74 + #endif
75 +
76   #define START_VAL 1
77  
78   char *copy_string(char *orig_ptr, const char *newtext){
# Line 75 | Line 99 | void init_disk_stat(int start, int end, disk_stat_t *d
99          }
100   }
101  
102 + int is_valid_fs_type(const char *type) {
103 +        const char *types[] = VALID_FS_TYPES;
104 +        int i;
105 +
106 +        for (i = 0; i < (sizeof types / sizeof *types); i++) {
107 +                if (strcmp(types[i], type) == 0) {
108 +                        return 1;
109 +                }
110 +        }
111 +        return 0;
112 + }
113 +
114   disk_stat_t *get_disk_stats(int *entries){
115  
116          static disk_stat_t *disk_stats;
117          static int watermark=-1;
118  
119 <        char *fs_types[] = VALID_FS_TYPES;
84 <        int x, valid_type;
119 >        int valid_type;
120          int num_disks=0;
121 < #if defined(LINUX) || defined (SOLARIS)
121 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
122          FILE *f;
123   #endif
124  
# Line 93 | Line 128 | disk_stat_t *get_disk_stats(int *entries){
128          struct mnttab mp;
129          struct statvfs fs;
130   #endif
131 < #ifdef LINUX
131 > #if defined(LINUX) || defined(CYGWIN)
132          struct mntent *mp;
133          struct statfs fs;
134   #endif
135 < #ifdef FREEBSD
135 > #ifdef ALLBSD
136          int nummnt;
137          struct statfs *mp;
138   #endif
# Line 110 | Line 145 | disk_stat_t *get_disk_stats(int *entries){
145                  watermark=START_VAL;
146                  init_disk_stat(0, watermark-1, disk_stats);
147          }
148 < #ifdef FREEBSD
148 > #ifdef ALLBSD
149          nummnt=getmntinfo(&mp , MNT_LOCAL);
150          if (nummnt<=0){
151                  return NULL;
152          }
153          for(;nummnt--; mp++){
154 <                valid_type=0;
120 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
121 <                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
122 <                                valid_type=1;
123 <                                break;
124 <                        }
125 <                }
154 >                valid_type = is_valid_fs_type(mp->f_fstypename);
155   #endif
156  
157 < #ifdef LINUX
157 > #if defined(LINUX) || defined(CYGWIN)
158          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
159                  return NULL;
160          }
# Line 135 | Line 164 | disk_stat_t *get_disk_stats(int *entries){
164                          continue;
165                  }      
166  
167 <                valid_type=0;
139 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
140 <                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
141 <                                valid_type=1;
142 <                                break;
143 <                        }
144 <                }
167 >                valid_type = is_valid_fs_type(mp->mnt_type);
168   #endif
169  
170   #ifdef SOLARIS
# Line 152 | Line 175 | disk_stat_t *get_disk_stats(int *entries){
175                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
176                          continue;
177                  }
178 <                valid_type=0;
156 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
157 <                        if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
158 <                                valid_type=1;
159 <                                break;
160 <                        }
161 <                }
178 >                valid_type = is_valid_fs_type(mp.mnt_fstype);
179   #endif
180  
181                  if(valid_type){
# Line 174 | Line 191 | disk_stat_t *get_disk_stats(int *entries){
191                          }
192  
193                          disk_ptr=disk_stats+num_disks;
194 < #ifdef FREEBSD
194 > #ifdef ALLBSD
195                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
196                                  return NULL;
197                          }
# Line 196 | Line 213 | disk_stat_t *get_disk_stats(int *entries){
213                          /* Freebsd doesn't have a "available" inodes */
214                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
215   #endif
216 < #ifdef LINUX
216 > #if defined(LINUX) || defined(CYGWIN)
217                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
218                                  return NULL;
219                          }
# Line 250 | Line 267 | disk_stat_t *get_disk_stats(int *entries){
267  
268          *entries=num_disks;    
269  
270 <        /* If this fails, there is very little i can do about it, so i'll ignore it :) */
271 < #if defined(LINUX) || defined(SOLARIS)
270 >        /* If this fails, there is very little i can do about it, so
271 >           I'll ignore it :) */
272 > #if defined(LINUX) || defined(CYGWIN)
273 >        endmntent(f);
274 > #endif
275 > #if defined(SOLARIS)
276          fclose(f);
277   #endif
278  
# Line 306 | Line 327 | typedef struct {
327   diskio_stat_t *get_diskio_stats(int *entries){
328  
329          static int sizeof_diskio_stats=0;
330 + #ifndef LINUX
331          diskio_stat_t *diskio_stats_ptr;
332 + #endif
333  
334   #ifdef SOLARIS
335          kstat_ctl_t *kc;
# Line 317 | Line 340 | diskio_stat_t *get_diskio_stats(int *entries){
340          FILE *f;
341          char *line_ptr;
342          int major, minor;
320        char dev_letter;
343          int has_pp_stats = 1;
344          static partition *parts = NULL;
345          static int alloc_parts = 0;
346          int i, n;
347          time_t now;
348 +        const char *format;
349   #endif
350   #ifdef FREEBSD
351          static struct statinfo stats;
# Line 333 | Line 356 | diskio_stat_t *get_diskio_stats(int *entries){
356          long sel_gen;
357          struct devstat *dev_ptr;
358   #endif
359 + #ifdef NETBSD
360 +        struct disk_sysctl *stats;
361 +        int num_disks, i;
362 +        int mib[3];
363 +        size_t size;
364 + #endif
365 +
366          num_diskio=0;
367  
368 + #ifdef NETBSD
369 +        mib[0] = CTL_HW;
370 +        mib[1] = HW_DISKSTATS;
371 +        mib[2] = sizeof(struct disk_sysctl);
372 +
373 +        if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
374 +                return NULL;
375 +        }
376 +        num_disks = size / sizeof(struct disk_sysctl);
377 +
378 +        stats = malloc(size);
379 +        if (stats == NULL) {
380 +                return NULL;
381 +        }
382 +
383 +        if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) {
384 +                return NULL;
385 +        }
386 +
387 +        for (i = 0; i < num_disks; i++) {
388 +                u_int64_t rbytes, wbytes;
389 +
390 + #ifdef HAVE_DK_RBYTES
391 +                rbytes = stats[i].dk_rbytes;
392 +                wbytes = stats[i].dk_wbytes;
393 + #else
394 +                /* Before 1.7, NetBSD merged reads and writes. */
395 +                rbytes = wbytes = stats[i].dk_bytes;
396 + #endif
397 +
398 +                /* Don't keep stats for disks that have never been used. */
399 +                if (rbytes == 0 && wbytes == 0) {
400 +                        continue;
401 +                }
402 +
403 +                diskio_stats = diskio_stat_malloc(num_diskio + 1,
404 +                                                  &sizeof_diskio_stats,
405 +                                                  diskio_stats);
406 +                if (diskio_stats == NULL) {
407 +                        return NULL;
408 +                }
409 +                diskio_stats_ptr = diskio_stats + num_diskio;
410 +                
411 +                diskio_stats_ptr->read_bytes = rbytes;
412 +                diskio_stats_ptr->write_bytes = wbytes;
413 +                if (diskio_stats_ptr->disk_name != NULL) {
414 +                        free(diskio_stats_ptr->disk_name);
415 +                }
416 +                diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
417 +                diskio_stats_ptr->systime = time(NULL);
418 +        
419 +                num_diskio++;  
420 +        }
421 +
422 +        free(stats);
423 + #endif
424 +
425   #ifdef FREEBSD
426          if (!stats_init) {
427                  stats.dinfo=malloc(sizeof(struct devinfo));
341                bzero(stats.dinfo, sizeof(struct devinfo));
428                  if(stats.dinfo==NULL) return NULL;
429 +                bzero(stats.dinfo, sizeof(struct devinfo));
430                  stats_init = 1;
431          }
432   #ifdef FREEBSD5
433          if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
434 <        /* Not aware of a get all devices, so i said 999. If we ever                                                                           * find a machine with more than 999 disks, then i'll change                                                                           * this number :)                                                                                                                      */
434 >        /* Not aware of a get all devices, so i said 999. If we ever
435 >         * find a machine with more than 999 disks, then i'll change
436 >         * this number :)
437 >         */
438          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;
439   #else
440          if ((getdevs(&stats)) < 0) return NULL;
# Line 414 | Line 504 | diskio_stat_t *get_diskio_stats(int *entries){
504  
505                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
506  
507 <                        diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
507 >                        diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name));
508                          diskio_stats_ptr->systime=time(NULL);
509                          num_diskio++;
510                  }
# Line 428 | Line 518 | diskio_stat_t *get_diskio_stats(int *entries){
518          n = 0;
519  
520          /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
521 <           have statistics in here too, so we can use those directly. */
521 >           have statistics in here too, so we can use those directly.
522 >           2.6 kernels have /proc/diskstats instead with almost (but not quite)
523 >           the same format. */
524  
525 <        f = fopen("/proc/partitions", "r");
525 >        f = fopen("/proc/diskstats", "r");
526 >        format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld";
527 >        if (f == NULL) {
528 >                f = fopen("/proc/partitions", "r");
529 >                format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld";
530 >        }
531          if (f == NULL) goto out;
532          now = time(NULL);
533  
# Line 439 | Line 536 | diskio_stat_t *get_diskio_stats(int *entries){
536                  char *s;
537                  long long rsect, wsect;
538  
539 <                int nr = sscanf(line_ptr,
443 <                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
539 >                int nr = sscanf(line_ptr, format,
540                          &major, &minor, name, &rsect, &wsect);
541                  if (nr < 3) continue;
446                if (nr < 5) {
447                        has_pp_stats = 0;
448                        rsect = 0;
449                        wsect = 0;
450                }
542  
543                  /* Skip device names ending in numbers, since they're
544                     partitions. */
# Line 456 | Line 547 | diskio_stat_t *get_diskio_stats(int *entries){
547                  --s;
548                  if (*s >= '0' && *s <= '9') continue;
549  
550 +                if (nr < 5) {
551 +                        has_pp_stats = 0;
552 +                        rsect = 0;
553 +                        wsect = 0;
554 +                }
555 +
556                  diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
557                          diskio_stats);
558                  if (diskio_stats == NULL) goto out;
# Line 480 | Line 577 | diskio_stat_t *get_diskio_stats(int *entries){
577                  n++;
578          }
579  
580 +        fclose(f);
581 +        f = NULL;
582 +
583          if (!has_pp_stats) {
584 <                /* This is an older kernel without stats in /proc/partitions.
585 <                   Read what we can from /proc/stat instead. */
584 >                /* This is an older kernel where /proc/partitions doesn't
585 >                   contain stats. Read what we can from /proc/stat instead, and
586 >                   fill in the appropriate bits of the list allocated above. */
587  
588                  f = fopen("/proc/stat", "r");
589                  if (f == NULL) goto out;
# Line 549 | Line 650 | diskio_stat_t *get_diskio_stats(int *entries){
650          num_diskio = n;
651   out:
652          if (f != NULL) fclose(f);
653 + #endif
654  
655 + #ifdef CYGWIN
656 +        return NULL;
657   #endif
658 +
659          *entries=num_diskio;
660  
661          return diskio_stats;
# Line 590 | Line 695 | diskio_stat_t *get_diskio_stats_diff(int *entries){
695          }
696  
697          diskio_stats_ptr=get_diskio_stats(&disks);
698 +        if (diskio_stats_ptr == NULL) {
699 +                return NULL;
700 +        }
701          diskio_stats_diff_ptr=diskio_stats_diff;
702  
703          for(x=0;x<sizeof_diskio_stats_diff;x++){

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines