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.5 by pajs, Sun Feb 23 22:58:08 2003 UTC vs.
Revision 1.14 by pajs, Tue Mar 11 12:57:07 2003 UTC

# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 #include "statgrab.h"
25   #include <stdlib.h>
27 #include <stdio.h>
28 #include "ukcprog.h"
26   #include <string.h>
27 + #include "statgrab.h"
28  
29   #ifdef SOLARIS
30 + #include <stdio.h>
31   #include <sys/mnttab.h>
33 #include <sys/types.h>
32   #include <sys/statvfs.h>
33   #include <kstat.h>
36
34   #define VALID_FS_TYPES {"ufs", "tmpfs"}
35 + #endif
36  
37 + #ifdef LINUX
38 + #include <stdio.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   #define START_VAL 1
# Line 71 | Line 75 | disk_stat_t *get_disk_stats(int *entries){
75  
76          char *fs_types[] = VALID_FS_TYPES;
77          int x, valid_type;
74
78          int num_disks=0;
76        struct mnttab mp;
77        struct statvfs fs;
79          FILE *f;
80  
81          disk_stat_t *disk_ptr;
82  
83 + #ifdef SOLARIS
84 +        struct mnttab mp;
85 +        struct statvfs fs;
86 + #endif
87 + #ifdef LINUX
88 +        struct mntent *mp;
89 +        struct statfs fs;
90 + #endif
91 +
92          if(watermark==-1){
93                  disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
94                  if(disk_stats==NULL){
# Line 87 | Line 97 | disk_stat_t *get_disk_stats(int *entries){
97                  watermark=START_VAL;
98                  init_disk_stat(0, watermark-1, disk_stats);
99          }
100 + #ifdef LINUX
101 +        if ((f=setmntent("/etc/mtab", "r" ))==NULL){
102 +                return NULL;
103 +        }
104  
105 +        while((mp=getmntent(f))){
106 +                if((statfs(mp->mnt_dir, &fs)) !=0){
107 +                        continue;
108 +                }      
109  
110 +                valid_type=0;
111 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
112 +                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
113 +                                valid_type=1;
114 +                                break;
115 +                        }
116 +                }
117 + #endif
118 +
119 + #ifdef SOLARIS
120          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
121                  return NULL;
122          }
# Line 96 | Line 124 | disk_stat_t *get_disk_stats(int *entries){
124                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
125                          continue;
126                  }
99
127                  valid_type=0;
128                  for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
129                          if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
# Line 104 | Line 131 | disk_stat_t *get_disk_stats(int *entries){
131                                  break;
132                          }
133                  }
134 + #endif
135  
136                  if(valid_type){
137                          if(num_disks>watermark-1){
# Line 118 | Line 146 | disk_stat_t *get_disk_stats(int *entries){
146                          }
147  
148                          disk_ptr=disk_stats+num_disks;
149 <        
149 > #ifdef LINUX
150 >                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
151 >                                return NULL;
152 >                        }
153 >                                
154 >                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
155 >                                return NULL;
156 >                        }
157 >
158 >                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
159 >                                return NULL;
160 >                        }
161 >                        disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
162 >                        disk_ptr->avail = (long long)fs.f_bsize * (long long)fs.f_bavail;
163 >                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_bsize * (long long)fs.f_bfree);
164 >
165 >                        disk_ptr->total_inodes=(long long)fs.f_files;
166 >                        disk_ptr->free_inodes=(long long)fs.f_ffree;
167 >                        /* Linux doesn't have a "available" inodes */
168 >                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
169 > #endif
170 >
171 > #ifdef SOLARIS
172                          /* Memory leak in event of realloc failing */
173                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
174 <                           Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE woul prob be upwards
175 <                           of a k each */
174 >                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
175 >                         * be upwards of a k each
176 >                         */
177                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
178                                  return NULL;
179                          }
# Line 142 | Line 193 | disk_stat_t *get_disk_stats(int *entries){
193                          disk_ptr->total_inodes=(long long)fs.f_files;
194                          disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
195                          disk_ptr->free_inodes=(long long)fs.f_favail;
196 <
196 > #endif
197                          num_disks++;
198                  }
199          }
# Line 155 | Line 206 | disk_stat_t *get_disk_stats(int *entries){
206          return disk_stats;
207  
208   }
158
209   void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
210  
211          for(diskio_stats+=start; start<end; start++){
# Line 199 | Line 249 | diskio_stat_t *get_diskio_stats(int *entries){
249          static int sizeof_diskio_stats=0;
250          diskio_stat_t *diskio_stats_ptr;
251  
252 + #ifdef SOLARIS
253          kstat_ctl_t *kc;
254          kstat_t *ksp;
255          kstat_io_t kios;
256 + #endif
257 + #ifdef LINUX
258 +        FILE *f;
259 +        char *line_ptr;
260 +        int major, minor;
261 +        char dev_letter;
262 + #endif
263 +        num_diskio=0;
264  
265 + #ifdef SOLARIS
266          if ((kc = kstat_open()) == NULL) {
267                  return NULL;
268          }
269  
210        num_diskio=0;
211
270          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
271                  if (!strcmp(ksp->ks_class, "disk")) {
272  
# Line 231 | Line 289 | diskio_stat_t *get_diskio_stats(int *entries){
289                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
290  
291                          diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
292 +                        diskio_stats_ptr->systime=time(NULL);
293                          num_diskio++;
294                  }
295          }
296  
297          kstat_close(kc);
298 + #endif
299  
300 + #ifdef LINUX
301 +        f=fopen("/proc/stat", "r");
302 +        if(f==NULL){
303 +                *entries=0;
304 +                fclose(f);
305 +                return NULL;
306 +        }
307 +        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
308 +                *entries=0;
309 +                fclose(f);
310 +                return NULL;
311 +        }
312 +        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
313 +                line_ptr++;
314 +                if(*line_ptr=='\0'){
315 +                        break;
316 +                }
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;
323 +
324 +
325 +                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
326 +                        &major, \
327 +                        &minor, \
328 +                        &diskio_stats_ptr->read_bytes, \
329 +                        &diskio_stats_ptr->write_bytes))!=4) {
330 +                                continue;
331 +                }
332 +
333 +                /* We read the number of blocks. Blocks are stored in 512 bytes */
334 +                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
335 +                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
336 +
337 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
338 +
339 +                switch(major){
340 +                        case 2:
341 +                                if(minor==0){
342 +                                        diskio_stats_ptr->disk_name=strdup("fd0");
343 +                                }
344 +                                break;
345 +
346 +                        case 3:
347 +                                if(minor==0){
348 +                                        diskio_stats_ptr->disk_name=strdup("hda");
349 +                                }else{
350 +                                        diskio_stats_ptr->disk_name=strdup("hdb");
351 +                                }
352 +                                break;
353 +
354 +                        case 22:
355 +                                if(minor==0){
356 +                                        diskio_stats_ptr->disk_name=strdup("hdc");
357 +                                }else{
358 +                                        diskio_stats_ptr->disk_name=strdup("hdd");
359 +                                }
360 +                                break;
361 +                        case 8:
362 +                                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);
365 +                                break;
366 +                        default:
367 +                                /* 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);
370 +                                break;
371 +                }
372 +
373 +                diskio_stats_ptr->systime=time(NULL);
374 +                num_diskio++;
375 +        }
376 +
377 + #endif
378          *entries=num_diskio;
379  
380          return diskio_stats;
381   }
382  
383 < diskio_stat_t *get_diskio_stat_diff(int *entries){
383 > diskio_stat_t *get_diskio_stats_diff(int *entries){
384          static diskio_stat_t *diskio_stats_diff=NULL;
385          static int sizeof_diskio_stats_diff=0;
386          diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
# Line 303 | Line 441 | diskio_stat_t *get_diskio_stat_diff(int *entries){
441                  diskio_stats_diff_ptr++;        
442  
443          }
444 <
444 >        
445 >        *entries=sizeof_diskio_stats_diff;
446          return diskio_stats_diff;
447   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines