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.5 by pajs, Mon Mar 11 13:42:40 2002 UTC vs.
Revision 1.9 by pajs, Tue Apr 23 14:22:35 2002 UTC

# Line 7 | Line 7
7   #include <sys/vfs.h>
8   #include <utmp.h>
9   #include <pwd.h>
10 + #include <mntent.h>
11  
12   int die() {
13      exit (1);
14   }
15  
16   void getLoadAv() {
17 <    FILE *f;
18 <    char *loadavg;
19 <    char *load_p;
20 <      
21 <    if ((f=fopen("/proc/loadavg", "r" ))==NULL) {
22 <        errf("Failed to open load averages (%m)");
23 <        die();
24 <    }
25 <    
26 <    if ((loadavg=fpgetline(f)) == NULL) {
27 <        errf("Failed to read any data for load averages (%m)");
28 <        die();
29 <    }
30 <    
31 <    if ((fclose(f)) != 0) {
32 <        errf("Failed to close file (%m).");
33 <        die();
34 <    }
35 <    
36 <    load_p=strtok(loadavg, " ");
37 <    printf("packet.load.load1 %s\n",load_p);
38 <    for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
39 <    load_p++;
40 <    if (load_p == NULL) abort();
41 <    load_p=strtok(load_p, " ");
42 <    if (load_p == NULL) abort();
43 <    printf("packet.load.load5 %s\n",load_p);
44 <    for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
45 <    load_p++;
46 <    if (load_p == NULL) abort();
47 <    load_p=strtok(load_p, " ");
48 <    if (load_p == NULL) abort();
49 <    printf("packet.load.load15 %s\n",load_p);
17 > #ifdef OLDLIBC
18 >  
19 >  FILE *f;
20 >  char *loadavg;
21 >  char *load_p;
22 >  
23 >  if ((f=fopen("/proc/loadavg", "r" ))==NULL) {
24 >    errf("Failed to open load averages (%m)");
25 >    die();
26 >  }
27 >  
28 >  if ((loadavg=fpgetline(f)) == NULL) {
29 >    errf("Failed to read any data for load averages (%m)");
30 >    die();
31 >  }
32 >  
33 >  if ((fclose(f)) != 0) {
34 >    errf("Failed to close file (%m).");
35 >    die();
36 >  }
37 >  
38 >  load_p=strtok(loadavg, " ");
39 >  printf("packet.load.load1 %s\n",load_p);
40 >  for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
41 >  load_p++;
42 >  if (load_p == NULL) abort();
43 >  load_p=strtok(load_p, " ");
44 >  if (load_p == NULL) abort();
45 >  printf("packet.load.load5 %s\n",load_p);
46 >  for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
47 >  load_p++;
48 >  if (load_p == NULL) abort();
49 >  load_p=strtok(load_p, " ");
50 >  if (load_p == NULL) abort();
51 >  printf("packet.load.load15 %s\n",load_p);
52 >
53 > #else
54 >  double loadav[3];
55 >
56 >  if((getloadavg(loadav,3)) == -1){
57 >    errf("Failed to get load averages (%m)");
58 >    die();
59 >  }
60 >  
61 >  printf("packet.load.load1 %.2f\n",loadav[0]);
62 >  printf("packet.load.load5 %.2f\n",loadav[1]);
63 >  printf("packet.load.load15 %.2f\n",loadav[2]);
64 > #endif
65   }
66  
67   void getMemInfo() {
68      char *line;
69 <    char *mem;
70 <    char *swap;
69 >    char *mem=NULL;
70 >    char *swap=NULL;
71      char *ch;
72      long memstat[6];
73      long swapstat[3];
# Line 80 | Line 96 | void getMemInfo() {
96          errf("Failed to close file (%m).");
97          die();
98      }
99 <    
99 >  
100 >    if (mem==NULL || swap==NULL){
101 >        errf("Failed to obtain information required for memory and swap stats");
102 >        die();
103 >    }
104 >
105      /* Get the info we want from the 2 read in lines */
106      
107      ch = strchr(mem, ' ');
# Line 227 | Line 248 | void cpustats() {
248   }
249  
250   void processStats() {
251 <    int sleeping=0;
251 >    int sleeping=-1;
252      int zombie=0;
253      int stopped=0;
254      int running=0;
# Line 244 | Line 265 | void processStats() {
265      }
266      
267      while((line=fpgetline(f)) != NULL) {
268 <        line_p=strchr(line, ' ');
269 <        line_p++;
270 <        if (line_p==NULL) abort();
271 <        /* Ok, we should now be at the state :) .. */
272 <        if (*line_p=='S') sleeping++;
273 <        if (*line_p=='R') running++;
274 <        if (*line_p=='Z') zombie++;
275 <        if (*line_p=='T') stopped++;
268 >
269 >      line_p=line;
270 >      for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
271 >      line_p=strchr(line_p, ' ');
272 >      for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
273 >      if (line_p==NULL) abort();
274 >      /* Ok, we should now be at the state :) .. */
275 >      if (*line_p=='S') sleeping++;
276 >      if (*line_p=='R') running++;
277 >      if (*line_p=='Z') zombie++;
278 >      if (*line_p=='T') stopped++;
279      }
280      
281      if((pclose(f)) == -1) {
# Line 320 | Line 344 | void osStats() {
344   }
345  
346   void diskStats() {
347 <    struct statfs df;
348 <    FILE *f;
349 <    char *line;
350 <    char *line_p;
351 <    long bstok;
352 <    int diskcnt=0;
353 <    
354 <    if ((f=fopen("/etc/mtab", "r")) == NULL) {
355 <        errf("Failed to get uptime stats (%m)");
356 <        die();
347 >
348 >  struct mntent *mp;
349 >  struct statfs df;
350 >  FILE *f;
351 >  int counter=0;
352 >
353 >  if ((f=fopen("/etc/mtab", "r" ))==NULL){
354 >      errf("Failed to open mounts (%m)");
355 >      die();
356 >  }
357 >
358 >  while((mp=getmntent(f))){
359 >    if ((statfs(mp->mnt_dir, &df)) !=0){
360 >      errf("Failed to gets fs stats (%m)");
361 >      continue;
362      }
363 +
364 +    if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) ||  (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue;
365      
366 <    while((line=fpgetline(f)) != NULL) {
367 <        line_p=strchr(line, ' ');
368 <        if(line_p==NULL) abort();
369 <        *line_p='\0';
370 <        line_p++;
371 <        if(line_p==NULL) abort();
372 <        printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
373 <        line=strchr(line_p, ' ');
374 <        if(line==NULL) abort();
375 <        *line='\0';
376 <        printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
377 <        if((statfs(line_p, &df)) != 0) {
378 <            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 <    }
366 >      printf("packet.disk.p%d.attributes.mount %s\n", counter, mp->mnt_dir);
367 >      printf("packet.disk.p%d.attributes.name %s\n", counter, mp->mnt_fsname);
368 >      printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_bsize/1024) * df.f_blocks));
369 >      printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree)));
370 >      printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_bsize/1024) * df.f_bavail);
371 >      printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files);
372 >      printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree);
373 >    
374 >      counter++;
375 >  }
376 >
377 >  
378 >
379   }
380  
381   int main() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines