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.4
Committed: Fri Mar 8 16:04:38 2002 UTC (23 years, 9 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.3: +330 -397 lines
Log Message:
Been parsed by the tdb code tidier.

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