/*
 * Copyright (c) 1999-2007, OpenFWTK Development Group
 * All rights reserved. See LICENSE.
 */

/*
 * hqdaemon.c, a firewall system monitoring agent for hqmon
 * (C) Copyright 1999-2002 ArkanoiD <ark@eltex.ru>
 * (C) Copyright 1999 Alexander Snarskii <snar@paranoia.ru>
 *
 */

#include <sys/param.h>
#include <sys/mount.h>

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#ifdef 	 linux
#include <sys/vfs.h>
#endif
#ifdef	 SOLARIS
#include <sys/loadavg.h>
#include <sys/statvfs.h>
#endif
#ifdef	 HPUX
#include <sys/statvfs.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include "firewall.h"
#include "firewall2.h"

static	char*	moduleId ATTR_UNUSED = "$Id: hqdaemon.c,v 1.7 2007/09/10 02:49:13 arkenoi Exp $";

int 
main(int argc,char* argv[])
{ 
	Cfg		*cf;
	char buffer[125];
	float fa = 0.1,fa2 = 3;
	double lda;
	unsigned du1,du2;
	float stb;
#if defined(SOLARIS) || defined(HPUX)
	struct statvfs statfsbuf;
#else
	struct statfs statfsbuf;
#endif

	proxy_init();
	if (!proxy_conf_hosts(proxy_confp,proxy_stats.rladdr,
				proxy_stats.riaddr))
		exit(1);

	if ((cf = cfg_get("load",proxy_confp)) != (Cfg *)0) {
		if (cf->argc != 2) {
			syslog(LLEV,"fwtkcfgerr: load must have 2 parameters, line %d",cf->ln);
			exit(1);
		}
		sscanf((cf->argv[0]),"%f",&fa);
		sscanf((cf->argv[1]),"%f",&fa2);
		if(getloadavg(&lda,1)==-1) { 
			printf("ER Can't get load averages: %s\n",strerror(errno));
			syslog(LLEV,"fwtksyserr: cannot get load averages: %s",strerror(errno));
		} else {
			if(lda>fa2) { 
				syslog(LLEV,"error: LA(1) value critical,%5.2f\n",(float)lda);
				printf("ER LoadAverage %5.2f\n",(float)lda);
			} else if(lda>fa) { 
				syslog(LLEV,"warning: LA(1) value high,%5.2f\n",(float)lda);
				printf("WR LoadAverage %5.2f\n",(float)lda);
			} else { 
				printf("OK LoadAverage %5.2f\n",(float)lda);
			}
		}
	}
 
	cf = cfg_get("disk",proxy_confp);
	while (cf != (Cfg *)0) {
		if (cf->argc != 3) {
			syslog(LLEV,"fwtkcfgerr: disk must have 3 parameters, line %d",
			       cf->ln);
			exit(1);
		}
		strncpy(buffer,cf->argv[0],sizeof(buffer));
		du1 = atoi(cf->argv[1]);
		du2 = atoi(cf->argv[2]);

                bzero((void*)&statfsbuf,sizeof(statfsbuf));
#ifdef SOLARIS
                if(statvfs(buffer,&statfsbuf)==-1) {
#else
                if(statfs(buffer,&statfsbuf)==-1) {
#endif
			printf("ER can't get disk statistics: %s\n",strerror(errno));
			syslog(LLEV,"fwtksyserr: cannot get disk statistics: %s",strerror(errno));
                } else {
        	        stb=((float)(statfsbuf.f_blocks-statfsbuf.f_bavail))/statfsbuf.f_blocks*100; 
			if(stb>du2) {
				syslog(LLEV,"error: filesystem %s usage critical, %5.2f%%",buffer,stb);
				printf("ER filesystem %s : %5.2f\n",buffer,stb);
       		        } else if(stb>du1) {
				syslog(LLEV,"warning: filesystem %s usage high, %5.2f%%",buffer,stb);
	                        printf("WR filesystem %s : %5.2f\n",buffer,stb);
        	        } else {
                	        printf("OK filesystem %s : %5.2f\n",buffer,stb);
             		}
		}
		cf = cfg_get("disk",(Cfg*)0);
	}
	exit(0);
}
