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