1 |
< |
/* |
1 |
> |
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
3 |
> |
* http://www.i-scream.org |
4 |
> |
* Copyright (C) 2000-2004 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. |
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 program is distributed in the hope that it will be useful, |
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 |
14 |
< |
* GNU General Public License for more details. |
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 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. |
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 |
> |
* $Id$ |
22 |
|
*/ |
23 |
|
|
24 |
|
#ifdef HAVE_CONFIG_H |
25 |
|
#include "config.h" |
26 |
|
#endif |
27 |
|
|
28 |
+ |
#include "statgrab.h" |
29 |
+ |
#if defined(SOLARIS) || defined(LINUX) |
30 |
|
#include <stdio.h> |
31 |
|
#include <stdlib.h> |
32 |
< |
#include <unistd.h> |
33 |
< |
#include "statgrab.h" |
32 |
> |
#include <sys/types.h> |
33 |
> |
#include <dirent.h> |
34 |
> |
#include <string.h> |
35 |
> |
#endif |
36 |
|
|
37 |
|
#ifdef SOLARIS |
38 |
|
#include <procfs.h> |
39 |
|
#include <limits.h> |
33 |
– |
#include <dirent.h> |
34 |
– |
#include <string.h> |
40 |
|
#define PROC_LOCATION "/proc" |
41 |
|
#define MAX_FILE_LENGTH PATH_MAX |
42 |
|
#endif |
43 |
+ |
#ifdef LINUX |
44 |
+ |
#include <limits.h> |
45 |
+ |
#include <unistd.h> |
46 |
+ |
#include <sys/stat.h> |
47 |
+ |
#include <fcntl.h> |
48 |
+ |
#define PROC_LOCATION "/proc" |
49 |
+ |
#define MAX_FILE_LENGTH PATH_MAX |
50 |
+ |
#endif |
51 |
+ |
#ifdef ALLBSD |
52 |
+ |
#include <stdlib.h> |
53 |
+ |
#include <sys/param.h> |
54 |
+ |
#include <sys/sysctl.h> |
55 |
+ |
#if defined(FREEBSD) || defined(DFBSD) |
56 |
+ |
#include <sys/user.h> |
57 |
+ |
#else |
58 |
+ |
#include <sys/proc.h> |
59 |
+ |
#endif |
60 |
+ |
#endif |
61 |
|
|
62 |
< |
process_stat_t *get_process_stats(){ |
62 |
> |
int get_proc_snapshot(proc_state_t **ps){ |
63 |
> |
proc_state_t *proc_state = NULL; |
64 |
> |
proc_state_t *proc_state_ptr; |
65 |
> |
int proc_state_size = 0; |
66 |
> |
#if defined(SOLARIS) || defined(LINUX) |
67 |
> |
DIR *proc_dir; |
68 |
> |
struct dirent *dir_entry; |
69 |
> |
char filename[MAX_FILE_LENGTH]; |
70 |
> |
FILE *f; |
71 |
> |
#ifdef SOLARIS |
72 |
> |
psinfo_t process_info; |
73 |
> |
#endif |
74 |
> |
#ifdef LINUX |
75 |
> |
char s; |
76 |
> |
/* If someone has a executable of 4k filename length, they deserve to get it truncated :) */ |
77 |
> |
char ps_name[4096]; |
78 |
> |
char *ptr; |
79 |
> |
static char *psargs = NULL; |
80 |
> |
static int psarg_size = 0; |
81 |
> |
unsigned long stime, utime; |
82 |
> |
int x; |
83 |
> |
int fn; |
84 |
> |
int toread; |
85 |
> |
ssize_t size; |
86 |
> |
int t_read; |
87 |
> |
#endif |
88 |
|
|
89 |
< |
static process_stat_t process_stat; |
89 |
> |
if((proc_dir=opendir(PROC_LOCATION))==NULL){ |
90 |
> |
return -1; |
91 |
> |
} |
92 |
|
|
93 |
< |
psinfo_t process_info; |
94 |
< |
DIR *proc_dir; |
45 |
< |
struct dirent *dir_entry; |
46 |
< |
char filename[MAX_FILE_LENGTH]; |
47 |
< |
FILE *f; |
93 |
> |
while((dir_entry=readdir(proc_dir))!=NULL){ |
94 |
> |
if(atoi(dir_entry->d_name) == 0) continue; |
95 |
|
|
96 |
< |
process_stat.sleeping=0; |
97 |
< |
process_stat.running=0; |
98 |
< |
process_stat.zombie=0; |
99 |
< |
process_stat.stopped=0; |
100 |
< |
process_stat.running=0; |
96 |
> |
#ifdef SOLARIS |
97 |
> |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name); |
98 |
> |
#endif |
99 |
> |
#ifdef LINUX |
100 |
> |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/stat", dir_entry->d_name); |
101 |
> |
#endif |
102 |
> |
if((f=fopen(filename, "r"))==NULL){ |
103 |
> |
/* Open failed.. Process since vanished, or the path was too long. |
104 |
> |
* Ah well, move onwards to the next one */ |
105 |
> |
continue; |
106 |
> |
} |
107 |
> |
#ifdef SOLARIS |
108 |
> |
fread(&process_info, sizeof(psinfo_t), 1, f); |
109 |
> |
#endif |
110 |
|
|
111 |
< |
if((proc_dir=opendir(PROC_LOCATION))==NULL){ |
112 |
< |
return NULL; |
113 |
< |
} |
111 |
> |
proc_state = realloc(proc_state, (1+proc_state_size)*sizeof(proc_state_t)); |
112 |
> |
proc_state_ptr = proc_state+proc_state_size; |
113 |
> |
#ifdef SOLARIS |
114 |
> |
proc_state_ptr->pid = process_info.pr_pid; |
115 |
> |
proc_state_ptr->parent = process_info.pr_ppid; |
116 |
> |
proc_state_ptr->pgid = process_info.pr_pgid; |
117 |
> |
proc_state_ptr->uid = process_info.pr_uid; |
118 |
> |
proc_state_ptr->euid = process_info.pr_euid; |
119 |
> |
proc_state_ptr->gid = process_info.pr_gid; |
120 |
> |
proc_state_ptr->egid = process_info.pr_egid; |
121 |
> |
proc_state_ptr->proc_size = (process_info.pr_size) * 1024; |
122 |
> |
proc_state_ptr->proc_resident = (process_info.pr_rssize) * 1024; |
123 |
> |
proc_state_ptr->time_spent = process_info.pr_time.tv_sec; |
124 |
> |
proc_state_ptr->cpu_percent = (process_info.pr_pctcpu * 100.0) / 0x8000; |
125 |
> |
proc_state_ptr->process_name = strdup(process_info.pr_fname); |
126 |
> |
proc_state_ptr->proctitle = strdup(process_info.pr_psargs); |
127 |
|
|
128 |
< |
while((dir_entry=readdir(proc_dir))!=NULL){ |
129 |
< |
if(atoi(dir_entry->d_name) == 0) continue; |
130 |
< |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name); |
128 |
> |
if(process_info.pr_lwp.pr_state==1) proc_state_ptr->state = SLEEPING; |
129 |
> |
if(process_info.pr_lwp.pr_state==2) proc_state_ptr->state = RUNNING; |
130 |
> |
if(process_info.pr_lwp.pr_state==3) proc_state_ptr->state = ZOMBIE; |
131 |
> |
if(process_info.pr_lwp.pr_state==4) proc_state_ptr->state = STOPPED; |
132 |
> |
if(process_info.pr_lwp.pr_state==6) proc_state_ptr->state = RUNNING; |
133 |
> |
#endif |
134 |
> |
#ifdef LINUX |
135 |
> |
x = fscanf(f, "%d %4096s %c %d %d %*d %*d %*d %*lu %*lu %*lu %*lu %*lu %lu %lu %*ld %*ld %*ld %d %*ld %*ld %*lu %llu %llu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*d %*d\n", &(proc_state_ptr->pid), ps_name, &s, &(proc_state_ptr->parent), &(proc_state_ptr->pgid), &utime, &stime, &(proc_state_ptr->nice), &(proc_state_ptr->proc_size), &(proc_state_ptr->proc_resident)); |
136 |
> |
proc_state_ptr->proc_resident = proc_state_ptr->proc_resident * getpagesize(); |
137 |
> |
if(s == 'S') proc_state_ptr->state = SLEEPING; |
138 |
> |
if(s == 'R') proc_state_ptr->state = RUNNING; |
139 |
> |
if(s == 'Z') proc_state_ptr->state = ZOMBIE; |
140 |
> |
if(s == 'T') proc_state_ptr->state = STOPPED; |
141 |
> |
if(s == 'D') proc_state_ptr->state = STOPPED; |
142 |
> |
|
143 |
> |
/* pa_name[0] should = '(' */ |
144 |
> |
ptr = strchr(&ps_name[1], ')'); |
145 |
> |
if(ptr !=NULL) *ptr='\0'; |
146 |
> |
proc_state_ptr->process_name = strdup(&ps_name[1]); |
147 |
|
|
148 |
< |
if((f=fopen(filename, "r"))==NULL){ |
149 |
< |
/* Open failed.. Process since vanished, or the path was too long. |
150 |
< |
* Ah well, move onwards to the next one */ |
151 |
< |
continue; |
148 |
> |
/* Need to do cpu */ |
149 |
> |
|
150 |
> |
|
151 |
> |
/* proctitle */ |
152 |
> |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/cmdline", dir_entry->d_name); |
153 |
> |
|
154 |
> |
if((fn=open(filename, O_RDONLY)) == -1){ |
155 |
> |
/* Open failed.. Process since vanished, or the path was too long. |
156 |
> |
* Ah well, move onwards to the next one */ |
157 |
> |
continue; |
158 |
> |
} |
159 |
> |
#define PSARG_START_SIZE 128 |
160 |
> |
if(psargs == NULL){ |
161 |
> |
psargs = malloc(PSARG_START_SIZE); |
162 |
> |
psarg_size = PSARG_START_SIZE; |
163 |
|
} |
164 |
+ |
ptr = psargs; |
165 |
+ |
t_read = 0; |
166 |
+ |
toread = psarg_size; |
167 |
+ |
while((size = read(fn, ptr, toread)) == toread){ |
168 |
+ |
psargs = realloc(psargs, (psarg_size + PSARG_START_SIZE)); |
169 |
+ |
ptr = psargs+psarg_size; |
170 |
+ |
t_read = psarg_size; |
171 |
+ |
psarg_size+=PSARG_START_SIZE; |
172 |
+ |
toread = PSARG_START_SIZE; |
173 |
+ |
} |
174 |
+ |
if(size != -1) t_read+=size; |
175 |
|
|
176 |
< |
fread(&process_info, sizeof(psinfo_t), 1, f); |
177 |
< |
fclose(f); |
176 |
> |
ptr = psargs; |
177 |
> |
for(x=0; x<t_read; x++){ |
178 |
> |
if (*ptr == '\0') *ptr = ' '; |
179 |
> |
ptr++; |
180 |
> |
} |
181 |
> |
/* for safety sake */ |
182 |
> |
psargs[t_read] = '\0'; |
183 |
|
|
184 |
< |
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); |
184 |
> |
proc_state_ptr->proctitle = strdup(psargs); |
185 |
|
|
186 |
< |
return &process_stat; |
186 |
> |
#endif |
187 |
> |
|
188 |
> |
proc_state_size++; |
189 |
> |
#endif |
190 |
> |
|
191 |
> |
|
192 |
> |
fclose(f); |
193 |
> |
} |
194 |
> |
closedir(proc_dir); |
195 |
> |
*ps = proc_state; |
196 |
> |
|
197 |
> |
return proc_state_size; |
198 |
|
} |
199 |
+ |
|