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 <strings.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 |
< |
|
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; |
51 |
|
struct system_disks_t *next_disk; |
52 |
|
}system_disks_t; |
53 |
|
|
54 |
< |
char *disk_stats(){ |
54 |
> |
char *get_disk_stats(){ |
55 |
|
system_disks_t *sd=NULL; |
56 |
|
system_disks_t *sd_ptr=sd; |
30 |
– |
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){ |
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 |
+ |
#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 |
+ |
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 |
+ |
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 |
+ |
mp++; |
189 |
+ |
} |
190 |
+ |
#endif |
191 |
+ |
|
192 |
+ |
|
193 |
|
sd_ptr=sd; |
194 |
+ |
xml_disk_stats=strdup("<disk>"); |
195 |
+ |
|
196 |
|
while(sd_ptr!=NULL){ |
197 |
< |
printf("%s\n", sd_ptr->disk->device_name); |
198 |
< |
printf("%s\n", sd_ptr->disk->mnt_point); |
199 |
< |
printf("%ld\n", sd_ptr->disk->size); |
200 |
< |
printf("%ld\n", sd_ptr->disk->used); |
201 |
< |
printf("%ld\n", sd_ptr->disk->avail); |
202 |
< |
|
197 |
> |
xml_disk_stats_ptr=xml_disk_stats; |
198 |
> |
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 |
> |
free(xml_disk_stats_ptr); |
203 |
|
sd_ptr=sd_ptr->next_disk; |
204 |
+ |
counter++; |
205 |
|
} |
206 |
+ |
xml_disk_stats_ptr=xml_disk_stats; |
207 |
+ |
xml_disk_stats=strf("%s</disk>",xml_disk_stats); |
208 |
+ |
free(xml_disk_stats_ptr); |
209 |
|
|
210 |
< |
return "jibble"; |
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 |
> |
|
221 |
> |
return xml_disk_stats; |
222 |
|
} |
223 |
|
|
86 |
– |
int main(){ |
87 |
– |
printf("%s",disk_stats()); |
88 |
– |
exit(0); |
89 |
– |
} |
90 |
– |
|
91 |
– |
|
92 |
– |
|