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.7 by pajs, Tue Mar 19 10:38:05 2002 UTC vs.
Revision 1.11 by pajs, Fri May 3 13:18:19 2002 UTC

# Line 7 | Line 7
7   #include <sys/vfs.h>
8   #include <utmp.h>
9   #include <pwd.h>
10 + #include <mntent.h>
11 + #include <dirent.h>
12 + #include <limits.h>
13  
14   int die() {
15      exit (1);
16   }
17  
18   void getLoadAv() {
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);
19 > #ifdef OLDLIBC
20 >  
21 >  FILE *f;
22 >  char *loadavg;
23 >  char *load_p;
24 >  
25 >  if ((f=fopen("/proc/loadavg", "r" ))==NULL) {
26 >    errf("Failed to open load averages (%m)");
27 >    die();
28 >  }
29 >  
30 >  if ((loadavg=fpgetline(f)) == NULL) {
31 >    errf("Failed to read any data for load averages (%m)");
32 >    die();
33 >  }
34 >  
35 >  if ((fclose(f)) != 0) {
36 >    errf("Failed to close file (%m).");
37 >    die();
38 >  }
39 >  
40 >  load_p=strtok(loadavg, " ");
41 >  printf("packet.load.load1 %s\n",load_p);
42 >  for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
43 >  load_p++;
44 >  if (load_p == NULL) abort();
45 >  load_p=strtok(load_p, " ");
46 >  if (load_p == NULL) abort();
47 >  printf("packet.load.load5 %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.load15 %s\n",load_p);
54 >
55 > #else
56 >  double loadav[3];
57 >
58 >  if((getloadavg(loadav,3)) == -1){
59 >    errf("Failed to get load averages (%m)");
60 >    die();
61 >  }
62 >  
63 >  printf("packet.load.load1 %.2f\n",loadav[0]);
64 >  printf("packet.load.load5 %.2f\n",loadav[1]);
65 >  printf("packet.load.load15 %.2f\n",loadav[2]);
66 > #endif
67   }
68  
69   void getMemInfo() {
# Line 232 | Line 250 | void cpustats() {
250   }
251  
252   void processStats() {
253 <    int sleeping=-1;
253 >    int sleeping=0;
254      int zombie=0;
255      int stopped=0;
256      int running=0;
257      int nousers=0;
258      char *line;
259 <    char *line_p;
259 >    char *line_ptr;
260      struct utmp *entry;
261 <    
261 >    DIR *procdir;
262 >    struct dirent *procent;
263 >    char fname[_POSIX_PATH_MAX];
264      FILE *f;
265 <    
266 <    if((f=popen("/bin/ps -Al" , "r")) == NULL) {
267 <        errf("Failed to get process stats (%m)");
268 <        die();
265 >    
266 >    chdir("/proc");
267 >    if((procdir=opendir(".")) == NULL){
268 >      errf("Failed to open proc (%m)");
269 >      exit(1);
270      }
250    
251    while((line=fpgetline(f)) != NULL) {
271  
272 <      line_p=line;
273 <      for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
274 <      line_p=strchr(line_p, ' ');
275 <      for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
276 <      if (line_p==NULL) abort();
277 <      /* Ok, we should now be at the state :) .. */
278 <      if (*line_p=='S') sleeping++;
279 <      if (*line_p=='R') running++;
280 <      if (*line_p=='Z') zombie++;
281 <      if (*line_p=='T') stopped++;
272 >    while((procent = readdir(procdir)) != NULL){
273 >      if(atoi(procent->d_name) == 0) continue;
274 >      strncpy(fname, procent->d_name, _POSIX_PATH_MAX-7);
275 >      strcat(fname, "/status");
276 >
277 >      if((f=fopen(fname, "r")) == NULL){
278 >         errf("Failed to open process stat (%m)");
279 >         exit(1);
280 >      }
281 >    
282 >      while((line=fpgetline(f)) != NULL){
283 >         if(strncasecmp(line,"State:",6)==0) break;
284 >      }
285 >
286 >      line_ptr=line;
287 >      for(;*line_ptr != ' ';line_ptr++);
288 >      line_ptr--;
289 >      
290 >      if(*line_ptr=='R') running++;
291 >      if(*line_ptr=='S') sleeping++;
292 >      if(*line_ptr=='Z') zombie++;
293 >      if(*line_ptr=='T') stopped++;
294 >      if(*line_ptr=='D') stopped++;
295 >
296      }
297 <    
298 <    if((pclose(f)) == -1) {
266 <        errf("Failed to close process stats (%m)");
267 <        die();
268 <    }
269 <    
297 >    closedir(procdir);
298 >
299      printf("packet.users.list");
300      
301      while((entry=getutent()) != NULL) {
# Line 328 | Line 357 | void osStats() {
357   }
358  
359   void diskStats() {
360 <    struct statfs df;
361 <    FILE *f;
362 <    char *line;
363 <    char *line_p;
364 <    long bstok;
365 <    int diskcnt=0;
366 <    
367 <    if ((f=fopen("/etc/mtab", "r")) == NULL) {
368 <        errf("Failed to get uptime stats (%m)");
369 <        die();
360 >
361 >  struct mntent *mp;
362 >  struct statfs df;
363 >  FILE *f;
364 >  int counter=0;
365 >
366 >  if ((f=fopen("/etc/mtab", "r" ))==NULL){
367 >      errf("Failed to open mounts (%m)");
368 >      die();
369 >  }
370 >
371 >  while((mp=getmntent(f))){
372 >    if ((statfs(mp->mnt_dir, &df)) !=0){
373 >      errf("Failed to gets fs stats (%m)");
374 >      continue;
375      }
376 +
377 +    if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) ||  (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue;
378      
379 <    while((line=fpgetline(f)) != NULL) {
380 <        line_p=strchr(line, ' ');
381 <        if(line_p==NULL) abort();
382 <        *line_p='\0';
383 <        line_p++;
384 <        if(line_p==NULL) abort();
385 <        printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
386 <        line=strchr(line_p, ' ');
387 <        if(line==NULL) abort();
388 <        *line='\0';
389 <        printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
390 <        if((statfs(line_p, &df)) != 0) {
391 <            errf("Failed to get Filesystem stats (%m)");
356 <            die();
357 <        }
358 <        bstok=(df.f_bsize/1024);
359 <        printf("packet.disk.p%d.attributes.kbytes %ld\n",diskcnt,bstok*df.f_blocks);
360 <        printf("packet.disk.p%d.attributes.used %ld\n" ,diskcnt, ((bstok*df.f_blocks)-(bstok*df.f_bfree)));
361 <        printf("packet.disk.p%d.attributes.avail %ld\n", diskcnt, (df.f_bsize/1024)*df.f_bavail);
362 <        printf("packet.disk.p%d.attributes.totalinodes %ld\n", diskcnt, df.f_files);
363 <        printf("packet.disk.p%d.attributes.freeinodes %ld\n", diskcnt, df.f_ffree);
364 <        diskcnt++;
365 <    }
379 >      printf("packet.disk.p%d.attributes.mount %s\n", counter, mp->mnt_dir);
380 >      printf("packet.disk.p%d.attributes.name %s\n", counter, mp->mnt_fsname);
381 >      printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_bsize/1024) * df.f_blocks));
382 >      printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree)));
383 >      printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_bsize/1024) * df.f_bavail);
384 >      printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files);
385 >      printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree);
386 >    
387 >      counter++;
388 >  }
389 >
390 >  
391 >
392   }
393  
394   int main() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines