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.4 by tdb, Fri Mar 8 16:04:38 2002 UTC vs.
Revision 1.14 by tdb, Tue May 21 16:47:12 2002 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * Copyright (C) 2000-2002 i-scream
5 + *
6 + * This program is free software; you can redistribute it and/or
7 + * modify it under the terms of the GNU General Public License
8 + * as published by the Free Software Foundation; either version 2
9 + * of the License, or (at your option) any later version.
10 + *
11 + * This program is distributed in the hope that it will be useful,
12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + * GNU General Public License for more details.
15 + *
16 + * You should have received a copy of the GNU General Public License
17 + * along with this program; if not, write to the Free Software
18 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + */
20 +
21   #include <stdio.h>
22   #include <stdlib.h>
23   #include "ukcprog.h"
# Line 7 | Line 27
27   #include <sys/vfs.h>
28   #include <utmp.h>
29   #include <pwd.h>
30 + #include <mntent.h>
31 + #include <dirent.h>
32 + #include <limits.h>
33  
34   int die() {
35      exit (1);
36   }
37  
38   void getLoadAv() {
39 <    FILE *f;
40 <    char *loadavg;
41 <    char *load_p;
42 <      
43 <    if ((f=fopen("/proc/loadavg", "r" ))==NULL) {
44 <        errf("Failed to open load averages (%m)");
45 <        die();
46 <    }
47 <    
48 <    if ((loadavg=fpgetline(f)) == NULL) {
49 <        errf("Failed to read any data for load averages (%m)");
50 <        die();
51 <    }
52 <    
53 <    if ((fclose(f)) != 0) {
54 <        errf("Failed to close file (%m).");
55 <        die();
56 <    }
57 <    
58 <    load_p=strtok(loadavg, " ");
59 <    printf("packet.load.load1 %s\n",load_p);
60 <    for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
61 <    load_p++;
62 <    if (load_p == NULL) abort();
63 <    load_p=strtok(load_p, " ");
64 <    if (load_p == NULL) abort();
65 <    printf("packet.load.load5 %s\n",load_p);
66 <    for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
67 <    load_p++;
68 <    if (load_p == NULL) abort();
69 <    load_p=strtok(load_p, " ");
70 <    if (load_p == NULL) abort();
71 <    printf("packet.load.load15 %s\n",load_p);
39 > #ifdef OLDLIBC
40 >  
41 >  FILE *f;
42 >  char *loadavg;
43 >  char *load_p;
44 >  
45 >  if ((f=fopen("/proc/loadavg", "r" ))==NULL) {
46 >    errf("Failed to open load averages (%m)");
47 >    die();
48 >  }
49 >  
50 >  if ((loadavg=fpgetline(f)) == NULL) {
51 >    errf("Failed to read any data for load averages (%m)");
52 >    die();
53 >  }
54 >  
55 >  if ((fclose(f)) != 0) {
56 >    errf("Failed to close file (%m).");
57 >    die();
58 >  }
59 >  
60 >  load_p=strtok(loadavg, " ");
61 >  printf("packet.load.load1 %s\n",load_p);
62 >  for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
63 >  load_p++;
64 >  if (load_p == NULL) abort();
65 >  load_p=strtok(load_p, " ");
66 >  if (load_p == NULL) abort();
67 >  printf("packet.load.load5 %s\n",load_p);
68 >  for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
69 >  load_p++;
70 >  if (load_p == NULL) abort();
71 >  load_p=strtok(load_p, " ");
72 >  if (load_p == NULL) abort();
73 >  printf("packet.load.load15 %s\n",load_p);
74 >
75 > #else
76 >  double loadav[3];
77 >
78 >  if((getloadavg(loadav,3)) == -1){
79 >    errf("Failed to get load averages (%m)");
80 >    die();
81 >  }
82 >  
83 >  printf("packet.load.load1 %.2f\n",loadav[0]);
84 >  printf("packet.load.load5 %.2f\n",loadav[1]);
85 >  printf("packet.load.load15 %.2f\n",loadav[2]);
86 > #endif
87   }
88  
89   void getMemInfo() {
90      char *line;
91 <    char *mem;
92 <    char *swap;
91 >    char *mem=NULL;
92 >    char *swap=NULL;
93      char *ch;
94      long memstat[6];
95      long swapstat[3];
# Line 80 | Line 118 | void getMemInfo() {
118          errf("Failed to close file (%m).");
119          die();
120      }
121 <    
121 >  
122 >    if (mem==NULL || swap==NULL){
123 >        errf("Failed to obtain information required for memory and swap stats");
124 >        die();
125 >    }
126 >
127      /* Get the info we want from the 2 read in lines */
128      
129      ch = strchr(mem, ' ');
# Line 218 | Line 261 | void cpustats() {
261      usage=((((float)idle)/((float)total))*100.00);
262      printf("packet.cpu.idle %3.2f\n", usage);
263      
264 <    /* Cos i-scream's expexts this to be sent :/ */
264 >    /* Cos i-scream's expects this to be sent :/ */
265      printf("packet.cpu.iowait 0\n");
266 <    
266 >    printf("packet.cpu.swap 0\n");  
267 >
268      free(line[0]);
269      free(line[1]);
270   }
# Line 232 | Line 276 | void processStats() {
276      int running=0;
277      int nousers=0;
278      char *line;
279 <    char *line_p;
279 >    char *line_ptr;
280      struct utmp *entry;
281 <    
281 >    DIR *procdir;
282 >    struct dirent *procent;
283 >    char fname[_POSIX_PATH_MAX];
284      FILE *f;
285 <    
286 <    if((f=popen("/bin/ps -Al" , "r")) == NULL) {
287 <        errf("Failed to get process stats (%m)");
288 <        die();
285 >    
286 >    chdir("/proc");
287 >    if((procdir=opendir(".")) == NULL){
288 >      errf("Failed to open proc (%m)");
289 >      exit(1);
290      }
291 <    
292 <    while((line=fpgetline(f)) != NULL) {
293 <        line_p=strchr(line, ' ');
294 <        line_p++;
295 <        if (line_p==NULL) abort();
296 <        /* Ok, we should now be at the state :) .. */
297 <        if (*line_p=='S') sleeping++;
298 <        if (*line_p=='R') running++;
299 <        if (*line_p=='Z') zombie++;
300 <        if (*line_p=='T') stopped++;
291 >
292 >    while((procent = readdir(procdir)) != NULL){
293 >      if(atoi(procent->d_name) == 0) continue;
294 >      strncpy(fname, procent->d_name, _POSIX_PATH_MAX-7);
295 >      strcat(fname, "/status");
296 >
297 >      if((f=fopen(fname, "r")) == NULL){
298 >         errf("Failed to open process stat (%m)");
299 >         exit(1);
300 >      }
301 >    
302 >      while((line=fpgetline(f)) != NULL){
303 >         if(strncasecmp(line,"State:",6)==0) break;
304 >      }
305 >
306 >      line_ptr=line;
307 >      for(;*line_ptr++ != '\t';);
308 >    
309 >
310 >      if(*line_ptr=='R') running++;
311 >      if(*line_ptr=='S') sleeping++;
312 >      if(*line_ptr=='Z') zombie++;
313 >      if(*line_ptr=='T') stopped++;
314 >      if(*line_ptr=='D') stopped++;
315 >
316      }
317 <    
318 <    if((pclose(f)) == -1) {
257 <        errf("Failed to close process stats (%m)");
258 <        die();
259 <    }
260 <    
317 >    closedir(procdir);
318 >
319      printf("packet.users.list");
320      
321      while((entry=getutent()) != NULL) {
# Line 269 | Line 327 | void processStats() {
327      
328      printf("\npacket.users.count %d\n", nousers);
329        
330 <    printf("packet.processes.total %d\n",sleeping);
331 <    printf("packet.processes.sleeping %d\n",running);
330 >    printf("packet.processes.sleeping %d\n",sleeping);
331 >    printf("packet.processes.cpu %d\n",running);
332      printf("packet.processes.zombie %d\n",zombie);
333      printf("packet.processes.stopped %d\n", stopped);
334      printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
# Line 319 | Line 377 | void osStats() {
377   }
378  
379   void diskStats() {
380 <    struct statfs df;
381 <    FILE *f;
382 <    char *line;
383 <    char *line_p;
384 <    long bstok;
385 <    int diskcnt=0;
386 <    
387 <    if ((f=fopen("/etc/mtab", "r")) == NULL) {
388 <        errf("Failed to get uptime stats (%m)");
389 <        die();
380 >
381 >  struct mntent *mp;
382 >  struct statfs df;
383 >  FILE *f;
384 >  int counter=0;
385 >
386 >  if ((f=fopen("/etc/mtab", "r" ))==NULL){
387 >      errf("Failed to open mounts (%m)");
388 >      die();
389 >  }
390 >
391 >  while((mp=getmntent(f))){
392 >    if ((statfs(mp->mnt_dir, &df)) !=0){
393 >      errf("Failed to gets fs stats (%m)");
394 >      continue;
395      }
396 +
397 +    if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) ||  (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue;
398      
399 <    while((line=fpgetline(f)) != NULL) {
400 <        line_p=strchr(line, ' ');
401 <        if(line_p==NULL) abort();
402 <        *line_p='\0';
403 <        line_p++;
404 <        if(line_p==NULL) abort();
405 <        printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
406 <        line=strchr(line_p, ' ');
407 <        if(line==NULL) abort();
408 <        *line='\0';
409 <        printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
410 <        if((statfs(line_p, &df)) != 0) {
411 <            errf("Failed to get Filesystem stats (%m)");
347 <            die();
348 <        }
349 <        bstok=(df.f_bsize/1024);
350 <        printf("packet.disk.p%d.attributes.kbytes %ld\n",diskcnt,bstok*df.f_blocks);
351 <        printf("packet.disk.p%d.attributes.used %ld\n" ,diskcnt, ((bstok*df.f_blocks)-(bstok*df.f_bfree)));
352 <        printf("packet.disk.p%d.attributes.avail %ld\n", diskcnt, (df.f_bsize/1024)*df.f_bavail);
353 <        printf("packet.disk.p%d.attributes.totalinodes %ld\n", diskcnt, df.f_files);
354 <        printf("packet.disk.p%d.attributes.freeinodes %ld\n", diskcnt, df.f_ffree);
355 <        diskcnt++;
356 <    }
399 >      printf("packet.disk.p%d.attributes.mount %s\n", counter, mp->mnt_dir);
400 >      printf("packet.disk.p%d.attributes.name %s\n", counter, mp->mnt_fsname);
401 >      printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_bsize/1024) * df.f_blocks));
402 >      printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree)));
403 >      printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_bsize/1024) * df.f_bavail);
404 >      printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files);
405 >      printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree);
406 >    
407 >      counter++;
408 >  }
409 >
410 >  
411 >
412   }
413  
414   int main() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines