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.10
Committed: Tue May 21 16:16:41 2002 UTC (22 years, 4 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.9: +3 -3 lines
Log Message:
Bug fixes

File Contents

# User Rev Content
1 tdb 1.7 /*
2     * i-scream central monitoring system
3     * Copyright (C) 2000-2002 i-scream
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     */
19    
20 pajs 1.1 #include <stdlib.h>
21     #include <stdio.h>
22 pajs 1.4 #include "ukcprog.h"
23 pajs 1.2 #include <string.h>
24 pajs 1.1
25     #ifdef SOLARIS
26     #include <sys/mnttab.h>
27     #include <sys/types.h>
28     #include <sys/statvfs.h>
29     #endif
30 pajs 1.2 #ifdef LINUX
31     #include <mntent.h>
32     #include <sys/vfs.h>
33     #endif
34     #ifdef FREEBSD
35     #include <sys/param.h>
36     #include <sys/ucred.h>
37     #include <sys/mount.h>
38     #endif
39 pajs 1.1 typedef struct {
40     char *device_name;
41     char *mnt_point;
42     long size;
43     long used;
44     long avail;
45     long t_inodes;
46     long f_inodes;
47     }disk_stat_t;
48    
49     typedef struct system_disks_t{
50     disk_stat_t *disk;
51     struct system_disks_t *next_disk;
52     }system_disks_t;
53    
54 pajs 1.2 char *get_disk_stats(){
55 pajs 1.1 system_disks_t *sd=NULL;
56     system_disks_t *sd_ptr=sd;
57     int counter=0;
58 pajs 1.4 char *xml_disk_stats;
59 pajs 1.5 char *xml_disk_stats_ptr;
60 pajs 1.1 #ifdef SOLARIS
61     struct mnttab mp;
62     struct statvfs df;
63 pajs 1.3 FILE *f;
64 pajs 1.1 #endif
65     #ifdef LINUX
66     struct mntent *mp;
67     struct statfs df;
68 pajs 1.3 FILE *f;
69 pajs 1.1 #endif
70     #ifdef FREEBSD
71     int nummnt;
72     struct statfs *mp;
73     #endif
74    
75     #ifdef SOLARIS
76     if ((f=fopen("/etc/mnttab", "r" ))==NULL){
77     errf("Failed to open mounts (%m)");
78     return NULL;
79     }
80     while((getmntent(f, &mp)) == 0){
81     if ((statvfs(mp.mnt_mountp, &df)) !=0){
82     errf("Failed to gets fs stats (%m)");
83     continue;
84     }
85     if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){
86 pajs 1.4 if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){
87     errf("malloc failed (%m)");
88     return NULL;
89     }
90     if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){
91     errf("malloc failed (%m)");
92     return NULL;
93     }
94     if((sd_ptr->disk->device_name=strdup(mp.mnt_special)) == NULL) {
95     errf("strdup failed (%m)");
96     return NULL;
97     }
98     if((sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp)) == NULL){
99     errf("strdup failed (%m)");
100     return NULL;
101     }
102 pajs 1.1 sd_ptr->disk->size=((df.f_frsize/1024) * df.f_blocks);
103     sd_ptr->disk->used=(((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree));
104     sd_ptr->disk->avail=(df.f_frsize/1024) * df.f_bavail;
105     sd_ptr->disk->t_inodes=df.f_files;
106     sd_ptr->disk->f_inodes=df.f_ffree;
107     sd_ptr->next_disk=sd;
108     sd=sd_ptr;
109     }
110    
111     }
112    
113 pajs 1.6 if((fclose(f))!=0){
114     errf("Failed to close mnttab");
115     return NULL;
116     }
117    
118 pajs 1.1 #endif
119 pajs 1.10 #ifdef LINUX
120     if ((f=setmntent("/etc/mtab", "r" ))==NULL){
121 pajs 1.2 errf("Failed to open mounts (%m)");
122     return NULL;
123     }
124    
125     while((mp=getmntent(f))){
126     if ((statfs(mp->mnt_dir, &df)) !=0){
127     errf("Failed to gets fs stats (%m)");
128     continue;
129     }
130    
131     if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue;
132 pajs 1.4 if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){
133     errf("malloc failed (%m)");
134     return NULL;
135     }
136     if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){
137     errf("malloc failed (%m)");
138     return NULL;
139     }
140     if((sd_ptr->disk->device_name=strdup(mp->mnt_fsname)) == NULL){
141     errf("strdup failed (%m)");
142     return NULL;
143     }
144     if((sd_ptr->disk->mnt_point=strdup(mp->mnt_dir)) == NULL){
145     errf("strdup failed (%m)");
146     return NULL;
147     }
148 pajs 1.2 sd_ptr->disk->size=((df.f_bsize/1024) * df.f_blocks);
149     sd_ptr->disk->used=((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree);
150     sd_ptr->disk->avail=((df.f_bsize/1024) * df.f_bavail);
151     sd_ptr->disk->t_inodes=df.f_files;
152     sd_ptr->disk->f_inodes=df.f_ffree;
153     sd_ptr->next_disk=sd;
154     sd=sd_ptr;
155     }
156 pajs 1.8
157 pajs 1.10 if ((endmntent(f)) != 0) {
158 pajs 1.8 errf("Failed to close file (%m)");
159     return NULL;
160     }
161    
162 pajs 1.2 #endif
163     #ifdef FREEBSD
164     nummnt=getmntinfo(&mp , MNT_LOCAL);
165     if (nummnt<=0){
166     errf("Failed to get disk stats (%m)");
167     return NULL;
168     }
169    
170 pajs 1.4 for(counter=0;counter<nummnt;counter++){
171     if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){
172     errf("malloc failed (%m)");
173     return NULL;
174     }
175     if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){
176     errf("malloc failed (%m)");
177     return NULL;
178     }
179     if((sd_ptr->disk->device_name=strdup(mp->f_mntfromname))==NULL){
180     errf("strdup failed (%m)");
181     return NULL;
182     }
183     if((sd_ptr->disk->mnt_point=strdup(mp->f_mntonname))==NULL){
184     errf("strdup failed (%m)");
185     return NULL;
186     }
187 pajs 1.2 sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks);
188     sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree);
189     sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail);
190     sd_ptr->disk->t_inodes=mp->f_files;
191     sd_ptr->disk->f_inodes=mp->f_ffree;
192     sd_ptr->next_disk=sd;
193     sd=sd_ptr;
194 pajs 1.3 mp++;
195 pajs 1.2 }
196     #endif
197    
198    
199 pajs 1.1 sd_ptr=sd;
200 pajs 1.4 xml_disk_stats=strdup("<disk>");
201    
202 pajs 1.9 for(counter=0;sd_ptr!=NULL;counter++){
203 pajs 1.5 xml_disk_stats_ptr=xml_disk_stats;
204 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){
205     errf("strf failed (%m)");
206     return NULL;
207     }
208 pajs 1.5 free(xml_disk_stats_ptr);
209 pajs 1.1 sd_ptr=sd_ptr->next_disk;
210     }
211 pajs 1.6 xml_disk_stats_ptr=xml_disk_stats;
212 pajs 1.4 xml_disk_stats=strf("%s</disk>",xml_disk_stats);
213 pajs 1.6 free(xml_disk_stats_ptr);
214 pajs 1.5
215     /* Cleaning up */
216     sd_ptr=sd;
217     while(sd_ptr!=NULL){
218     sd=sd_ptr->next_disk;
219     free(sd_ptr->disk->device_name);
220     free(sd_ptr->disk->mnt_point);
221     free(sd_ptr->disk);
222     free(sd_ptr);
223     sd_ptr=sd;
224     }
225 pajs 1.4
226     return xml_disk_stats;
227 pajs 1.1 }
228