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