#!/bin/bash
#--变量
err_echo(){
    echo -e "\\033[31m[Error]: $1 \\033[0m"
    exit 1
}

info_echo(){
    echo -e "\\033[32m [Info]: $1 \\033[0m"
}

warn_echo(){
    echo -e "\\033[33m [Warning]: $1 \\033[0m"
}

check_exit(){
    if [ $? -ne 0 ]; then
        err_echo "$1"
        exit 1
    fi
}

#临时关闭selinux
info_echo "disabled selinux...."
setenforce 0 

#配置yum源
info_echo "config yum source....."
rpm -Uvh http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm 1>/dev/null

info_echo "setup tool pakeages...."
yum -y install wget  1>/dev/null
check_exit

yum -y install perl perl-Net-Telnet perl-Net-DNS perl-LDAP perl-libwww-perl perl-RadiusPerl perl-IO-Socket-SSL perl-Socket6 perl-CGI-SpeedyCGI perl-FCGI perl-CGI-SpeedCGI perl-Time-HiRes perl-ExtUtils-MakeMaker perl-RRD-Simple rrdtool rrdtool-perl curl fping echoping  httpd httpd-devel gcc make  wget libxml2-devel libpng-devel glib pango pango-devel freetype freetype-devel fontconfig cairo cairo-devel libart_lgpl libart_lgpl-devel mod_fastcgi 1>/dev/null
check_exit

if [ -f /root/smokeping-2.6.9.tar.gz ]; then 
    warn_echo "/root/smokeping-2.6.9.tar.gz is exist"
    else
    info_echo "download smokeping...."
    wget http://oss.oetiker.ch/smokeping/pub/smokeping-2.6.9.tar.gz 1>/dev/null
fi
check_exit

if [ -f /usr/local/src/smokeping-2.6.9 ]; then
        rm -rf /usr/local/src/smokeping-2.6.9
fi

info_echo "tar smokeping to /usr/local/src"    
tar -xvf smokeping-2.6.9.tar.gz -C /usr/local/src  1>/dev/null

if [ -f /usr/local/smokeping ]; then 
    mv /usr/local/smokeping  smokeping_back
fi

cd /usr/local/src/smokeping-2.6.9

info_echo "set up smokeping....."
./configure -prefix=/usr/local/smokeping   1>/dev/null
if [ $? -ne 0 ];then
echo -e "\\033[33m first setup faild \\033[0m"

info_echo " per handle symbiosis......"
./setup/build-perl-modules.sh /usr/local/smokeping/thirdparty  1>/dev/null
check_exit

info_echo "full install smokeping....."  
./configure -prefix=/usr/local/smokeping   1>/dev/null
/usr/bin/gmake install  1>/dev/null
else
/usr/bin/gmake install  1>/dev/null
check_exit
fi

cd /usr/local/smokeping
mkdir cache data var 1>/dev/null
check_exit

info_echo "chomod dir permission"
#修改目录权限
chown -R apache:apache cache data var htdocs

mv /usr/local/smokeping/htdocs/smokeping.fcgi.dist  /usr/local/smokeping/htdocs/smokeping.fcgi

mv /usr/local/smokeping/etc/config.dist  /usr/local/smokeping/etc/config
cp -f /usr/local/smokeping/etc/config /usr/local/smokeping/etc/config.back

info_echo "edit smokeping config....."
#修改smokeping配置文件
IP=`/sbin/ifconfig|sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\) .*/\1/1p'|sed -n '1p'`
sed -i "/cgiurl/c cgiurl = http:\/\/$IP/smokeping.cgi" /usr/local/smokeping/etc/config
sed -i "/imgurl/c imgurl = http:\/\/$IP/cache"  /usr/local/smokeping/etc/config

chmod 600 /usr/local/smokeping/etc/smokeping_secrets.dist


info_echo "edit httpd config....."
#修改apache配置文件
cp -rf /etc/httpd/conf/httpd.conf  /etc/httpd/conf/httpd.conf.back

cat >> /etc/httpd/conf/httpd.conf <<'EOF'
Alias /cache "/usr/local/smokeping/cache/"
Alias /cropper "/usr/local/smokeping/htdocs/cropper/"
Alias /smokeping "/usr/local/smokeping/htdocs/smokeping.fcgi"

AllowOverride None
Options All
AddHandler cgi-script .fcgi .cgi
Order allow,deny
Allow from all
DirectoryIndex smokeping.fcgi

EOF

#提供启动脚本
info_echo "edit auto manger script"

if [ -f /etc/init.d/smokeping ];then
    warn_echo "/etc/init.d/smokeping is exist"
else
    touch /etc/init.d/smokeping
    cat > /etc/init.d/smokeping <<'EOF'
#!/bin/bash

#chkconfig: 2345 80 05
 # Description: Smokeping init.d script
 # Create by : Mox
 # Get function from functions library
 . /etc/init.d/functions
 # Start the service Smokeping
smokeping=/usr/local/smokeping/bin/smokeping
prog=smokeping
pidfile=${PIDFILE-/usr/local/smokeping/var/smokeping.pid}
lockfile=${LOCKFILE-/var/lock/subsys/smokeping}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
LOG=/var/log/smokeping.log

start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $smokeping $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}


 # Restart the service Smokeping
stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $smokeping
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

 case "$1" in
 start)
        start
;;
 stop)
        stop
;;
 status)
        status -p ${pidfile} $httpd
        RETVAL=$?
;;
 restart)
        stop
        start
        ;;
 *)
        echo $"Usage: $prog {start|stop|restart|status}"
        RETVAL=2

esac

EOF

chmod +x /etc/init.d/smokeping
chkconfig smokeping on
fi
if [ $? -ne 0 ];then
echo -e "\\033[32m smokeping setup successfull \\033[0m"
fi