#!/bin/bash
DN="\Z1\ZrDN\Z0\ZR";
UP="\Zr\Z2UP\Z0\ZR";
if hostname -I > /etc/hostip; then
    _IP=" Private IP: `cat /etc/hostip`"
  if [ -f "/etc/pbx/.phone" ]; then
    _PHONE=`cat /etc/pbx/.phone`
  else
    _PHONE=" "
  fi
    _PUBLIC="Public Info: `curl -s -S --user-agent "Mozilla/4.0" http://myip.incrediblepbx.com | awk 'NR==2'` $_PHONE"
else
   _IP=""
   _PUBLIC=""
fi
version=`cat /etc/pbx/.version`
astversion=`asterisk -rx "core show version" | grep -m 2 ^ | tail -1 | cut -f 2 -d " "`
fpbxversion=`cat /var/www/html/admin/modules/core/module.xml | grep version | head -n 1 | cut -f 2 -d ">" | cut -f 1 -d "<"`
#OSrelease=`cat /etc/os-release | grep "PRETTY_NAME" | cut -f 2 -d "=" | sed "s|\"||g" | sed "s|(Core)|Core|"`
OSrelease=" `cat /etc/redhat-release | cut -f 1 -d "(" | sed 's|Scientific |Scientific Linux|' | sed 's|Linux release||'` "

test=`cat /etc/redhat-release | grep "Scientific Linux"`
if [[ -z $test ]]; then
 TITLE="Incredible PBX/FAX $version for CentOS 7"
else
 TITLE="  Incredible PBX $version for Scientific Linux"
fi

test=`asterisk -rx "cdr show status" | grep Enabled`
if [[ -z $test ]]; then
 asterisk="$DN"
else
 asterisk="$UP"
fi
test=`service httpd status | grep running`
if [[ -z $test ]]; then
 apache="$DN"
else
 apache="$UP"
fi
test=`ps aux | grep -c mysqld`
if [ "$test" -lt "2" ]; then
 mysql="$DN"
else
 mysql="$UP"
fi
test=`service webmin status | grep -c running`
if [[ -z $test ]]; then
 webmin="$DN"
else
 webmin="$UP"
fi
test=`service sshd status | grep -c running`
if [[ -z $test ]]; then
 ssh="$DN"
else
 ssh="$UP"
fi
test=`ps aux | grep -c sendmail`
if [ "$test" -lt "2" ]; then
 sendmail="$DN"
else
 sendmail="$UP"
fi
test=`service fail2ban status | grep running`
if [[ -z "$test" ]]; then
 fail2ban="$DN"
else
 fail2ban="$UP"
fi
test=`ps aux | grep -c ntpd`
if [ "$test" -lt "2" ]; then
 ntpd="$DN"
else
 ntpd="$UP"
fi
test=`iptables -nL | grep DROP`
if [[ -z $test ]]; then
 IPtables="$DN"
else
 IPtables="$UP"
fi
#IPPORT=`ifconfig | head -1 | cut -f 1 -d ":"`
IPPORT=`ifconfig | grep inet | head -1 | cut -f 2 -d ":" | cut -f 1 -d " "`
#IPPORT='eth0'
test=`/sbin/ifconfig | grep -c inet`
if [ "$test" -lt "2" ]; then
 eth0="$DN"
else
 eth0="$UP"
fi
RAM=`free -m | grep -m 2 ^ | tr -s " " | tail -1 | cut -f 4 -d " "`MB
if [ -z "$RAM" ]; then
 RAM="`free | grep -m 2 ^ | tail -1 | cut -f 18 -d " "`"
 if [ -z "$RAM" ]; then
  RAM=" `free | grep -m 2 ^ | tail -1 | cut -f 19 -d " "`"
 fi
fi
DISK=`df -h | tr -s ' ' |grep " /" | head -n 1 | cut -f 4 -d " "`B
if [ -z "$DISK" ]; then
 DISK=" `df -h | grep -m 2 ^ | tail -1 | cut -f 9 -d " "`"
fi
if [ "${#DISK}" -eq "4" ]; then
 DISK="$DISK"
fi

test=`cat /etc/pbx/.version`
if [[ "$test" < "13-12.3" ]]; then
 gvoauth="$DN"
else
 gvoauth="$UP"
fi
test=`ps aux | grep hylafax | head -1 | cut -f 4 -d "/" | cut -f 1 -d "d"`
if [[ "$test" -eq "hfax" ]]; then
 hfax="$UP"
else
 hfax="$DN"
fi

test=`ps aux | grep nrservice | head -1 | cut -f 4 -d "/"`
if [[ "$test" -eq "nrservice" ]]; then
 nrclient="$UP"
else
 nrclient="$DN"
fi

test=`ps aux | grep -c faxgetty`
if [ "$test" -lt "2" ]; then
 faxgetty="$DN"
else
 faxgetty="$UP"
fi
test=`ps aux | grep -c iaxmodem`
if [ "$test" -lt "2" ]; then
 iaxmodem="$DN"
else
 iaxmodem="$UP"
fi

#test=`asterisk -rx "module show like sccp" | grep -c Running`
#if [[ -z $test ]]; then
# sccp="$DN"
#else
# sccp="$UP"
#fi

test=`ps aux | grep -c knockd`
if [ $test < 2 ]; then
 knockd="$DN"
else
 knockd="$UP"
fi


NOW=`date`

TOP="\n\ZbAsterisk\ZB: $asterisk      \ZbApache\ZB: $apache  \Zb  MySQL\ZB: $mysql\n\ZbSendMail\ZB: $sendmail    \ZbIPtables\ZB: $IPtables      \ZbSSH\ZB: $ssh\n\ZbLAN port\ZB: $eth0    \ZbFail2Ban\ZB: $fail2ban   \ZbWebmin\ZB: $webmin\n\n\Zr\Z4\Zb"
TOP="\n\ZbAsterisk\ZB: $asterisk      \ZbApache\ZB: $apache  \Zb  MySQL\ZB: $mysql\n\ZbSendMail\ZB: $sendmail    \ZbIPtables\ZB: $IPtables      \ZbSSH\ZB: $ssh\n\ZbLAN port\ZB: $eth0    \ZbFail2Ban\ZB: $fail2ban   \ZbWebmin\ZB: $webmin\n\ZbGV OAUTH\ZB: $gvoauth  \Zb PortKnock\ZB: $knockd   \ZbNR VPN\ZB: $nrclient\n\ZbFaxGetty\ZB: $faxgetty  \Zb IAX Modem\ZB: $iaxmodem  \ZbHylaFax\ZB: $hfax\n\n\Zr\Z4\Zb"
MID="RAM\ZB:$RAM $OSrelease \ZbDisk\ZB:$DISK\ZR\Z0\n\n\Z5\Zb"
BOT="Asterisk $astversion   Incredible $fpbxversion\ZB\Z0\n\n\ZR\Zb$_IP\n\n$_PUBLIC\ZB\n\n\Zb\Z4System Time: $NOW"

MSGLEN=`echo ${#MID}`

 MID2=`echo $MID| sed -e 's/:/: /g'`
 MID=`echo $MID2| sed -e 's/Disk/    Disk/'`
 MID2=`echo $MID| sed -e 's/Cen/  Cen/'`
 MID=`echo $MID2| sed -e 's/Disk/ Disk/'`

MSG="$TOP$MID2$BOT"

dialog --no-lines --colors --title "$TITLE" --msgbox "$MSG" 20 47

clear
