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.8
Committed: Sun May 19 12:33:53 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.7: +6 -0 lines
Log Message:
Another linux file forgotten to be closed :/

File Contents

# Content
1 /*
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 #include <stdlib.h>
21 #include <stdio.h>
22 #include "ukcprog.h"
23 #include <string.h>
24
25 #ifdef SOLARIS
26 #include <sys/mnttab.h>
27 #include <sys/types.h>
28 #include <sys/statvfs.h>
29 #endif
30 #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 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 char *get_disk_stats(){
55 system_disks_t *sd=NULL;
56 system_disks_t *sd_ptr=sd;
57 int counter=0;
58 char *xml_disk_stats;
59 char *xml_disk_stats_ptr;
60 #ifdef SOLARIS
61 struct mnttab mp;
62 struct statvfs df;
63 FILE *f;
64 #endif
65 #ifdef LINUX
66 struct mntent *mp;
67 struct statfs df;
68 FILE *f;
69 #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 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 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 if((fclose(f))!=0){
114 errf("Failed to close mnttab");
115 return NULL;
116 }
117
118 #endif
119 #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 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 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
157 if ((fclose(f)) != 0) {
158 errf("Failed to close file (%m)");
159 return NULL;
160 }
161
162 #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 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 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 mp++;
195 }
196 #endif
197
198
199 sd_ptr=sd;
200 xml_disk_stats=strdup("<disk>");
201
202 while(sd_ptr!=NULL){
203 xml_disk_stats_ptr=xml_disk_stats;
204 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 free(xml_disk_stats_ptr);
209 sd_ptr=sd_ptr->next_disk;
210 counter++;
211 }
212 xml_disk_stats_ptr=xml_disk_stats;
213 xml_disk_stats=strf("%s</disk>",xml_disk_stats);
214 free(xml_disk_stats_ptr);
215
216 /* Cleaning up */
217 sd_ptr=sd;
218 while(sd_ptr!=NULL){
219 sd=sd_ptr->next_disk;
220 free(sd_ptr->disk->device_name);
221 free(sd_ptr->disk->mnt_point);
222 free(sd_ptr->disk);
223 free(sd_ptr);
224 sd_ptr=sd;
225 }
226
227 return xml_disk_stats;
228 }
229