设为首页 收藏本站
查看: 763|回复: 0

一键搭建nagios监控系统之一 脚本篇

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-6-15 08:31:19 | 显示全部楼层 |阅读模式
1,脚本内容如下:

    #! /bin/bash  

    ##################  nagios server configure ########################  

    installserver() {  

    read -p " Please input the alarm  email address : " email  

    read -p " Please input the login nagios user: " loginuser  

    read -p " Please input the login nagios pass: " loginpass  

      

    echo  "###############  Install nagios server #################"  

    yum -y install httpd php php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc httpd-manual mod_ssl mod_perl  zlib libpng  freetype  libart_lgpl libart_lgpl-devel libxml*   pango*  

      

    wget http://prdownloads.sourceforge.n ... nagios-3.2.3.tar.gz  

    useradd nagios -s /sbin/nologin  

    tar -xvf  nagios-3.2.3.tar.gz  

    cd nagios-3.2.3  

    ./configure --prefix=/usr/local/nagios  

    make all  

    make install  

    make install-init  

    make install-config  

    make install-commandmode  

    make install-webconf  

    cd  ..  

      

    wget http://prdownloads.sourceforge.n ... ugins-1.4.15.tar.gz  

    tar -xvf  nagios-plugins-1.4.15.tar.gz  

    cd nagios-plugins-1.4.15  

    ./configure  

    make  

    make install  

    cd ..  

      

    wget http://nchc.dl.sourceforge.net/s ... os/nrpe-2.12.tar.gz  

    tar -xvf  nrpe-2.12.tar.gz  

    cd nrpe-2.12  

    ./configure  

    make all  

    make install  

    make install-plugin  

    make install-daemon  

    make install-daemon-config  

    cd ..  

      

    wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz  

    tar -xvf  rrdtool-1.4.7.tar.gz  

    cd rrdtool-1.4.7  

    ./configure --prefix=/usr/local/rrdtool/ &&make &&make install  

    cd ..  

      

    wget http://pkgs.fedoraproject.org/re ... f/pnp-0.4.12.tar.gz  

    tar -xvf  pnp-0.4.12.tar.gz  

    cd pnp-0.4.12  

    ./configure --with-nagios-user=nagios --with-nagios-group-nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool  

    make   

    make all   

    make install   

    make install-config   

    make install-init  

    cd ..  

      

    wget http://218.6.16.170/somenagios.tar  

    tar -xvf somenagios.tar -C /usr/local/nagios/libexec/  

      

    echo  "################install nagios server  completed#############"  

      

    echo "################# configure nagios server start###############"  

    htpasswd -bc /usr/local/nagios/etc/htpasswd.users  $loginuser $loginpass  

      

    # cgi.cfg configure#  

    sed -i "s#use_authentication=1#use_authentication=0#g" /usr/local/nagios/etc/cgi.cfg  

      

    #nagios.cfg configure#  

    sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg  

    sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg  

    sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg' /usr/local/nagios/etc/nagios.cfg  

    sed -i "s#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#\#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#g"  /usr/local/nagios/etc/nagios.cfg      

    sed -i "s#process_performance_data=0#process_performance_data=1#g" /usr/local/nagios/etc/nagios.cfg  

    sed -i "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/g" /usr/local/nagios/etc/nagios.cfg  

    sed -i "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/g" /usr/local/nagios/etc/nagios.cfg  

      

    #command.cfg configure#  

     

    line1=`sed -n '/process-host-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`  

    line2=`sed -n '/process-service-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`  

      

    sed -i "$(($line1+1))"d /usr/local/nagios/etc/objects/commands.cfg  

    sed -i "$(($line1+1))i\        command_line /usr/local/nagios/libexec/process_perfdata.pl"  /usr/local/nagios/etc/objects/commands.cfg  

    sed -i "$(($line2+1))"d /usr/local/nagios/etc/objects/commands.cfg  

    sed -i "$(($line2+1))i\        command_line /usr/local/nagios/libexec/process_perfdata.pl"  /usr/local/nagios/etc/objects/commands.cfg  

      

    echo '  

    define command{  

            command_name    check_nrpe  

            command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30  

            }  

      

    define command {  

           command_name  check_mysql  

           command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -unagdb -pnagdb -d nagdb  

      

           }  

    '>>/usr/local/nagios/etc/objects/commands.cfg  

      

    #templates.cfg configure#  

      

    echo '  

    define host {  

            name       hosts-pnp  

            register   0  

            action_url /nagios/pnp/index.php?host=$HOSTNAME$  

            process_perf_data               1  

      

    }  

    define service {  

            name       services-pnp  

            register   0  

            action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$  

            process_perf_data               1  

      

    }'>>/usr/local/nagios/etc/objects/templates.cfg  

      

    #pnp configure#  

      

    cp  /usr/local/nagios/etc/pnp/process_perfdata.cfg-sample  /usr/local/nagios/etc/pnp/process_perfdata.cfg   

    cp  /usr/local/nagios/etc/pnp/npcd.cfg-sample  /usr/local/nagios/etc/pnp/npcd.cfg  

    cp  /usr/local/nagios/etc/pnp/rra.cfg-sample  /usr/local/nagios/etc/pnp/rra.cfg   

    chown -R nagios:nagios  /usr/local/nagios/etc/pnp/*  

      

    sed -i 's#LOG_LEVEL = 0#LOG_LEVEL = 2#g'  /usr/local/nagios/etc/pnp/process_perfdata.cfg  

      

    #contacts.cfg configure#  

      

    sed -i  "s#nagios@localhost#$email#g" /usr/local/nagios/etc/objects/contacts.cfg   

    clear  

      

    echo "################### nagios server install and  configure completed,plase add hosts and services  then start it ##################"  

      

    }  

     

    restartnagios(){  

        echo "stopping ......"  

        service nagios stop  

        killall -9 nrpe  

        service httpd stop  

        echo "starting ......"  

        service nagios start  

        /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  

        service httpd start  

    }  

    addservices(){  

      

    read -p "please input the alias name : "  host  

    read -p "please input the clint ip : "  ip  

    echo "define host {  

           host_name                 $host  

            use            linux-server,hosts-pnp  

           alias                      nagios server  

           address                   $ip  

           contact_groups             admins  

           check_command              check-host-alive  

           max_check_attempts         5  

           notification_interval      10  

           notification_period        24x7  

           notification_options        d,u,r  

           }" >> /usr/local/nagios/etc/objects/hosts.cfg  

      

      

    echo "define hostgroup {  

            hostgroup_name myserver  

            alias          myserver  

            members        $host  

    }" >>/usr/local/nagios/etc/objects/hostgroup.cfg  

     

    echo "define service {  

            host_name     $host  

            use local-service,services-pnp  

            service_description   CPU  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_nrpe!check_cpu  

           }  

    define service {  

            host_name     $host  

            use local-service,services-pnp  

            service_description   CPU_load  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_nrpe!check_load  

           }  

    define service {  

            host_name     $host  

            use local-service,services-pnp  

            service_description   HTTP  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_nrpe!check_http  

           }  

    define service {  

            host_name     $host  

            use local-service,services-pnp  

            service_description   MEMORY  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_nrpe!check_mem  

           }  

    define service {  

            host_name     $host  

            service_description   connect-count  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_nrpe!check_ips  

           }  

    define service {  

            host_name     $host  

            service_description   DISK  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_nrpe!check_disk  

           }  

    define service {  

            host_name     $host  

            service_description   mysql  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command   check_mysql  

           }  

    define service {  

            host_name     $host  

            service_description   PING  

            use local-service,services-pnp  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 1  

            retry_check_interval 1  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_ping!100.0,20%!500.0,60%  

           }  

    define service {  

            host_name     $host  

            use local-service,services-pnp  

            service_description  FTP  

            check_period          24x7  

            max_check_attempts    4  

            normal_check_interval 60  

            retry_check_interval 10  

            contact_groups        admins  

            notification_interval   10  

            notification_period     24x7  

            notification_options    w,u,c,r  

            check_command    check_nrpe!check_ftp  

           }  

      

    " >>/usr/local/nagios/etc/objects/services.cfg  

    echo "###############  Add services completed ########"  

    }  

    ######################### nagios  configure  completed ########################  

      

    ####################### client  server configure ########################  

      

    clientnagios() {  

      

    ### install soft###  

    read -p "Please input the nagios server ip : " nagiosip  

      

    read -p "Please input the mysql password : " mysqlpass  

      

    useradd nagios  

    yum install zlib libpng  freetype  libart_lgpl libart_lgpl-devel libxml*   pango*  

      

    wget http://prdownloads.sourceforge.n ... ugins-1.4.15.tar.gz  

    tar -xvf  nagios-plugins-1.4.15.tar.gz  

    cd nagios-plugins-1.4.15  

    ./configure  

    make  

    make install  

    cd ..  

      

    wget http://nchc.dl.sourceforge.net/s ... os/nrpe-2.12.tar.gz  

    tar -xvf  nrpe-2.12.tar.gz  

    cd nrpe-2.12  

    ./configure  

    make all  

    make install  

    make install-plugin  

    make install-daemon  

    make install-daemon-config  

    cd ..  

      

    wget http://218.6.16.170/somenagios.tar  

    tar -xvf somenagios.tar -C /usr/local/nagios/libexec/  

    ### install completed ###

    ### configure ####  

      

    rm /usr/local/nagios/etc/nrpe.cfg  

      

    cat >>/usr/local/nagios/etc/nrpe.cfg<<EOF  

    log_facility=daemon  

    pid_file=/var/run/nrpe.pid  

    server_port=5666  

    nrpe_user=nagios  

    nrpe_group=nagios  

    allowed_hosts=$nagiosip  

    dont_blame_nrpe=1  

    debug=1  

    command_timeout=30  

    connection_timeout=300  

    command[check_http]=/usr/local/nagios/libexec/check_http -H  -u 127.0.0.1 /index.php -t 60  

    command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -A -i '/dev/shm'  

    command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh  

    command[check_mem]=/usr/local/nagios/libexec/check_mem.sh  

    command[check_ips]=/usr/local/nagios/libexec/ip_conn.sh  400 600  

    command[check_load]=/usr/local/nagios/libexec/check_load -w 14,12,10 -c 18,15,12  

    command[check_ftp]=/usr/local/nagios/libexec/check_ftp 127.0.0.1 -w 10 -c 60  

    EOF  

      

    mysql -uroot -p"$mysqlpass" <<EOF  

    create database nagdb;  

    grant all on nagdb.* to nagdb@"$nagiosip" identified by 'nagdb';  

    flush privileges;  

    \q  

    EOF  

      

    killall -9 nrpe  

    /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  

      

    ############ configure completed ############  

      

    }  

      

    real=`grep -l '\^H' /root/.bash_profile`  

    if [ $? -eq 1 ];then  

    echo  'stty erase ^H' >> /root/.bash_profile  

    source /root/.bash_profile  

    fi  

    echo -e '\033[0;33;1m #################nagios################## \033[0m'   

    echo "nagios server install and configure or addserver          please input : 1"  

    echo "  clint server install or configure                please input : 2"  

    echo "      exit                             please input : 3"  

    echo -e '\033[0;33;1m ######################################### \033[0m'  

      

    read -p "please chose : " frist  

      

    if [ $frist -eq 1 ];then  

      

    echo -e '\033[0;33;1m #################nagios################## \033[0m'  

    echo "nagios server install and configue                    please input : 1"  

    echo "  Add server and services                      please input : 2"  

    echo "      exit                             please input : 3"  

    echo -e '\033[0;33;1m ######################################### \033[0m'  

    read -p "please chose : " nagiosfrist  

        if [ $nagiosfrist -eq 1 ];then  

                        installserver  

                        addservices  

                        restartnagios  

        elif [ $nagiosfrist -eq 2 ];then  

                        addservices  

                        restartnagios  

        elif [  $nagiosfrist -eq 3 ];then  

                        clear  

                        exit  

        else  

                        echo "plase chose [1|2|3]"  

        fi               

         

    elif [ $frist -eq 2 ];then  

    clientnagios  

     

    elif [ $frist -eq 3 ];then  

    clear  

    exit  

    else   

    echo "plase chose [1|2|3]"  

    fi  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-77292-1-1.html 上篇帖子: 浅谈 nagios监控配置 下篇帖子: 一键搭建nagios监控系统之二 脚本解释篇 监控系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表