ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/plugins/linux/linux.c
Revision: 1.7
Committed: Tue Mar 19 10:38:05 2002 UTC (23 years, 9 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.6: +12 -9 lines
Log Message:
Fixed a out by one error on the number of sleeping process. Also made it
the parsing the same as solaris's (more SYSV happy ps output)

File Contents

# Content
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "ukcprog.h"
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/utsname.h>
7 #include <sys/vfs.h>
8 #include <utmp.h>
9 #include <pwd.h>
10
11 int die() {
12 exit (1);
13 }
14
15 void getLoadAv() {
16 FILE *f;
17 char *loadavg;
18 char *load_p;
19
20 if ((f=fopen("/proc/loadavg", "r" ))==NULL) {
21 errf("Failed to open load averages (%m)");
22 die();
23 }
24
25 if ((loadavg=fpgetline(f)) == NULL) {
26 errf("Failed to read any data for load averages (%m)");
27 die();
28 }
29
30 if ((fclose(f)) != 0) {
31 errf("Failed to close file (%m).");
32 die();
33 }
34
35 load_p=strtok(loadavg, " ");
36 printf("packet.load.load1 %s\n",load_p);
37 for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
38 load_p++;
39 if (load_p == NULL) abort();
40 load_p=strtok(load_p, " ");
41 if (load_p == NULL) abort();
42 printf("packet.load.load5 %s\n",load_p);
43 for(; (*load_p != ' ') && (*load_p != '\0'); load_p++);
44 load_p++;
45 if (load_p == NULL) abort();
46 load_p=strtok(load_p, " ");
47 if (load_p == NULL) abort();
48 printf("packet.load.load15 %s\n",load_p);
49 }
50
51 void getMemInfo() {
52 char *line;
53 char *mem=NULL;
54 char *swap=NULL;
55 char *ch;
56 long memstat[6];
57 long swapstat[3];
58 long tmp;
59 int counter=0;
60
61 FILE *f;
62
63 if ((f=fopen("/proc/meminfo", "r" ))==NULL) {
64 errf("Failed to open memory stats (%m)");
65 die();
66 }
67
68 while(((line=fpgetline(f)) != NULL) && (counter < 2)) {
69 if (((strncmp("Mem: ",line,5)) == 0)) {
70 mem=strdup(line);
71 counter++;
72 }
73 if (((strncmp(line,"Swap: ",6)) == 0)) {
74 swap=strdup(line);
75 counter++;
76 }
77 }
78
79 if ((fclose(f)) != 0) {
80 errf("Failed to close file (%m).");
81 die();
82 }
83
84 if (mem==NULL || swap==NULL){
85 errf("Failed to obtain information required for memory and swap stats");
86 die();
87 }
88
89 /* Get the info we want from the 2 read in lines */
90
91 ch = strchr(mem, ' ');
92 if (ch == NULL) abort();
93 *ch = '\0';
94 ch++;
95
96 /* By now we should of skipped the mem: bit.. onto the numbers */
97
98 for(counter=0;counter<6;counter++) {
99 for (; (*ch == ' '); ch++);
100 if (ch == NULL) abort();
101 memstat[counter]=atol(ch);
102 ch++;
103 for(; (*ch != ' ') && (*ch != '\0'); ch++);
104 if (ch == NULL) abort();
105 }
106
107 /* Now swap.. */
108 ch = strchr(swap, ' ');
109 if (ch == NULL) abort();
110 *ch = '\0';
111 ch++;
112
113 for(counter=0;counter<3;counter++) {
114 for (; (*ch == ' '); ch++);
115 if (ch == NULL) abort();
116 swapstat[counter]=atol(ch);
117 ch++;
118 for(; (*ch != ' ') && (*ch != '\0'); ch++);
119 if (ch == NULL) abort();
120 }
121
122 printf("packet.memory.total %ld\n",((memstat[0]/1024)/1024));
123
124 /* Due to batty linux we do some maths to work out roughly what the free ram is */
125 tmp=((memstat[1] - memstat[4])/1024)/1024;
126 printf("packet.memory.used %ld\n",tmp);
127 tmp=((memstat[2] + memstat[4])/1024)/1024;
128 printf("packet.memory.free %ld\n",tmp);
129
130 printf("packet.swap.total %ld\n",((swapstat[0]/1024)/1024));
131 printf("packet.swap.used %ld\n",((swapstat[1]/1024)/1024));
132 printf("packet.swap.free %ld\n",((swapstat[2])/1024)/1024);
133
134 free(mem);
135 free(swap);
136 }
137
138 void cpustats() {
139 char *tmp;
140 char *line[2];
141 char *line_p[2];
142 long cpustats[4][2];
143 long user, kernel, idle;
144 long total;
145 int x,y;
146 float usage;
147 FILE *f;
148
149 if ((f=fopen("/proc/stat", "r" ))==NULL) {
150 errf("Failed to open cpu stats (%m)");
151 die();
152 }
153
154 if((tmp=fpgetline(f)) == NULL) {
155 errf("Failed to read cpu stats (%m)");
156 die();
157 }
158
159 if((line[0]=strdup(tmp)) == NULL) {
160 errf("strdup failed (%m)");
161 die();
162 }
163
164 if ((fclose(f)) != 0) {
165 errf("Failed to close file (%m).");
166 die();
167 }
168
169 sleep(1);
170
171 if ((f=fopen("/proc/stat", "r" ))==NULL) {
172 errf("Failed to open cpu stats (%m)");
173 die();
174 }
175
176 if((tmp=fpgetline(f)) == NULL) {
177 errf("Failed to read cpu stats (%m)");
178 die();
179 }
180
181 if((line[1]=strdup(tmp)) == NULL) {
182 errf("strdup failed (%m)");
183 die();
184 }
185
186 if ((fclose(f)) != 0) {
187 errf("Failed to close file (%m).");
188 die();
189 }
190
191 for(x=0;x<2;x++) {
192 line_p[x] = strchr(line[x], ' ');
193 if (line_p[x] == NULL) abort();
194 *line_p[x] = '\0';
195 line_p[x]++;
196 }
197
198 /* Now should be passed "cpu " */
199
200 for(x=0;x<2;x++) {
201 for(y=0;y<4;y++) {
202 for (; (*line_p[x] == ' '); line_p[x]++);
203 if (line_p[x] == NULL) abort();
204 cpustats[y][x]=atol(line_p[x]);
205 line_p[x]++;
206 for(; (*line_p[x] != ' ') && (*line_p[x] != '\0'); line_p[x]++);
207 if (line_p[x] == NULL) abort();
208 for (; (*line_p[x] == ' '); line_p[x]++);
209 }
210 }
211
212 user=cpustats[0][1]-cpustats[0][0]+cpustats[1][1]-cpustats[1][0];
213 kernel=cpustats[2][1]-cpustats[2][0];
214 idle=cpustats[3][1]-cpustats[3][0];
215
216 /* use total to get the total number and then work out the percentages */
217 total=user+kernel+idle;
218
219 usage=((((float)user)/((float)total))*100.00);
220 printf("packet.cpu.user %3.2f\n", usage);
221 usage=((((float)kernel)/((float)total))*100.00);
222 printf("packet.cpu.kernel %3.2f\n", usage);
223 usage=((((float)idle)/((float)total))*100.00);
224 printf("packet.cpu.idle %3.2f\n", usage);
225
226 /* Cos i-scream's expects this to be sent :/ */
227 printf("packet.cpu.iowait 0\n");
228 printf("packet.cpu.swap 0\n");
229
230 free(line[0]);
231 free(line[1]);
232 }
233
234 void processStats() {
235 int sleeping=-1;
236 int zombie=0;
237 int stopped=0;
238 int running=0;
239 int nousers=0;
240 char *line;
241 char *line_p;
242 struct utmp *entry;
243
244 FILE *f;
245
246 if((f=popen("/bin/ps -Al" , "r")) == NULL) {
247 errf("Failed to get process stats (%m)");
248 die();
249 }
250
251 while((line=fpgetline(f)) != NULL) {
252
253 line_p=line;
254 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
255 line_p=strchr(line_p, ' ');
256 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
257 if (line_p==NULL) abort();
258 /* Ok, we should now be at the state :) .. */
259 if (*line_p=='S') sleeping++;
260 if (*line_p=='R') running++;
261 if (*line_p=='Z') zombie++;
262 if (*line_p=='T') stopped++;
263 }
264
265 if((pclose(f)) == -1) {
266 errf("Failed to close process stats (%m)");
267 die();
268 }
269
270 printf("packet.users.list");
271
272 while((entry=getutent()) != NULL) {
273 if(entry->ut_type==USER_PROCESS) {
274 printf(" %s",entry->ut_user);
275 nousers++;
276 }
277 }
278
279 printf("\npacket.users.count %d\n", nousers);
280
281 printf("packet.processes.sleeping %d\n",sleeping);
282 printf("packet.processes.cpu %d\n",running);
283 printf("packet.processes.zombie %d\n",zombie);
284 printf("packet.processes.stopped %d\n", stopped);
285 printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
286 }
287
288 void uptimeStats() {
289 char *line;
290 char *line_p;
291 FILE *f;
292
293 if ((f=fopen("/proc/uptime", "r")) == NULL) {
294 errf("Failed to get uptime stats (%m)");
295 die();
296 }
297
298 if ((line=fpgetline(f)) == NULL) {
299 errf("Failed to read uptime stats (%m)");
300 die();
301 }
302
303 if ((fclose(f)) != 0) {
304 errf("Failed to close file (%m).");
305 die();
306 }
307
308 line_p=strchr(line, '.');
309 if (line_p==NULL) abort();
310 *line_p='\0';
311
312 printf("packet.os.uptime %s\n", line);
313 }
314
315 void osStats() {
316 struct utsname os;
317
318 if((uname(&os)) != 0) {
319 errf("Failed to get os stats (%m)");
320 die();
321 }
322
323 printf("packet.os.name %s\n", os.sysname);
324 printf("packet.os.release %s\n" , os.release);
325 printf("packet.os.version %s\n", os.version);
326 printf("packet.os.sysname %s\n" , os.nodename);
327 printf("packet.os.platform %s\n", os.machine);
328 }
329
330 void diskStats() {
331 struct statfs df;
332 FILE *f;
333 char *line;
334 char *line_p;
335 long bstok;
336 int diskcnt=0;
337
338 if ((f=fopen("/etc/mtab", "r")) == NULL) {
339 errf("Failed to get uptime stats (%m)");
340 die();
341 }
342
343 while((line=fpgetline(f)) != NULL) {
344 line_p=strchr(line, ' ');
345 if(line_p==NULL) abort();
346 *line_p='\0';
347 line_p++;
348 if(line_p==NULL) abort();
349 printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
350 line=strchr(line_p, ' ');
351 if(line==NULL) abort();
352 *line='\0';
353 printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
354 if((statfs(line_p, &df)) != 0) {
355 errf("Failed to get Filesystem stats (%m)");
356 die();
357 }
358 bstok=(df.f_bsize/1024);
359 printf("packet.disk.p%d.attributes.kbytes %ld\n",diskcnt,bstok*df.f_blocks);
360 printf("packet.disk.p%d.attributes.used %ld\n" ,diskcnt, ((bstok*df.f_blocks)-(bstok*df.f_bfree)));
361 printf("packet.disk.p%d.attributes.avail %ld\n", diskcnt, (df.f_bsize/1024)*df.f_bavail);
362 printf("packet.disk.p%d.attributes.totalinodes %ld\n", diskcnt, df.f_files);
363 printf("packet.disk.p%d.attributes.freeinodes %ld\n", diskcnt, df.f_ffree);
364 diskcnt++;
365 }
366 }
367
368 int main() {
369 getLoadAv();
370 getMemInfo();
371 cpustats();
372 processStats();
373 uptimeStats();
374 osStats();
375 diskStats();
376 fflush(stdout);
377 return 0;
378 }