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.9 by pajs, Tue Mar 4 12:55:14 2003 UTC vs.
Revision 1.20 by ats, Thu Aug 28 23:05:56 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 <sys/vfs.h>
39   #include <mntent.h>
40 + #include "tools.h"
41   #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
42   #endif
43  
44 + #ifdef FREEBSD
45 + #include <sys/param.h>
46 + #include <sys/ucred.h>
47 + #include <sys/mount.h>
48 + #include <sys/dkstat.h>
49 + #include <devstat.h>
50 + #define VALID_FS_TYPES {"ufs", "mfs"}
51 + #endif
52   #define START_VAL 1
53  
54   char *copy_string(char *orig_ptr, const char *newtext){
# Line 75 | Line 83 | disk_stat_t *get_disk_stats(int *entries){
83          char *fs_types[] = VALID_FS_TYPES;
84          int x, valid_type;
85          int num_disks=0;
86 + #if defined(LINUX) || defined (SOLARIS)
87          FILE *f;
88 + #endif
89  
90          disk_stat_t *disk_ptr;
91  
92   #ifdef SOLARIS
93 <        struct mnttab *mp;
93 >        struct mnttab mp;
94          struct statvfs fs;
95   #endif
96   #ifdef LINUX
97          struct mntent *mp;
98          struct statfs fs;
99   #endif
100 + #ifdef FREEBSD
101 +        int nummnt;
102 +        struct statfs *mp;
103 + #endif
104  
105          if(watermark==-1){
106                  disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
# Line 96 | Line 110 | disk_stat_t *get_disk_stats(int *entries){
110                  watermark=START_VAL;
111                  init_disk_stat(0, watermark-1, disk_stats);
112          }
113 + #ifdef FREEBSD
114 +        nummnt=getmntinfo(&mp , MNT_LOCAL);
115 +        if (nummnt<=0){
116 +                return NULL;
117 +        }
118 +        for(;nummnt--; mp++){
119 +                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 +                }
126 + #endif
127 +
128   #ifdef LINUX
129          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
130                  return NULL;
# Line 145 | Line 174 | disk_stat_t *get_disk_stats(int *entries){
174                          }
175  
176                          disk_ptr=disk_stats+num_disks;
177 + #ifdef FREEBSD
178 +                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
179 +                                return NULL;
180 +                        }
181 +
182 +                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
183 +                                return NULL;
184 +                        }
185 +
186 +                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
187 +                                return NULL;
188 +                        }
189 +
190 +                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
191 +                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
192 +                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
193 +
194 +                        disk_ptr->total_inodes=(long long)mp->f_files;
195 +                        disk_ptr->free_inodes=(long long)mp->f_ffree;
196 +                        /* Freebsd doesn't have a "available" inodes */
197 +                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
198 + #endif
199   #ifdef LINUX
200                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
201                                  return NULL;
# Line 200 | Line 251 | disk_stat_t *get_disk_stats(int *entries){
251          *entries=num_disks;    
252  
253          /* If this fails, there is very little i can do about it, so i'll ignore it :) */
254 + #if defined(LINUX) || defined(SOLARIS)
255          fclose(f);
256 + #endif
257  
258          return disk_stats;
259  
260   }
208 #ifdef SOLARIS
261   void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
262  
263          for(diskio_stats+=start; start<end; start++){
# Line 244 | Line 296 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
296   static diskio_stat_t *diskio_stats=NULL;        
297   static int num_diskio=0;        
298  
299 + #ifdef LINUX
300 + typedef struct {
301 +        int major;
302 +        int minor;
303 + } partition;
304 + #endif
305 +
306   diskio_stat_t *get_diskio_stats(int *entries){
307  
308          static int sizeof_diskio_stats=0;
309          diskio_stat_t *diskio_stats_ptr;
310  
311 + #ifdef SOLARIS
312          kstat_ctl_t *kc;
313          kstat_t *ksp;
314          kstat_io_t kios;
315 + #endif
316 + #ifdef LINUX
317 +        FILE *f;
318 +        char *line_ptr;
319 +        int major, minor;
320 +        char dev_letter;
321 +        int has_pp_stats = 1;
322 +        static partition *parts = NULL;
323 +        static int alloc_parts = 0;
324 +        int i, n;
325 +        time_t now;
326 + #endif
327 + #ifdef FREEBSD
328 +        struct statinfo stats;
329 +        int counter;
330 +        struct device_selection *dev_sel = NULL;
331 +        int n_selected, n_selections;
332 +        long sel_gen;
333 +        struct devstat *dev_ptr;
334 + #endif
335 +        num_diskio=0;
336  
337 + #ifdef FREEBSD
338 +        stats.dinfo=malloc(sizeof(struct devinfo));
339 +        if(stats.dinfo==NULL) return NULL;
340 +        if ((getdevs(&stats)) < 0) return NULL;
341 +        /* Not aware of a get all devices, so i said 999. If we ever
342 +         * find a machine with more than 999 disks, then i'll change
343 +         * this number :)
344 +         */
345 +        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;
346 +
347 +        for(counter=0;counter<stats.dinfo->numdevs;counter++){
348 +                dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
349 +
350 +                /* Throw away devices that have done nothing, ever.. Eg "odd"
351 +                 * devices.. like mem, proc.. and also doesn't report floppy
352 +                 * drives etc unless they are doing stuff :)
353 +                 */
354 +                if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
355 +                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
356 +                        return NULL;
357 +                }
358 +                diskio_stats_ptr=diskio_stats+num_diskio;
359 +                
360 +                diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
361 +                diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
362 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
363 +                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
364 +                diskio_stats_ptr->systime=time(NULL);
365 +
366 +                num_diskio++;
367 +        }
368 +        free(dev_sel);
369 +        free(stats.dinfo);
370 +
371 + #endif
372 + #ifdef SOLARIS
373          if ((kc = kstat_open()) == NULL) {
374                  return NULL;
375          }
376  
260        num_diskio=0;
261
377          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
378                  if (!strcmp(ksp->ks_class, "disk")) {
379  
# Line 281 | Line 396 | diskio_stat_t *get_diskio_stats(int *entries){
396                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
397  
398                          diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
399 +                        diskio_stats_ptr->systime=time(NULL);
400                          num_diskio++;
401                  }
402          }
403  
404          kstat_close(kc);
405 + #endif
406  
407 + #ifdef LINUX
408 +        num_diskio = 0;
409 +        n = 0;
410 +
411 +        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
412 +           have statistics in here too, so we can use those directly. */
413 +
414 +        f = fopen("/proc/partitions", "r");
415 +        if (f == NULL) goto out;
416 +        now = time(NULL);
417 +
418 +        while ((line_ptr = f_read_line(f, "")) != NULL) {
419 +                char name[20];
420 +                char *s;
421 +                long long rsect, wsect;
422 +
423 +                int nr = sscanf(line_ptr,
424 +                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
425 +                        &major, &minor, name, &rsect, &wsect);
426 +                if (nr < 3) continue;
427 +                if (nr < 5) {
428 +                        has_pp_stats = 0;
429 +                        rsect = 0;
430 +                        wsect = 0;
431 +                }
432 +
433 +                /* Skip device names ending in numbers, since they're
434 +                   partitions. */
435 +                s = name;
436 +                while (*s != '\0') s++;
437 +                --s;
438 +                if (*s >= '0' && *s <= '9') continue;
439 +
440 +                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
441 +                        diskio_stats);
442 +                if (diskio_stats == NULL) goto out;
443 +                if (n >= alloc_parts) {
444 +                        alloc_parts += 16;
445 +                        parts = realloc(parts, alloc_parts * sizeof *parts);
446 +                        if (parts == NULL) {
447 +                                alloc_parts = 0;
448 +                                goto out;
449 +                        }
450 +                }
451 +
452 +                if (diskio_stats[n].disk_name != NULL)
453 +                        free(diskio_stats[n].disk_name);
454 +                diskio_stats[n].disk_name = strdup(name);
455 +                diskio_stats[n].read_bytes = rsect * 512;
456 +                diskio_stats[n].write_bytes = wsect * 512;
457 +                diskio_stats[n].systime = now;
458 +                parts[n].major = major;
459 +                parts[n].minor = minor;
460 +
461 +                n++;
462 +        }
463 +
464 +        if (!has_pp_stats) {
465 +                /* This is an older kernel without stats in /proc/partitions.
466 +                   Read what we can from /proc/stat instead. */
467 +
468 +                f = fopen("/proc/stat", "r");
469 +                if (f == NULL) goto out;
470 +                now = time(NULL);
471 +        
472 +                line_ptr = f_read_line(f, "disk_io:");
473 +                if (line_ptr == NULL) goto out;
474 +        
475 +                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
476 +                        long long rsect, wsect;
477 +
478 +                        if (*++line_ptr == '\0') break;
479 +        
480 +                        if((sscanf(line_ptr,
481 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
482 +                                &major, &minor, &rsect, &wsect)) != 4) {
483 +                                        continue;
484 +                        }
485 +
486 +                        /* Find the corresponding device from earlier.
487 +                           Just to add to the fun, "minor" is actually the disk
488 +                           number, not the device minor, so we need to figure
489 +                           out the real minor number based on the major!
490 +                           This list is not exhaustive; if you're running
491 +                           an older kernel you probably don't have fancy
492 +                           I2O hardware anyway... */
493 +                        switch (major) {
494 +                        case 3:
495 +                        case 21:
496 +                        case 22:
497 +                        case 33:
498 +                        case 34:
499 +                        case 36:
500 +                        case 56:
501 +                        case 57:
502 +                        case 88:
503 +                        case 89:
504 +                        case 90:
505 +                        case 91:
506 +                                minor *= 64;
507 +                                break;
508 +                        case 9:
509 +                        case 43:
510 +                                break;
511 +                        default:
512 +                                minor *= 16;
513 +                                break;
514 +                        }
515 +                        for (i = 0; i < n; i++) {
516 +                                if (major == parts[i].major
517 +                                        && minor == parts[i].minor)
518 +                                        break;
519 +                        }
520 +                        if (i == n) continue;
521 +
522 +                        /* We read the number of blocks. Blocks are stored in
523 +                           512 bytes */
524 +                        diskio_stats[i].read_bytes = rsect * 512;
525 +                        diskio_stats[i].write_bytes = wsect * 512;
526 +                        diskio_stats[i].systime = now;
527 +                }
528 +        }
529 +
530 +        num_diskio = n;
531 + out:
532 +        if (f != NULL) fclose(f);
533 +
534 + #endif
535          *entries=num_diskio;
536  
537          return diskio_stats;
# Line 357 | Line 602 | diskio_stat_t *get_diskio_stats_diff(int *entries){
602          *entries=sizeof_diskio_stats_diff;
603          return diskio_stats_diff;
604   }
360 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines