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.15 by pajs, Mon Mar 31 11:30:54 2003 UTC vs.
Revision 1.32 by ats, Sun Oct 19 11:15:30 2003 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2003 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
# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 + #include <stdio.h>
26   #include <stdlib.h>
27   #include <string.h>
28   #include "statgrab.h"
29  
30   #ifdef SOLARIS
30 #include <stdio.h>
31   #include <sys/mnttab.h>
32   #include <sys/statvfs.h>
33   #include <kstat.h>
# Line 35 | Line 35
35   #endif
36  
37   #ifdef LINUX
38 < #include <stdio.h>
38 > #include <time.h>
39   #include <sys/vfs.h>
40   #include <mntent.h>
41   #include "tools.h"
42 < #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
42 > #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
43 >                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
44 >                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
45 >                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
46 >                        "umsdos", "vfat", "xfs", "jfs"}
47   #endif
48  
49 + #ifdef ALLBSD
50 + #include <sys/param.h>
51 + #include <sys/ucred.h>
52 + #include <sys/mount.h>
53 + #define VALID_FS_TYPES {"ufs", "mfs", "ffs"}
54 + #endif
55 + #ifdef FREEBSD
56 + #include <sys/dkstat.h>
57 + #include <devstat.h>
58 + #endif
59 +
60   #define START_VAL 1
61  
62   char *copy_string(char *orig_ptr, const char *newtext){
# Line 76 | Line 91 | disk_stat_t *get_disk_stats(int *entries){
91          char *fs_types[] = VALID_FS_TYPES;
92          int x, valid_type;
93          int num_disks=0;
94 + #if defined(LINUX) || defined (SOLARIS)
95          FILE *f;
96 + #endif
97  
98          disk_stat_t *disk_ptr;
99  
# Line 88 | Line 105 | disk_stat_t *get_disk_stats(int *entries){
105          struct mntent *mp;
106          struct statfs fs;
107   #endif
108 + #ifdef ALLBSD
109 +        int nummnt;
110 +        struct statfs *mp;
111 + #endif
112  
113          if(watermark==-1){
114                  disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
# Line 97 | Line 118 | disk_stat_t *get_disk_stats(int *entries){
118                  watermark=START_VAL;
119                  init_disk_stat(0, watermark-1, disk_stats);
120          }
121 + #ifdef ALLBSD
122 +        nummnt=getmntinfo(&mp , MNT_LOCAL);
123 +        if (nummnt<=0){
124 +                return NULL;
125 +        }
126 +        for(;nummnt--; mp++){
127 +                valid_type=0;
128 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
129 +                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
130 +                                valid_type=1;
131 +                                break;
132 +                        }
133 +                }
134 + #endif
135 +
136   #ifdef LINUX
137          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
138                  return NULL;
# Line 146 | Line 182 | disk_stat_t *get_disk_stats(int *entries){
182                          }
183  
184                          disk_ptr=disk_stats+num_disks;
185 + #ifdef ALLBSD
186 +                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
187 +                                return NULL;
188 +                        }
189 +
190 +                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
191 +                                return NULL;
192 +                        }
193 +
194 +                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
195 +                                return NULL;
196 +                        }
197 +
198 +                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
199 +                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
200 +                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
201 +
202 +                        disk_ptr->total_inodes=(long long)mp->f_files;
203 +                        disk_ptr->free_inodes=(long long)mp->f_ffree;
204 +                        /* Freebsd doesn't have a "available" inodes */
205 +                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
206 + #endif
207   #ifdef LINUX
208                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
209                                  return NULL;
# Line 201 | Line 259 | disk_stat_t *get_disk_stats(int *entries){
259          *entries=num_disks;    
260  
261          /* If this fails, there is very little i can do about it, so i'll ignore it :) */
262 + #if defined(LINUX) || defined(SOLARIS)
263          fclose(f);
264 + #endif
265  
266          return disk_stats;
267  
# Line 244 | Line 304 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
304   static diskio_stat_t *diskio_stats=NULL;        
305   static int num_diskio=0;        
306  
307 + #ifdef LINUX
308 + typedef struct {
309 +        int major;
310 +        int minor;
311 + } partition;
312 + #endif
313 +
314   diskio_stat_t *get_diskio_stats(int *entries){
315  
316          static int sizeof_diskio_stats=0;
# Line 259 | Line 326 | diskio_stat_t *get_diskio_stats(int *entries){
326          char *line_ptr;
327          int major, minor;
328          char dev_letter;
329 +        int has_pp_stats = 1;
330 +        static partition *parts = NULL;
331 +        static int alloc_parts = 0;
332 +        int i, n;
333 +        time_t now;
334   #endif
335 + #ifdef FREEBSD
336 +        static struct statinfo stats;
337 +        static int stats_init = 0;
338 +        int counter;
339 +        struct device_selection *dev_sel = NULL;
340 +        int n_selected, n_selections;
341 +        long sel_gen;
342 +        struct devstat *dev_ptr;
343 + #endif
344 + #ifdef NETBSD
345 +        /* FIXME get_diskio_stats NYI on NetBSD.
346 +         * See vmstat/dkstats.c in NetBSD source for examples.
347 +         */
348 + #endif
349          num_diskio=0;
350  
351 + #ifdef FREEBSD
352 +        if (!stats_init) {
353 +                stats.dinfo=malloc(sizeof(struct devinfo));
354 +                bzero(stats.dinfo, sizeof(struct devinfo));
355 +                if(stats.dinfo==NULL) return NULL;
356 +                stats_init = 1;
357 +        }
358 + #ifdef FREEBSD5
359 +        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
360 +        /* Not aware of a get all devices, so i said 999. If we ever
361 +         * find a machine with more than 999 disks, then i'll change
362 +         * this number :)
363 +         */
364 +        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;
365 + #else
366 +        if ((getdevs(&stats)) < 0) return NULL;
367 +        /* Not aware of a get all devices, so i said 999. If we ever
368 +         * find a machine with more than 999 disks, then i'll change
369 +         * this number :)
370 +         */
371 +        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;
372 + #endif
373 +
374 +        for(counter=0;counter<stats.dinfo->numdevs;counter++){
375 +                dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
376 +
377 +                /* Throw away devices that have done nothing, ever.. Eg "odd"
378 +                 * devices.. like mem, proc.. and also doesn't report floppy
379 +                 * drives etc unless they are doing stuff :)
380 +                 */
381 + #ifdef FREEBSD5
382 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
383 + #else
384 +                if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
385 + #endif
386 +                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
387 +                        return NULL;
388 +                }
389 +                diskio_stats_ptr=diskio_stats+num_diskio;
390 +
391 + #ifdef FREEBSD5        
392 +                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
393 +                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
394 + #else
395 +                diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
396 +                diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
397 + #endif
398 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
399 +                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
400 +                diskio_stats_ptr->systime=time(NULL);
401 +
402 +                num_diskio++;
403 +        }
404 +        free(dev_sel);
405 +
406 + #endif
407   #ifdef SOLARIS
408          if ((kc = kstat_open()) == NULL) {
409                  return NULL;
# Line 298 | Line 440 | diskio_stat_t *get_diskio_stats(int *entries){
440   #endif
441  
442   #ifdef LINUX
443 <        f=fopen("/proc/stat", "r");
444 <        if(f==NULL){
445 <                *entries=0;
446 <                fclose(f);
447 <                return NULL;
448 <        }
449 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
450 <                *entries=0;
451 <                fclose(f);
452 <                return NULL;
453 <        }
454 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
455 <                line_ptr++;
456 <                if(*line_ptr=='\0'){
457 <                        break;
443 >        num_diskio = 0;
444 >        n = 0;
445 >
446 >        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
447 >           have statistics in here too, so we can use those directly. */
448 >
449 >        f = fopen("/proc/partitions", "r");
450 >        if (f == NULL) goto out;
451 >        now = time(NULL);
452 >
453 >        while ((line_ptr = f_read_line(f, "")) != NULL) {
454 >                char name[20];
455 >                char *s;
456 >                long long rsect, wsect;
457 >
458 >                int nr = sscanf(line_ptr,
459 >                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
460 >                        &major, &minor, name, &rsect, &wsect);
461 >                if (nr < 3) continue;
462 >                if (nr < 5) {
463 >                        has_pp_stats = 0;
464 >                        rsect = 0;
465 >                        wsect = 0;
466                  }
317                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
318                        fclose(f);
319                        *entries=0;
320                        return NULL;
321                }
322                diskio_stats_ptr=diskio_stats+num_diskio;
467  
468 +                /* Skip device names ending in numbers, since they're
469 +                   partitions. */
470 +                s = name;
471 +                while (*s != '\0') s++;
472 +                --s;
473 +                if (*s >= '0' && *s <= '9') continue;
474  
475 <                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
476 <                        &major, \
477 <                        &minor, \
478 <                        &diskio_stats_ptr->read_bytes, \
479 <                        &diskio_stats_ptr->write_bytes))!=4) {
480 <                                continue;
475 >                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
476 >                        diskio_stats);
477 >                if (diskio_stats == NULL) goto out;
478 >                if (n >= alloc_parts) {
479 >                        alloc_parts += 16;
480 >                        parts = realloc(parts, alloc_parts * sizeof *parts);
481 >                        if (parts == NULL) {
482 >                                alloc_parts = 0;
483 >                                goto out;
484 >                        }
485                  }
486  
487 <                /* We read the number of blocks. Blocks are stored in 512 bytes */
488 <                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
489 <                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
487 >                if (diskio_stats[n].disk_name != NULL)
488 >                        free(diskio_stats[n].disk_name);
489 >                diskio_stats[n].disk_name = strdup(name);
490 >                diskio_stats[n].read_bytes = rsect * 512;
491 >                diskio_stats[n].write_bytes = wsect * 512;
492 >                diskio_stats[n].systime = now;
493 >                parts[n].major = major;
494 >                parts[n].minor = minor;
495  
496 <                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
496 >                n++;
497 >        }
498  
499 <                switch(major){
500 <                        case 2:
501 <                                if(minor==0){
342 <                                        diskio_stats_ptr->disk_name=strdup("fd0");
343 <                                }
344 <                                break;
499 >        if (!has_pp_stats) {
500 >                /* This is an older kernel without stats in /proc/partitions.
501 >                   Read what we can from /proc/stat instead. */
502  
503 <                        case 3:
504 <                                if(minor==0){
505 <                                        diskio_stats_ptr->disk_name=strdup("hda");
506 <                                }else{
507 <                                        diskio_stats_ptr->disk_name=strdup("hdb");
508 <                                }
509 <                                break;
503 >                f = fopen("/proc/stat", "r");
504 >                if (f == NULL) goto out;
505 >                now = time(NULL);
506 >        
507 >                line_ptr = f_read_line(f, "disk_io:");
508 >                if (line_ptr == NULL) goto out;
509 >        
510 >                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
511 >                        long long rsect, wsect;
512  
513 +                        if (*++line_ptr == '\0') break;
514 +        
515 +                        if((sscanf(line_ptr,
516 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
517 +                                &major, &minor, &rsect, &wsect)) != 4) {
518 +                                        continue;
519 +                        }
520 +
521 +                        /* Find the corresponding device from earlier.
522 +                           Just to add to the fun, "minor" is actually the disk
523 +                           number, not the device minor, so we need to figure
524 +                           out the real minor number based on the major!
525 +                           This list is not exhaustive; if you're running
526 +                           an older kernel you probably don't have fancy
527 +                           I2O hardware anyway... */
528 +                        switch (major) {
529 +                        case 3:
530 +                        case 21:
531                          case 22:
532 <                                if(minor==0){
533 <                                        diskio_stats_ptr->disk_name=strdup("hdc");
534 <                                }else{
535 <                                        diskio_stats_ptr->disk_name=strdup("hdd");
536 <                                }
532 >                        case 33:
533 >                        case 34:
534 >                        case 36:
535 >                        case 56:
536 >                        case 57:
537 >                        case 88:
538 >                        case 89:
539 >                        case 90:
540 >                        case 91:
541 >                                minor *= 64;
542                                  break;
543 <                        case 8:
544 <                                dev_letter='a'+(minor/16);
363 <                                diskio_stats_ptr->disk_name=malloc(4);
364 <                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
543 >                        case 9:
544 >                        case 43:
545                                  break;
546                          default:
547 <                                /* I have no idea what it is then :) */
368 <                                diskio_stats_ptr->disk_name=malloc(16);
369 <                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
547 >                                minor *= 16;
548                                  break;
549 <                }
549 >                        }
550 >                        for (i = 0; i < n; i++) {
551 >                                if (major == parts[i].major
552 >                                        && minor == parts[i].minor)
553 >                                        break;
554 >                        }
555 >                        if (i == n) continue;
556  
557 <                diskio_stats_ptr->systime=time(NULL);
558 <                num_diskio++;
557 >                        /* We read the number of blocks. Blocks are stored in
558 >                           512 bytes */
559 >                        diskio_stats[i].read_bytes = rsect * 512;
560 >                        diskio_stats[i].write_bytes = wsect * 512;
561 >                        diskio_stats[i].systime = now;
562 >                }
563          }
564 <        fclose(f);
564 >
565 >        num_diskio = n;
566 > out:
567 >        if (f != NULL) fclose(f);
568  
569   #endif
570          *entries=num_diskio;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines