ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/examples/process_snapshot.c
Revision: 1.1
Committed: Sat Apr 3 16:55:38 2004 UTC (20 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
Log Message:
Add process_snapshot example for testing the new process stats code.

File Contents

# Content
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 * $Id$
21 */
22
23 #include <stdio.h>
24 #include <statgrab.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 int main(){
29 proc_state_t *ps;
30 int ps_size;
31 int x;
32 char *state;
33
34 /* Initialise statgrab */
35 statgrab_init();
36
37 /* Drop setuid/setgid privileges. */
38 if (statgrab_drop_privileges() != 0) {
39 perror("Error. Failed to drop privileges");
40 return 1;
41 }
42
43 ps_size = get_proc_snapshot(&ps);
44
45 printf("%5s %5s %5s %5s %5s %5s %5s %6s %6s %9s %-10s %-4s %-8s %-20s %s\n",
46 "pid", "ppid", "pgid", "uid", "euid", "gid", "egid", "size", "res", "time", "cpu", "nice", "state", "name", "title");
47
48 for(x=0;x<ps_size;x++){
49 switch (ps->state) {
50 case RUNNING:
51 state = "RUNNING";
52 break;
53 case SLEEPING:
54 state = "SLEEPING";
55 break;
56 case STOPPED:
57 state = "STOPPED";
58 break;
59 case ZOMBIE:
60 state = "ZOMBIE";
61 break;
62 }
63 printf("%5d %5d %5d %5d %5d %5d %5d %5dM %5dM %8ds %10f %4d %-8s %-20s %s\n",
64 (int)ps->pid, (int)ps->parent, (int)ps->pgid, (int)ps->uid,
65 (int)ps->euid, (int)ps->gid, (int)ps->egid,
66 (int)(ps->proc_size / (1024*1024)),
67 (int)(ps->proc_resident / (1024*1024)),
68 (int)ps->time_spent, (float)ps->cpu_percent,
69 (int)ps->nice, state,
70 ps->process_name, ps->proctitle);
71 ps++;
72 }
73 return 0;
74 }