内网监控利器――Nagios

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

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

安装必要的包

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

建立相应的用户和用户组

useradd -m nagiospasswd nagiosgroupadd nagcmdusermod -a -G nagcmd nagiosusermod -a -G nagcmd apache

下载并解压nagios的安装包

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

开始configure

复制代码
cd nagios-3.2.3./configure --with-command-group=nagcmdmake allmakeinstallmakeinstall-initmakeinstall-configmakeinstall-commandmode
复制代码

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

安装web接口

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

安装nagios监控插件

复制代码
tar xzf nagios-plugins-1.4.11.tar.gzcd nagios-plugins-1.4.11./configure --with-nagios-user=nagios --with-nagios-group=nagiosmakemakeinstall#添加开机自启动chkconfig --add nagioschkconfig nagios on
复制代码

校验配置文件

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

二、nagios配置

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

复制代码
# 'notify-host-by-email' command definitiondefine 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 definitiondefine 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.51game_web2     10.96.19.52game_ope     10.96.19.53game_db1     10.96.19.54game_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)