1 |
tdb |
1.1 |
/* |
2 |
|
|
* i-scream central monitoring system |
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. |
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 |
tdb |
1.7 |
* $Id: process_snapshot.c,v 1.6 2004/04/04 23:57:33 tdb Exp $ |
21 |
tdb |
1.1 |
*/ |
22 |
|
|
|
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <statgrab.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <unistd.h> |
27 |
|
|
|
28 |
|
|
int main(){ |
29 |
tdb |
1.7 |
sg_process_stats *ps; |
30 |
tdb |
1.1 |
int ps_size; |
31 |
|
|
int x; |
32 |
tdb |
1.4 |
char *state = NULL; |
33 |
tdb |
1.1 |
|
34 |
|
|
/* Initialise statgrab */ |
35 |
tdb |
1.7 |
sg_init(); |
36 |
tdb |
1.1 |
|
37 |
|
|
/* Drop setuid/setgid privileges. */ |
38 |
tdb |
1.7 |
if (sg_drop_privileges() != 0) { |
39 |
tdb |
1.1 |
perror("Error. Failed to drop privileges"); |
40 |
|
|
return 1; |
41 |
|
|
} |
42 |
|
|
|
43 |
tdb |
1.7 |
ps = sg_get_process_stats(&ps_size); |
44 |
tdb |
1.2 |
|
45 |
tdb |
1.7 |
if(ps == NULL){ |
46 |
tdb |
1.2 |
fprintf(stderr, "Failed to get process snapshot\n"); |
47 |
|
|
exit(1); |
48 |
|
|
} |
49 |
tdb |
1.1 |
|
50 |
|
|
printf("%5s %5s %5s %5s %5s %5s %5s %6s %6s %9s %-10s %-4s %-8s %-20s %s\n", |
51 |
|
|
"pid", "ppid", "pgid", "uid", "euid", "gid", "egid", "size", "res", "time", "cpu", "nice", "state", "name", "title"); |
52 |
|
|
|
53 |
|
|
for(x=0;x<ps_size;x++){ |
54 |
|
|
switch (ps->state) { |
55 |
tdb |
1.7 |
case SG_PROCESS_STATE_RUNNING: |
56 |
tdb |
1.1 |
state = "RUNNING"; |
57 |
|
|
break; |
58 |
tdb |
1.7 |
case SG_PROCESS_STATE_SLEEPING: |
59 |
tdb |
1.1 |
state = "SLEEPING"; |
60 |
|
|
break; |
61 |
tdb |
1.7 |
case SG_PROCESS_STATE_STOPPED: |
62 |
tdb |
1.1 |
state = "STOPPED"; |
63 |
|
|
break; |
64 |
tdb |
1.7 |
case SG_PROCESS_STATE_ZOMBIE: |
65 |
tdb |
1.1 |
state = "ZOMBIE"; |
66 |
tdb |
1.3 |
break; |
67 |
tdb |
1.7 |
case SG_PROCESS_STATE_UNKNOWN: |
68 |
|
|
default: |
69 |
tdb |
1.3 |
state = "UNKNOWN"; |
70 |
tdb |
1.1 |
break; |
71 |
|
|
} |
72 |
|
|
printf("%5d %5d %5d %5d %5d %5d %5d %5dM %5dM %8ds %10f %4d %-8s %-20s %s\n", |
73 |
|
|
(int)ps->pid, (int)ps->parent, (int)ps->pgid, (int)ps->uid, |
74 |
|
|
(int)ps->euid, (int)ps->gid, (int)ps->egid, |
75 |
|
|
(int)(ps->proc_size / (1024*1024)), |
76 |
|
|
(int)(ps->proc_resident / (1024*1024)), |
77 |
|
|
(int)ps->time_spent, (float)ps->cpu_percent, |
78 |
|
|
(int)ps->nice, state, |
79 |
|
|
ps->process_name, ps->proctitle); |
80 |
|
|
ps++; |
81 |
|
|
} |
82 |
|
|
return 0; |
83 |
|
|
} |