45 |
#include <time.h> |
#include <time.h> |
46 |
#endif |
#endif |
47 |
|
|
|
#ifdef HAVE_SYS_LOADAVG_H |
|
|
#include <sys/loadavg.h> |
|
|
#endif |
|
|
|
|
48 |
#ifdef HAVE_STRING_H |
#ifdef HAVE_STRING_H |
49 |
#include <string.h> |
#include <string.h> |
50 |
#endif |
#endif |
54 |
#include "monitor.h" |
#include "monitor.h" |
55 |
#include "monit_process.h" |
#include "monit_process.h" |
56 |
#include "process/process.h" |
#include "process/process.h" |
57 |
|
#include "process/sysdep.h" |
58 |
|
|
59 |
/** |
/** |
60 |
* General purpose /proc methods. |
* General purpose /proc methods. |
79 |
|
|
80 |
int include_children= TRUE; |
int include_children= TRUE; |
81 |
int num_cpus=1; |
int num_cpus=1; |
82 |
|
int mem_kbyte_max=0; |
83 |
|
|
84 |
|
|
85 |
/** |
/** |
100 |
* @param pid The process id |
* @param pid The process id |
101 |
* @return TRUE if succeeded otherwise FALSE. |
* @return TRUE if succeeded otherwise FALSE. |
102 |
*/ |
*/ |
103 |
int update_process_data(Process_T p, pid_t pid) { |
int update_process_data(Process_T p, ProcessTree_T *pt, int treesize, pid_t pid) { |
104 |
|
|
105 |
ProcInfo_T pi; |
ProcInfo_T pi; |
106 |
|
ProcessTree_T *leaf; |
107 |
|
|
108 |
ASSERT(p); |
ASSERT(p); |
109 |
|
|
116 |
|
|
117 |
} |
} |
118 |
|
|
119 |
|
if ((leaf = findprocess(pid, pt, treesize)) != NULL ) { |
120 |
|
|
121 |
|
pi->children=leaf->children_sum; |
122 |
|
pi->total_mem_kbyte=leaf->mem_kbyte_sum; |
123 |
|
|
124 |
|
if ( mem_kbyte_max == 0 ) { |
125 |
|
|
126 |
|
pi->total_mem_percent=0; |
127 |
|
|
128 |
|
} else { |
129 |
|
|
130 |
|
pi->total_mem_percent=(int) ((double) leaf->mem_kbyte_sum * 1000.0 / mem_kbyte_max); |
131 |
|
|
132 |
|
} |
133 |
|
|
134 |
|
} else { |
135 |
|
|
136 |
|
pi->children=0; |
137 |
|
pi->total_mem_kbyte=pi->mem_kbyte; |
138 |
|
pi->total_mem_percent=pi->mem_percent; |
139 |
|
|
140 |
|
} |
141 |
|
|
142 |
return TRUE; |
return TRUE; |
143 |
} |
} |
144 |
|
|
149 |
*/ |
*/ |
150 |
int update_loadavg(void) { |
int update_loadavg(void) { |
151 |
|
|
152 |
if ( -1 == getloadavg( Run.loadavg, 3) ) { |
if ( -1 == getloadavg_sysdep( Run.loadavg, 3) ) { |
153 |
|
|
154 |
return FALSE; |
return FALSE; |
155 |
|
|
157 |
|
|
158 |
return TRUE; |
return TRUE; |
159 |
} |
} |
160 |
|
|
161 |
|
|
162 |
|
/** |
163 |
|
* Initilize the process tree |
164 |
|
* @param reference reference of ProcessTree |
165 |
|
* @return treesize>=0 if succeeded otherwise <0. |
166 |
|
*/ |
167 |
|
int initprocesstree(ProcessTree_T ** reference) { |
168 |
|
|
169 |
|
int treesize; |
170 |
|
int i; |
171 |
|
ProcessTree_T * pt; |
172 |
|
|
173 |
|
ASSERT(reference); |
174 |
|
|
175 |
|
if ((treesize = initprocesstree_sysdep(reference)) <= 0) { |
176 |
|
|
177 |
|
return 0; |
178 |
|
|
179 |
|
} |
180 |
|
|
181 |
|
pt = * reference; |
182 |
|
|
183 |
|
for ( i = 0; i < treesize; i ++ ) { |
184 |
|
|
185 |
|
if ( pt[i].ppid == 0 ) { |
186 |
|
|
187 |
|
continue; |
188 |
|
|
189 |
|
} |
190 |
|
|
191 |
|
pt[i].parent = (struct myprocesstree *) findprocess(pt[i].ppid, pt, treesize); |
192 |
|
|
193 |
|
if ( pt[i].parent == NULL ) { |
194 |
|
|
195 |
|
/* inconsitancy found, most probably a race condition |
196 |
|
we might lack accuracy but we remain stable! */ |
197 |
|
|
198 |
|
pt[i].pid=0; |
199 |
|
|
200 |
|
continue; |
201 |
|
|
202 |
|
} |
203 |
|
|
204 |
|
if (! connectchild((ProcessTree_T *) pt[i].parent, &pt[i])) { |
205 |
|
|
206 |
|
/* connection to parent process has failed, this is |
207 |
|
usually caused in the part above */ |
208 |
|
|
209 |
|
pt[i].pid=0; |
210 |
|
|
211 |
|
continue; |
212 |
|
|
213 |
|
} |
214 |
|
|
215 |
|
} |
216 |
|
|
217 |
|
fillprocesstree((struct myprocesstree *) findprocess(1, pt, treesize)); |
218 |
|
|
219 |
|
return treesize; |
220 |
|
|
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
/** |
225 |
|
* Search a leaf in the processtree |
226 |
|
* @param pid pid of the process |
227 |
|
* @param pt processtree |
228 |
|
* @param treesize size of the processtree |
229 |
|
* @return pointer of the process if succeeded otherwise NULL. |
230 |
|
*/ |
231 |
|
ProcessTree_T * findprocess(int pid, ProcessTree_T * pt, int size) { |
232 |
|
|
233 |
|
int i; |
234 |
|
|
235 |
|
ASSERT(pt); |
236 |
|
|
237 |
|
if (( pid == 0 ) || ( size <= 0 )) { |
238 |
|
|
239 |
|
return NULL; |
240 |
|
|
241 |
|
} |
242 |
|
|
243 |
|
for ( i = 0; i < size; i ++ ) { |
244 |
|
|
245 |
|
if ( pid == pt[i].pid ) { |
246 |
|
|
247 |
|
return &pt[i]; |
248 |
|
|
249 |
|
} |
250 |
|
|
251 |
|
} |
252 |
|
|
253 |
|
return NULL; |
254 |
|
|
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
|
* Initilize the process tree |
259 |
|
* @param pt processtree |
260 |
|
* @param treesize size of the processtree |
261 |
|
*/ |
262 |
|
void delprocesstree(ProcessTree_T * pt, int treesize) { |
263 |
|
|
264 |
|
int i; |
265 |
|
|
266 |
|
ASSERT(pt); |
267 |
|
|
268 |
|
if ( treesize <= 0 ) { |
269 |
|
|
270 |
|
return; |
271 |
|
|
272 |
|
} |
273 |
|
|
274 |
|
for ( i = 0; i < treesize; i ++ ) { |
275 |
|
|
276 |
|
if ( pt[i].children!=NULL ) { |
277 |
|
|
278 |
|
free(pt[i].children); |
279 |
|
|
280 |
|
} |
281 |
|
|
282 |
|
} |
283 |
|
|
284 |
|
free(pt); |
285 |
|
|
286 |
|
return; |
287 |
|
|
288 |
|
} |