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.3 by tdb, Fri Mar 8 14:54:32 2002 UTC vs.
Revision 1.6 by pajs, Mon Mar 11 13:51:11 2002 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines