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.10 by pajs, Tue Mar 4 18:06:16 2003 UTC vs.
Revision 1.30 by ats, Sun Oct 19 00:10: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"}
43   #endif
44  
45 + #ifdef FREEBSD
46 + #include <sys/param.h>
47 + #include <sys/ucred.h>
48 + #include <sys/mount.h>
49 + #include <sys/dkstat.h>
50 + #include <devstat.h>
51 + #define VALID_FS_TYPES {"ufs", "mfs"}
52 + #endif
53   #define START_VAL 1
54  
55   char *copy_string(char *orig_ptr, const char *newtext){
# Line 76 | Line 84 | disk_stat_t *get_disk_stats(int *entries){
84          char *fs_types[] = VALID_FS_TYPES;
85          int x, valid_type;
86          int num_disks=0;
87 + #if defined(LINUX) || defined (SOLARIS)
88          FILE *f;
89 + #endif
90  
91          disk_stat_t *disk_ptr;
92  
93   #ifdef SOLARIS
94 <        struct mnttab *mp;
94 >        struct mnttab mp;
95          struct statvfs fs;
96   #endif
97   #ifdef LINUX
98          struct mntent *mp;
99          struct statfs fs;
100   #endif
101 + #ifdef FREEBSD
102 +        int nummnt;
103 +        struct statfs *mp;
104 + #endif
105  
106          if(watermark==-1){
107                  disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
# Line 97 | Line 111 | disk_stat_t *get_disk_stats(int *entries){
111                  watermark=START_VAL;
112                  init_disk_stat(0, watermark-1, disk_stats);
113          }
114 + #ifdef FREEBSD
115 +        nummnt=getmntinfo(&mp , MNT_LOCAL);
116 +        if (nummnt<=0){
117 +                return NULL;
118 +        }
119 +        for(;nummnt--; mp++){
120 +                valid_type=0;
121 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
122 +                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
123 +                                valid_type=1;
124 +                                break;
125 +                        }
126 +                }
127 + #endif
128 +
129   #ifdef LINUX
130          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
131                  return NULL;
# Line 146 | Line 175 | disk_stat_t *get_disk_stats(int *entries){
175                          }
176  
177                          disk_ptr=disk_stats+num_disks;
178 + #ifdef FREEBSD
179 +                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
180 +                                return NULL;
181 +                        }
182 +
183 +                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
184 +                                return NULL;
185 +                        }
186 +
187 +                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
188 +                                return NULL;
189 +                        }
190 +
191 +                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
192 +                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
193 +                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
194 +
195 +                        disk_ptr->total_inodes=(long long)mp->f_files;
196 +                        disk_ptr->free_inodes=(long long)mp->f_ffree;
197 +                        /* Freebsd doesn't have a "available" inodes */
198 +                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
199 + #endif
200   #ifdef LINUX
201                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
202                                  return NULL;
# Line 201 | Line 252 | disk_stat_t *get_disk_stats(int *entries){
252          *entries=num_disks;    
253  
254          /* If this fails, there is very little i can do about it, so i'll ignore it :) */
255 + #if defined(LINUX) || defined(SOLARIS)
256          fclose(f);
257 + #endif
258  
259          return disk_stats;
260  
# Line 244 | Line 297 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
297   static diskio_stat_t *diskio_stats=NULL;        
298   static int num_diskio=0;        
299  
300 + #ifdef LINUX
301 + typedef struct {
302 +        int major;
303 +        int minor;
304 + } partition;
305 + #endif
306 +
307   diskio_stat_t *get_diskio_stats(int *entries){
308  
309          static int sizeof_diskio_stats=0;
# Line 259 | Line 319 | diskio_stat_t *get_diskio_stats(int *entries){
319          char *line_ptr;
320          int major, minor;
321          char dev_letter;
322 +        int has_pp_stats = 1;
323 +        static partition *parts = NULL;
324 +        static int alloc_parts = 0;
325 +        int i, n;
326 +        time_t now;
327   #endif
328 <
328 > #ifdef FREEBSD
329 >        static struct statinfo stats;
330 >        static int stats_init = 0;
331 >        int counter;
332 >        struct device_selection *dev_sel = NULL;
333 >        int n_selected, n_selections;
334 >        long sel_gen;
335 >        struct devstat *dev_ptr;
336 > #endif
337          num_diskio=0;
338  
339 + #ifdef FREEBSD
340 +        if (!stats_init) {
341 +                stats.dinfo=malloc(sizeof(struct devinfo));
342 +                bzero(stats.dinfo, sizeof(struct devinfo));
343 +                if(stats.dinfo==NULL) return NULL;
344 +                stats_init = 1;
345 +        }
346 + #ifdef FREEBSD5
347 +        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
348 +        /* Not aware of a get all devices, so i said 999. If we ever
349 +         * find a machine with more than 999 disks, then i'll change
350 +         * this number :)
351 +         */
352 +        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;
353 + #else
354 +        if ((getdevs(&stats)) < 0) return NULL;
355 +        /* Not aware of a get all devices, so i said 999. If we ever
356 +         * find a machine with more than 999 disks, then i'll change
357 +         * this number :)
358 +         */
359 +        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;
360 + #endif
361 +
362 +        for(counter=0;counter<stats.dinfo->numdevs;counter++){
363 +                dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
364 +
365 +                /* Throw away devices that have done nothing, ever.. Eg "odd"
366 +                 * devices.. like mem, proc.. and also doesn't report floppy
367 +                 * drives etc unless they are doing stuff :)
368 +                 */
369 + #ifdef FREEBSD5
370 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
371 + #else
372 +                if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
373 + #endif
374 +                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
375 +                        return NULL;
376 +                }
377 +                diskio_stats_ptr=diskio_stats+num_diskio;
378 +
379 + #ifdef FREEBSD5        
380 +                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
381 +                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
382 + #else
383 +                diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
384 +                diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
385 + #endif
386 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
387 +                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
388 +                diskio_stats_ptr->systime=time(NULL);
389 +
390 +                num_diskio++;
391 +        }
392 +        free(dev_sel);
393 +
394 + #endif
395   #ifdef SOLARIS
396          if ((kc = kstat_open()) == NULL) {
397                  return NULL;
# Line 290 | Line 419 | diskio_stat_t *get_diskio_stats(int *entries){
419                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
420  
421                          diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
422 +                        diskio_stats_ptr->systime=time(NULL);
423                          num_diskio++;
424                  }
425          }
# Line 298 | Line 428 | diskio_stat_t *get_diskio_stats(int *entries){
428   #endif
429  
430   #ifdef LINUX
431 <        f=fopen("/proc/stat", "r");
432 <        if(f==NULL){
433 <                return NULL;
434 <        }
435 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
436 <                return NULL;
437 <        }
438 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
439 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
440 <                        fclose(f);
441 <                        return NULL;
431 >        num_diskio = 0;
432 >        n = 0;
433 >
434 >        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
435 >           have statistics in here too, so we can use those directly. */
436 >
437 >        f = fopen("/proc/partitions", "r");
438 >        if (f == NULL) goto out;
439 >        now = time(NULL);
440 >
441 >        while ((line_ptr = f_read_line(f, "")) != NULL) {
442 >                char name[20];
443 >                char *s;
444 >                long long rsect, wsect;
445 >
446 >                int nr = sscanf(line_ptr,
447 >                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
448 >                        &major, &minor, name, &rsect, &wsect);
449 >                if (nr < 3) continue;
450 >                if (nr < 5) {
451 >                        has_pp_stats = 0;
452 >                        rsect = 0;
453 >                        wsect = 0;
454                  }
313                diskio_stats_ptr=diskio_stats+num_diskio;
455  
456 +                /* Skip device names ending in numbers, since they're
457 +                   partitions. */
458 +                s = name;
459 +                while (*s != '\0') s++;
460 +                --s;
461 +                if (*s >= '0' && *s <= '9') continue;
462  
463 <                sscanf(line_ptr, "(%d,%d):(%*d, %lld, %*d, %lld, %*d)", \
464 <                        &major, \
465 <                        &minor, \
466 <                        &diskio_stats_ptr->read_bytes, \
467 <                        &diskio_stats_ptr->write_bytes);
463 >                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
464 >                        diskio_stats);
465 >                if (diskio_stats == NULL) goto out;
466 >                if (n >= alloc_parts) {
467 >                        alloc_parts += 16;
468 >                        parts = realloc(parts, alloc_parts * sizeof *parts);
469 >                        if (parts == NULL) {
470 >                                alloc_parts = 0;
471 >                                goto out;
472 >                        }
473 >                }
474  
475 <                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
475 >                if (diskio_stats[n].disk_name != NULL)
476 >                        free(diskio_stats[n].disk_name);
477 >                diskio_stats[n].disk_name = strdup(name);
478 >                diskio_stats[n].read_bytes = rsect * 512;
479 >                diskio_stats[n].write_bytes = wsect * 512;
480 >                diskio_stats[n].systime = now;
481 >                parts[n].major = major;
482 >                parts[n].minor = minor;
483  
484 <                switch(major){
485 <                        case 3:
326 <                                if(minor==0){
327 <                                        diskio_stats_ptr->disk_name=strdup("hda");
328 <                                }else{
329 <                                        diskio_stats_ptr->disk_name=strdup("hdb");
330 <                                }
331 <                                break;
484 >                n++;
485 >        }
486  
487 +        if (!has_pp_stats) {
488 +                /* This is an older kernel without stats in /proc/partitions.
489 +                   Read what we can from /proc/stat instead. */
490 +
491 +                f = fopen("/proc/stat", "r");
492 +                if (f == NULL) goto out;
493 +                now = time(NULL);
494 +        
495 +                line_ptr = f_read_line(f, "disk_io:");
496 +                if (line_ptr == NULL) goto out;
497 +        
498 +                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
499 +                        long long rsect, wsect;
500 +
501 +                        if (*++line_ptr == '\0') break;
502 +        
503 +                        if((sscanf(line_ptr,
504 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
505 +                                &major, &minor, &rsect, &wsect)) != 4) {
506 +                                        continue;
507 +                        }
508 +
509 +                        /* Find the corresponding device from earlier.
510 +                           Just to add to the fun, "minor" is actually the disk
511 +                           number, not the device minor, so we need to figure
512 +                           out the real minor number based on the major!
513 +                           This list is not exhaustive; if you're running
514 +                           an older kernel you probably don't have fancy
515 +                           I2O hardware anyway... */
516 +                        switch (major) {
517 +                        case 3:
518 +                        case 21:
519                          case 22:
520 <                                if(minor==0){
521 <                                        diskio_stats_ptr->disk_name=strdup("hdc");
522 <                                }else{
523 <                                        diskio_stats_ptr->disk_name=strdup("hdd");
524 <                                }
525 <                        case 8:
526 <                                dev_letter='a'+(minor/16);
527 <                                diskio_stats_ptr->disk_name=malloc(4);
528 <                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
520 >                        case 33:
521 >                        case 34:
522 >                        case 36:
523 >                        case 56:
524 >                        case 57:
525 >                        case 88:
526 >                        case 89:
527 >                        case 90:
528 >                        case 91:
529 >                                minor *= 64;
530 >                                break;
531 >                        case 9:
532 >                        case 43:
533 >                                break;
534                          default:
535 <                                /* I have no idea what it is then :) */
536 <                                diskio_stats_ptr->disk_name=malloc(16);
537 <                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
538 <                }
535 >                                minor *= 16;
536 >                                break;
537 >                        }
538 >                        for (i = 0; i < n; i++) {
539 >                                if (major == parts[i].major
540 >                                        && minor == parts[i].minor)
541 >                                        break;
542 >                        }
543 >                        if (i == n) continue;
544  
545 <                num_diskio++;
545 >                        /* We read the number of blocks. Blocks are stored in
546 >                           512 bytes */
547 >                        diskio_stats[i].read_bytes = rsect * 512;
548 >                        diskio_stats[i].write_bytes = wsect * 512;
549 >                        diskio_stats[i].systime = now;
550 >                }
551          }
552 +
553 +        num_diskio = n;
554 + out:
555 +        if (f != NULL) fclose(f);
556  
557   #endif
558          *entries=num_diskio;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines