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.13 by tdb, Sat May 18 18:15:56 2002 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines