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.5
Committed: Mon May 13 13:21:36 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.4: +14 -0 lines
Log Message:
Free'ing everything properly

File Contents

# User Rev Content
1 pajs 1.1 #include <stdlib.h>
2     #include <stdio.h>
3 pajs 1.4 #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 pajs 1.4 char *xml_disk_stats;
40 pajs 1.5 char *xml_disk_stats_ptr;
41 pajs 1.1 #ifdef SOLARIS
42     struct mnttab mp;
43     struct statvfs df;
44 pajs 1.3 FILE *f;
45 pajs 1.1 #endif
46     #ifdef LINUX
47     struct mntent *mp;
48     struct statfs df;
49 pajs 1.3 FILE *f;
50 pajs 1.1 #endif
51     #ifdef FREEBSD
52     int nummnt;
53     struct statfs *mp;
54     #endif
55    
56     #ifdef SOLARIS
57     if ((f=fopen("/etc/mnttab", "r" ))==NULL){
58     errf("Failed to open mounts (%m)");
59     return NULL;
60     }
61     while((getmntent(f, &mp)) == 0){
62     if ((statvfs(mp.mnt_mountp, &df)) !=0){
63     errf("Failed to gets fs stats (%m)");
64     continue;
65     }
66     if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){
67 pajs 1.4 if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){
68     errf("malloc failed (%m)");
69     return NULL;
70     }
71     if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){
72     errf("malloc failed (%m)");
73     return NULL;
74     }
75     if((sd_ptr->disk->device_name=strdup(mp.mnt_special)) == NULL) {
76     errf("strdup failed (%m)");
77     return NULL;
78     }
79     if((sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp)) == NULL){
80     errf("strdup failed (%m)");
81     return NULL;
82     }
83 pajs 1.1 sd_ptr->disk->size=((df.f_frsize/1024) * df.f_blocks);
84     sd_ptr->disk->used=(((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree));
85     sd_ptr->disk->avail=(df.f_frsize/1024) * df.f_bavail;
86     sd_ptr->disk->t_inodes=df.f_files;
87     sd_ptr->disk->f_inodes=df.f_ffree;
88     sd_ptr->next_disk=sd;
89     sd=sd_ptr;
90     }
91    
92     }
93    
94     #endif
95 pajs 1.2 #ifdef linux
96     if ((f=fopen("/etc/mtab", "r" ))==NULL){
97     errf("Failed to open mounts (%m)");
98     return NULL;
99     }
100    
101     while((mp=getmntent(f))){
102     if ((statfs(mp->mnt_dir, &df)) !=0){
103     errf("Failed to gets fs stats (%m)");
104     continue;
105     }
106    
107     if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue;
108 pajs 1.4 if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){
109     errf("malloc failed (%m)");
110     return NULL;
111     }
112     if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){
113     errf("malloc failed (%m)");
114     return NULL;
115     }
116     if((sd_ptr->disk->device_name=strdup(mp->mnt_fsname)) == NULL){
117     errf("strdup failed (%m)");
118     return NULL;
119     }
120     if((sd_ptr->disk->mnt_point=strdup(mp->mnt_dir)) == NULL){
121     errf("strdup failed (%m)");
122     return NULL;
123     }
124 pajs 1.2 sd_ptr->disk->size=((df.f_bsize/1024) * df.f_blocks);
125     sd_ptr->disk->used=((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree);
126     sd_ptr->disk->avail=((df.f_bsize/1024) * df.f_bavail);
127     sd_ptr->disk->t_inodes=df.f_files;
128     sd_ptr->disk->f_inodes=df.f_ffree;
129     sd_ptr->next_disk=sd;
130     sd=sd_ptr;
131     }
132     #endif
133     #ifdef FREEBSD
134     nummnt=getmntinfo(&mp , MNT_LOCAL);
135     if (nummnt<=0){
136     errf("Failed to get disk stats (%m)");
137     return NULL;
138     }
139    
140 pajs 1.4 for(counter=0;counter<nummnt;counter++){
141     if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){
142     errf("malloc failed (%m)");
143     return NULL;
144     }
145     if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){
146     errf("malloc failed (%m)");
147     return NULL;
148     }
149     if((sd_ptr->disk->device_name=strdup(mp->f_mntfromname))==NULL){
150     errf("strdup failed (%m)");
151     return NULL;
152     }
153     if((sd_ptr->disk->mnt_point=strdup(mp->f_mntonname))==NULL){
154     errf("strdup failed (%m)");
155     return NULL;
156     }
157 pajs 1.2 sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks);
158     sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree);
159     sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail);
160     sd_ptr->disk->t_inodes=mp->f_files;
161     sd_ptr->disk->f_inodes=mp->f_ffree;
162     sd_ptr->next_disk=sd;
163     sd=sd_ptr;
164 pajs 1.3 mp++;
165 pajs 1.2 }
166     #endif
167    
168    
169 pajs 1.1 sd_ptr=sd;
170 pajs 1.4 xml_disk_stats=strdup("<disk>");
171    
172 pajs 1.1 while(sd_ptr!=NULL){
173 pajs 1.5 xml_disk_stats_ptr=xml_disk_stats;
174 pajs 1.4 if((xml_disk_stats=strf("%s<p%d name=\"%s\" mount=\"%s\" kbytes=\"%ld\" used=\"%ld\" avail=\"%ld\" totalinodes=\"%ld\" freeinodes=\"%ld\"></p%d>", xml_disk_stats, counter, sd_ptr->disk->device_name, sd_ptr->disk->mnt_point, sd_ptr->disk->size, sd_ptr->disk->used, sd_ptr->disk->avail, sd_ptr->disk->t_inodes, sd_ptr->disk->f_inodes, counter)) == NULL){
175     errf("strf failed (%m)");
176     return NULL;
177     }
178 pajs 1.5 free(xml_disk_stats_ptr);
179 pajs 1.1 sd_ptr=sd_ptr->next_disk;
180 pajs 1.2 counter++;
181 pajs 1.1 }
182    
183 pajs 1.4 xml_disk_stats=strf("%s</disk>",xml_disk_stats);
184 pajs 1.5
185     /* Cleaning up */
186     sd_ptr=sd;
187     while(sd_ptr!=NULL){
188     sd=sd_ptr->next_disk;
189     free(sd_ptr->disk->device_name);
190     free(sd_ptr->disk->mnt_point);
191     free(sd_ptr->disk);
192     free(sd_ptr);
193     sd_ptr=sd;
194     }
195 pajs 1.4
196     return xml_disk_stats;
197 pajs 1.1 }
198