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

[经验分享] Nagios监控lvs服务

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-6-26 09:03:27 | 显示全部楼层 |阅读模式
1在lvs 服务器上安装nrpe客户端:

1.1,rpm方式安装nrpe客户端

下载地址: nagios_nrpe.rar (729.5 KB, 下载次数: 0)

    [iyunv@localhost nagios]# ll  
      
    总计 768  
    -rw-r--r-- 1 root root 713389 12-16 12:08 nagios-plugins-1.4.11-1.x86_64.rpm  
    -rw-r--r-- 1 root root  32706 12-16 12:09 nrpe-2.12-1.x86_64.rpm  
    -rw-r--r-- 1 root root  18997 12-16 12:08 nrpe-plugin-2.12-1.x86_64.rpm  
    [iyunv@localhost nagios]# rpm -ivh *.rpm --nodeps  --force  





1.2 在配置文件最末尾,添加配置信息以及监控主机服务器ip地址

    [iyunv@ localhost nagios]# vim /etc/nagios/nrpe.cfg  
    # add by tim on 2014-06-11  
    command[check_users]=/usr/local/nagios/libexec/check_users -w 8 -c 15  
    command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20  
    command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda  
    command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z  
    #command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 50 -c 80  
    command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 750 -c 800  
    command[check-host-alive]=/usr/local/nagios/libexec/check_ping -H localhost -w 3000.0,80% -c 5000.0,100% -p 5  
    allowed_hosts = 127.0.0.1, 10.2xx.3.xx  

check下命令是否生效:

    [iyunv@web-9 nrpe-2.15]# /usr/local/nagios/libexec/check_users -w 8 -c 15  
    USERS OK - 2 users currently logged in |users=2;8;15;0  
    [iyunv@web-9 nrpe-2.15]#  

看到已经USERS OK -….命令已经生效。



1.3 启动nrpe报错如下:

    [iyunv@web-9 ~]# service nrpe restart  
    Shutting down nrpe:                                        [失败]  
    Starting nrpe: /usr/sbin/nrpe: error while loading shared libraries: libssl.so.6: cannot open shared object file: No such file or directory  
                                                               [失败]  
    [iyunv@web-9 ~]#  
    [iyunv@db-m2-slave-1 nagios_client]# service nrpe start  
    Starting nrpe: /usr/sbin/nrpe: error while loading shared libraries: libssl.so.6: cannot open shared object file: No such file or directory  
                                                               [失败]  
    [iyunv@db-m2-slave-1 nagios_client]#  

建立连接

    [iyunv@db-m2-slave-1 nagios_client]# ln -s /usr/lib64/libssl.so /usr/lib64/libssl.so.6  
     (如果没有libssl.so,就采用别的libssl.so.10来做软连接,ln -s /usr/lib64/libssl.so.10 /usr/lib64/libssl.so.6)  
    [iyunv@db-m2-slave-1 nagios_client]#  

再重新启动如下:

    [iyunv@db-m2-slave-1 nagios_client]# service nrpe start  
    Starting nrpe: /usr/sbin/nrpe: error while loading shared libraries: libcrypto.so.6: cannot open shared object file: No such file or directory  
                                                               [失败]  
    [iyunv@web-10 ~]# ll /usr/lib64/libcrypto.so  
    lrwxrwxrwx. 1 root root 18 10月 13 2013 /usr/lib64/libcrypto.so -> libcrypto.so.1.0.0  
    [iyunv@db-m2-slave-1 nagios_client]#  

再建链接:

    [iyunv@db-m2-slave-1 nagios_client]# ln -s /usr/lib64/libcrypto.so /usr/lib64/libcrypto.so.6  
    (或者如果没有libcrypto.so,就采用libcrypto.so.10做软连接, ln -s /usr/lib64/libcrypto.so.10 /usr/lib64/libcrypto.so.6)  
    [iyunv@db-m2-slave-1 nagios_client]# service nrpe start  
    Starting nrpe:                                             [确定]  
    [iyunv@db-m2-slave-1 nagios_client]#  



1.4 检测下nrpe是否正常运行:

去nagios服务器端check下

    [iyunv@cache-2 ~]#  /usr/local/nagios/libexec/check_nrpe -H xx.xx3.xx  
    NRPE v2.12  
    [iyunv@cache-2 ~]#  


[iyunv@cache-2 ~]#  /usr/local/nagios/libexec/check_nrpe -H xx.xx3.xx

NRPE v2.12

[iyunv@cache-2 ~]#

看到返回NRPE v2.15表示已经连接成功。



2 编写shell脚本实现lvs监控

2.1 监控脚本
Nagios里面没有现成的监控lvs的状态脚本,所以需要去网上找一个简单的监控脚本check_lvs.sh,copy到/usr/lib/nagios/plugins/目录,赋予nagios权限,脚本内容如下:

    #!/bin/bash   
    # http://www.ohlinux.com/archives/632/  
    # add by tim on 20140613  
    USAGE_Method=\"$(basename $0)[-h|--hostname] <Free ip or hostname> [-w|--warning] <Free integer> [-c|--critical] <Free integer>\"  
    USAGE_Value=\"warning value must be small than critical value: `basename $0` $*\"  
    STATE_OK=0  
    STATE_WARNING=1  
    STATE_CRITICAL=2  
    STATE_UNKNOWN=3  
      
    if [ $# -lt 4 ];then  
        echo  
        echo \"Usage: $USAGE_Method\"  
        echo  
        exit 0  
    fi  
    while [ $# -gt 0 ];  
    do  
        case \"$1\" in  
        -w|--warning)  
        shift  
        warning=$1  
        ;;  
        -c|--critical)  
        shift  
        critical=$1  
        ;;  
        esac  
        shift  
    done  
      
      
    if [[ $warning == $critical || $warning -gt $critical ]]  
    then  
        #echo $warning  
        #echo $critical  
        echo \"$USAGE_Value\"  
        echo \"Usage: $USAGE_Method\"  
        exit 0  
    fi  
      
      
    ACT_COUNT=0  
    Inactive_count=0  
    stat1=`sudo ipvsadm | grep http | grep Route|wc -l`  
    if [ $stat1 -ne 0 ];then  
        for NUM in `sudo ipvsadm | grep http | grep Route | awk \'{print $5}\'`  
        do  
             ACT_COUNT=$(($ACT_COUNT+ $NUM))  
        done  
        for NUM in `sudo ipvsadm | grep http | grep Route | awk \'{print $6}\'`  
        do  
            Inactive_count=$(($Inactive_count+ $NUM))  
        done  
    else  
        echo \" stat1:$stat1, lvs critical,lvs is down now.\"  
        exit 3  
    fi  
      
      
      
    if [[ \"$ACT_COUNT\" -gt \"$critical\" ]]  
    then  
        echo \"critical - lvs connetion is : $ACT_COUNT active\"  
        exit 2  
    fi  
    if [[ \"$ACT_COUNT\" -gt \"$warning\" && \"$ACT_COUNT\" -lt \"$critical\" ]]  
    then  
        echo \"warning - lvs connetions is : $ACT_COUNT active\"  
        exit 1  
    fi  
    if [[ \"$ACT_COUNT\" -lt \"$warning\" || $ACT_COUNT == 0 ]]  
    then  
        echo \"LVS OK - LVS is running (conn: $ACT_COUNT active, $Inactive_count inactive)|active=$ACT_COUNT;69999;99999;0; inactive=$Inactive_count;69999;99999;0;\"  
        exit 0  
    fi  


2.2 nrpe.cfg里面配置如下

Vim /etc/nagios/nrpe.cfg,在里面添加一行check_lvs命令:

    command[check_lvs]=/usr/lib/nagios/plugins/check_lvs -w 300 -c 600   

之后重启nrpe

    [iyunv@/root/nagios/check_lvs ~]# service nrpe restart;  
    Shutting down nrpe:                                        [确定]  
    Starting nrpe:                                             [确定]  
    [iyunv@/root/nagios/check_lvs ~]#service nrpe restart;  


2.3 去nagios服务端check一下

      

    [iyunv@cache-2 ~]#  /usr/local/nagios/libexec/check_nrpe -H 1x.xx4.x.x5 -c check_lvs  
     lvs critical,lvs is down now.  
    [iyunv@cache-2 ~]#  

看到check出来lvs服务已经处于down模式。

说明:由于check_lvs是要调用ipvsadm命令来获取LVS状态的,而ipvsadm命令是只能以root用户来运行的, 所以需要将nagios用户设置成可以无需密码直接su成root,这样就能以nagios用户运行命令sudo /usr/lib/nagios/plugins/check_lvs 。在centos系统中,无法直接调用sudo命令,需要修改/etc/sudoers, 找到 #Defaults requiretty 并取消注释,另外新增一行。表示nagios用户不需要登陆终端就可以调用命令,如下所示:

    Defaults    requiretty  
    Defaults:nagios    !requiretty  
    #添加nagios 请求sudo,允许特定指令时(可跟参数),不需要密码(如)。  
    nagios ALL=(ALL) NOPASSWD: ALL  

再去naigos服务器上面check下,已经生效,如下所示:

    [iyunv@cache-2 etc]# /usr/local/nagios/libexec/check_nrpe -H 10.xx.xx.xx -c check_lvs  
    LVS OK - LVS is running (conn: 16 active, 77 inactive)|active=16;69999;99999;0; inactive=77;69999;99999;0;  
    [iyunv@cache-2 etc]#  



2.4 在nagios服务器上添加配置

    vim services.cfg  
    define service{  
            host_name               lvs-lan  
            service_description     Check lvs  
            check_command           check_nrpe!check_lvs  
            max_check_attempts      5  
            normal_check_interval   3  
            retry_check_interval    2  
            check_period            24x7  
            notification_interval   10  
            notification_period     24x7  
            notification_options    w,c,r  
            contact_groups          opsweb  
            }  
    vim objects/commands.cfg  
    define command{  
            command_name    check_lvs  
            command_line    $USER1$/check_lvs -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$  
            }  

之后重新加载nagios既完成了对lvs的监控服务。

    [iyunv@cache-2 etc]# service nagios reload  
    Running configuration check...  
    Reloading nagios configuration...  
    done  
    [iyunv@cache-2 etc]#  

至此,nagios下面对lvs服务的监控已经完成。

运维网声明 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-21056-1-1.html 上篇帖子: linux lvs集群nat模式(比上一篇的lvs nat实用) 下篇帖子: Keepalived学习笔记--LVS+Keepalived+Apache负载均衡架构 监控
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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