1 |
|
#include <stdlib.h> |
2 |
|
#include <stdio.h> |
3 |
|
#include <ukcprog.h> |
4 |
< |
#include <strings.h> |
4 |
> |
#include <string.h> |
5 |
|
|
6 |
|
#ifdef SOLARIS |
7 |
|
#include <sys/mnttab.h> |
8 |
|
#include <sys/types.h> |
9 |
|
#include <sys/statvfs.h> |
10 |
|
#endif |
11 |
< |
|
11 |
> |
#ifdef LINUX |
12 |
> |
#include <mntent.h> |
13 |
> |
#include <sys/vfs.h> |
14 |
> |
#endif |
15 |
> |
#ifdef FREEBSD |
16 |
> |
#include <sys/param.h> |
17 |
> |
#include <sys/ucred.h> |
18 |
> |
#include <sys/mount.h> |
19 |
> |
#endif |
20 |
|
typedef struct { |
21 |
|
char *device_name; |
22 |
|
char *mnt_point; |
32 |
|
struct system_disks_t *next_disk; |
33 |
|
}system_disks_t; |
34 |
|
|
35 |
< |
char *disk_stats(){ |
35 |
> |
char *get_disk_stats(){ |
36 |
|
system_disks_t *sd=NULL; |
37 |
|
system_disks_t *sd_ptr=sd; |
30 |
– |
FILE *f; |
38 |
|
int counter=0; |
39 |
|
#ifdef SOLARIS |
40 |
|
struct mnttab mp; |
41 |
|
struct statvfs df; |
42 |
+ |
FILE *f; |
43 |
|
#endif |
44 |
|
#ifdef LINUX |
45 |
|
struct mntent *mp; |
46 |
|
struct statfs df; |
47 |
+ |
FILE *f; |
48 |
|
#endif |
49 |
|
#ifdef FREEBSD |
50 |
|
int nummnt; |
78 |
|
} |
79 |
|
|
80 |
|
#endif |
81 |
+ |
#ifdef linux |
82 |
+ |
if ((f=fopen("/etc/mtab", "r" ))==NULL){ |
83 |
+ |
errf("Failed to open mounts (%m)"); |
84 |
+ |
return NULL; |
85 |
+ |
} |
86 |
+ |
|
87 |
+ |
while((mp=getmntent(f))){ |
88 |
+ |
if ((statfs(mp->mnt_dir, &df)) !=0){ |
89 |
+ |
errf("Failed to gets fs stats (%m)"); |
90 |
+ |
continue; |
91 |
+ |
} |
92 |
+ |
|
93 |
+ |
if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue; |
94 |
+ |
sd_ptr=malloc(sizeof(system_disks_t)); |
95 |
+ |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
96 |
+ |
sd_ptr->disk->device_name=strdup(mp->mnt_fsname); |
97 |
+ |
sd_ptr->disk->mnt_point=strdup(mp->mnt_dir); |
98 |
+ |
sd_ptr->disk->size=((df.f_bsize/1024) * df.f_blocks); |
99 |
+ |
sd_ptr->disk->used=((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree); |
100 |
+ |
sd_ptr->disk->avail=((df.f_bsize/1024) * df.f_bavail); |
101 |
+ |
sd_ptr->disk->t_inodes=df.f_files; |
102 |
+ |
sd_ptr->disk->f_inodes=df.f_ffree; |
103 |
+ |
sd_ptr->next_disk=sd; |
104 |
+ |
sd=sd_ptr; |
105 |
+ |
} |
106 |
+ |
#endif |
107 |
+ |
#ifdef FREEBSD |
108 |
+ |
nummnt=getmntinfo(&mp , MNT_LOCAL); |
109 |
+ |
if (nummnt<=0){ |
110 |
+ |
errf("Failed to get disk stats (%m)"); |
111 |
+ |
return NULL; |
112 |
+ |
} |
113 |
+ |
|
114 |
+ |
for(;counter<nummnt;counter++){ |
115 |
+ |
sd_ptr=malloc(sizeof(system_disks_t)); |
116 |
+ |
sd_ptr->disk=malloc(sizeof(disk_stat_t)); |
117 |
+ |
sd_ptr->disk->device_name=strdup(mp->f_mntfromname); |
118 |
+ |
sd_ptr->disk->mnt_point=strdup(mp->f_mntonname); |
119 |
+ |
sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks); |
120 |
+ |
sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree); |
121 |
+ |
sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail); |
122 |
+ |
sd_ptr->disk->t_inodes=mp->f_files; |
123 |
+ |
sd_ptr->disk->f_inodes=mp->f_ffree; |
124 |
+ |
sd_ptr->next_disk=sd; |
125 |
+ |
sd=sd_ptr; |
126 |
+ |
mp++; |
127 |
+ |
} |
128 |
+ |
#endif |
129 |
+ |
|
130 |
+ |
|
131 |
|
sd_ptr=sd; |
132 |
|
while(sd_ptr!=NULL){ |
133 |
|
printf("%s\n", sd_ptr->disk->device_name); |
137 |
|
printf("%ld\n", sd_ptr->disk->avail); |
138 |
|
|
139 |
|
sd_ptr=sd_ptr->next_disk; |
140 |
+ |
counter++; |
141 |
|
} |
142 |
|
|
143 |
|
return "jibble"; |
144 |
|
} |
145 |
|
|
146 |
|
int main(){ |
147 |
< |
printf("%s",disk_stats()); |
147 |
> |
printf("%s",get_disk_stats()); |
148 |
|
exit(0); |
149 |
|
} |
150 |
|
|