| 1 |
+ |
/* |
| 2 |
+ |
* i-scream central monitoring system |
| 3 |
+ |
* Copyright (C) 2000-2002 i-scream |
| 4 |
+ |
* |
| 5 |
+ |
* This program is free software; you can redistribute it and/or |
| 6 |
+ |
* modify it under the terms of the GNU General Public License |
| 7 |
+ |
* as published by the Free Software Foundation; either version 2 |
| 8 |
+ |
* of the License, or (at your option) any later version. |
| 9 |
+ |
* |
| 10 |
+ |
* This program is distributed in the hope that it will be useful, |
| 11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
+ |
* GNU General Public License for more details. |
| 14 |
+ |
* |
| 15 |
+ |
* You should have received a copy of the GNU General Public License |
| 16 |
+ |
* along with this program; if not, write to the Free Software |
| 17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 |
+ |
*/ |
| 19 |
+ |
|
| 20 |
|
#include <stdio.h> |
| 21 |
|
#include <stdlib.h> |
| 22 |
|
#include "ukcprog.h" |
| 26 |
|
#include <sys/vfs.h> |
| 27 |
|
#include <utmp.h> |
| 28 |
|
#include <pwd.h> |
| 29 |
+ |
#include <mntent.h> |
| 30 |
+ |
#include <dirent.h> |
| 31 |
+ |
#include <limits.h> |
| 32 |
|
|
| 33 |
|
int die() { |
| 34 |
|
exit (1); |
| 35 |
|
} |
| 36 |
|
|
| 37 |
|
void getLoadAv() { |
| 38 |
< |
FILE *f; |
| 39 |
< |
char *loadavg; |
| 40 |
< |
char *load_p; |
| 41 |
< |
|
| 42 |
< |
if ((f=fopen("/proc/loadavg", "r" ))==NULL) { |
| 43 |
< |
errf("Failed to open load averages (%m)"); |
| 44 |
< |
die(); |
| 45 |
< |
} |
| 46 |
< |
|
| 47 |
< |
if ((loadavg=fpgetline(f)) == NULL) { |
| 48 |
< |
errf("Failed to read any data for load averages (%m)"); |
| 49 |
< |
die(); |
| 50 |
< |
} |
| 51 |
< |
|
| 52 |
< |
if ((fclose(f)) != 0) { |
| 53 |
< |
errf("Failed to close file (%m)."); |
| 54 |
< |
die(); |
| 55 |
< |
} |
| 56 |
< |
|
| 57 |
< |
load_p=strtok(loadavg, " "); |
| 58 |
< |
printf("packet.load.load1 %s\n",load_p); |
| 59 |
< |
for(; (*load_p != ' ') && (*load_p != '\0'); load_p++); |
| 60 |
< |
load_p++; |
| 61 |
< |
if (load_p == NULL) abort(); |
| 62 |
< |
load_p=strtok(load_p, " "); |
| 63 |
< |
if (load_p == NULL) abort(); |
| 64 |
< |
printf("packet.load.load5 %s\n",load_p); |
| 65 |
< |
for(; (*load_p != ' ') && (*load_p != '\0'); load_p++); |
| 66 |
< |
load_p++; |
| 67 |
< |
if (load_p == NULL) abort(); |
| 68 |
< |
load_p=strtok(load_p, " "); |
| 69 |
< |
if (load_p == NULL) abort(); |
| 70 |
< |
printf("packet.load.load15 %s\n",load_p); |
| 38 |
> |
#ifdef OLDLIBC |
| 39 |
> |
|
| 40 |
> |
FILE *f; |
| 41 |
> |
char *loadavg; |
| 42 |
> |
char *load_p; |
| 43 |
> |
|
| 44 |
> |
if ((f=fopen("/proc/loadavg", "r" ))==NULL) { |
| 45 |
> |
errf("Failed to open load averages (%m)"); |
| 46 |
> |
die(); |
| 47 |
> |
} |
| 48 |
> |
|
| 49 |
> |
if ((loadavg=fpgetline(f)) == NULL) { |
| 50 |
> |
errf("Failed to read any data for load averages (%m)"); |
| 51 |
> |
die(); |
| 52 |
> |
} |
| 53 |
> |
|
| 54 |
> |
if ((fclose(f)) != 0) { |
| 55 |
> |
errf("Failed to close file (%m)."); |
| 56 |
> |
die(); |
| 57 |
> |
} |
| 58 |
> |
|
| 59 |
> |
load_p=strtok(loadavg, " "); |
| 60 |
> |
printf("packet.load.load1 %s\n",load_p); |
| 61 |
> |
for(; (*load_p != ' ') && (*load_p != '\0'); load_p++); |
| 62 |
> |
load_p++; |
| 63 |
> |
if (load_p == NULL) abort(); |
| 64 |
> |
load_p=strtok(load_p, " "); |
| 65 |
> |
if (load_p == NULL) abort(); |
| 66 |
> |
printf("packet.load.load5 %s\n",load_p); |
| 67 |
> |
for(; (*load_p != ' ') && (*load_p != '\0'); load_p++); |
| 68 |
> |
load_p++; |
| 69 |
> |
if (load_p == NULL) abort(); |
| 70 |
> |
load_p=strtok(load_p, " "); |
| 71 |
> |
if (load_p == NULL) abort(); |
| 72 |
> |
printf("packet.load.load15 %s\n",load_p); |
| 73 |
> |
|
| 74 |
> |
#else |
| 75 |
> |
double loadav[3]; |
| 76 |
> |
|
| 77 |
> |
if((getloadavg(loadav,3)) == -1){ |
| 78 |
> |
errf("Failed to get load averages (%m)"); |
| 79 |
> |
die(); |
| 80 |
> |
} |
| 81 |
> |
|
| 82 |
> |
printf("packet.load.load1 %.2f\n",loadav[0]); |
| 83 |
> |
printf("packet.load.load5 %.2f\n",loadav[1]); |
| 84 |
> |
printf("packet.load.load15 %.2f\n",loadav[2]); |
| 85 |
> |
#endif |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
void getMemInfo() { |
| 89 |
|
char *line; |
| 90 |
< |
char *mem; |
| 91 |
< |
char *swap; |
| 90 |
> |
char *mem=NULL; |
| 91 |
> |
char *swap=NULL; |
| 92 |
|
char *ch; |
| 93 |
|
long memstat[6]; |
| 94 |
|
long swapstat[3]; |
| 117 |
|
errf("Failed to close file (%m)."); |
| 118 |
|
die(); |
| 119 |
|
} |
| 120 |
< |
|
| 120 |
> |
|
| 121 |
> |
if (mem==NULL || swap==NULL){ |
| 122 |
> |
errf("Failed to obtain information required for memory and swap stats"); |
| 123 |
> |
die(); |
| 124 |
> |
} |
| 125 |
> |
|
| 126 |
|
/* Get the info we want from the 2 read in lines */ |
| 127 |
|
|
| 128 |
|
ch = strchr(mem, ' '); |
| 260 |
|
usage=((((float)idle)/((float)total))*100.00); |
| 261 |
|
printf("packet.cpu.idle %3.2f\n", usage); |
| 262 |
|
|
| 263 |
< |
/* Cos i-scream's expexts this to be sent :/ */ |
| 263 |
> |
/* Cos i-scream's expects this to be sent :/ */ |
| 264 |
|
printf("packet.cpu.iowait 0\n"); |
| 265 |
< |
|
| 265 |
> |
printf("packet.cpu.swap 0\n"); |
| 266 |
> |
|
| 267 |
|
free(line[0]); |
| 268 |
|
free(line[1]); |
| 269 |
|
} |
| 275 |
|
int running=0; |
| 276 |
|
int nousers=0; |
| 277 |
|
char *line; |
| 278 |
< |
char *line_p; |
| 278 |
> |
char *line_ptr; |
| 279 |
|
struct utmp *entry; |
| 280 |
< |
|
| 280 |
> |
DIR *procdir; |
| 281 |
> |
struct dirent *procent; |
| 282 |
> |
char fname[_POSIX_PATH_MAX]; |
| 283 |
|
FILE *f; |
| 284 |
< |
|
| 285 |
< |
if((f=popen("/bin/ps -Al" , "r")) == NULL) { |
| 286 |
< |
errf("Failed to get process stats (%m)"); |
| 287 |
< |
die(); |
| 284 |
> |
|
| 285 |
> |
chdir("/proc"); |
| 286 |
> |
if((procdir=opendir(".")) == NULL){ |
| 287 |
> |
errf("Failed to open proc (%m)"); |
| 288 |
> |
exit(1); |
| 289 |
|
} |
| 290 |
< |
|
| 291 |
< |
while((line=fpgetline(f)) != NULL) { |
| 292 |
< |
line_p=strchr(line, ' '); |
| 293 |
< |
line_p++; |
| 294 |
< |
if (line_p==NULL) abort(); |
| 295 |
< |
/* Ok, we should now be at the state :) .. */ |
| 296 |
< |
if (*line_p=='S') sleeping++; |
| 297 |
< |
if (*line_p=='R') running++; |
| 298 |
< |
if (*line_p=='Z') zombie++; |
| 299 |
< |
if (*line_p=='T') stopped++; |
| 290 |
> |
|
| 291 |
> |
while((procent = readdir(procdir)) != NULL){ |
| 292 |
> |
if(atoi(procent->d_name) == 0) continue; |
| 293 |
> |
strncpy(fname, procent->d_name, _POSIX_PATH_MAX-7); |
| 294 |
> |
strcat(fname, "/status"); |
| 295 |
> |
|
| 296 |
> |
if((f=fopen(fname, "r")) == NULL){ |
| 297 |
> |
errf("Failed to open process stat (%m)"); |
| 298 |
> |
exit(1); |
| 299 |
> |
} |
| 300 |
> |
|
| 301 |
> |
while((line=fpgetline(f)) != NULL){ |
| 302 |
> |
if(strncasecmp(line,"State:",6)==0) break; |
| 303 |
> |
} |
| 304 |
> |
|
| 305 |
> |
line_ptr=line; |
| 306 |
> |
for(;*line_ptr++ != '\t';); |
| 307 |
> |
|
| 308 |
> |
|
| 309 |
> |
if(*line_ptr=='R') running++; |
| 310 |
> |
if(*line_ptr=='S') sleeping++; |
| 311 |
> |
if(*line_ptr=='Z') zombie++; |
| 312 |
> |
if(*line_ptr=='T') stopped++; |
| 313 |
> |
if(*line_ptr=='D') stopped++; |
| 314 |
> |
|
| 315 |
|
} |
| 316 |
< |
|
| 317 |
< |
if((pclose(f)) == -1) { |
| 257 |
< |
errf("Failed to close process stats (%m)"); |
| 258 |
< |
die(); |
| 259 |
< |
} |
| 260 |
< |
|
| 316 |
> |
closedir(procdir); |
| 317 |
> |
|
| 318 |
|
printf("packet.users.list"); |
| 319 |
|
|
| 320 |
|
while((entry=getutent()) != NULL) { |
| 326 |
|
|
| 327 |
|
printf("\npacket.users.count %d\n", nousers); |
| 328 |
|
|
| 329 |
< |
printf("packet.processes.total %d\n",sleeping); |
| 330 |
< |
printf("packet.processes.sleeping %d\n",running); |
| 329 |
> |
printf("packet.processes.sleeping %d\n",sleeping); |
| 330 |
> |
printf("packet.processes.cpu %d\n",running); |
| 331 |
|
printf("packet.processes.zombie %d\n",zombie); |
| 332 |
|
printf("packet.processes.stopped %d\n", stopped); |
| 333 |
|
printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped)); |
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
void diskStats() { |
| 379 |
< |
struct statfs df; |
| 380 |
< |
FILE *f; |
| 381 |
< |
char *line; |
| 382 |
< |
char *line_p; |
| 383 |
< |
long bstok; |
| 384 |
< |
int diskcnt=0; |
| 385 |
< |
|
| 386 |
< |
if ((f=fopen("/etc/mtab", "r")) == NULL) { |
| 387 |
< |
errf("Failed to get uptime stats (%m)"); |
| 388 |
< |
die(); |
| 379 |
> |
|
| 380 |
> |
struct mntent *mp; |
| 381 |
> |
struct statfs df; |
| 382 |
> |
FILE *f; |
| 383 |
> |
int counter=0; |
| 384 |
> |
|
| 385 |
> |
if ((f=fopen("/etc/mtab", "r" ))==NULL){ |
| 386 |
> |
errf("Failed to open mounts (%m)"); |
| 387 |
> |
die(); |
| 388 |
> |
} |
| 389 |
> |
|
| 390 |
> |
while((mp=getmntent(f))){ |
| 391 |
> |
if ((statfs(mp->mnt_dir, &df)) !=0){ |
| 392 |
> |
errf("Failed to gets fs stats (%m)"); |
| 393 |
> |
continue; |
| 394 |
|
} |
| 395 |
+ |
|
| 396 |
+ |
if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue; |
| 397 |
|
|
| 398 |
< |
while((line=fpgetline(f)) != NULL) { |
| 399 |
< |
line_p=strchr(line, ' '); |
| 400 |
< |
if(line_p==NULL) abort(); |
| 401 |
< |
*line_p='\0'; |
| 402 |
< |
line_p++; |
| 403 |
< |
if(line_p==NULL) abort(); |
| 404 |
< |
printf("packet.disk.p%d.attributes.name %s\n",diskcnt,line); |
| 405 |
< |
line=strchr(line_p, ' '); |
| 406 |
< |
if(line==NULL) abort(); |
| 407 |
< |
*line='\0'; |
| 408 |
< |
printf("packet.disk.p%d.attributes.mount %s\n",diskcnt,line_p); |
| 409 |
< |
if((statfs(line_p, &df)) != 0) { |
| 410 |
< |
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 |
< |
} |
| 398 |
> |
printf("packet.disk.p%d.attributes.mount %s\n", counter, mp->mnt_dir); |
| 399 |
> |
printf("packet.disk.p%d.attributes.name %s\n", counter, mp->mnt_fsname); |
| 400 |
> |
printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_bsize/1024) * df.f_blocks)); |
| 401 |
> |
printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree))); |
| 402 |
> |
printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_bsize/1024) * df.f_bavail); |
| 403 |
> |
printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files); |
| 404 |
> |
printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree); |
| 405 |
> |
|
| 406 |
> |
counter++; |
| 407 |
> |
} |
| 408 |
> |
|
| 409 |
> |
|
| 410 |
> |
|
| 411 |
|
} |
| 412 |
|
|
| 413 |
|
int main() { |