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.5
Committed: Mon Mar 11 13:42:40 2002 UTC (22 years, 7 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.4: +5 -4 lines
Log Message:
2 bug fixes.. Erm it should still compile ;)

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     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 pajs 1.1 }
132    
133 tdb 1.4 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 pajs 1.1
164 tdb 1.4 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 pajs 1.1
181 tdb 1.4 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 pajs 1.5 /* Cos i-scream's expects this to be sent :/ */
222 tdb 1.4 printf("packet.cpu.iowait 0\n");
223 pajs 1.5 printf("packet.cpu.swap 0\n");
224    
225 tdb 1.4 free(line[0]);
226     free(line[1]);
227 pajs 1.1 }
228    
229 tdb 1.4 void processStats() {
230     int sleeping=0;
231     int zombie=0;
232     int stopped=0;
233     int running=0;
234     int nousers=0;
235     char *line;
236     char *line_p;
237     struct utmp *entry;
238    
239     FILE *f;
240    
241     if((f=popen("/bin/ps -Al" , "r")) == NULL) {
242     errf("Failed to get process stats (%m)");
243     die();
244     }
245    
246     while((line=fpgetline(f)) != NULL) {
247     line_p=strchr(line, ' ');
248     line_p++;
249     if (line_p==NULL) abort();
250     /* Ok, we should now be at the state :) .. */
251     if (*line_p=='S') sleeping++;
252     if (*line_p=='R') running++;
253     if (*line_p=='Z') zombie++;
254     if (*line_p=='T') stopped++;
255     }
256    
257     if((pclose(f)) == -1) {
258     errf("Failed to close process stats (%m)");
259     die();
260     }
261    
262     printf("packet.users.list");
263    
264     while((entry=getutent()) != NULL) {
265     if(entry->ut_type==USER_PROCESS) {
266     printf(" %s",entry->ut_user);
267     nousers++;
268     }
269     }
270    
271     printf("\npacket.users.count %d\n", nousers);
272    
273 pajs 1.5 printf("packet.processes.sleeping %d\n",sleeping);
274     printf("packet.processes.cpu %d\n",running);
275 tdb 1.4 printf("packet.processes.zombie %d\n",zombie);
276     printf("packet.processes.stopped %d\n", stopped);
277     printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
278 pajs 1.1 }
279    
280 tdb 1.4 void uptimeStats() {
281     char *line;
282     char *line_p;
283     FILE *f;
284    
285     if ((f=fopen("/proc/uptime", "r")) == NULL) {
286     errf("Failed to get uptime stats (%m)");
287     die();
288     }
289    
290     if ((line=fpgetline(f)) == NULL) {
291     errf("Failed to read uptime stats (%m)");
292     die();
293     }
294    
295     if ((fclose(f)) != 0) {
296     errf("Failed to close file (%m).");
297     die();
298     }
299    
300     line_p=strchr(line, '.');
301     if (line_p==NULL) abort();
302     *line_p='\0';
303    
304     printf("packet.os.uptime %s\n", line);
305 pajs 1.1 }
306    
307 tdb 1.4 void osStats() {
308     struct utsname os;
309    
310     if((uname(&os)) != 0) {
311     errf("Failed to get os stats (%m)");
312     die();
313     }
314    
315     printf("packet.os.name %s\n", os.sysname);
316     printf("packet.os.release %s\n" , os.release);
317     printf("packet.os.version %s\n", os.version);
318     printf("packet.os.sysname %s\n" , os.nodename);
319     printf("packet.os.platform %s\n", os.machine);
320 pajs 1.1 }
321    
322 tdb 1.4 void diskStats() {
323     struct statfs df;
324     FILE *f;
325     char *line;
326     char *line_p;
327     long bstok;
328     int diskcnt=0;
329    
330     if ((f=fopen("/etc/mtab", "r")) == NULL) {
331     errf("Failed to get uptime stats (%m)");
332     die();
333     }
334    
335     while((line=fpgetline(f)) != NULL) {
336     line_p=strchr(line, ' ');
337     if(line_p==NULL) abort();
338     *line_p='\0';
339     line_p++;
340     if(line_p==NULL) abort();
341     printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line);
342     line=strchr(line_p, ' ');
343     if(line==NULL) abort();
344     *line='\0';
345     printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p);
346     if((statfs(line_p, &df)) != 0) {
347     errf("Failed to get Filesystem stats (%m)");
348     die();
349     }
350     bstok=(df.f_bsize/1024);
351     printf("packet.disk.p%d.attributes.kbytes %ld\n",diskcnt,bstok*df.f_blocks);
352     printf("packet.disk.p%d.attributes.used %ld\n" ,diskcnt, ((bstok*df.f_blocks)-(bstok*df.f_bfree)));
353     printf("packet.disk.p%d.attributes.avail %ld\n", diskcnt, (df.f_bsize/1024)*df.f_bavail);
354     printf("packet.disk.p%d.attributes.totalinodes %ld\n", diskcnt, df.f_files);
355     printf("packet.disk.p%d.attributes.freeinodes %ld\n", diskcnt, df.f_ffree);
356     diskcnt++;
357     }
358 pajs 1.1 }
359    
360 tdb 1.4 int main() {
361 pajs 1.1 getLoadAv();
362     getMemInfo();
363     cpustats();
364     processStats();
365     uptimeStats();
366     osStats();
367     diskStats();
368     fflush(stdout);
369     return 0;
370     }