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