ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/plugins/linux/linux.c
(Generate patch)

Comparing projects/cms/source/host/ihost-perl/plugins/linux/linux.c (file contents):
Revision 1.1 by pajs, Thu Mar 7 18:47:17 2002 UTC vs.
Revision 1.5 by pajs, Mon Mar 11 13:42:40 2002 UTC

# Line 1 | Line 1
1   #include <stdio.h>
2   #include <stdlib.h>
3 < #include <local/ukcprog.h>
3 > #include "ukcprog.h"
4   #include <string.h>
5   #include <unistd.h>
6   #include <sys/utsname.h>
# Line 8 | Line 8
8   #include <utmp.h>
9   #include <pwd.h>
10  
11 < int die()
12 < {
13 <   exit (1);
11 > int die() {
12 >    exit (1);
13   }
14  
15 < void getLoadAv()
16 < {
17 <   FILE *f;
18 <   char *loadavg;
20 <   char *load_p;
15 > void getLoadAv() {
16 >    FILE *f;
17 >    char *loadavg;
18 >    char *load_p;
19        
20 <   if ((f=fopen("/proc/loadavg", "r" ))==NULL)
21 <     {
22 <        errf("Failed to open load averages (%m)");
23 <        die();
24 <     }
25 <  
26 <  
27 <   if ((loadavg=fpgetline(f)) == NULL)
28 <     {
29 <        errf("Failed to read any data for load averages (%m)");
30 <        die();
31 <     }
32 <  
33 <  
34 <   if ((fclose(f)) != 0)
35 <     {
36 <        
37 <        errf("Failed to close file (%m).");
38 <        die();
39 <     }
40 <  
41 <
42 <  
43 <
44 <   load_p=strtok(loadavg, " ");
45 <   printf("packet.load.load1 %s\n",load_p);
46 <   for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
47 <   load_p++;
48 <   if (load_p == NULL) abort();
51 <   load_p=strtok(load_p, " ");
52 <   if (load_p == NULL) abort();
53 <   printf("packet.load.load5 %s\n",load_p);
54 <   for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
55 <   load_p++;
56 <   if (load_p == NULL) abort();
57 <   load_p=strtok(load_p, " ");
58 <   if (load_p == NULL) abort();
59 <   printf("packet.load.load15 %s\n",load_p);
60 <
20 >    if ((f=fopen("/proc/loadavg", "r" ))==NULL) {
21 >        errf("Failed to open load averages (%m)");
22 >        die();
23 >    }
24 >    
25 >    if ((loadavg=fpgetline(f)) == NULL) {
26 >        errf("Failed to read any data for load averages (%m)");
27 >        die();
28 >    }
29 >    
30 >    if ((fclose(f)) != 0) {
31 >        errf("Failed to close file (%m).");
32 >        die();
33 >    }
34 >    
35 >    load_p=strtok(loadavg, " ");
36 >    printf("packet.load.load1 %s\n",load_p);
37 >    for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
38 >    load_p++;
39 >    if (load_p == NULL) abort();
40 >    load_p=strtok(load_p, " ");
41 >    if (load_p == NULL) abort();
42 >    printf("packet.load.load5 %s\n",load_p);
43 >    for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
44 >    load_p++;
45 >    if (load_p == NULL) abort();
46 >    load_p=strtok(load_p, " ");
47 >    if (load_p == NULL) abort();
48 >    printf("packet.load.load15 %s\n",load_p);
49   }
50  
51 < void getMemInfo()
52 < {
53 <   char *line;
54 <   char *mem;
55 <   char *swap;
56 <   char *ch;
57 <   long memstat[6];
58 <   long swapstat[3];
59 <   long tmp;
60 <   int counter=0;
61 <
62 <   FILE *f;
63 <  
64 <   if ((f=fopen("/proc/meminfo", "r" ))==NULL)
65 <     {
66 <        errf("Failed to open memory stats (%m)");
67 <        die();
68 <     }
69 <  
70 <   /* Readin being the number of lines we want to get.. Once we have them, why bother continuing */
71 <   while(((line=fpgetline(f)) != NULL) && (counter < 2) )
72 <     {
73 <        
74 <        if (((strncmp("Mem: ",line,5)) == 0))
75 <          {
76 <             mem=strdup(line);
77 <             counter++;
78 <          }
79 <        if (((strncmp(line,"Swap: ",6)) == 0))
80 <          {
81 <             swap=strdup(line);
82 <             counter++;
83 <          }
84 <     }
85 <  
86 <  
87 <   if ((fclose(f)) != 0)
88 <     {
89 <        errf("Failed to close file (%m).");
90 <        die();
91 <     }
92 <
93 <   /* Get the info we want from the 2 read in lines */
94 <  
95 <   ch = strchr(mem, ' ');
96 <   if (ch == NULL) abort();
97 <   *ch = '\0';
98 <   ch++;
99 <   /* By now we should of skipped the mem: bit.. onto the numbers */
100 <  
101 <   for(counter=0;counter<6;counter++)
102 <     {
103 <        for (; (*ch == ' '); ch++);
104 <        if (ch == NULL) abort();
105 <        memstat[counter]=atol(ch);
106 <        ch++;
107 <        for(; (*ch != ' ') && (*ch != '\0'); ch++);
108 <        if (ch == NULL) abort();
109 <     }
110 <        
111 <      
112 <   /* Now swap.. */
113 <   ch = strchr(swap, ' ');
114 <   if (ch == NULL) abort();
115 <   *ch = '\0';
116 <   ch++;
117 <  
118 <   for(counter=0;counter<3;counter++)
119 <     {  
120 <        for (; (*ch == ' '); ch++);
121 <        if (ch == NULL) abort();
122 <        swapstat[counter]=atol(ch);
123 <        ch++;  
124 <        for(; (*ch != ' ') && (*ch != '\0'); ch++);
125 <        if (ch == NULL) abort();
126 <        
127 <     }
128 <  
129 <   printf("packet.memory.total %ld\n",memstat[0]);
130 <   /* Due to batty linux we do some maths to work out roughly what the free ram is */
143 <   tmp=memstat[1] - memstat[4];
144 <   printf("packet.memory.used %ld\n",tmp);
145 <   tmp=memstat[2] + memstat[4];
146 <   printf("packet.memory.free %ld\n",tmp);
147 <
148 <   printf("packet.swap.total %ld\n",swapstat[0]);
149 <   printf("packet.swap.used %ld\n",swapstat[1]);
150 <   printf("packet.swap.free %ld\n",swapstat[2]);
151 <  
152 <   free(mem);
153 <   free(swap);
154 <  
51 > void getMemInfo() {
52 >    char *line;
53 >    char *mem;
54 >    char *swap;
55 >    char *ch;
56 >    long memstat[6];
57 >    long swapstat[3];
58 >    long tmp;
59 >    int counter=0;
60 >    
61 >    FILE *f;
62 >    
63 >    if ((f=fopen("/proc/meminfo", "r" ))==NULL) {
64 >        errf("Failed to open memory stats (%m)");
65 >        die();
66 >    }
67 >    
68 >    while(((line=fpgetline(f)) != NULL) && (counter < 2)) {
69 >        if (((strncmp("Mem: ",line,5)) == 0)) {
70 >            mem=strdup(line);
71 >            counter++;
72 >        }
73 >        if (((strncmp(line,"Swap: ",6)) == 0)) {
74 >            swap=strdup(line);
75 >            counter++;
76 >        }
77 >    }
78 >    
79 >    if ((fclose(f)) != 0) {
80 >        errf("Failed to close file (%m).");
81 >        die();
82 >    }
83 >    
84 >    /* Get the info we want from the 2 read in lines */
85 >    
86 >    ch = strchr(mem, ' ');
87 >    if (ch == NULL) abort();
88 >    *ch = '\0';
89 >    ch++;
90 >    
91 >    /* By now we should of skipped the mem: bit.. onto the numbers */
92 >    
93 >    for(counter=0;counter<6;counter++) {
94 >        for (; (*ch == ' '); ch++);
95 >        if (ch == NULL) abort();
96 >        memstat[counter]=atol(ch);
97 >        ch++;
98 >        for(; (*ch != ' ') && (*ch != '\0'); ch++);
99 >        if (ch == NULL) abort();
100 >    }
101 >    
102 >    /* Now swap.. */
103 >    ch = strchr(swap, ' ');
104 >    if (ch == NULL) abort();
105 >    *ch = '\0';
106 >    ch++;
107 >    
108 >    for(counter=0;counter<3;counter++) {
109 >        for (; (*ch == ' '); ch++);
110 >        if (ch == NULL) abort();
111 >        swapstat[counter]=atol(ch);
112 >        ch++;
113 >        for(; (*ch != ' ') && (*ch != '\0'); ch++);
114 >        if (ch == NULL) abort();
115 >    }
116 >    
117 >    printf("packet.memory.total %ld\n",((memstat[0]/1024)/1024));
118 >    
119 >    /* Due to batty linux we do some maths to work out roughly what the free ram is */
120 >    tmp=((memstat[1] - memstat[4])/1024)/1024;
121 >    printf("packet.memory.used %ld\n",tmp);
122 >    tmp=((memstat[2] + memstat[4])/1024)/1024;
123 >    printf("packet.memory.free %ld\n",tmp);
124 >    
125 >    printf("packet.swap.total %ld\n",((swapstat[0]/1024)/1024));
126 >    printf("packet.swap.used %ld\n",((swapstat[1]/1024)/1024));
127 >    printf("packet.swap.free %ld\n",((swapstat[2])/1024)/1024);
128 >      
129 >    free(mem);
130 >    free(swap);
131   }
132  
133 < void cpustats()
134 < {
135 <   char *tmp;
136 <   char *line[2];
137 <   char *line_p[2];
138 <   long cpustats[4][2];
139 <   long user, kernel, idle;
140 <   long total;
141 <   int x,y;
142 <   float usage;
143 <   FILE *f;
144 <  
145 <   if ((f=fopen("/proc/stat", "r" ))==NULL)
146 <     {
147 <        errf("Failed to open cpu stats (%m)");
148 <        die();
149 <     }
150 <
151 <   if((tmp=fpgetline(f)) == NULL)
152 <     {
153 <        errf("Failed to read cpu stats (%m)");
154 <        die();
155 <     }
156 <
157 <   if((line[0]=strdup(tmp)) == NULL)
158 <     {
159 <        errf("strdup failed (%m)");
160 <        die();
161 <     }
162 <  
187 <   if ((fclose(f)) != 0)
188 <     {
189 <        errf("Failed to close file (%m).");
190 <        die();
191 <     }
133 > void cpustats() {
134 >    char *tmp;
135 >    char *line[2];
136 >    char *line_p[2];
137 >    long cpustats[4][2];
138 >    long user, kernel, idle;
139 >    long total;
140 >    int x,y;
141 >    float usage;
142 >    FILE *f;
143 >    
144 >    if ((f=fopen("/proc/stat", "r" ))==NULL) {
145 >        errf("Failed to open cpu stats (%m)");
146 >        die();
147 >    }
148 >    
149 >    if((tmp=fpgetline(f)) == NULL) {
150 >        errf("Failed to read cpu stats (%m)");
151 >        die();
152 >    }
153 >    
154 >    if((line[0]=strdup(tmp)) == NULL) {
155 >        errf("strdup failed (%m)");
156 >        die();
157 >    }
158 >    
159 >    if ((fclose(f)) != 0) {
160 >        errf("Failed to close file (%m).");
161 >        die();
162 >    }
163      
164 <   sleep(1);
165 <  
166 <   if ((f=fopen("/proc/stat", "r" ))==NULL)
167 <     {
168 <        errf("Failed to open cpu stats (%m)");
169 <        die();
170 <     }
171 <  
172 <   if((tmp=fpgetline(f)) == NULL)
173 <     {
174 <        errf("Failed to read cpu stats (%m)");
175 <        die();
176 <     }
177 <  
178 <   if((line[1]=strdup(tmp)) == NULL)
179 <     {
209 <        errf("strdup failed (%m)");
210 <        die();
211 <     }
164 >    sleep(1);
165 >    
166 >    if ((f=fopen("/proc/stat", "r" ))==NULL) {
167 >        errf("Failed to open cpu stats (%m)");
168 >        die();
169 >    }
170 >    
171 >    if((tmp=fpgetline(f)) == NULL) {
172 >        errf("Failed to read cpu stats (%m)");
173 >        die();
174 >    }
175 >    
176 >    if((line[1]=strdup(tmp)) == NULL) {
177 >        errf("strdup failed (%m)");
178 >        die();
179 >    }
180        
181 <     if ((fclose(f)) != 0)
182 <     {
183 <        errf("Failed to close file (%m).");
184 <        die();
185 <     }
186 <  
187 <      for(x=0;x<2;x++)
188 <     {
189 <        line_p[x] = strchr(line[x], ' ');
190 <        if (line_p[x] == NULL) abort();
191 <        *line_p[x] = '\0';
192 <        line_p[x]++;
193 <     }
194 <  
195 <   /* Now should be passed "cpu " */
196 <  
197 <  
198 <   for(x=0;x<2;x++)
199 <     {
200 <        for(y=0;y<4;y++)
201 <          {
202 <             for (; (*line_p[x] == ' '); line_p[x]++);
203 <             if (line_p[x] == NULL) abort();
204 <             cpustats[y][x]=atol(line_p[x]);
205 <             line_p[x]++;      
206 <             for(; (*line_p[x] != ' ') && (*line_p[x] != '\0'); line_p[x]++);
207 <             if (line_p[x] == NULL) abort();
208 <             for (; (*line_p[x] == ' '); line_p[x]++);
209 <          }
210 <        
211 <     }
212 <  
213 <   user=cpustats[0][1]-cpustats[0][0]+cpustats[1][1]-cpustats[1][0];
214 <   kernel=cpustats[2][1]-cpustats[2][0];
215 <   idle=cpustats[3][1]-cpustats[3][0];
216 <  
217 <   /* use total to get the total number and then work out the percentages */
218 <   total=user+kernel+idle;
219 <  
220 <   usage=((((float)user)/((float)total))*100.00);
221 <   printf("packet.cpu.user %3.2f\n", usage);
222 <   usage=((((float)kernel)/((float)total))*100.00);
223 <   printf("packet.cpu.kernel %3.2f\n", usage);
224 <   usage=((((float)idle)/((float)total))*100.00);
225 <   printf("packet.cpu.idle %3.2f\n", usage);
226 <  
259 <   /* Cos iscreams broke ;) */
260 <   printf("packet.cpu.iowait 0\n");
261 <  
262 <   free(line[0]);
263 <   free(line[1]);
181 >    if ((fclose(f)) != 0) {
182 >        errf("Failed to close file (%m).");
183 >        die();
184 >    }
185 >    
186 >    for(x=0;x<2;x++) {
187 >        line_p[x] = strchr(line[x], ' ');
188 >        if (line_p[x] == NULL) abort();
189 >        *line_p[x] = '\0';
190 >        line_p[x]++;
191 >    }
192 >    
193 >    /* Now should be passed "cpu " */
194 >    
195 >    for(x=0;x<2;x++) {
196 >        for(y=0;y<4;y++) {
197 >            for (; (*line_p[x] == ' '); line_p[x]++);
198 >            if (line_p[x] == NULL) abort();
199 >            cpustats[y][x]=atol(line_p[x]);
200 >            line_p[x]++;    
201 >            for(; (*line_p[x] != ' ') && (*line_p[x] != '\0'); line_p[x]++);
202 >            if (line_p[x] == NULL) abort();
203 >            for (; (*line_p[x] == ' '); line_p[x]++);
204 >        }
205 >    }
206 >    
207 >    user=cpustats[0][1]-cpustats[0][0]+cpustats[1][1]-cpustats[1][0];
208 >    kernel=cpustats[2][1]-cpustats[2][0];
209 >    idle=cpustats[3][1]-cpustats[3][0];
210 >    
211 >    /* use total to get the total number and then work out the percentages */
212 >    total=user+kernel+idle;
213 >    
214 >    usage=((((float)user)/((float)total))*100.00);
215 >    printf("packet.cpu.user %3.2f\n", usage);
216 >    usage=((((float)kernel)/((float)total))*100.00);
217 >    printf("packet.cpu.kernel %3.2f\n", usage);
218 >    usage=((((float)idle)/((float)total))*100.00);
219 >    printf("packet.cpu.idle %3.2f\n", usage);
220 >    
221 >    /* Cos i-scream's expects this to be sent :/ */
222 >    printf("packet.cpu.iowait 0\n");
223 >    printf("packet.cpu.swap 0\n");  
224 >
225 >    free(line[0]);
226 >    free(line[1]);
227   }
228  
229 < void processStats()
230 < {
231 <   int sleeping=0;
232 <   int zombie=0;
233 <   int stopped=0;
234 <   int running=0;
235 <   int nousers=0;
236 <   char *line;
237 <   char *line_p;
238 <   struct utmp *entry;
239 <
240 <   FILE *f;
241 <
242 <   if((f=popen("/bin/ps -Al" , "r")) == NULL)
243 <     {
244 <        errf("Failed to get process stats (%m)");
245 <        die();
246 <     }
247 <  
248 <   while((line=fpgetline(f)) != NULL)
249 <     {
250 <        line_p=strchr(line, ' ');
251 <        line_p++;
252 <        if (line_p==NULL) abort();
253 <        /* Ok, we should now be at the state :) .. */
254 <        if (*line_p=='S') sleeping++;
255 <        if (*line_p=='R') running++;
256 <        if (*line_p=='Z') zombie++;
257 <        if (*line_p=='T') stopped++;
258 <          
259 <     }
260 <  
261 <   if((pclose(f)) == -1)
262 <     {
263 <        errf("Failed to close process stats (%m)");
264 <        die();
265 <     }
266 <
267 <
268 <   printf("packet.users.list");
269 <
270 <   while((entry=getutent()) != NULL)
271 <     {
272 <        if(entry->ut_type==USER_PROCESS)
273 <          {
274 <             printf(" %s",entry->ut_user);
275 <             nousers++;
276 <          }
277 <        
315 <     }
316 <  
317 <   printf("\npacket.users.count %d\n", nousers);
318 <  
319 <   printf("packet.processes.total %d\n",sleeping);
320 <   printf("packet.processes.sleeping %d\n",running);
321 <   printf("packet.processes.zombie %d\n",zombie);
322 <   printf("packet.processes.stopped %d\n", stopped);
323 <   printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
324 <  
229 > void processStats() {
230 >    int sleeping=0;
231 >    int zombie=0;
232 >    int stopped=0;
233 >    int running=0;
234 >    int nousers=0;
235 >    char *line;
236 >    char *line_p;
237 >    struct utmp *entry;
238 >    
239 >    FILE *f;
240 >    
241 >    if((f=popen("/bin/ps -Al" , "r")) == NULL) {
242 >        errf("Failed to get process stats (%m)");
243 >        die();
244 >    }
245 >    
246 >    while((line=fpgetline(f)) != NULL) {
247 >        line_p=strchr(line, ' ');
248 >        line_p++;
249 >        if (line_p==NULL) abort();
250 >        /* Ok, we should now be at the state :) .. */
251 >        if (*line_p=='S') sleeping++;
252 >        if (*line_p=='R') running++;
253 >        if (*line_p=='Z') zombie++;
254 >        if (*line_p=='T') stopped++;
255 >    }
256 >    
257 >    if((pclose(f)) == -1) {
258 >        errf("Failed to close process stats (%m)");
259 >        die();
260 >    }
261 >    
262 >    printf("packet.users.list");
263 >    
264 >    while((entry=getutent()) != NULL) {
265 >        if(entry->ut_type==USER_PROCESS) {
266 >            printf(" %s",entry->ut_user);
267 >            nousers++;
268 >        }
269 >    }
270 >    
271 >    printf("\npacket.users.count %d\n", nousers);
272 >      
273 >    printf("packet.processes.sleeping %d\n",sleeping);
274 >    printf("packet.processes.cpu %d\n",running);
275 >    printf("packet.processes.zombie %d\n",zombie);
276 >    printf("packet.processes.stopped %d\n", stopped);
277 >    printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
278   }
279  
280 < void uptimeStats()
281 < {
282 <   char *line;
283 <   char *line_p;
284 <   FILE *f;
285 <  
286 <   if ((f=fopen("/proc/uptime", "r")) == NULL)
287 <     {
288 <        errf("Failed to get uptime stats (%m)");
289 <        die();
290 <     }
291 <  
292 <   if ((line=fpgetline(f)) == NULL)
293 <     {  
294 <        errf("Failed to read uptime stats (%m)");
295 <        die();
296 <     }
297 <
298 <   if ((fclose(f)) != 0)
299 <     {
300 <        errf("Failed to close file (%m).");
301 <        die();
302 <     }
303 <  
304 <  
352 <   line_p=strchr(line, '.');
353 <   if (line_p==NULL) abort();
354 <   *line_p='\0';
355 <  
356 <   printf("packet.os.uptime %s\n", line);
357 <
280 > void uptimeStats() {
281 >    char *line;
282 >    char *line_p;
283 >    FILE *f;
284 >    
285 >    if ((f=fopen("/proc/uptime", "r")) == NULL) {
286 >        errf("Failed to get uptime stats (%m)");
287 >        die();
288 >    }
289 >    
290 >    if ((line=fpgetline(f)) == NULL) {
291 >        errf("Failed to read uptime stats (%m)");
292 >        die();
293 >    }
294 >    
295 >    if ((fclose(f)) != 0) {
296 >        errf("Failed to close file (%m).");
297 >        die();
298 >    }
299 >    
300 >    line_p=strchr(line, '.');
301 >    if (line_p==NULL) abort();
302 >    *line_p='\0';
303 >    
304 >    printf("packet.os.uptime %s\n", line);
305   }
306  
307 <
308 < void osStats()
309 < {
310 <
311 <   struct utsname os;
312 <  
313 <   if((uname(&os)) != 0)
314 <     {
315 <        errf("Failed to get os stats (%m)");
316 <        die();
317 <     }
318 <  
319 <   printf("packet.os.name %s\n", os.sysname);
373 <   printf("packet.os.release %s\n" , os.release);
374 <   printf("packet.os.version %s\n", os.version);
375 <   printf("packet.os.sysname %s\n" , os.nodename);
376 <   printf("packet.os.platform %s\n", os.machine);
377 <  
307 > void osStats() {
308 >    struct utsname os;
309 >    
310 >    if((uname(&os)) != 0) {
311 >        errf("Failed to get os stats (%m)");
312 >        die();
313 >    }
314 >    
315 >    printf("packet.os.name %s\n", os.sysname);
316 >    printf("packet.os.release %s\n" , os.release);
317 >    printf("packet.os.version %s\n", os.version);
318 >    printf("packet.os.sysname %s\n" , os.nodename);
319 >    printf("packet.os.platform %s\n", os.machine);
320   }
321  
322 < void diskStats()
323 < {
324 <   struct statfs df;
325 <   FILE *f;
326 <   char *line;
327 <   char *line_p;
328 <   long bstok;
329 <   int diskcnt=0;
330 <  
331 <   if ((f=fopen("/etc/mtab", "r")) == NULL)
332 <     {
333 <        errf("Failed to get uptime stats (%m)");
334 <        die();
335 <     }  
336 <  
337 <   while((line=fpgetline(f)) != NULL)
338 <     {
339 <        line_p=strchr(line, ' ');
340 <        if(line_p==NULL) abort();
341 <        *line_p='\0';
342 <        line_p++;
343 <        if(line_p==NULL) abort();
344 <        printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
345 <        line=strchr(line_p, ' ');
346 <        if(line==NULL) abort();
347 <        *line='\0';
348 <        printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
349 <        if((statfs(line_p, &df)) != 0)
350 <          {
351 <             errf("Failed to get Filesystem stats (%m)");
352 <             die();
353 <          }
354 <        bstok=(df.f_bsize/1024);
355 <        printf("packet.disk.p%d.attributes.kbytes %ld\n",diskcnt,bstok*df.f_blocks);
356 <        printf("packet.disk.p%d.attributes.used %ld\n" ,diskcnt, ((bstok*df.f_blocks)-(bstok*df.f_bfree)));
357 <        printf("packet.disk.p%d.attributes.avail %ld\n", diskcnt, (df.f_bsize/1024)*df.f_bavail);
416 <        /*
417 <        printf("packet.disk.p%d.attributes.totalinodes %ld\n", diskcnt, df.f_files);
418 <        printf("packet.disk.p%d.attributes.freeinodes %ld\n", diskcnt, df.f_ffree);
419 <         */
420 <        diskcnt++;
421 <     }
422 <  
423 <  
322 > void diskStats() {
323 >    struct statfs df;
324 >    FILE *f;
325 >    char *line;
326 >    char *line_p;
327 >    long bstok;
328 >    int diskcnt=0;
329 >    
330 >    if ((f=fopen("/etc/mtab", "r")) == NULL) {
331 >        errf("Failed to get uptime stats (%m)");
332 >        die();
333 >    }
334 >    
335 >    while((line=fpgetline(f)) != NULL) {
336 >        line_p=strchr(line, ' ');
337 >        if(line_p==NULL) abort();
338 >        *line_p='\0';
339 >        line_p++;
340 >        if(line_p==NULL) abort();
341 >        printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
342 >        line=strchr(line_p, ' ');
343 >        if(line==NULL) abort();
344 >        *line='\0';
345 >        printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
346 >        if((statfs(line_p, &df)) != 0) {
347 >            errf("Failed to get Filesystem stats (%m)");
348 >            die();
349 >        }
350 >        bstok=(df.f_bsize/1024);
351 >        printf("packet.disk.p%d.attributes.kbytes %ld\n",diskcnt,bstok*df.f_blocks);
352 >        printf("packet.disk.p%d.attributes.used %ld\n" ,diskcnt, ((bstok*df.f_blocks)-(bstok*df.f_bfree)));
353 >        printf("packet.disk.p%d.attributes.avail %ld\n", diskcnt, (df.f_bsize/1024)*df.f_bavail);
354 >        printf("packet.disk.p%d.attributes.totalinodes %ld\n", diskcnt, df.f_files);
355 >        printf("packet.disk.p%d.attributes.freeinodes %ld\n", diskcnt, df.f_ffree);
356 >        diskcnt++;
357 >    }
358   }
359  
360 <
427 < int main()
428 < {
360 > int main() {
361     getLoadAv();
362     getMemInfo();
363     cpustats();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines