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> |
22 |
> |
#include "ukcprog.h" |
23 |
|
#include <string.h> |
24 |
|
|
25 |
|
#ifdef SOLARIS |
54 |
|
char *get_disk_stats(){ |
55 |
|
system_disks_t *sd=NULL; |
56 |
|
system_disks_t *sd_ptr=sd; |
38 |
– |
FILE *f; |
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; |
83 |
|
continue; |
84 |
|
} |
85 |
|
if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){ |
86 |
< |
sd_ptr=malloc(sizeof(system_disks_t)); |
87 |
< |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
88 |
< |
sd_ptr->disk->device_name=strdup(mp.mnt_special); |
89 |
< |
sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp); |
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; |
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){ |
129 |
|
} |
130 |
|
|
131 |
|
if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue; |
132 |
< |
sd_ptr=malloc(sizeof(system_disks_t)); |
133 |
< |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
134 |
< |
sd_ptr->disk->device_name=strdup(mp->mnt_fsname); |
135 |
< |
sd_ptr->disk->mnt_point=strdup(mp->mnt_dir); |
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); |
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); |
167 |
|
return NULL; |
168 |
|
} |
169 |
|
|
170 |
< |
for(;counter<nummnt;counter++){ |
171 |
< |
sd_ptr=malloc(sizeof(system_disks_t)); |
172 |
< |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
173 |
< |
sd_ptr->disk->device_name=strdup(mp->f_mntfromname); |
174 |
< |
sd_ptr->disk->mnt_point=strdup(mp->f_mntonname); |
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); |
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 |
< |
printf("%s\n", sd_ptr->disk->device_name); |
204 |
< |
printf("%s\n", sd_ptr->disk->mnt_point); |
205 |
< |
printf("%ld\n", sd_ptr->disk->size); |
206 |
< |
printf("%ld\n", sd_ptr->disk->used); |
207 |
< |
printf("%ld\n", sd_ptr->disk->avail); |
208 |
< |
|
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 |
< |
return "jibble"; |
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 |
|
|
144 |
– |
int main(){ |
145 |
– |
printf("%s",get_disk_stats()); |
146 |
– |
exit(0); |
147 |
– |
} |
148 |
– |
|
149 |
– |
|
150 |
– |
|