1 |
pajs |
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 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include "config.h" |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <stdio.h> |
26 |
|
|
#include <stdlib.h> |
27 |
|
|
#include <unistd.h> |
28 |
tdb |
1.2 |
#include "statgrab.h" |
29 |
pajs |
1.1 |
|
30 |
|
|
#ifdef SOLARIS |
31 |
|
|
#include <procfs.h> |
32 |
|
|
#include <limits.h> |
33 |
|
|
#include <dirent.h> |
34 |
|
|
#include <string.h> |
35 |
|
|
#define PROC_LOCATION "/proc" |
36 |
|
|
#define MAX_FILE_LENGTH PATH_MAX |
37 |
|
|
#endif |
38 |
|
|
|
39 |
|
|
process_stat_t *get_process_stats(){ |
40 |
|
|
|
41 |
|
|
static process_stat_t process_stat; |
42 |
|
|
|
43 |
|
|
psinfo_t process_info; |
44 |
|
|
DIR *proc_dir; |
45 |
|
|
struct dirent *dir_entry; |
46 |
|
|
char filename[MAX_FILE_LENGTH]; |
47 |
|
|
FILE *f; |
48 |
|
|
|
49 |
|
|
process_stat.sleeping=0; |
50 |
|
|
process_stat.running=0; |
51 |
|
|
process_stat.zombie=0; |
52 |
|
|
process_stat.stopped=0; |
53 |
|
|
process_stat.running=0; |
54 |
|
|
|
55 |
|
|
if((proc_dir=opendir(PROC_LOCATION))==NULL){ |
56 |
|
|
return NULL; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
while((dir_entry=readdir(proc_dir))!=NULL){ |
60 |
|
|
if(atoi(dir_entry->d_name) == 0) continue; |
61 |
|
|
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name); |
62 |
|
|
|
63 |
|
|
if((f=fopen(filename, "r"))==NULL){ |
64 |
|
|
/* Open failed.. Process since vanished, or the path was too long. |
65 |
|
|
* Ah well, move onwards to the next one */ |
66 |
|
|
continue; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
fread(&process_info, sizeof(psinfo_t), 1, f); |
70 |
|
|
fclose(f); |
71 |
|
|
|
72 |
|
|
if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++; |
73 |
|
|
if(process_info.pr_lwp.pr_state==2) process_stat.running++; |
74 |
|
|
if(process_info.pr_lwp.pr_state==3) process_stat.zombie++; |
75 |
|
|
if(process_info.pr_lwp.pr_state==4) process_stat.stopped++; |
76 |
|
|
if(process_info.pr_lwp.pr_state==6) process_stat.running++; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
closedir(proc_dir); |
80 |
|
|
|
81 |
pajs |
1.4 |
|
82 |
|
|
process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped; |
83 |
pajs |
1.1 |
return &process_stat; |
84 |
|
|
} |