1 |
|
#include <stdlib.h> |
2 |
|
#include <stdio.h> |
3 |
< |
#include <ukcprog.h> |
3 |
> |
#include "ukcprog.h" |
4 |
|
#include <string.h> |
5 |
|
|
6 |
|
#ifdef SOLARIS |
36 |
|
system_disks_t *sd=NULL; |
37 |
|
system_disks_t *sd_ptr=sd; |
38 |
|
int counter=0; |
39 |
+ |
char *xml_disk_stats; |
40 |
+ |
char *xml_disk_stats_ptr; |
41 |
|
#ifdef SOLARIS |
42 |
|
struct mnttab mp; |
43 |
|
struct statvfs df; |
64 |
|
continue; |
65 |
|
} |
66 |
|
if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){ |
67 |
< |
sd_ptr=malloc(sizeof(system_disks_t)); |
68 |
< |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
69 |
< |
sd_ptr->disk->device_name=strdup(mp.mnt_special); |
70 |
< |
sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp); |
67 |
> |
if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){ |
68 |
> |
errf("malloc failed (%m)"); |
69 |
> |
return NULL; |
70 |
> |
} |
71 |
> |
if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){ |
72 |
> |
errf("malloc failed (%m)"); |
73 |
> |
return NULL; |
74 |
> |
} |
75 |
> |
if((sd_ptr->disk->device_name=strdup(mp.mnt_special)) == NULL) { |
76 |
> |
errf("strdup failed (%m)"); |
77 |
> |
return NULL; |
78 |
> |
} |
79 |
> |
if((sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp)) == NULL){ |
80 |
> |
errf("strdup failed (%m)"); |
81 |
> |
return NULL; |
82 |
> |
} |
83 |
|
sd_ptr->disk->size=((df.f_frsize/1024) * df.f_blocks); |
84 |
|
sd_ptr->disk->used=(((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree)); |
85 |
|
sd_ptr->disk->avail=(df.f_frsize/1024) * df.f_bavail; |
91 |
|
|
92 |
|
} |
93 |
|
|
94 |
+ |
if((fclose(f))!=0){ |
95 |
+ |
errf("Failed to close mnttab"); |
96 |
+ |
return NULL; |
97 |
+ |
} |
98 |
+ |
|
99 |
|
#endif |
100 |
|
#ifdef linux |
101 |
|
if ((f=fopen("/etc/mtab", "r" ))==NULL){ |
110 |
|
} |
111 |
|
|
112 |
|
if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue; |
113 |
< |
sd_ptr=malloc(sizeof(system_disks_t)); |
114 |
< |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
115 |
< |
sd_ptr->disk->device_name=strdup(mp->mnt_fsname); |
116 |
< |
sd_ptr->disk->mnt_point=strdup(mp->mnt_dir); |
113 |
> |
if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){ |
114 |
> |
errf("malloc failed (%m)"); |
115 |
> |
return NULL; |
116 |
> |
} |
117 |
> |
if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){ |
118 |
> |
errf("malloc failed (%m)"); |
119 |
> |
return NULL; |
120 |
> |
} |
121 |
> |
if((sd_ptr->disk->device_name=strdup(mp->mnt_fsname)) == NULL){ |
122 |
> |
errf("strdup failed (%m)"); |
123 |
> |
return NULL; |
124 |
> |
} |
125 |
> |
if((sd_ptr->disk->mnt_point=strdup(mp->mnt_dir)) == NULL){ |
126 |
> |
errf("strdup failed (%m)"); |
127 |
> |
return NULL; |
128 |
> |
} |
129 |
|
sd_ptr->disk->size=((df.f_bsize/1024) * df.f_blocks); |
130 |
|
sd_ptr->disk->used=((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree); |
131 |
|
sd_ptr->disk->avail=((df.f_bsize/1024) * df.f_bavail); |
142 |
|
return NULL; |
143 |
|
} |
144 |
|
|
145 |
< |
for(;counter<nummnt;counter++){ |
146 |
< |
sd_ptr=malloc(sizeof(system_disks_t)); |
147 |
< |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
148 |
< |
sd_ptr->disk->device_name=strdup(mp->f_mntfromname); |
149 |
< |
sd_ptr->disk->mnt_point=strdup(mp->f_mntonname); |
145 |
> |
for(counter=0;counter<nummnt;counter++){ |
146 |
> |
if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){ |
147 |
> |
errf("malloc failed (%m)"); |
148 |
> |
return NULL; |
149 |
> |
} |
150 |
> |
if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){ |
151 |
> |
errf("malloc failed (%m)"); |
152 |
> |
return NULL; |
153 |
> |
} |
154 |
> |
if((sd_ptr->disk->device_name=strdup(mp->f_mntfromname))==NULL){ |
155 |
> |
errf("strdup failed (%m)"); |
156 |
> |
return NULL; |
157 |
> |
} |
158 |
> |
if((sd_ptr->disk->mnt_point=strdup(mp->f_mntonname))==NULL){ |
159 |
> |
errf("strdup failed (%m)"); |
160 |
> |
return NULL; |
161 |
> |
} |
162 |
|
sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks); |
163 |
|
sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree); |
164 |
|
sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail); |
172 |
|
|
173 |
|
|
174 |
|
sd_ptr=sd; |
175 |
+ |
xml_disk_stats=strdup("<disk>"); |
176 |
+ |
|
177 |
|
while(sd_ptr!=NULL){ |
178 |
< |
printf("%s\n", sd_ptr->disk->device_name); |
179 |
< |
printf("%s\n", sd_ptr->disk->mnt_point); |
180 |
< |
printf("%ld\n", sd_ptr->disk->size); |
181 |
< |
printf("%ld\n", sd_ptr->disk->used); |
182 |
< |
printf("%ld\n", sd_ptr->disk->avail); |
183 |
< |
|
178 |
> |
xml_disk_stats_ptr=xml_disk_stats; |
179 |
> |
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){ |
180 |
> |
errf("strf failed (%m)"); |
181 |
> |
return NULL; |
182 |
> |
} |
183 |
> |
free(xml_disk_stats_ptr); |
184 |
|
sd_ptr=sd_ptr->next_disk; |
185 |
|
counter++; |
186 |
|
} |
187 |
+ |
xml_disk_stats_ptr=xml_disk_stats; |
188 |
+ |
xml_disk_stats=strf("%s</disk>",xml_disk_stats); |
189 |
+ |
free(xml_disk_stats_ptr); |
190 |
|
|
191 |
< |
return "jibble"; |
191 |
> |
/* Cleaning up */ |
192 |
> |
sd_ptr=sd; |
193 |
> |
while(sd_ptr!=NULL){ |
194 |
> |
sd=sd_ptr->next_disk; |
195 |
> |
free(sd_ptr->disk->device_name); |
196 |
> |
free(sd_ptr->disk->mnt_point); |
197 |
> |
free(sd_ptr->disk); |
198 |
> |
free(sd_ptr); |
199 |
> |
sd_ptr=sd; |
200 |
> |
} |
201 |
> |
|
202 |
> |
return xml_disk_stats; |
203 |
|
} |
204 |
|
|
146 |
– |
int main(){ |
147 |
– |
printf("%s",get_disk_stats()); |
148 |
– |
exit(0); |
149 |
– |
} |
150 |
– |
|
151 |
– |
|
152 |
– |
|