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.4
Committed: Mon May 13 12:47:15 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.3: +60 -28 lines
Log Message:
Disk stats now works on all systems giving back a char * of xml

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