#!/bin/bash
## TODO
# integrate onappdump
# log options for cmdrun/other multiargument options
# apf/csf block reasons

# Just a global PATH so we can find common binaries
PATH=/sbin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin:$PATH ; export PATH
ver="2.6"
banner="%nocagent%"
hostname=`hostname`
logger=`which logger 2> /dev/null`
log="/usr/local/nocagent/access_log"
user=`whoami`
remote_host="$REMOTE_HOST"
# cloak 129.121.178.207, noc 129.121.182.30, projects 129.121.182.37, rc 174.136.13.202
trusted_hosts="129.121.178.207 129.121.182.30 129.121.182.37 174.136.13.202 216.172.189.252 216.172.189.253 162.241.210.213"

if [ ! -f "$log" ]; then
	touch $log
	chmod 600 $log	
fi

eout() {
	msg=$1
	echo "$(date +"%b %e %H:%M:%S") $(hostname -s) nocagent[$$]: $msg" >> $log
	if [ -f "$logger" ]; then
		$logger -p crit -t "nocagent[$$]" "$msg"
	fi
}

if [ -z "$remote_host" ]; then
	msg="denied untrusted connection from unknown"
	eout "$msg"
	echo $banner && echo "invalid auth goodbye."
	exit 1
fi
for trust in $trusted_hosts; do
	if [ "$remote_host" == "$trust" ]; then
		ipauth=1
	fi
done
if [ -z "$ipauth" ]; then
	msg="denied untrusted connection from $remote_host"
	eout "$msg"
	echo $banner && echo "invalid auth goodbye."
	exit 1
else
	msg="accepted trusted connection from $remote_host"
	eout "$msg"
fi

altkey="ab635ebb9a521895893e0d07d8a0f9c0"
if [ ! -f "/usr/local/nocagent/.agentkey" ]; then
	key="b5bb08a1991400338fb3c146896c3231"
else
	key=`cat /usr/local/nocagent/.agentkey`
fi

kill_stale() {
	for stale in `ps -U root -o 'pid etime cmd' | grep -w nocagent | grep -v grep | tr -d ':' | tr ' ' '%'`; do
		pid=`echo $stale | tr '%' ' ' | awk '{print$1}'`
		etime=`echo $stale | tr '%' ' ' | tr -d '-' | awk '{print$2}'`
		if [ "$etime" -ge "500" ]; then
			kill -9 $pid 2> /dev/null
		fi
	done
	kill -9 $(ps auxww | grep nocagent | grep -wE "Ts|Z|ZN|TNs" | grep -v grep | awk '{print$2}' | tr '\n' ' ') 2> /dev/null
}

spawn() {
echo $banner
echo -n "auth> "
read keyin
keyin=`echo $keyin | tr -d '\r' | tr -d '\t' | tr -d '\v' | tr -d '\n'`
if [ "$keyin" == "$key" ] || [ "$keyin" == "$altkey" ]; then
	msg="validated md5auth from $remote_host"
	eout "$msg"
	echo "auth accepted"
	echo -n "cmd> "
	read input
else 
	msg="invalid md5auth from $remote_host"
	eout "$msg"
	echo "invalid auth goodbye."
	exit
fi

tlog() {
 BASERUN="/usr/local/nocagent"
 if [ "$1" == "" ] && [ "$2" == "" ]; then
	exit 1
 else
	FILE="$1"
	TLOG="$2"
 fi
 if [ ! -f "$FILE" ]; then
	echo "$FILE is not a valid file, aborting"
	exit 1
 elif [ ! -d "$BASERUN" ]; then
	echo "$BASERUN is not a valid operating path, aborting."
	exit 1
 fi
 if [ ! -f "$BASERUN/$TLOG" ]; then
	echo "0" > $BASERUN/$TLOG
 fi
 TSIZE=`cat $BASERUN/$TLOG` >> /dev/null 2>&1
 getsize() {
 if [ ! -f "$BASERUN/$TLOG" ]; then
	SIZE=`stat -c %s $FILE`
	echo $SIZE > $BASERUN/$TLOG
        NEWSIZE=`stat -c %s $FILE`
 elif [ -f "$BASERUN/$TLOG" ] && [ "$TSIZE" == "0" ]; then
        SIZE=`stat -c %s $FILE`
        echo $SIZE > $BASERUN/$TLOG
        NEWSIZE=`stat -c %s $FILE`
 else
	SIZE=`cat $BASERUN/$TLOG`
	NEWSIZE=`stat -c %s $FILE`
 fi
 }
 getsize
 if [ ! "$NEWSIZE" -lt "$SIZE" ] && [ "$NEWSIZE" -ne "$SIZE" ]; then
	tail -c `expr $NEWSIZE - $SIZE` $FILE
	echo $NEWSIZE > $BASERUN/$TLOG
 elif [ "$NEWSIZE" -lt "$SIZE" ]; then 
 if [ -f "$FILE.1" ]; then
        RTSIZE=`stat -c %s $FILE.1`
        tail -c `expr $RTSIZE - $SIZE` $FILE.1
 elif [ -f "$FILE.1.gz" ]; then
	gunzip $FILE.1.gz
        RTSIZE=`stat -c %s $FILE.1`
        tail -c `expr $RTSIZE - $SIZE` $FILE.1
	gzip $FILE.1
 fi
	SIZE=0
	echo "$NEWSIZE" > $BASERUN/$TLOG
	tail -c `expr $NEWSIZE - $SIZE` $FILE
        echo $NEWSIZE > $BASERUN/$TLOG
 fi
}

input_command=`echo $input | awk '{print$1}'`
input_opts=`echo $input | awk '{print$2,$3,$4,$5,$6,$7,$8,$9,$10}'`

case "$input_command" in
cmdrun)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	$input_opts
	;;
vzdump)
	vzdump
	;;
xenlist)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -f "/usr/sbin/vzlist" ]; then
		/usr/sbin/vzlist -a | grep -vE "CTID|ServiceCT" | awk '{print$5}' | sort
	elif [ -f "/usr/sbin/xm" ]; then
		/usr/sbin/xm list | grep -vE "Domain-0|State" | awk '{print$1}' | sort
	fi

#	ls /etc/xen/auto/| sort
#	/usr/bin/virsh list | awk '{print$2}' | grep -vE 'Name|Domain-0|^$'
	;;
xenstop)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -f "/usr/sbin/vzctl" ]; then
		vmctl="/usr/sbin/vzctl"
		input_opts=`/usr/sbin/vzlist -a -h $input_opts | awk '{print$1}' | grep -v CTID`
		offcall="stop"
	elif [ -f "/usr/sbin/xm" ]; then
		vmctl="/usr/sbin/xm"
		offcall="destroy"
	elif [ -f "/usr/bin/virsh" ]; then
		vmctl="/usr/bin/virsh"
		offcall="destroy"
	fi
	$vmctl $offcall $input_opts
	;;
xenstart)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
        if [ -f "/usr/sbin/vzctl" ]; then
                vmctl="/usr/sbin/vzctl"
                input_opts=`/usr/sbin/vzlist -a -h $input_opts | awk '{print$1}' | grep -v CTID`
        elif [ -f "/usr/sbin/xm" ]; then
                vmctl="/usr/sbin/xm"
        elif [ -f "/usr/bin/virsh" ]; then
                vmctl="/usr/bin/virsh"
        fi
	$vmctl start $input_opts
	;;
xensuspend)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
        if [ -f "/usr/sbin/vzctl" ]; then
                vmctl="/usr/sbin/vzctl"
                input_opts=`/usr/sbin/vzlist -a -h $input_opts | awk '{print$1}' | grep -v CTID`
        elif [ -f "/usr/sbin/xm" ]; then
                vmctl="/usr/sbin/xm"
        elif [ -f "/usr/bin/virsh" ]; then
                vmctl="/usr/bin/virsh"
        fi
	$vmctl suspend $input_opts
	;;
xenresume)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
        if [ -f "/usr/sbin/vzctl" ]; then
                vmctl="/usr/sbin/vzctl"
                input_opts=`/usr/sbin/vzlist -a -h $input_opts | awk '{print$1}' | grep -v CTID`
        elif [ -f "/usr/sbin/xm" ]; then
                vmctl="/usr/sbin/xm"
        elif [ -f "/usr/bin/virsh" ]; then
                vmctl="/usr/bin/virsh"
        fi
	$vmctl resume $input_opts
	;;
xenstate)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -f "/usr/sbin/vzlist" ]; then
		/usr/sbin/vzlist -a -h $input_opts | grep -v CTID | awk '{print$3}'
	elif [ -f "/usr/sbin/xm" ]; then
		/usr/sbin/xm list $input_opts | grep -vE "Domain-0|State" | awk '{print$5}'
	elif [ -f "/usr/bin/virsh" ]; then
		/usr/bin/virsh domstate $input_opts
	fi

#	/usr/bin/virsh list | grep -iw "$input_opts" | awk '{print$3" "$4}' | grep -wE 'blocked|running|no state'
	;;
getagentver)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	echo "$ver"
	;;
getagentkey)
	msg="executed input command '$input_command' from $remote_host"
	if [ -f "/usr/local/nocagent/.agentkey" ]; then
		cat /usr/local/nocagent/.agentkey
	fi
	;;
upgradeagent|agentupgrade)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	sh -c "cd /tmp ; rm -f install ; wget -t 3 -T 3 http://129.121.182.30/agent/install ; sh install ; rm -f install" >> /dev/null 2>&1
	echo "upgrade completed"
	;;
doresetkey|resetkey|agentresetkey)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
        rm -f /usr/local/nocagent/.agentkey
        ;;
dokeygen|keygen|agentkeygen)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	keygen $input_opts
	;;
doping|ping|agentping)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	ping $input_opts
	;;
gethostname)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	gethostname
        ;;
getiface)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getiface
	;;
getaltiface)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getaltiface
	;;
getmip)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getmip
        ;;
getips)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getips
        ;;
getmac)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
        getmac
        ;;
getcpu)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getcpu
	;;
getmem)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getmem
	;;
getos)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getos
	;;
getkern)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	getkern
	;;
getbans|apflistdeny)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	apflistdeny
	;;
getallow|apflistallow)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	apflistallow
	;;
apfdeny|deny)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	apfdeny $input_opts
	;;
apfallow|allow)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	apfallow $input_opts
	;;
apfdeltrust|deltrust)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	apfdeltrust $input_opts
	;;
apfclearbans|clearbans)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	apfclearbans
	;;
getacct)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	wc -l /etc/passwd | awk '{print$1}'
	;;
getcp)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -d "/var/cpanel" ]; then
		echo "1"
	else
		echo "0"
	fi
	;;
getbuagent)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -d "/etc/buagent" ]; then
                echo "1"
        else
                echo "0"
	fi
	;;
getfant)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -d "/var/netenberg/fantastico_de_luxe" ]; then
                echo "1"
        else
                echo "0"
	fi
	;;
editrdns)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
        editrdns $input_opts
        ;;
bindip)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	bindip $input_opts
	;;
passwd)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	passwd $input_opts	
	;;
resetah)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -f "/root/.accesshash" ] && [ -f "/usr/local/cpanel/bin/realmkaccesshash" ]; then
		REMOTE_USER=root /usr/local/cpanel/bin/realmkaccesshash
		cat /root/.accesshash | tr -d '\n'
	fi
	;;
getah)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -f "/root/.accesshash" ] && [ -f "/usr/local/cpanel/bin/realmkaccesshash" ]; then
		cat /root/.accesshash | tr -d '\n'
	fi
	;;
get_abuse_fguard|get_fguard_topoffenders)
        msg="executed input command '$input_command' from $remote_host"
        eout "$msg"
        localaddrs=`/sbin/ip addr list | grep -w inet | tr '/' ' ' | awk '{print$2}' | tr '\n' '|' | sed '$s/.$//'`
	tmpf="/usr/local/nocagent/.tmp.$$.$RANDOM"
	maxlines=1000000
	touch $tmpf ; chmod 600 $tmpf
	if [ -f "/var/log/fguard.log" ]; then
		for ip in `grep 'connection flood' /var/log/fguard.log  | grep "$(date +"%b")" | awk '{print$13}' | sort -n | uniq -c | sort -n | tail -n 15 | egrep -v "$localaddrs" | awk '{print$2}'`; do
			count=$[$(grep $ip /var/log/fguard.log | awk '{print$15}' | paste -sd+)]
			echo "$count $ip" >> $tmpf
			unset count
		done
	fi
	if [ -f "/usr/local/nginx/logs/error.log" ]; then
		nice -n 19 tail -n $maxlines /usr/local/nginx/logs/error.log | egrep 'limiting connections by zone' | tr -d ',' | awk '{print$12}' | egrep -v "$localaddrs" | sort -n | uniq -c | sort -n | awk '$1>=100' | tail -n 15 >> $tmpf
	fi
	cat $tmpf | awk '{print$1,$2}' | sort -n -k1
	rm -f $tmpf
	;;
get_abuse_bfd|get_bfd_topoffenders)
        msg="executed input command '$input_command' from $remote_host"
        eout "$msg"
        localaddrs=`/sbin/ip addr list | grep -w inet | tr '/' ' ' | awk '{print$2}' | tr '\n' '|' | sed '$s/.$//'`
        maxhosts="50"
	if [ -d "/usr/local/bfd/stats/" ]; then
		nice -n 19 find /usr/local/bfd/stats/ -type f -mtime -2 | xargs -I{} cat {}| awk '{print$2}' | sort -n | uniq -c | sort -n | awk '$1>=2' | tail -n 25 |  egrep -v "$localaddrs" | awk '{print$1,$2}'
	fi
	;;
get_abuse_modsec|get_modsec_topoffenders)
        msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	localaddrs=`/sbin/ip addr list | grep -w inet | tr '/' ' ' | awk '{print$2}' | tr '\n' '|' | sed '$s/.$//'`
	maxlines="100000"
	maxhosts=100
	if [ -f "/usr/local/apache/logs/error_log" ]; then
		nice -n 19 tail -n $maxlines /usr/local/apache/logs/error_log | grep "ModSecurity: Access denied" | tr -d '[]' | awk '{print$8}' | sort -n | uniq -c | awk '$1>=100' | sort -n | tail -n${maxhosts} | egrep -v "$localaddrs" | awk '{print$1,$2}'
	fi
	;;
get_abuse_eximauth|get_eximauth_topoffenders)
        msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	localaddrs=`/sbin/ip addr list | grep -w inet | tr '/' ' ' | awk '{print$2}' | tr '\n' '|' | sed '$s/.$//'`
	maxlines="1000000"
	maxhosts=100
	if [ -f "/var/log/exim_mainlog" ]; then
		tmpf="/usr/local/nocagent/.tmp.$$.$RANDOM"
		touch $tmpf ; chmod 600 $tmpf
		nice -n 19 tail -n $maxlines /var/log/exim_mainlog | egrep 'No such person at this address|535 Incorrect authentication data' > $tmpf
	        nice -n 19 egrep '535 Incorrect authentication data' $tmpf | awk '{print$9}' | tr -d '[]' | cut -d ':' -f1  | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | sort | uniq -c | awk '$1>=50'| sort -n | tail -n 10 > $tmpf.out
		nice -n 19 egrep 'No such person at this address' $tmpf | awk '{print$5}' | tr -d '[]' | cut -d ':' -f1  | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | sort | uniq -c | awk '$1>=10' | sort -n | tail -n 20 >> $tmpf.out
		cat $tmpf.out | egrep -vw '$localaddrs' | sort -n | tail -n 50
		rm -f $tmpf $tmpf.out
	fi
	;;
get_abuse_eximspam|get_eximspam_topoffenders)
        msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	localaddrs=`/sbin/ip addr list | grep -w inet | tr '/' ' ' | awk '{print$2}' | tr '\n' '|' | sed '$s/.$//'`
	maxlines="1000000"
	maxhosts=100
	if [ -f "/var/log/exim_mainlog" ]; then
		tmpf="/usr/local/nocagent/.tmp.$$.$RANDOM"
		touch $tmpf ; chmod 600 $tmpf
		nice -n 19 tail -n $maxlines /var/log/exim_mainlog | egrep 'SpamAssassin as.*detected message as spam|is in an RBL|The mail server detected your message as spam and has prevented delivery' > $tmpf
		nice -n 19 egrep '.*is in an RBL' $tmpf | awk '{print$13}' | tr -d '[]' | cut -d ':' -f1  | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | sort | uniq -c | awk '$1>=10'| sort -n | tail -n 20 >> $tmpf.out
		nice -n 19 egrep 'SpamAssassin as.*detected message as spam|The mail server detected your message as spam and has prevented delivery' $tmpf | awk '{print$5}' | tr -d '[]' | cut -d ':' -f1  | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | sort | uniq -c | awk '$1>=10' | sort -n | tail -n 20 >> $tmpf.out
		cat $tmpf.out | egrep -vw '$localaddrs' | sort -n | tail -n 50
		rm -f $tmpf $tmpf.out
	fi
	;;
getmodsecevents|getmodsec)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -f "/usr/local/apache/logs/error_log" ]; then
		tlog /usr/local/apache/logs/error_log .tlog.modsec.error_log | grep "ModSecurity: Access denied" | sed -n -e '/tag/s/.*\[tag "\(.*\)"\] \[hostname "\([^ ]*\)"\].*/\1:\2:\0/p' | tr ':[]' ' '| awk '{print$3,$4,$5,$6":"$7":"$8,$9,$12,$1,$2}'
	elif [ -f "/var/log/httpd/error_log" ]; then
		tlog /var/log/httpd/error_log .tlog.modsec.error_log | grep "ModSecurity: Access denied" | sed -n -e '/tag/s/.*\[tag "\(.*\)"\] \[hostname "\([^ ]*\)"\].*/\1:\2:\0/p' | tr ':[]' ' '| awk '{print$3,$4,$5,$6":"$7":"$8,$9,$12,$1,$2}'	
	fi
	;;
getcpaneldump|cpaneldump|getcpdump)
	msg="executed input command '$input_command' from $remote_host"
	eout "$msg"
	if [ -d "/var/cpanel" ]; then
	 for user in `ls /var/cpanel/users`; do
	        TESTD=$(getent passwd $user | cut -f6 -d: | head -n1 | grep $user)
	        if [[ "$TESTD" =~ "$user" ]] && [ -d $TESTD ]; then
	                cat /var/cpanel/users/$user | grep "=" | sort | tr '\n' ',' | sed '$s/.$/\n/'
	        fi
	 done
	fi
	;;
serverweight)
	if [ -d "/var/log/sa" ]; then
		uptime=`cat /proc/uptime | cut -d'.' -f1`
		if [ "$uptime" -lt "86400" ]; then
		        uptime="2"
		else
		        uptime="0"
		fi

		cpu=`echo "scale=2 ; (100 - $(LC_TIME='POSIX' sar -f /var/log/sa/sa$(date -d '1 day ago' +'%d') | tail -n1 | awk '{print$8}')) / 10" | bc`
		iow=`echo "scale=2 ;  $(LC_TIME='POSIX' sar -f /var/log/sa/sa$(date -d '1 day ago' +'%d') | tail -n1 | awk '{print$6}')" | bc`
		if [ "$(echo $iow | cut -d'.' -f1 | awk '{print"0"$1}')" -ge "2" ]; then
		        multi=5
		else
		        multi=2
		fi
		iow=`echo "scale=2 ; $iow * $multi" | bc`
		load=`LC_TIME='POSIX' sar -f /var/log/sa/sa$(date -d '1 day ago' +'%d') -q | grep -viE "^$|average|linux|ldavg" | awk '{print$4}' | sort -n | awk 'BEGIN{c=0} {total[c]=$1; c++;} END{print total[int(NR*0.95-0.5)]}'`
		duse=`df -h /home | awk '{print$5}' | tail -n1 | tr -d '%'`
		if [ "$duse" -ge "85" ]; then
		        multi=5
		else
		        multi=2
		fi
		duse=`echo "scale=2 ; $duse / 85 * $multi" | bc`
		users=`echo "scale=2 ; $(ls /var/cpanel/users | wc -l) / 100" | bc`
		weight=`echo "scale=2; $cpu + $iow + $load + $duse + $users + $uptime" | bc`
		echo "host:$HOSTNAME cpu:$cpu iowait:$iow load95th:$load disk:$duse users:$users uptime:$uptime weight:$weight"
	else
		echo "error: no sar data available"
	fi
	;;
*)
	msg="unknown input command '$input_command' from $remote_host"
	eout "$msg"
	echo "operation not permitted"
esac
exit 0
}

vzdump() {
 for vz in `vzlist -a | grep -vE "ServiceCT|HOSTNAME" | tr ' ' '%'`; do
        id=`echo $vz | tr '%' ' ' | awk '{print$1}'`
        if [ "$id" != "1" ]; then
         state=`echo $vz | tr '%' ' ' | awk '{print$3}'`
         ip=`echo $vz | tr '%' ' ' | awk '{print$4}'`
         if [ -z "$ip" ]; then
                ip="-"
         fi
         file="/etc/vz/conf/$id.conf"
         if [ -f "$file" ]; then
                name=`cat $file | grep -w HOSTNAME | cut -d'=' -f2 | tr -d '"'`
                if [ -z "$name" ]; then
                        name="unknown"
                fi
                dist=`cat $file | grep -w DISTRIBUTION | cut -d'=' -f2 | tr -d '"'`
                templ=`cat $file | grep -w OSTEMPLATE | cut -d '=' -f2 | tr -d '".'`
                if [ -z "$templ" ]; then
                        templ="unknown"
                fi
                arch=`cat $file | grep -w ARCH | cut -d'=' -f2 | tr -d '"'`
                mem=`cat $file | grep -w SLMMEMORYLIMIT | cut -d'=' -f2 | tr '":' ' ' | awk '{print$2}'`
                if [ -z "$mem" ]; then
                        mem=`cat $file | grep -w KMEMSIZE | cut -d'=' -f2 | tr '":' ' ' | awk '{print$1}'`
                fi
                mem=`echo "scale=0; $mem / 1048000" | bc`
                disk=`cat $file | grep -w DISKSPACE | cut -d'=' -f2 | tr '":' ' ' | awk '{print$1}'`
                disk=`echo "scale=0; $disk / 1048000" | bc`
                if [ -z "$mem" ]; then
                        mem=0
                fi
                if [ "$mem" -ge "1000" ]; then
                        mem=`echo "scale=1; $mem / 1000" | bc`
                        mem="${mem}G"
                else
                        mem="${mem}M"
                fi
                echo "$id,$name,$ip,$state,$templ,$mem,$disk"
         fi
        fi
 done
}

passwd() {
	if [ "$1" ]; then
		user=`echo $1 | tr ':' ' ' | awk '{print$1}'`
		pass=`echo $1 | tr ':' ' ' | awk '{print$2}'`
		if [ ! -z "$user" ] && [ ! -z "$pass" ]; then
			echo "$1" | /usr/sbin/chpasswd
			echo "updated password for $user"
		else
			echo "invalid paramters for passwd"
		fi
	fi
}

keygen() {
	newkey=`md5sum /proc/net/tcp | awk '{print$1}'`
	touch /usr/local/nocagent/.agentkey ; chmod 600 /usr/local/nocagent/.agentkey
	echo $newkey > /usr/local/nocagent/.agentkey
	echo $newkey
}

bindip() {
addr=$1
	if [ -f "/etc/ips" ] && [ -f "/etc/init.d/ipaliases" ]; then
		echo "$addr" >> /etc/ips
		/etc/init.d/ipaliases stop >> /dev/null 2>&1 
		sleep 2
		/etc/init.d/ipaliases start >> /dev/null 2>&1
		echo "$addr bound successfully."
	else
		echo "could not bind address on this system, requires manual binding."
	fi
}

ping() {
	echo "pong"
}

apfclearbans() {
	# clear all local bans
	:> /etc/apf/deny_hosts.rules
	:> /etc/apf/glob_deny_hosts.rules
	sh -c '/etc/apf/apf -e >> /dev/null 2>&1 &' >> /dev/null 2>&1 &
}

apfallow() {
	# allow_glob IP
	addr=`echo $1 | tr -d '\r' | tr -d '\t' | tr -d '\v' | tr -d '\n'`
	if [ -f "/usr/sbin/csf" ]; then
		/usr/sbin/csf -a $addr
	elif [ -f "/etc/apf/apf" ]; then
		/etc/apf/apf -a $addr
	fi
}

apfdeny() {
	# ban_glob IP
	addr=`echo $1 | tr -d '\r' | tr -d '\t' | tr -d '\v' | tr -d '\n'`
	if [ -f "/usr/sbin/csf" ]; then
		/usr/sbin/csf -d $addr
	elif [ -f "/etc/apf/apf" ]; then
		/etc/apf/apf -d $addr
	fi
}

apflistdeny() {
	if [ -f "/etc/csf/csf.deny" ]; then
		cat /etc/csf/csf.deny  | awk '{print$1}' | grep -v "#" | grep -v '^$'
	elif [ -f "/etc/apf/deny_hosts.rules" ]; then
		cat /etc/apf/deny_hosts.rules  | grep -v "#" | grep -v '^$'
	fi
}

apflistallow() {
	if [ -f "/etc/csf/csf.allow" ]; then
		cat /etc/csf/csf.allow  | awk '{print$1}' | grep -v "#" | grep -v '^$'
	elif [ -f "/etc/apf/allow_hosts.rules" ]; then
		cat /etc/apf/allow_hosts.rules  | grep -v "#" | grep -v '^$'
	fi
}

apfdeltrust() {
	uaddr=$1
	if [ "$uaddr" ]; then
	 if [ -f "/etc/apf/apf" ]; then
		/etc/apf/apf -u $uaddr
	 elif [ -f "/usr/sbin/csf" ]; then
		/usr/sbin/csf -ar $uaddr
		/usr/sbin/csf -dr $uaddr
	 fi
	fi
}

gethostname() {
	hostname
}

getos() {
	cat /etc/redhat-release
}

getcpu() {
	echo -n "# cpus         : " ; cat /proc/cpuinfo | grep "model name" | wc -l
	cat /proc/cpuinfo | grep "model name" | tail -n 1
	cat /proc/cpuinfo | grep "MHz" | tail -n 1
}

getmem() {
	free -m | grep -i mem | awk '{print$2}'
}

getkern() {
	/bin/uname -r
}

getiface() {
 routed_iface=`route -n | awk '{print$1" "$2" "$4" "$8}' | grep -w UG | grep -w "0\.0\.0\.0" | awk '{print$4}' | tail -n1`
 echo $routed_iface
}

getaltiface() {
 routed_iface=`route -n | awk '{print$1" "$2" "$4" "$8}' | grep -w UG | grep -w "0\.0\.0\.0" | awk '{print$4}' | tail -n1`
 alt_iface=`/sbin/route -n | grep -vE "UG|Iface|Kernel|$routed_iface|-" | awk '{print$8}' | sort | uniq`
 for i in $alt_iface; do
        ip=`/sbin/ip addr list $i | tr '/' ' ' | grep -w inet | grep -v ":" | awk '{print$2}'`
	if [ ! "$str" == "" ]; then
	        str="$str $ip"
	else
        	str="$ip"
	fi
 done
 echo -n "$str"
}

getmip() {
 routed_iface=`route -n | awk '{print$1" "$2" "$4" "$8}' | grep -w UG | grep -w "0\.0\.0\.0" | awk '{print$4}' | tail -n1`
 if [[ "$routed_iface" =~ "venet" ]]; then
	 main_ip=`/sbin/ip addr list ${routed_iface}:0 | tr '/' ' ' | grep -w inet | awk '{print$2}' | sed -n '2p'`
 else
	 main_ip=`/sbin/ip addr list $routed_iface | tr '/' ' ' | grep -w inet | grep -v ":" | awk '{print$2}'`
 fi
 echo $main_ip
}

getmac() {
 routed_iface=`route -n | awk '{print$1" "$2" "$4" "$8}' | grep -w UG | grep -w "0\.0\.0\.0" | awk '{print$4}' | tail -n1`
 macaddr=`/sbin/ifconfig $routed_iface | grep -i hwaddr | awk '{print$5}'`
 echo $macaddr
}

getips() {
 routed_iface=`route -n | awk '{print$1" "$2" "$4" "$8}' | grep -w UG | grep -w "0\.0\.0\.0" | awk '{print$4}' | tail -n1`
 main_ip=`/sbin/ip addr list $routed_iface | tr '/' ' ' | grep -w inet | grep -v ":" | awk '{print$2}'`
 addon_ip=`/sbin/ip addr list $routed_iface | tr '/' ' ' | grep -w inet | awk '{print$2}' | grep -vw $main_ip | grep -v 127.0.0.1 | tr '\n' ' '`
 if [ -d "/etc/vz" ] && [ -f "/usr/sbin/vzctl" ]; then
  vzip=$(for id in `vzlist | grep -vE "HOSTNAME|ServiceCT" | awk '{print$1}'`; do /usr/sbin/vzctl exec $id ip addr list | grep -w inet | grep venet | grep -vE "127.0.0.1|10\.+[0-9]\.+[0-9]\.+[0-9]" | tr '/' ' ' | awk '{print$2}' | tr '\n' ' '; done)
 fi
 echo $addon_ip $vzip
}

editrdns() {
 file=/usr/local/syncdns/edit_rdns
 addr=$1
 dns=$2
 if [ ! "$addr" == "" ] && [ ! "$dns" == "" ]; then
  $file $addr $dns
 else
  echo "EAGAIN 11 try again"
  exit 1
 fi
}

kill_stale
spawn $1 $2 $3
exit
