#!/bin/bash
#note: This script is to install smokeping for CentOS
#Author :[email protected]
#Date: 2012.11.20
#release: 1.3    2012.11.20
#release: 1.4    2013.01.07    

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
export PATH

stop_firewall()
{
    /etc/init.d/iptables stop
    chkconfig --level 345 iptables off
    selinux=`getenforce`
    if [ ${selinux} = Enforcing ];then
        echo "SElinux is open,I will Stop It."
        setenforce 0
    fi    
}

stop_services()
{
     if [ -f /etc/init.d/sendmail ];then
            /etc/init.d/sendmail stop
            chkconfig --level 345 sendmail off
    else
            echo "sendmail not start!"
    fi
}

start_services()
{
    /etc/init.d/httpd stop
    /etc/init.d/httpd start
    chkconfig --level 345 httpd on
    /etc/init.d/smokeping stop
    /etc/init.d/smokeping start
    chkconfig --level 345 smokeping on
}

time_sync()
{
    if [ -f /var/spool/cron/root ];then
            if [ `grep "ntp.app.joy.cn" /var/spool/cron/root |wc -l` -eq 0 ];then
                echo "1 * * * * /usr/sbin/ntpdate ntp.app.joy.cn; hwclock -w 1>/dev/null 2>&1" >> /var/spool/cron/root
            fi
    else
            touch /var/spool/cron/root
            echo "1 * * * * /usr/sbin/ntpdate ntp.app.joy.cn; hwclock -w 1>/dev/null 2>&1" >> /var/spool/cron/root
    fi

}

update_source5()
{
        system_arch=`arch`
        if [ "x${system_arch}" == "x" ];then
                     echo "system_arch error"
                     exit 1
        elif [ "x${system_arch}" == "xx86_64" ];then
                     rpm -Uvh http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
        else
                     rpm -Uvh http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
        fi
}


update_source6()
{
        system_arch=`arch`
#RRDtools path                                                                                                                                            
     # RRDs_path=`find /usr -name RRDs | grep "linux-thread-multi"`                                                
                
        if [ "x${system_arch}" == "x" ];then                                                                                                    
                echo "system_arch error"
                exit 1
        elif [ "x${sys_info}" == "xx86_64" ];then                                                                                    
                     #rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel//6/x86_64/epel-release-6-7.noarch.rpm
                     rpm -Uvh http://mirrors.ustc.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
        else    
                     #rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
                     rpm -Uvh http://mirrors.ustc.edu.cn/epel/6/i386/epel-release-6-8.noarch.rpm
        fi        
}


install_packets_ver5()
{
        yum install -y httpd
        yum install -y rrdtool
        yum install -y fping
        yum install -y echoping
        yum install -y curl
        yum install -y perl perl-Net-Telnet perl-Net-DNS
        um install -y perl-LDAP perl-libwww-perl perl-RadiusPerl
        um install -y perl-IO-Socket-SSL perl-Socket6 perl-CGI-SpeedyCGI
        mv ${current_dir}/config_dir/httpd.conf /etc/httpd/conf/
        service httpd start
        chkconfig httpd on
}

install_packets_ver6()
{
        yum -y install libxml2-devel libpng-devel glib pango pango-devel freetypefreetype-devel fontconfig cairo cairo-devel                                                                                                    
        yum -y install libart_lgpl libart_lgpl-devel perl perl-Net-Telnet perl-Net-DNS perl-LDAP perl-libwww-perl perl-RadiusPerl
        yum -y install perl-IO-Socket-SSL perl-Socket6 perl-CGI-SpeedyCGI rrdtool-perl
        yum -y install httpd fping echoping curl perl perl-Net-Telnet perl-Net-DNS perl-LDAP perl-libwww-perl perl-RadiusPerl
        yum -y install perl-IO-Socket-SSL perl-Socket6 perl-CGI-SpeedyCGI
        yum -y install cjkuni-ukai-fonts
        yum -y install cjkuni-uming-fonts
        yum -y install wqy-zenhei-fonts
        yum -y install cjkuni-fonts-common
        mv ${current_dir}/config_dir/httpd.conf /etc/httpd/conf/
        service    httpd start
        chkconfig httpd on
}


determine_version()
{
    standard_version="6.0"
    current_version=`lsb_release -r |awk '{print $2}'`
    yum clean dbcache
    if [[ ${current_version} < ${standard_version} ]];then
             echo "Version:CentOS ${current_version}"
             update_source5
             yum list
             yum makecache
             install_packets_ver5
    else
             echo "Version:CentOS ${current_version}"
             update_source6
             yum list
             yum makecache
             install_packets_ver6
    fi
}

ins_rrdtool()
{
    cd ${current_dir}
    tar zxvf rrdtool-1.4.5.tar.gz
    cd rrdtool-1.4.5
    sed -i 's/setlocale(LC_NUMERIC, "C")/setlocale(LC_ALL, "zh_CN.gb2312")/g' src/rrd_graph.c
    ./configure --prefix=/usr/local/rrdtool --disable-ctl --disable-python
    make
    make install
}

ins_smokeping()
{
        cd ${current_dir}
        if [ -d /opt/smokeping ];then
                rm -rf /opt/smokeping
        fi
        local_ip=`ifconfig |grep "Bcast" |awk '{print $2}' |sed 's/addr://g'`
        tar zxvf smokeping-2.4.2.tar.gz
        mv smokeping-2.4.2 /opt/smokeping
        sed -i "s#IPREPLACE#${local_ip}#g"    ${current_dir}/config_dir/config
        cp -f ${current_dir}/config_dir/smokeping             /opt/smokeping/bin/
        cp -f ${current_dir}/config_dir/smokeping.cgi     /opt/smokeping/htdocs/
        cp -f ${current_dir}/config_dir/tr.cgi                    /opt/smokeping/htdocs/
        cp -f ${current_dir}/config_dir/config                    /opt/smokeping/etc/
        cp -f ${current_dir}/config_dir/basepage.html     /opt/smokeping/etc/
        cp -f ${current_dir}/config_dir/smokemail             /opt/smokeping/etc/
        cp -f ${current_dir}/config_dir/tmail                     /opt/smokeping/etc/
        cp -f ${current_dir}/config_dir/smokeping_secrets        /opt/smokeping/etc/smokeping_secrets
        cp -f ${current_dir}/config_dir/smokeping-service    /etc/init.d/smokeping
        cp -f ${current_dir}/config_dir/a.flv         /var/www/html/
        chmod 600 /opt/smokeping/etc/smokeping_secrets
        chmod 4775 /bin/traceroute
        mkdir -p /var/www/html/smokeping/img
        mkdir -p /var/www/html/smokeping/script/
        mkdir -p /opt/smokeping/data
        mkdir -p /opt/smokeping/var
        chown -R apache:apache /var/www/html/smokeping/img        
        ln -s /opt/smokeping/htdocs/cropper /var/www/html/smokeping/cropper
        ln -s /opt/smokeping/htdocs/resource /var/www/html/smokeping/resource
        ln -s /opt/smokeping/htdocs/script/Tr.js /var/www/html/smokeping/script/Tr.js
        ln -s /opt/smokeping/htdocs/smokeping.cgi /var/www/html/smokeping/smokeping.cgi
        ln -s /opt/smokeping/htdocs/tr.cgi /var/www/html/smokeping/tr.cgi
        chmod 755 /etc/init.d/smokeping
}


main()
{
    stop_firewall
    stop_services
    time_sync
    determine_version
    ins_rrdtool
    ins_smokeping
    start_services
}


current_dir=`pwd`
sed -i 's/^LANG.*$/LANG="zh_CN.UTF-8"/g' /etc/sysconfig/i18n
main