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.3
Committed: Mon May 13 11:37:41 2002 UTC (22 years, 4 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.2: +3 -1 lines
Log Message:
Should now work on FREEBSD ;)

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     int counter=0;
39     #ifdef SOLARIS
40     struct mnttab mp;
41     struct statvfs df;
42 pajs 1.3 FILE *f;
43 pajs 1.1 #endif
44     #ifdef LINUX
45     struct mntent *mp;
46     struct statfs df;
47 pajs 1.3 FILE *f;
48 pajs 1.1 #endif
49     #ifdef FREEBSD
50     int nummnt;
51     struct statfs *mp;
52     #endif
53    
54     #ifdef SOLARIS
55     if ((f=fopen("/etc/mnttab", "r" ))==NULL){
56     errf("Failed to open mounts (%m)");
57     return NULL;
58     }
59     while((getmntent(f, &mp)) == 0){
60     if ((statvfs(mp.mnt_mountp, &df)) !=0){
61     errf("Failed to gets fs stats (%m)");
62     continue;
63     }
64     if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){
65     sd_ptr=malloc(sizeof(system_disks_t));
66     sd_ptr->disk=malloc(sizeof(disk_stat_t));
67     sd_ptr->disk->device_name=strdup(mp.mnt_special);
68     sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp);
69     sd_ptr->disk->size=((df.f_frsize/1024) * df.f_blocks);
70     sd_ptr->disk->used=(((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree));
71     sd_ptr->disk->avail=(df.f_frsize/1024) * df.f_bavail;
72     sd_ptr->disk->t_inodes=df.f_files;
73     sd_ptr->disk->f_inodes=df.f_ffree;
74     sd_ptr->next_disk=sd;
75     sd=sd_ptr;
76     }
77    
78     }
79    
80     #endif
81 pajs 1.2 #ifdef linux
82     if ((f=fopen("/etc/mtab", "r" ))==NULL){
83     errf("Failed to open mounts (%m)");
84     return NULL;
85     }
86    
87     while((mp=getmntent(f))){
88     if ((statfs(mp->mnt_dir, &df)) !=0){
89     errf("Failed to gets fs stats (%m)");
90     continue;
91     }
92    
93     if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue;
94     sd_ptr=malloc(sizeof(system_disks_t));
95     sd_ptr->disk=malloc(sizeof(disk_stat_t));
96     sd_ptr->disk->device_name=strdup(mp->mnt_fsname);
97     sd_ptr->disk->mnt_point=strdup(mp->mnt_dir);
98     sd_ptr->disk->size=((df.f_bsize/1024) * df.f_blocks);
99     sd_ptr->disk->used=((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree);
100     sd_ptr->disk->avail=((df.f_bsize/1024) * df.f_bavail);
101     sd_ptr->disk->t_inodes=df.f_files;
102     sd_ptr->disk->f_inodes=df.f_ffree;
103     sd_ptr->next_disk=sd;
104     sd=sd_ptr;
105     }
106     #endif
107     #ifdef FREEBSD
108     nummnt=getmntinfo(&mp , MNT_LOCAL);
109     if (nummnt<=0){
110     errf("Failed to get disk stats (%m)");
111     return NULL;
112     }
113    
114     for(;counter<nummnt;counter++){
115     sd_ptr=malloc(sizeof(system_disks_t));
116     sd_ptr->disk=malloc(sizeof(disk_stat_t));
117     sd_ptr->disk->device_name=strdup(mp->f_mntfromname);
118     sd_ptr->disk->mnt_point=strdup(mp->f_mntonname);
119     sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks);
120     sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree);
121     sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail);
122     sd_ptr->disk->t_inodes=mp->f_files;
123     sd_ptr->disk->f_inodes=mp->f_ffree;
124     sd_ptr->next_disk=sd;
125     sd=sd_ptr;
126 pajs 1.3 mp++;
127 pajs 1.2 }
128     #endif
129    
130    
131 pajs 1.1 sd_ptr=sd;
132     while(sd_ptr!=NULL){
133     printf("%s\n", sd_ptr->disk->device_name);
134     printf("%s\n", sd_ptr->disk->mnt_point);
135     printf("%ld\n", sd_ptr->disk->size);
136     printf("%ld\n", sd_ptr->disk->used);
137     printf("%ld\n", sd_ptr->disk->avail);
138    
139     sd_ptr=sd_ptr->next_disk;
140 pajs 1.2 counter++;
141 pajs 1.1 }
142    
143     return "jibble";
144     }
145    
146     int main(){
147 pajs 1.2 printf("%s",get_disk_stats());
148 pajs 1.1 exit(0);
149     }
150    
151    
152