| 1 |
|
#include <stdio.h> |
| 2 |
|
#include <stdlib.h> |
| 3 |
< |
#include "ukcprog.h" |
| 3 |
> |
#include <local/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 |
+ |
#include <mntent.h> |
| 11 |
+ |
#include <dirent.h> |
| 12 |
+ |
#include <limits.h> |
| 13 |
|
|
| 14 |
|
int die() { |
| 15 |
|
exit (1); |
| 16 |
|
} |
| 17 |
|
|
| 18 |
|
void getLoadAv() { |
| 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); |
| 19 |
> |
#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 |
> |
|
| 55 |
> |
#else |
| 56 |
> |
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 |
> |
#endif |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
void getMemInfo() { |
| 70 |
|
char *line; |
| 71 |
< |
char *mem; |
| 72 |
< |
char *swap; |
| 71 |
> |
char *mem=NULL; |
| 72 |
> |
char *swap=NULL; |
| 73 |
|
char *ch; |
| 74 |
|
long memstat[6]; |
| 75 |
|
long swapstat[3]; |
| 98 |
|
errf("Failed to close file (%m)."); |
| 99 |
|
die(); |
| 100 |
|
} |
| 101 |
< |
|
| 101 |
> |
|
| 102 |
> |
if (mem==NULL || swap==NULL){ |
| 103 |
> |
errf("Failed to obtain information required for memory and swap stats"); |
| 104 |
> |
die(); |
| 105 |
> |
} |
| 106 |
> |
|
| 107 |
|
/* Get the info we want from the 2 read in lines */ |
| 108 |
|
|
| 109 |
|
ch = strchr(mem, ' '); |
| 241 |
|
usage=((((float)idle)/((float)total))*100.00); |
| 242 |
|
printf("packet.cpu.idle %3.2f\n", usage); |
| 243 |
|
|
| 244 |
< |
/* Cos i-scream's expexts this to be sent :/ */ |
| 244 |
> |
/* Cos i-scream's expects this to be sent :/ */ |
| 245 |
|
printf("packet.cpu.iowait 0\n"); |
| 246 |
< |
|
| 246 |
> |
printf("packet.cpu.swap 0\n"); |
| 247 |
> |
|
| 248 |
|
free(line[0]); |
| 249 |
|
free(line[1]); |
| 250 |
|
} |
| 256 |
|
int running=0; |
| 257 |
|
int nousers=0; |
| 258 |
|
char *line; |
| 259 |
< |
char *line_p; |
| 259 |
> |
char *line_ptr; |
| 260 |
|
struct utmp *entry; |
| 261 |
< |
|
| 261 |
> |
DIR *procdir; |
| 262 |
> |
struct dirent *procent; |
| 263 |
> |
char fname[_POSIX_PATH_MAX]; |
| 264 |
|
FILE *f; |
| 265 |
< |
|
| 266 |
< |
if((f=popen("/bin/ps -Al" , "r")) == NULL) { |
| 267 |
< |
errf("Failed to get process stats (%m)"); |
| 268 |
< |
die(); |
| 265 |
> |
|
| 266 |
> |
chdir("/proc"); |
| 267 |
> |
if((procdir=opendir(".")) == NULL){ |
| 268 |
> |
errf("Failed to open proc (%m)"); |
| 269 |
> |
exit(1); |
| 270 |
|
} |
| 271 |
< |
|
| 272 |
< |
while((line=fpgetline(f)) != NULL) { |
| 273 |
< |
line_p=strchr(line, ' '); |
| 274 |
< |
line_p++; |
| 275 |
< |
if (line_p==NULL) abort(); |
| 276 |
< |
/* Ok, we should now be at the state :) .. */ |
| 277 |
< |
if (*line_p=='S') sleeping++; |
| 278 |
< |
if (*line_p=='R') running++; |
| 279 |
< |
if (*line_p=='Z') zombie++; |
| 280 |
< |
if (*line_p=='T') stopped++; |
| 271 |
> |
|
| 272 |
> |
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 |
|
} |
| 297 |
< |
|
| 298 |
< |
if((pclose(f)) == -1) { |
| 257 |
< |
errf("Failed to close process stats (%m)"); |
| 258 |
< |
die(); |
| 259 |
< |
} |
| 260 |
< |
|
| 297 |
> |
closedir(procdir); |
| 298 |
> |
|
| 299 |
|
printf("packet.users.list"); |
| 300 |
|
|
| 301 |
|
while((entry=getutent()) != NULL) { |
| 307 |
|
|
| 308 |
|
printf("\npacket.users.count %d\n", nousers); |
| 309 |
|
|
| 310 |
< |
printf("packet.processes.total %d\n",sleeping); |
| 311 |
< |
printf("packet.processes.sleeping %d\n",running); |
| 310 |
> |
printf("packet.processes.sleeping %d\n",sleeping); |
| 311 |
> |
printf("packet.processes.cpu %d\n",running); |
| 312 |
|
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)); |
| 357 |
|
} |
| 358 |
|
|
| 359 |
|
void diskStats() { |
| 360 |
< |
struct statfs df; |
| 361 |
< |
FILE *f; |
| 362 |
< |
char *line; |
| 363 |
< |
char *line_p; |
| 364 |
< |
long bstok; |
| 365 |
< |
int diskcnt=0; |
| 366 |
< |
|
| 367 |
< |
if ((f=fopen("/etc/mtab", "r")) == NULL) { |
| 368 |
< |
errf("Failed to get uptime stats (%m)"); |
| 369 |
< |
die(); |
| 360 |
> |
|
| 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 |
|
|
| 379 |
< |
while((line=fpgetline(f)) != NULL) { |
| 380 |
< |
line_p=strchr(line, ' '); |
| 381 |
< |
if(line_p==NULL) abort(); |
| 382 |
< |
*line_p='\0'; |
| 383 |
< |
line_p++; |
| 384 |
< |
if(line_p==NULL) abort(); |
| 385 |
< |
printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line); |
| 386 |
< |
line=strchr(line_p, ' '); |
| 387 |
< |
if(line==NULL) abort(); |
| 388 |
< |
*line='\0'; |
| 389 |
< |
printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p); |
| 390 |
< |
if((statfs(line_p, &df)) != 0) { |
| 391 |
< |
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 |
< |
} |
| 379 |
> |
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 |
> |
|
| 387 |
> |
counter++; |
| 388 |
> |
} |
| 389 |
> |
|
| 390 |
> |
|
| 391 |
> |
|
| 392 |
|
} |
| 393 |
|
|
| 394 |
|
int main() { |