ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/process_stats.c
Revision: 1.5
Committed: Tue May 21 16:47:12 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.4: +1 -0 lines
Log Message:
Added URL to GPL headers.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 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
21 #include <stdio.h>
22 #include "ukcprog.h"
23 #include <string.h>
24 #ifdef LINUX
25 #include <dirent.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #endif
29 char *get_process_stats(){
30 char *xml_ps_stats;
31 int zombie=0;
32 int sleeping=0;
33 int running=0;
34 int stopped=0;
35 FILE *f;
36 char *line;
37 char *line_p;
38 #ifdef LINUX
39 DIR *procdir;
40 struct dirent *procent;
41 char fname[_POSIX_PATH_MAX];
42 #endif
43
44 #ifdef SOLARIS
45 #define SOL_PS_CMD "/bin/ps -Al"
46 if((f=popen(SOL_PS_CMD, "r")) == NULL) {
47 errf("Failed to execute the PS command (%m)");
48 return NULL;
49 }
50
51 /* Throw away the first line as its the header from PS */
52 fpgetline(f);
53
54 while((line=fpgetline(f)) != NULL) {
55 line_p=line;
56 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
57 line_p=strchr(line_p, ' ');
58 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
59 if (line_p==NULL) abort();
60 /* Ok, we should now be at the state :) .. */
61 if (*line_p=='S') sleeping++;
62 if (*line_p=='R') running++;
63 if (*line_p=='Z') zombie++;
64 if (*line_p=='T') stopped++;
65 }
66
67 if((pclose(f)) == -1) {
68 errf("Failed to close process stats (%m)");
69 return NULL;
70 }
71 #endif
72 #ifdef FREEBSD
73 #define FREEBSD_PS_CMD "/bin/ps -ax"
74
75 if((f=popen(FREEBSD_PS_CMD, "r")) == NULL) {
76 errf("Failed to execute ps");
77 return NULL;
78 }
79
80 /* Throw away first line */
81 fpgetline(f);
82 while((line=fpgetline(f)) != NULL) {
83 line_p=line;
84 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
85 line_p=strchr(line_p, ' ');
86 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
87 line_p=strchr(line_p, ' ');
88 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
89 if (line_p==NULL) abort();
90
91 if (*line_p=='S') sleeping++;
92 if (*line_p=='I') sleeping++;
93 if (*line_p=='R') running++;
94 if (*line_p=='D') running++;
95 if (*line_p=='Z') zombie++;
96 if (*line_p=='T') stopped++;
97 if (*line_p=='J') running++;
98 }
99
100 if((pclose(f)) != 0) {
101 errf("Failed to close ps (%m)");
102 return NULL;
103 }
104 #endif
105 #ifdef LINUX
106 chdir("/proc");
107 if((procdir=opendir(".")) == NULL){
108 errf("Failed to open proc (%m)");
109 return NULL;
110 }
111
112 while((procent = readdir(procdir)) != NULL){
113 if(atoi(procent->d_name) == 0) continue;
114 strncpy(fname, procent->d_name, _POSIX_PATH_MAX-7);
115 strcat(fname, "/status");
116
117 if((f=fopen(fname, "r")) == NULL){
118 errf("Failed to open process stat (%m)");
119 return NULL;
120 }
121
122 while((line=fpgetline(f)) != NULL){
123 if(strncasecmp(line,"State:",6)==0) break;
124 }
125
126 line_p=line;
127 for(;*line_p++ != '\t';);
128
129
130 if(*line_p=='R') running++;
131 if(*line_p=='S') sleeping++;
132 if(*line_p=='Z') zombie++;
133 if(*line_p=='T') stopped++;
134 if(*line_p=='D') stopped++;
135
136 if ((fclose(f)) != 0) {
137 errf("Failed to close file (%m)");
138 return NULL;
139 }
140
141 }
142 closedir(procdir);
143 #endif
144
145
146 if((xml_ps_stats=strf("<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>", sleeping, running, zombie, stopped, (sleeping+running+zombie+stopped))) == NULL){
147 errf("strf failed");
148 return NULL;
149 }
150 return xml_ps_stats;
151 }