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.6
Committed: Mon Mar 11 13:51:11 2002 UTC (22 years, 7 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.5: +8 -3 lines
Log Message:
Added catching for a shouldn't happen error.

File Contents

# User Rev Content
1 pajs 1.1 #include <stdio.h>
2     #include <stdlib.h>
3 tdb 1.3 #include "ukcprog.h"
4 pajs 1.1 #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 tdb 1.4 int die() {
12     exit (1);
13 pajs 1.1 }
14    
15 tdb 1.4 void getLoadAv() {
16     FILE *f;
17     char *loadavg;
18     char *load_p;
19 pajs 1.1
20 tdb 1.4 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 pajs 1.1 }
50    
51 tdb 1.4 void getMemInfo() {
52     char *line;
53 pajs 1.6 char *mem=NULL;
54     char *swap=NULL;
55 tdb 1.4 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 pajs 1.6
84     if (mem==NULL || swap==NULL){
85     errf("Failed to obtain information required for memory and swap stats");
86     die();
87     }
88    
89 tdb 1.4 /* 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 pajs 1.1 }
137    
138 tdb 1.4 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 pajs 1.1
169 tdb 1.4 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 pajs 1.1
186 tdb 1.4 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 pajs 1.5 /* Cos i-scream's expects this to be sent :/ */
227 tdb 1.4 printf("packet.cpu.iowait 0\n");
228 pajs 1.5 printf("packet.cpu.swap 0\n");
229    
230 tdb 1.4 free(line[0]);
231     free(line[1]);
232 pajs 1.1 }
233    
234 tdb 1.4 void processStats() {
235     int sleeping=0;
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     line_p=strchr(line, ' ');
253     line_p++;
254     if (line_p==NULL) abort();
255     /* Ok, we should now be at the state :) .. */
256     if (*line_p=='S') sleeping++;
257     if (*line_p=='R') running++;
258     if (*line_p=='Z') zombie++;
259     if (*line_p=='T') stopped++;
260     }
261    
262     if((pclose(f)) == -1) {
263     errf("Failed to close process stats (%m)");
264     die();
265     }
266    
267     printf("packet.users.list");
268    
269     while((entry=getutent()) != NULL) {
270     if(entry->ut_type==USER_PROCESS) {
271     printf(" %s",entry->ut_user);
272     nousers++;
273     }
274     }
275    
276     printf("\npacket.users.count %d\n", nousers);
277    
278 pajs 1.5 printf("packet.processes.sleeping %d\n",sleeping);
279     printf("packet.processes.cpu %d\n",running);
280 tdb 1.4 printf("packet.processes.zombie %d\n",zombie);
281     printf("packet.processes.stopped %d\n", stopped);
282     printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
283 pajs 1.1 }
284    
285 tdb 1.4 void uptimeStats() {
286     char *line;
287     char *line_p;
288     FILE *f;
289    
290     if ((f=fopen("/proc/uptime", "r")) == NULL) {
291     errf("Failed to get uptime stats (%m)");
292     die();
293     }
294    
295     if ((line=fpgetline(f)) == NULL) {
296     errf("Failed to read uptime stats (%m)");
297     die();
298     }
299    
300     if ((fclose(f)) != 0) {
301     errf("Failed to close file (%m).");
302     die();
303     }
304    
305     line_p=strchr(line, '.');
306     if (line_p==NULL) abort();
307     *line_p='\0';
308    
309     printf("packet.os.uptime %s\n", line);
310 pajs 1.1 }
311    
312 tdb 1.4 void osStats() {
313     struct utsname os;
314    
315     if((uname(&os)) != 0) {
316     errf("Failed to get os stats (%m)");
317     die();
318     }
319    
320     printf("packet.os.name %s\n", os.sysname);
321     printf("packet.os.release %s\n" , os.release);
322     printf("packet.os.version %s\n", os.version);
323     printf("packet.os.sysname %s\n" , os.nodename);
324     printf("packet.os.platform %s\n", os.machine);
325 pajs 1.1 }
326    
327 tdb 1.4 void diskStats() {
328     struct statfs df;
329     FILE *f;
330     char *line;
331     char *line_p;
332     long bstok;
333     int diskcnt=0;
334    
335     if ((f=fopen("/etc/mtab", "r")) == NULL) {
336     errf("Failed to get uptime stats (%m)");
337     die();
338     }
339    
340     while((line=fpgetline(f)) != NULL) {
341     line_p=strchr(line, ' ');
342     if(line_p==NULL) abort();
343     *line_p='\0';
344     line_p++;
345     if(line_p==NULL) abort();
346     printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
347     line=strchr(line_p, ' ');
348     if(line==NULL) abort();
349     *line='\0';
350     printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
351     if((statfs(line_p, &df)) != 0) {
352     errf("Failed to get Filesystem stats (%m)");
353     die();
354     }
355     bstok=(df.f_bsize/1024);
356     printf("packet.disk.p%d.attributes.kbytes %ld\n",diskcnt,bstok*df.f_blocks);
357     printf("packet.disk.p%d.attributes.used %ld\n" ,diskcnt, ((bstok*df.f_blocks)-(bstok*df.f_bfree)));
358     printf("packet.disk.p%d.attributes.avail %ld\n", diskcnt, (df.f_bsize/1024)*df.f_bavail);
359     printf("packet.disk.p%d.attributes.totalinodes %ld\n", diskcnt, df.f_files);
360     printf("packet.disk.p%d.attributes.freeinodes %ld\n", diskcnt, df.f_ffree);
361     diskcnt++;
362     }
363 pajs 1.1 }
364    
365 tdb 1.4 int main() {
366 pajs 1.1 getLoadAv();
367     getMemInfo();
368     cpustats();
369     processStats();
370     uptimeStats();
371     osStats();
372     diskStats();
373     fflush(stdout);
374     return 0;
375     }