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.7
Committed: Sat May 18 18:15:56 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.6: +19 -0 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

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.2 #ifdef linux
120     if ((f=fopen("/etc/mtab", "r" ))==NULL){
121     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     #endif
157     #ifdef FREEBSD
158     nummnt=getmntinfo(&mp , MNT_LOCAL);
159     if (nummnt<=0){
160     errf("Failed to get disk stats (%m)");
161     return NULL;
162     }
163    
164 pajs 1.4 for(counter=0;counter<nummnt;counter++){
165     if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){
166     errf("malloc failed (%m)");
167     return NULL;
168     }
169     if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){
170     errf("malloc failed (%m)");
171     return NULL;
172     }
173     if((sd_ptr->disk->device_name=strdup(mp->f_mntfromname))==NULL){
174     errf("strdup failed (%m)");
175     return NULL;
176     }
177     if((sd_ptr->disk->mnt_point=strdup(mp->f_mntonname))==NULL){
178     errf("strdup failed (%m)");
179     return NULL;
180     }
181 pajs 1.2 sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks);
182     sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree);
183     sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail);
184     sd_ptr->disk->t_inodes=mp->f_files;
185     sd_ptr->disk->f_inodes=mp->f_ffree;
186     sd_ptr->next_disk=sd;
187     sd=sd_ptr;
188 pajs 1.3 mp++;
189 pajs 1.2 }
190     #endif
191    
192    
193 pajs 1.1 sd_ptr=sd;
194 pajs 1.4 xml_disk_stats=strdup("<disk>");
195    
196 pajs 1.1 while(sd_ptr!=NULL){
197 pajs 1.5 xml_disk_stats_ptr=xml_disk_stats;
198 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){
199     errf("strf failed (%m)");
200     return NULL;
201     }
202 pajs 1.5 free(xml_disk_stats_ptr);
203 pajs 1.1 sd_ptr=sd_ptr->next_disk;
204 pajs 1.2 counter++;
205 pajs 1.1 }
206 pajs 1.6 xml_disk_stats_ptr=xml_disk_stats;
207 pajs 1.4 xml_disk_stats=strf("%s</disk>",xml_disk_stats);
208 pajs 1.6 free(xml_disk_stats_ptr);
209 pajs 1.5
210     /* Cleaning up */
211     sd_ptr=sd;
212     while(sd_ptr!=NULL){
213     sd=sd_ptr->next_disk;
214     free(sd_ptr->disk->device_name);
215     free(sd_ptr->disk->mnt_point);
216     free(sd_ptr->disk);
217     free(sd_ptr);
218     sd_ptr=sd;
219     }
220 pajs 1.4
221     return xml_disk_stats;
222 pajs 1.1 }
223