1 |
tdb |
1.3 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
|
|
* Copyright (C) 2000-2002 i-scream |
4 |
|
|
* |
5 |
|
|
* This program is free software; you can redistribute it and/or |
6 |
|
|
* modify it under the terms of the GNU General Public License |
7 |
|
|
* as published by the Free Software Foundation; either version 2 |
8 |
|
|
* of the License, or (at your option) any later version. |
9 |
|
|
* |
10 |
|
|
* This program is distributed in the hope that it will be useful, |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
* GNU General Public License for more details. |
14 |
|
|
* |
15 |
|
|
* You should have received a copy of the GNU General Public License |
16 |
|
|
* along with this program; if not, write to the Free Software |
17 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
pajs |
1.1 |
#include <stdio.h> |
21 |
|
|
#include "ukcprog.h" |
22 |
|
|
#include <string.h> |
23 |
|
|
#ifdef LINUX |
24 |
|
|
#include <dirent.h> |
25 |
|
|
#include <unistd.h> |
26 |
|
|
#include <stdlib.h> |
27 |
|
|
#endif |
28 |
|
|
char *get_process_stats(){ |
29 |
|
|
char *xml_ps_stats; |
30 |
|
|
int zombie=0; |
31 |
|
|
int sleeping=0; |
32 |
|
|
int running=0; |
33 |
|
|
int stopped=0; |
34 |
|
|
FILE *f; |
35 |
|
|
char *line; |
36 |
|
|
char *line_p; |
37 |
|
|
#ifdef LINUX |
38 |
|
|
DIR *procdir; |
39 |
|
|
struct dirent *procent; |
40 |
|
|
char fname[_POSIX_PATH_MAX]; |
41 |
|
|
#endif |
42 |
|
|
|
43 |
|
|
#ifdef SOLARIS |
44 |
|
|
#define SOL_PS_CMD "/bin/ps -Al" |
45 |
|
|
if((f=popen(SOL_PS_CMD, "r")) == NULL) { |
46 |
|
|
errf("Failed to execute the PS command (%m)"); |
47 |
|
|
return NULL; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
/* Throw away the first line as its the header from PS */ |
51 |
|
|
fpgetline(f); |
52 |
|
|
|
53 |
|
|
while((line=fpgetline(f)) != NULL) { |
54 |
|
|
line_p=line; |
55 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
56 |
|
|
line_p=strchr(line_p, ' '); |
57 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
58 |
|
|
if (line_p==NULL) abort(); |
59 |
|
|
/* Ok, we should now be at the state :) .. */ |
60 |
|
|
if (*line_p=='S') sleeping++; |
61 |
|
|
if (*line_p=='R') running++; |
62 |
|
|
if (*line_p=='Z') zombie++; |
63 |
|
|
if (*line_p=='T') stopped++; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
if((pclose(f)) == -1) { |
67 |
|
|
errf("Failed to close process stats (%m)"); |
68 |
|
|
return NULL; |
69 |
|
|
} |
70 |
|
|
#endif |
71 |
|
|
#ifdef FREEBSD |
72 |
|
|
#define FREEBSD_PS_CMD "/bin/ps -ax" |
73 |
|
|
|
74 |
|
|
if((f=popen(FREEBSD_PS_CMD, "r")) == NULL) { |
75 |
|
|
errf("Failed to execute ps"); |
76 |
|
|
return NULL; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/* Throw away first line */ |
80 |
|
|
fpgetline(f); |
81 |
|
|
while((line=fpgetline(f)) != NULL) { |
82 |
|
|
line_p=line; |
83 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
84 |
|
|
line_p=strchr(line_p, ' '); |
85 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
86 |
|
|
line_p=strchr(line_p, ' '); |
87 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
88 |
|
|
if (line_p==NULL) abort(); |
89 |
|
|
|
90 |
|
|
if (*line_p=='S') sleeping++; |
91 |
|
|
if (*line_p=='I') sleeping++; |
92 |
|
|
if (*line_p=='R') running++; |
93 |
|
|
if (*line_p=='D') running++; |
94 |
|
|
if (*line_p=='Z') zombie++; |
95 |
|
|
if (*line_p=='T') stopped++; |
96 |
|
|
if (*line_p=='J') running++; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
if((pclose(f)) != 0) { |
100 |
|
|
errf("Failed to close ps (%m)"); |
101 |
|
|
return NULL; |
102 |
|
|
} |
103 |
|
|
#endif |
104 |
|
|
#ifdef LINUX |
105 |
|
|
chdir("/proc"); |
106 |
|
|
if((procdir=opendir(".")) == NULL){ |
107 |
|
|
errf("Failed to open proc (%m)"); |
108 |
|
|
return NULL; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
while((procent = readdir(procdir)) != NULL){ |
112 |
|
|
if(atoi(procent->d_name) == 0) continue; |
113 |
|
|
strncpy(fname, procent->d_name, _POSIX_PATH_MAX-7); |
114 |
|
|
strcat(fname, "/status"); |
115 |
|
|
|
116 |
|
|
if((f=fopen(fname, "r")) == NULL){ |
117 |
|
|
errf("Failed to open process stat (%m)"); |
118 |
|
|
return NULL; |
119 |
|
|
} |
120 |
|
|
|
121 |
pajs |
1.4 |
while((line=fpgetline(f)) != NULL){ |
122 |
|
|
if(strncasecmp(line,"State:",6)==0) break; |
123 |
|
|
} |
124 |
pajs |
1.1 |
|
125 |
pajs |
1.4 |
line_p=line; |
126 |
|
|
for(;*line_p++ != '\t';); |
127 |
pajs |
1.1 |
|
128 |
|
|
|
129 |
pajs |
1.4 |
if(*line_p=='R') running++; |
130 |
|
|
if(*line_p=='S') sleeping++; |
131 |
|
|
if(*line_p=='Z') zombie++; |
132 |
|
|
if(*line_p=='T') stopped++; |
133 |
|
|
if(*line_p=='D') stopped++; |
134 |
|
|
|
135 |
|
|
if ((fclose(f)) != 0) { |
136 |
|
|
errf("Failed to close file (%m)"); |
137 |
|
|
return NULL; |
138 |
|
|
} |
139 |
pajs |
1.1 |
|
140 |
|
|
} |
141 |
|
|
closedir(procdir); |
142 |
|
|
#endif |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
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){ |
146 |
|
|
errf("strf failed"); |
147 |
|
|
return NULL; |
148 |
|
|
} |
149 |
|
|
return xml_ps_stats; |
150 |
|
|
} |