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.7 by tdb, Fri Feb 28 22:59:35 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 <stdlib.h>
25   #include <stdio.h>
26 + #include <stdlib.h>
27   #include <string.h>
28   #include "statgrab.h"
29  
# Line 34 | Line 34
34   #define VALID_FS_TYPES {"ufs", "tmpfs"}
35   #endif
36  
37 + #ifdef LINUX
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 67 | Line 83 | disk_stat_t *get_disk_stats(int *entries){
83  
84          char *fs_types[] = VALID_FS_TYPES;
85          int x, valid_type;
70
86          int num_disks=0;
87 <        struct mnttab mp;
73 <        struct statvfs fs;
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;
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));
108                  if(disk_stats==NULL){
# Line 83 | 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;
132 +        }
133  
134 +        while((mp=getmntent(f))){
135 +                if((statfs(mp->mnt_dir, &fs)) !=0){
136 +                        continue;
137 +                }      
138 +
139 +                valid_type=0;
140 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
141 +                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
142 +                                valid_type=1;
143 +                                break;
144 +                        }
145 +                }
146 + #endif
147 +
148 + #ifdef SOLARIS
149          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
150                  return NULL;
151          }
# Line 92 | Line 153 | disk_stat_t *get_disk_stats(int *entries){
153                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
154                          continue;
155                  }
95
156                  valid_type=0;
157                  for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
158                          if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
# Line 100 | Line 160 | disk_stat_t *get_disk_stats(int *entries){
160                                  break;
161                          }
162                  }
163 + #endif
164  
165                  if(valid_type){
166                          if(num_disks>watermark-1){
# Line 114 | Line 175 | disk_stat_t *get_disk_stats(int *entries){
175                          }
176  
177                          disk_ptr=disk_stats+num_disks;
178 <        
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;
203 >                        }
204 >                                
205 >                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
206 >                                return NULL;
207 >                        }
208 >
209 >                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
210 >                                return NULL;
211 >                        }
212 >                        disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
213 >                        disk_ptr->avail = (long long)fs.f_bsize * (long long)fs.f_bavail;
214 >                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_bsize * (long long)fs.f_bfree);
215 >
216 >                        disk_ptr->total_inodes=(long long)fs.f_files;
217 >                        disk_ptr->free_inodes=(long long)fs.f_ffree;
218 >                        /* Linux doesn't have a "available" inodes */
219 >                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
220 > #endif
221 >
222 > #ifdef SOLARIS
223                          /* Memory leak in event of realloc failing */
224                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
225 <                           Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE woul prob be upwards
226 <                           of a k each */
225 >                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
226 >                         * be upwards of a k each
227 >                         */
228                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
229                                  return NULL;
230                          }
# Line 138 | Line 244 | disk_stat_t *get_disk_stats(int *entries){
244                          disk_ptr->total_inodes=(long long)fs.f_files;
245                          disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
246                          disk_ptr->free_inodes=(long long)fs.f_favail;
247 <
247 > #endif
248                          num_disks++;
249                  }
250          }
# Line 146 | 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  
261   }
154
262   void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
263  
264          for(diskio_stats+=start; start<end; start++){
# Line 190 | 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;
310          diskio_stat_t *diskio_stats_ptr;
311  
312 + #ifdef SOLARIS
313          kstat_ctl_t *kc;
314          kstat_t *ksp;
315          kstat_io_t kios;
316 + #endif
317 + #ifdef LINUX
318 +        FILE *f;
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 + #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;
398          }
399  
206        num_diskio=0;
207
400          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
401                  if (!strcmp(ksp->ks_class, "disk")) {
402  
# Line 227 | 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          }
426  
427          kstat_close(kc);
428 + #endif
429  
430 + #ifdef LINUX
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 +                }
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 +                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[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 +                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 +                        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 +                                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 +                        /* 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;
559  
560          return diskio_stats;
# Line 299 | Line 621 | diskio_stat_t *get_diskio_stats_diff(int *entries){
621                  diskio_stats_diff_ptr++;        
622  
623          }
624 <
624 >        
625 >        *entries=sizeof_diskio_stats_diff;
626          return diskio_stats_diff;
627   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines