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 |
tdb |
1.2 |
#include "statgrab.h" |
28 |
pajs |
1.5 |
#include <sys/types.h> |
29 |
|
|
#include <dirent.h> |
30 |
|
|
#include <string.h> |
31 |
pajs |
1.1 |
|
32 |
|
|
#ifdef SOLARIS |
33 |
|
|
#include <procfs.h> |
34 |
|
|
#include <limits.h> |
35 |
pajs |
1.5 |
#define PROC_LOCATION "/proc" |
36 |
|
|
#define MAX_FILE_LENGTH PATH_MAX |
37 |
|
|
#endif |
38 |
|
|
#ifdef LINUX |
39 |
|
|
#include "tools.h" |
40 |
|
|
#include <linux/limits.h> |
41 |
pajs |
1.1 |
#define PROC_LOCATION "/proc" |
42 |
|
|
#define MAX_FILE_LENGTH PATH_MAX |
43 |
|
|
#endif |
44 |
|
|
|
45 |
|
|
process_stat_t *get_process_stats(){ |
46 |
|
|
|
47 |
|
|
static process_stat_t process_stat; |
48 |
|
|
|
49 |
|
|
DIR *proc_dir; |
50 |
|
|
struct dirent *dir_entry; |
51 |
|
|
char filename[MAX_FILE_LENGTH]; |
52 |
|
|
FILE *f; |
53 |
pajs |
1.5 |
#ifdef LINUX |
54 |
|
|
char *line_ptr; |
55 |
|
|
#endif |
56 |
|
|
#ifdef SOLARIS |
57 |
|
|
psinfo_t process_info; |
58 |
|
|
#endif |
59 |
pajs |
1.1 |
|
60 |
|
|
process_stat.sleeping=0; |
61 |
|
|
process_stat.running=0; |
62 |
|
|
process_stat.zombie=0; |
63 |
|
|
process_stat.stopped=0; |
64 |
pajs |
1.5 |
process_stat.total=0; |
65 |
pajs |
1.1 |
|
66 |
|
|
if((proc_dir=opendir(PROC_LOCATION))==NULL){ |
67 |
|
|
return NULL; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
while((dir_entry=readdir(proc_dir))!=NULL){ |
71 |
|
|
if(atoi(dir_entry->d_name) == 0) continue; |
72 |
pajs |
1.5 |
|
73 |
|
|
#ifdef SOLARIS |
74 |
pajs |
1.1 |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name); |
75 |
pajs |
1.5 |
#endif |
76 |
|
|
#ifdef LINUX |
77 |
|
|
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name); |
78 |
|
|
#endif |
79 |
pajs |
1.1 |
|
80 |
|
|
if((f=fopen(filename, "r"))==NULL){ |
81 |
|
|
/* Open failed.. Process since vanished, or the path was too long. |
82 |
|
|
* Ah well, move onwards to the next one */ |
83 |
|
|
continue; |
84 |
|
|
} |
85 |
|
|
|
86 |
pajs |
1.5 |
#ifdef SOLARIS |
87 |
pajs |
1.1 |
fread(&process_info, sizeof(psinfo_t), 1, f); |
88 |
|
|
if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++; |
89 |
|
|
if(process_info.pr_lwp.pr_state==2) process_stat.running++; |
90 |
|
|
if(process_info.pr_lwp.pr_state==3) process_stat.zombie++; |
91 |
|
|
if(process_info.pr_lwp.pr_state==4) process_stat.stopped++; |
92 |
|
|
if(process_info.pr_lwp.pr_state==6) process_stat.running++; |
93 |
pajs |
1.5 |
#endif |
94 |
|
|
#ifdef LINUX |
95 |
|
|
if((line_ptr=f_read_line(f, "State:"))==NULL){ |
96 |
|
|
fclose(f); |
97 |
|
|
continue; |
98 |
|
|
} |
99 |
|
|
if((line_ptr=strchr(line_ptr, '\t'))==NULL){ |
100 |
|
|
fclose(f); |
101 |
|
|
continue; |
102 |
|
|
} |
103 |
|
|
line_ptr++; |
104 |
|
|
if(line_ptr=='\0'){ |
105 |
|
|
fclose(f); |
106 |
|
|
continue; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
if(*line_ptr=='S') process_stat.sleeping++; |
110 |
|
|
if(*line_ptr=='R') process_stat.running++; |
111 |
|
|
if(*line_ptr=='Z') process_stat.zombie++; |
112 |
|
|
if(*line_ptr=='T') process_stat.stopped++; |
113 |
|
|
if(*line_ptr=='D') process_stat.stopped++; |
114 |
|
|
#endif |
115 |
|
|
|
116 |
|
|
fclose(f); |
117 |
pajs |
1.1 |
} |
118 |
|
|
closedir(proc_dir); |
119 |
|
|
|
120 |
pajs |
1.4 |
|
121 |
|
|
process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped; |
122 |
pajs |
1.1 |
return &process_stat; |
123 |
|
|
} |