ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/disk_stat.c
Revision: 1.2
Committed: Mon May 13 11:33:07 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.1: +62 -4 lines
Log Message:
Disks stats for all OS's. Not tested on FREEBSD yet ;)

File Contents

# User Rev Content
1 pajs 1.1 #include <stdlib.h>
2     #include <stdio.h>
3     #include <ukcprog.h>
4 pajs 1.2 #include <string.h>
5 pajs 1.1
6     #ifdef SOLARIS
7     #include <sys/mnttab.h>
8     #include <sys/types.h>
9     #include <sys/statvfs.h>
10     #endif
11 pajs 1.2 #ifdef LINUX
12     #include <mntent.h>
13     #include <sys/vfs.h>
14     #endif
15     #ifdef FREEBSD
16     #include <sys/param.h>
17     #include <sys/ucred.h>
18     #include <sys/mount.h>
19     #endif
20 pajs 1.1 typedef struct {
21     char *device_name;
22     char *mnt_point;
23     long size;
24     long used;
25     long avail;
26     long t_inodes;
27     long f_inodes;
28     }disk_stat_t;
29    
30     typedef struct system_disks_t{
31     disk_stat_t *disk;
32     struct system_disks_t *next_disk;
33     }system_disks_t;
34    
35 pajs 1.2 char *get_disk_stats(){
36 pajs 1.1 system_disks_t *sd=NULL;
37     system_disks_t *sd_ptr=sd;
38     FILE *f;
39     int counter=0;
40     #ifdef SOLARIS
41     struct mnttab mp;
42     struct statvfs df;
43     #endif
44     #ifdef LINUX
45     struct mntent *mp;
46     struct statfs df;
47     #endif
48     #ifdef FREEBSD
49     int nummnt;
50     struct statfs *mp;
51     #endif
52    
53     #ifdef SOLARIS
54     if ((f=fopen("/etc/mnttab", "r" ))==NULL){
55     errf("Failed to open mounts (%m)");
56     return NULL;
57     }
58     while((getmntent(f, &mp)) == 0){
59     if ((statvfs(mp.mnt_mountp, &df)) !=0){
60     errf("Failed to gets fs stats (%m)");
61     continue;
62     }
63     if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){
64     sd_ptr=malloc(sizeof(system_disks_t));
65     sd_ptr->disk=malloc(sizeof(disk_stat_t));
66     sd_ptr->disk->device_name=strdup(mp.mnt_special);
67     sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp);
68     sd_ptr->disk->size=((df.f_frsize/1024) * df.f_blocks);
69     sd_ptr->disk->used=(((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree));
70     sd_ptr->disk->avail=(df.f_frsize/1024) * df.f_bavail;
71     sd_ptr->disk->t_inodes=df.f_files;
72     sd_ptr->disk->f_inodes=df.f_ffree;
73     sd_ptr->next_disk=sd;
74     sd=sd_ptr;
75     }
76    
77     }
78    
79     #endif
80 pajs 1.2 #ifdef linux
81     if ((f=fopen("/etc/mtab", "r" ))==NULL){
82     errf("Failed to open mounts (%m)");
83     return NULL;
84     }
85    
86     while((mp=getmntent(f))){
87     if ((statfs(mp->mnt_dir, &df)) !=0){
88     errf("Failed to gets fs stats (%m)");
89     continue;
90     }
91    
92     if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue;
93     sd_ptr=malloc(sizeof(system_disks_t));
94     sd_ptr->disk=malloc(sizeof(disk_stat_t));
95     sd_ptr->disk->device_name=strdup(mp->mnt_fsname);
96     sd_ptr->disk->mnt_point=strdup(mp->mnt_dir);
97     sd_ptr->disk->size=((df.f_bsize/1024) * df.f_blocks);
98     sd_ptr->disk->used=((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree);
99     sd_ptr->disk->avail=((df.f_bsize/1024) * df.f_bavail);
100     sd_ptr->disk->t_inodes=df.f_files;
101     sd_ptr->disk->f_inodes=df.f_ffree;
102     sd_ptr->next_disk=sd;
103     sd=sd_ptr;
104     }
105     #endif
106     #ifdef FREEBSD
107     nummnt=getmntinfo(&mp , MNT_LOCAL);
108     if (nummnt<=0){
109     errf("Failed to get disk stats (%m)");
110     return NULL;
111     }
112    
113     for(;counter<nummnt;counter++){
114     sd_ptr=malloc(sizeof(system_disks_t));
115     sd_ptr->disk=malloc(sizeof(disk_stat_t));
116     sd_ptr->disk->device_name=strdup(mp->f_mntfromname);
117     sd_ptr->disk->mnt_point=strdup(mp->f_mntonname);
118     sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks);
119     sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree);
120     sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail);
121     sd_ptr->disk->t_inodes=mp->f_files;
122     sd_ptr->disk->f_inodes=mp->f_ffree;
123     sd_ptr->next_disk=sd;
124     sd=sd_ptr;
125     }
126     #endif
127    
128    
129 pajs 1.1 sd_ptr=sd;
130     while(sd_ptr!=NULL){
131     printf("%s\n", sd_ptr->disk->device_name);
132     printf("%s\n", sd_ptr->disk->mnt_point);
133     printf("%ld\n", sd_ptr->disk->size);
134     printf("%ld\n", sd_ptr->disk->used);
135     printf("%ld\n", sd_ptr->disk->avail);
136    
137     sd_ptr=sd_ptr->next_disk;
138 pajs 1.2 counter++;
139 pajs 1.1 }
140    
141     return "jibble";
142     }
143    
144     int main(){
145 pajs 1.2 printf("%s",get_disk_stats());
146 pajs 1.1 exit(0);
147     }
148    
149    
150