• 内网监控利器——Nagios


    在游戏运维中有最长用的就是使用nagios监控游戏服务器。nagios是一款比较成熟的监控软件。对被监控的主机从负载到进程、网络端口、系统服务等都能做监控,而且当出现问题时候能发出邮件警报。

    一、nagios的安装(root用户下安装)

    安装必要的包

    yum install httpd php gcc glibc glibc-common gd gd-devel

    建立相应的用户和用户组

    useradd -m nagios
    passwd nagios
    groupadd nagcmd
    usermod -a -G nagcmd nagios
    usermod -a -G nagcmd apache

    下载并解压nagios的安装包

    wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
    wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz
    tar -zxf nagios-3.2.3.tar.gz

    开始configure

    cd nagios-3.2.3
    ./configure --with-command-group=nagcmd
    make all
    make install
    make install-init
    make install-config
    make install-commandmode

    这样基本的就安装好了。但是现在不要启动nagios,还要修改/usr/local/nagios/etc/objects/contacts.cfg文件,定义报警邮箱。

    安装web接口

    make install-webconf  
    htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin  #添加web登录用户并设置密码
    service httpd restart

    安装nagios监控插件

    tar xzf nagios-plugins-1.4.11.tar.gz
    cd nagios-plugins-1.4.11
    ./configure --with-nagios-user=nagios --with-nagios-group=nagios
    make
    make install
    #添加开机自启动
    chkconfig --add nagios
    chkconfig nagios on

    校验配置文件

    /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
    #启动naigos
    service nagios start

     二、nagios配置

    首先是发送邮件报警命令的定义,给个例子如下,所有的命令定义最好都写在command.cfg中。

    # 'notify-host-by-email' command definition
    define command{
         command_name    notify-host-by-email
         command_line    /usr/bin/printf "%b" "***** TW GAME *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" |/usr/local/bin/sendEmail.pl -f "this is for define mail from" -t $CONTACTEMAIL$ -s "this is you mail smtp server" -u "** $NOTIFICATIONTYPE$ TW GAME : $HOSTALIAS$/$SERVICEDESC$ is $HOSTSTATE$ **" -xu "this is your mail account" -xp "this is your mail password"
            }
    
    # 'notify-service-by-email' command definition
    define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** TW GAME *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService:$SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/bin/sendEmail.pl -f Lenwood@mail.163.com -t $CONTACTEMAIL$  -s smtp.mail.163.com -u "** $NOTIFICATIONTYPE$ TW GAME : $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$**" -xu Lenwood@mail.163.com -xp passwordforserect
            }

    解释一下:

    (1)上面用到了很多变量,这都是nagios能够提供的,如$HOSTNAME$就是出问题的那台服务器的主机名。
    (2)粗体部分需要修改为自己用来发送邮件报警的邮箱,这个我就随便举了个例子。
    (3)这里使用sendEmail.pl这个perl脚本来发送邮件,不适用sendmail的原因是sendmail有时候不可靠,尤其是DNS解析有问题的时候。
     
    其他命令的定义先不写,多半都是使用/path/to/nagios/libexec/目录下的一些插件,如check_ping、check_smtp、check_http等。
     

    三、总结的nagios非常实用的技巧

    nagios主要是添加新机器比较恶心,所以我使用shell脚本的方式添加,这样不论是添加1台还是1000台的监控,只要有hostname和IP地址对应信息,都只需5分钟。
    例如,给出host记录如下
    #/home/nagios/hosts.txt内容如下
    game_web1     10.96.19.51
    game_web2     10.96.19.52
    game_ope     10.96.19.53
    game_db1     10.96.19.54
    game_db2     10.96.19.55
    添加新的服务器监控必须在nagios中定义主机和这个主机需要监控的服务。我的解决方法是分别写两个模板,然后通过/home/nagios/hosts.txt替换模板中的主机名和IP。
    (1)host定义模板
    host_template.cfg内容如下
    define host {
            use     linux-server
            host_name      hostname
            address ipaddress
            hostgroups      hostgroupname
            contact_groups  lenwood
    }

    好了,开始批量替换

    while read line;do host=`echo $line|awk '{print $1}'`;ip=`echo $line|awk '{print $2}'`;sed  "s/hostname/$host/g" host_template.cfg|sed "s/ipaddress/$ip/g"|sed "s/hostgroupname/GAME/g">>host.cfg;done</home/nagios/hosts.txt
    这样就都替换好了,其中GAME是事先定义好的“主机分组名”。
    (2)serive定义模板
    #service_template.cfg内容如下
    define service {
        host_name   hostname
        use generic-service
        service_description SSH
        check_command   check_ssh!22
        servicegroups   SSH
    #   notifications_enabled 0
        contact_groups  lenwood
    }
    .....还有其他很多服务的定义。

    然后就可以批量写SSH服务检测的定义了。命令如下:

    while read line;do host=`echo $line|awk '{print $1}'`;sed  "s/hostname/$host/g" service_template.cfg >$host.cfg;done</home/nagios/hosts.txt

    这就是我在使用nagios过程中的一些体会。

    -------------------------------------------------------------------------------------------------------------------------------------------------
    以上都为原创文章,转载请注明出处。

    我是Yiffy,以下是我的联系方式。
    http://www.cnblogs.com/baizhantang/
    有问题可以联系我,欢迎交流技术。

    By: Yiffy



  • 相关阅读:
    list 、set 、map 粗浅性能对比分析
    利用 Windows Azure 实现“云优先”
    利用代码改变世界 #AzureDev
    openssl 加密
    openssl 加密
    tls和ssl
    tls和ssl
    从输入框获取输入,插入到文本框
    从输入框获取输入,插入到文本框
    windows expect-5.21r1b1-setup.exe 下载链接
  • 原文地址:https://www.cnblogs.com/baizhantang/p/2797741.html
Copyright © 2020-2023  润新知