[Sysfence-commit] sysfence datastruct.h,1.6,1.7 getstats.c,1.16,1.17 getstats.h,1.9,1.10 mainloop.c,
Status: Alpha
Brought to you by:
emes
|
From: Michal S. <em...@us...> - 2004-05-31 10:30:45
|
Update of /cvsroot/sysfence/sysfence In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2754 Modified Files: datastruct.h getstats.c getstats.h mainloop.c Log Message: + functions fetching proc data Index: getstats.h =================================================================== RCS file: /cvsroot/sysfence/sysfence/getstats.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- getstats.h 28 May 2004 23:27:32 -0000 1.9 +++ getstats.h 31 May 2004 10:30:33 -0000 1.10 @@ -14,7 +14,8 @@ /* $Id$ */ -#define BUFSIZE 512 +#define BUFSIZE 512 +#define PROCDIRNAMELEN 25 #define PROC_DATA_SIZE 524288 // 512k Index: getstats.c =================================================================== RCS file: /cvsroot/sysfence/sysfence/getstats.c,v retrieving revision 1.16 retrieving revision 1.17 diff -u -d -r1.16 -r1.17 --- getstats.c 30 May 2004 18:23:33 -0000 1.16 +++ getstats.c 31 May 2004 10:30:33 -0000 1.17 @@ -20,6 +20,8 @@ #include <fcntl.h> #include <string.h> #include <sys/vfs.h> +#include <dirent.h> +#include <sys/stat.h> #include "sys/xalloc.h" #include "sys/exit.h" #include "parseopt/lex.h" @@ -116,6 +118,7 @@ return res; } +const char *procdir = "/proc"; const char *lafile = "/proc/loadavg"; int lafha; const char *memfile = "/proc/meminfo"; @@ -243,8 +246,87 @@ } } +char get_proc_state (sf_database *db, char *dir) +{ + char fnamebuf[ PROCDIRNAMELEN + 1 ]; + int statfha; + char state; + char *ptr = &fbuf[0]; + + snprintf (&fnamebuf[0], PROCDIRNAMELEN, "%s/status", dir); + statfha = open (&fnamebuf[0], O_RDONLY); + + /* cannot open file? process state is unknown for us */ + if (statfha < 0) return PROC_UNKNOWN; + else { + read (statfha, ptr, BUFSIZE); + /* second line looks like this: + * + * Status: L + * + * where L is letter indicating process' state + */ + ptr = fto_notspace (fto_space (fto_newline (ptr))); + switch (*ptr) { + case 'R': + state = PROC_RUN; + break; + case 'S': + state = PROC_SLEEP; + break; + case 'Z': + state = PROC_ZOMBIE; + break; + case 'T': + case 'D': + state = PROC_STOP; + break; + default: + state = PROC_UNKNOWN; + } + close (statfha); + } + return state; +} + void fetch_proc (sf_database *db) { + DIR *dh; + struct dirent *de; + struct stat statbuf; + long int pid; + char fnamebuf[ PROCDIRNAMELEN + 1 ]; + sf_proc_stat res; + + /* reset db */ + db->nr_proc = 0; + + dh = opendir (procdir); + if (!dh) bail_out (EXIT_IO, procdir); + + while (1) { + de = readdir (dh); + if (!de) return; + + /* check only dirs */ + if (de->d_type != DT_DIR) continue; + + /* try to convert file name to int */ + pid = atol (de->d_name); + /* zero means it's not a process' dir */ + if (!pid) continue; + + /* get real dir name */ + snprintf (&fnamebuf[0], PROCDIRNAMELEN, "%s/%ld", procdir, pid); + stat (&fnamebuf[0], &statbuf); + res.uid = statbuf.st_uid; + res.state = get_proc_state (db, &fnamebuf[0]); + + /* add process */ + add_proc_entry_to_array (db, &res); + } + + closedir (dh); return; } Index: mainloop.c =================================================================== RCS file: /cvsroot/sysfence/sysfence/mainloop.c,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -r1.13 -r1.14 --- mainloop.c 28 May 2004 20:36:50 -0000 1.13 +++ mainloop.c 31 May 2004 10:30:33 -0000 1.14 @@ -92,6 +92,7 @@ fetch_la (db); fetch_mem (db); fetch_fs (db); + fetch_proc (db); semaphore_post (semid); signals_handling (SIGUNBLOCK); Index: datastruct.h =================================================================== RCS file: /cvsroot/sysfence/sysfence/datastruct.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- datastruct.h 30 May 2004 18:23:33 -0000 1.6 +++ datastruct.h 31 May 2004 10:30:32 -0000 1.7 @@ -38,7 +38,8 @@ #define PROC_SLEEP 2 #define PROC_STOP 4 #define PROC_ZOMBIE 8 -#define PROC_ANY 15 +#define PROC_UNKNOWN 16 +#define PROC_ANY 31 /*************************************************************************** All-purpose list |