1 |
tdb |
1.17 |
/* |
2 |
pajs |
1.1 |
* i-scream central monitoring system |
3 |
tdb |
1.8 |
* http://www.i-scream.org |
4 |
tdb |
1.17 |
* Copyright (C) 2000-2004 i-scream |
5 |
pajs |
1.1 |
* |
6 |
tdb |
1.17 |
* This library is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
pajs |
1.1 |
* |
11 |
tdb |
1.17 |
* This library is distributed in the hope that it will be useful, |
12 |
pajs |
1.1 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
tdb |
1.17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
pajs |
1.1 |
* |
16 |
tdb |
1.17 |
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with this library; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
|
|
* 02111-1307 USA |
20 |
pajs |
1.1 |
*/ |
21 |
|
|
|
22 |
|
|
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include "config.h" |
24 |
|
|
#endif |
25 |
|
|
|
26 |
pajs |
1.6 |
#include "statgrab.h" |
27 |
|
|
#if defined(SOLARIS) || defined(LINUX) |
28 |
pajs |
1.1 |
#include <stdio.h> |
29 |
|
|
#include <stdlib.h> |
30 |
pajs |
1.5 |
#include <sys/types.h> |
31 |
|
|
#include <dirent.h> |
32 |
|
|
#include <string.h> |
33 |
pajs |
1.6 |
#endif |
34 |
pajs |
1.1 |
|
35 |
ats |
1.13 |
#include "tools.h" |
36 |
pajs |
1.1 |
#ifdef SOLARIS |
37 |
|
|
#include <procfs.h> |
38 |
|
|
#include <limits.h> |
39 |
pajs |
1.5 |
#define PROC_LOCATION "/proc" |
40 |
|
|
#define MAX_FILE_LENGTH PATH_MAX |
41 |
|
|
#endif |
42 |
|
|
#ifdef LINUX |
43 |
ats |
1.16 |
#include <limits.h> |
44 |
pajs |
1.1 |
#define PROC_LOCATION "/proc" |
45 |
|
|
#define MAX_FILE_LENGTH PATH_MAX |
46 |
|
|
#endif |
47 |
ats |
1.13 |
#ifdef ALLBSD |
48 |
pajs |
1.6 |
#include <kvm.h> |
49 |
|
|
#include <sys/param.h> |
50 |
|
|
#include <sys/sysctl.h> |
51 |
ats |
1.13 |
#endif |
52 |
|
|
#ifdef FREEBSD |
53 |
pajs |
1.6 |
#include <sys/user.h> |
54 |
|
|
#endif |
55 |
pajs |
1.1 |
|
56 |
|
|
process_stat_t *get_process_stats(){ |
57 |
|
|
|
58 |
|
|
static process_stat_t process_stat; |
59 |
|
|
|
60 |
pajs |
1.6 |
#if defined(SOLARIS) || defined(LINUX) |
61 |
pajs |
1.1 |
DIR *proc_dir; |
62 |
|
|
struct dirent *dir_entry; |
63 |
|
|
char filename[MAX_FILE_LENGTH]; |
64 |
|
|
FILE *f; |
65 |
pajs |
1.6 |
#endif |
66 |
pajs |
1.5 |
#ifdef LINUX |
67 |
|
|
char *line_ptr; |
68 |
|
|
#endif |
69 |
|
|
#ifdef SOLARIS |
70 |
|
|
psinfo_t process_info; |
71 |
|
|
#endif |
72 |
ats |
1.13 |
#ifdef ALLBSD |
73 |
pajs |
1.6 |
struct kinfo_proc *kp_stats; |
74 |
ats |
1.12 |
kvm_t *kvmd; |
75 |
pajs |
1.6 |
int procs; |
76 |
|
|
#endif |
77 |
pajs |
1.1 |
|
78 |
|
|
process_stat.sleeping=0; |
79 |
|
|
process_stat.running=0; |
80 |
|
|
process_stat.zombie=0; |
81 |
|
|
process_stat.stopped=0; |
82 |
pajs |
1.5 |
process_stat.total=0; |
83 |
pajs |
1.1 |
|
84 |
pajs |
1.7 |
#if defined(SOLARIS) || defined(LINUX) |
85 |
pajs |
1.1 |
if((proc_dir=opendir(PROC_LOCATION))==NULL){ |
86 |
|
|
return NULL; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
while((dir_entry=readdir(proc_dir))!=NULL){ |
90 |
|
|
if(atoi(dir_entry->d_name) == 0) continue; |
91 |
pajs |
1.5 |
|
92 |
|
|
#ifdef SOLARIS |
93 |
pajs |
1.1 |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name); |
94 |
pajs |
1.5 |
#endif |
95 |
|
|
#ifdef LINUX |
96 |
|
|
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name); |
97 |
|
|
#endif |
98 |
pajs |
1.1 |
|
99 |
|
|
if((f=fopen(filename, "r"))==NULL){ |
100 |
|
|
/* Open failed.. Process since vanished, or the path was too long. |
101 |
|
|
* Ah well, move onwards to the next one */ |
102 |
|
|
continue; |
103 |
|
|
} |
104 |
|
|
|
105 |
pajs |
1.5 |
#ifdef SOLARIS |
106 |
pajs |
1.1 |
fread(&process_info, sizeof(psinfo_t), 1, f); |
107 |
|
|
if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++; |
108 |
|
|
if(process_info.pr_lwp.pr_state==2) process_stat.running++; |
109 |
|
|
if(process_info.pr_lwp.pr_state==3) process_stat.zombie++; |
110 |
|
|
if(process_info.pr_lwp.pr_state==4) process_stat.stopped++; |
111 |
|
|
if(process_info.pr_lwp.pr_state==6) process_stat.running++; |
112 |
pajs |
1.5 |
#endif |
113 |
|
|
#ifdef LINUX |
114 |
|
|
if((line_ptr=f_read_line(f, "State:"))==NULL){ |
115 |
|
|
fclose(f); |
116 |
|
|
continue; |
117 |
|
|
} |
118 |
|
|
if((line_ptr=strchr(line_ptr, '\t'))==NULL){ |
119 |
|
|
fclose(f); |
120 |
|
|
continue; |
121 |
|
|
} |
122 |
|
|
line_ptr++; |
123 |
|
|
if(line_ptr=='\0'){ |
124 |
|
|
fclose(f); |
125 |
|
|
continue; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
if(*line_ptr=='S') process_stat.sleeping++; |
129 |
|
|
if(*line_ptr=='R') process_stat.running++; |
130 |
|
|
if(*line_ptr=='Z') process_stat.zombie++; |
131 |
|
|
if(*line_ptr=='T') process_stat.stopped++; |
132 |
|
|
if(*line_ptr=='D') process_stat.stopped++; |
133 |
|
|
#endif |
134 |
|
|
|
135 |
|
|
fclose(f); |
136 |
pajs |
1.1 |
} |
137 |
|
|
closedir(proc_dir); |
138 |
pajs |
1.6 |
#endif |
139 |
ats |
1.13 |
#ifdef ALLBSD |
140 |
ats |
1.12 |
if((kvmd = get_kvm()) == NULL){ |
141 |
pajs |
1.6 |
return NULL; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs); |
145 |
pajs |
1.1 |
|
146 |
pajs |
1.6 |
while(procs--){ |
147 |
pajs |
1.9 |
#ifdef FREEBSD5 |
148 |
|
|
if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++; |
149 |
pajs |
1.11 |
if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++; |
150 |
|
|
if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++; |
151 |
pajs |
1.9 |
if (kp_stats[procs].ki_stat == SRUN) process_stat.running++; |
152 |
|
|
if (kp_stats[procs].ki_stat == SIDL) process_stat.running++; |
153 |
|
|
if (kp_stats[procs].ki_stat == SZOMB) process_stat.zombie++; |
154 |
|
|
if (kp_stats[procs].ki_stat == SSTOP) process_stat.stopped++; |
155 |
pajs |
1.11 |
|
156 |
pajs |
1.9 |
#else |
157 |
pajs |
1.6 |
if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++; |
158 |
|
|
if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++; |
159 |
|
|
if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++; |
160 |
|
|
if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++; |
161 |
|
|
if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++; |
162 |
pajs |
1.9 |
#endif |
163 |
pajs |
1.6 |
} |
164 |
ats |
1.15 |
#endif |
165 |
|
|
|
166 |
|
|
#ifdef CYGWIN |
167 |
|
|
return NULL; |
168 |
pajs |
1.6 |
#endif |
169 |
pajs |
1.4 |
|
170 |
|
|
process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped; |
171 |
pajs |
1.10 |
|
172 |
pajs |
1.1 |
return &process_stat; |
173 |
|
|
} |