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.7
Committed: Mon Mar 3 12:32:36 2003 UTC (21 years, 8 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +0 -0 lines
State: FILE REMOVED
Log Message:
Following up on Pete's commit of the new ihost - the new configure stuff.
Also dropped the old libstatgrab.

File Contents

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