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