1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2004 i-scream |
5 |
* |
6 |
* 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 |
* |
11 |
* This library 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 GNU |
14 |
* Lesser General Public License for more details. |
15 |
* |
16 |
* 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 |
*/ |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include "config.h" |
24 |
#endif |
25 |
|
26 |
#include "statgrab.h" |
27 |
#if defined(SOLARIS) || defined(LINUX) |
28 |
#include <stdio.h> |
29 |
#include <stdlib.h> |
30 |
#include <sys/types.h> |
31 |
#include <dirent.h> |
32 |
#include <string.h> |
33 |
#endif |
34 |
|
35 |
#include "tools.h" |
36 |
#ifdef SOLARIS |
37 |
#include <procfs.h> |
38 |
#include <limits.h> |
39 |
#define PROC_LOCATION "/proc" |
40 |
#define MAX_FILE_LENGTH PATH_MAX |
41 |
#endif |
42 |
#ifdef LINUX |
43 |
#include <limits.h> |
44 |
#define PROC_LOCATION "/proc" |
45 |
#define MAX_FILE_LENGTH PATH_MAX |
46 |
#endif |
47 |
#ifdef ALLBSD |
48 |
#include <kvm.h> |
49 |
#include <sys/param.h> |
50 |
#include <sys/sysctl.h> |
51 |
#endif |
52 |
#ifdef FREEBSD |
53 |
#include <sys/user.h> |
54 |
#endif |
55 |
|
56 |
process_stat_t *get_process_stats(){ |
57 |
|
58 |
static process_stat_t process_stat; |
59 |
|
60 |
#if defined(SOLARIS) || defined(LINUX) |
61 |
DIR *proc_dir; |
62 |
struct dirent *dir_entry; |
63 |
char filename[MAX_FILE_LENGTH]; |
64 |
FILE *f; |
65 |
#endif |
66 |
#ifdef LINUX |
67 |
char *line_ptr; |
68 |
#endif |
69 |
#ifdef SOLARIS |
70 |
psinfo_t process_info; |
71 |
#endif |
72 |
#ifdef ALLBSD |
73 |
struct kinfo_proc *kp_stats; |
74 |
kvm_t *kvmd; |
75 |
int procs; |
76 |
#endif |
77 |
|
78 |
process_stat.sleeping=0; |
79 |
process_stat.running=0; |
80 |
process_stat.zombie=0; |
81 |
process_stat.stopped=0; |
82 |
process_stat.total=0; |
83 |
|
84 |
#if defined(SOLARIS) || defined(LINUX) |
85 |
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 |
|
92 |
#ifdef SOLARIS |
93 |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name); |
94 |
#endif |
95 |
#ifdef LINUX |
96 |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name); |
97 |
#endif |
98 |
|
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 |
#ifdef SOLARIS |
106 |
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 |
#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 |
} |
137 |
closedir(proc_dir); |
138 |
#endif |
139 |
#ifdef ALLBSD |
140 |
if((kvmd = get_kvm()) == NULL){ |
141 |
return NULL; |
142 |
} |
143 |
|
144 |
kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs); |
145 |
|
146 |
while(procs--){ |
147 |
#ifdef FREEBSD5 |
148 |
if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++; |
149 |
if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++; |
150 |
if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++; |
151 |
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 |
|
156 |
#else |
157 |
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 |
#endif |
163 |
} |
164 |
#endif |
165 |
|
166 |
#ifdef CYGWIN |
167 |
return NULL; |
168 |
#endif |
169 |
|
170 |
process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped; |
171 |
|
172 |
return &process_stat; |
173 |
} |