码迷,mamicode.com
首页 > 其他好文 > 详细

icinga实战安装部署及短信报警脚本

时间:2015-01-09 01:48:32      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:icinga nagios sms

icinga安装配置

一、icinga介绍:

    ICINGA项目是 Michael LuebbenHendrikBackerJoergLinge等人发起的,他们都是现有的Nagios项目社区委员会的成员,他们承诺,新的开源项目将完全兼容以前的Nagios应用程序及扩展功能。在新项目的网站上,他们是如此定义ICINGA的,这将是一个介于Nagios社区版和企业版间的产品。特别将致力于解决Nagios项目现在的问题,比如不能及时处理Nagios项目的bug、新功能不能及时添加等。还有在新的ICINGA项目中,将更好的实现数据库集成方面的功能,标准化第三发应用程序的接口等。
 环境:操作系统是Centos 6.4 64位版本
.软件下载
    Icingahttp://sourceforge.net/projects/icinga-cn/files/?source=navbar下载,我下载的是集成pnp4的版本,可以用Icinga收集到的数据用rrd作图并显示.另外,还需要下载对应的Plugins的包,这个是Icinga监控/检查必须用到的.没有Plugins,Icinga就是空架子

.软件安装
3.1 系统必要组件

a.编译需要用到的工具,类库     

yum install gcc glibc glibc-common gdgd-devel

yum install libjpeg libjpeg-devel libpnglibpng-devel

yum install rrdtool rrdtool-develperl-rrdtool perl perl-devel

b.LAMP套件

yum install httpd mysql mysql-server libdbilibdbi-devel libdbi-drivers libdbi-dbd-mysql php php-gd php-mysql

php组件可以根据需要自行添加需要的扩展

3.2 开始安装Icinga
a.创建Icinga用户以及用户组

/usr/sbin/groupadd icinga

/usr/sbin/useradd -m icinga

passwd icinga

/usr/sbin/usermod -a -G icinga apache

b.解压下载的icinga-pnp4-cn-1.8.4.tar.bz2,然后进入解压的目录,执行:

./configure --prefix=/usr/local/icinga--with-icinga-user=icinga --with-icinga-group=icinga --with-command-user=icinga--with-command-group=icinga --enable-idoutils--with-httpd-conf=/etc/httpd/conf.d --enable-nanosleep --enable-event-broker

configure程序会自动检测系统的环境,以及所依赖的包是否都安装上了.没有报错,执行以下操作:

make all

make install

make install-init

make install-config

make install-eventhandlers

make install-commandmode

make install-idoutils

make install-processperfdata

make install-plugins

make install-webconf 

chkconfig --add icinga

chkconfig --add ido2db

chkconfig icinga on

chkconfig ido2db on

icinga服务在下次服务器启动的时候,自动运行.以后需要启动/关闭Icinga服务的时候,可以通过service icinga [start|stop|restart]来进行,很方便.
idoutils支持mysql,PGsql,Oracle等数据库,默认支持mysql.
启动mysql服务,运行以下命令来创建icinga数据库,并给icinga数据库用户授权.

mysql -u root -p

CREATE DATABASE icinga;

GRANT USAGE ON icinga.* TO‘icinga‘@‘localhost‘

IDENTIFIED BY ‘icinga‘

WITH MAX_QUERIES_PER_HOUR 0

MAX_CONNECTIONS_PER_HOUR 0

MAX_UPDATES_PER_HOUR 0;

GRANT SELECT, INSERT, UPDATE, DELETE, DROP,CREATE VIEW, INDEX, EXECUTE

ON icinga.* TO ‘icinga‘@‘localhost‘;

FLUSH PRIVILEGES;

quit

mysql -u root -p icinga < mysql.sql

来导入数据库.
d.安装插件.
没有插件,Icinga只是一个空架子,现在我们来编译安装插件.tar解压下载得到的icinga(nagios)-cn-plugins-1.4.16.tar.bz2,然后执行以下命令.注意,文件名中的()需要用\(\)来转义.我是直接先将文件名mv更名之后,tar解压缩的.  

./configure --prefix=/usr/local/icinga --with-cgiurl=/icinga/cgi-bin --with-icinga-user=icinga --with-icinga-group=icinga

make

make install

    另外,icinga中文项目的plugins里面,新增加了一个addplugins-20130428.tar.gz,里面有很多新的check组件,然后解压,进入解压直接makeinstall就可以了.
    安装完成Icinga插件之后,就可以启动apache,ido2db,icinga服务了.
    在用浏览器访问icinga之前,用命令 添加用户web认证用户,命令会提示输入密码,并要求再次输入一次确认.
htpasswd -c /usr/local/icinga/etc/htpasswd.users icingaadmi   

 用浏览器访问了.浏览器输入http://192.168.2.18/icinga   输入htpasswd命令创建的icingaadmin用户名:icingaadmin和密码:%TGB^YHN,可以看到Icinga的界面.

三、配置实例

配置实例:icinga服务端172.31.2.18 客户端172.31.2.14 ,其他客户端与2.14一样 

服务端:/usr/local/icinga/icinga.cfg添加客户端配置文件

添加内容:cfg_file=/usr/local/icinga/etc/files/2.14.cfg

#mkdir /usr/local/icinga/etc/files/

客户端内容如下:

#vi  cfg_file=/usr/local/icinga/etc/files/2.14.cfg

define host{

       use                 linux-server           

       host_name               2.14

       alias                   mobile

       icon_image              redhat.gif

       statusmap_image         redhat.gd2

       address                172.31.2.14

       } 

define service{

       host_name                         2.14

       use                            generic-service

       service_description            Root Partition

        check_command                  check_nrpe!check_/

       }

define service{

       host_name                          2.14

       use                            generic-service

       service_description            Current Load

       check_command                  check_nrpe!check_load

       }

 

添加邮件报警通知联系人:/usr/local/icinga/etc/objects/contacts.cfg 

1.安装sendmail

 yuminstall  -y sendmail*    mailx

service sendmail restart

然后发送测试邮件,验证sendmail的可用性:

# echo "Hello World" | mailabc@abc.com 

2、配置nagios中报警邮箱

vi/usr/local/nagios/etc/objects/contacts.cfg

define contact{

       contact_name                   icingaadmin           

       use                            generic-contact        

       alias                          Icinga Admin         

       email                          icinga@localhost    

       email                           long.wang@51auto.com 

       }
3、设置报警方式:

 vi/usr/local/nagios/etc/objects/templates.cfg define contact{

name                           generic-contact       

service_notification_period     24×7                  

host_notification_period        24×7                  

service_notification_options    w,u,c,r,f,s           

host_notification_options       d,u,r,f,s              

service_notification_commands   notify-service-by-email

host_notification_commands      notify-host-by-email   

register                        0                      

客户端设置

#yum install nagios-plugins-all.x86_64

#yum install  nrpe -y

nrpe默认安装位置:

#/etc/nagios/nrpe.cfg

添加如下内容:

log_facility=daemon
pid_file=/var/run/nrpe/nrpe.pid
server_port=5666
nrpe_user=nrpe
nrpe_group=nrpe
allowed_hosts=127.0.0.1,172.0.0.0/8
dont_blame_nrpe=0
debug=0
command_timeout=60
connection_timeout=300
include_dir=/etc/nrpe.d/
command[check_users]=/usr/lib64/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib64/nagios/plugins/check_load -w 10,15,20 -c30,25,20
command[check_/]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_zombie_procs]=/usr/lib64/nagios/plugins/check_procs -w 5 -c 10 -sZ
command[check_total_procs]=/usr/lib64/nagios/plugins/check_procs -w 150 -c 200

重启:

#/etc/init.d/nrpe restart 

这样设置在web页面应该可以访问:其他客户端可结合puppet进行统一安装 

Icinga结合sms短信平台报警

1.企业短信平台:

/usr/bin/wget ‘http://sms.c8686.com/Api/BayouSmsApiEx.aspx?func=sendsms&username=xxxxxx&password=xxxxxxxxxxxxxxxxxxxxxxx&mobiles=xxxxxxxxxxx&message={$subject}‘ 

2.脚本/bin/nagios_sms.py

#cat/bin/nagios_sms.py

#!/usr/bin/envpython
# coding: utf-8
import sys
import urllib
import urllib2
"""
使用八优短信平台进行短信报警
接收参数输入
参数一:短信内容
参数二:接收人手机
使用方法
1.
单一手机
python nagios_sms.py "
报警" xxxxxxxxxx
2.
多手机支持
python nagios_sms.py "
报警"xxxxxxxxxxx,xxxxxxxxxxx
"""
def _process_keyword_coding(keyword):
    """
   
返回中文,将内容转成gbk编码
    """
    try:
        keyword = unicode(keyword,"gbk")
    except:
        keyword = keyword.decode("utf-8").encode("gbk")
    return keyword
def sendsms(message,mobiles):
    """
   
发送短信
   #http://sms.c8686.com/Api/BayouSmsApiEx.aspx?func=sendsms&username=xxxxxx&password=xxxxxxxxxxxxxxxxxxxxxx&mobiles=xxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxxxx&message={$subject}
    """
    func = ‘sendsms‘
    username = ‘xxxxxx‘
    password = ‘xxxxxxxxxxxxxxxxxxxxxxxxx‘
    #mobiles = ‘xxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxxxx‘  
    values = {‘func‘:func,
             ‘username‘:username,
             ‘password‘:password,
             ‘mobiles‘:mobiles,
             ‘message‘:message}   
    data = urllib.urlencode(values)
    post_url = ‘http://sms.c8686.com/Api/BayouSmsApiEx.aspx‘
    try:
        conn = urllib2.urlopen(post_url,data)
        print conn.read()
    except Exception , e:
        print e       
if __name__ == ‘__main__‘:
    message = sys.argv[1]
    mobiles = sys.argv[2]   
    #print "message===>",message
    #print "mobiles===>",mobiles
    #sendsms(message.encode(‘gbk‘),mobiles)
    sendsms(_process_keyword_coding(message),mobiles)
 

3.添加命令/usr/local/icinga/etc/objects/commands.cfg

添加如下内容:

 definecommand{
       command_name   notify-service-by-nagiossms
       command_line    /usr/bin/python /bin/nagios_sms.py "
通知类型:$NOTIFICATIONTYPE$     服务: $SERVICEDESC$      主机:$HOSTALIAS$                           地址: $HOSTADDRESS$    状态: $SERVICESTATE$     日期/时间: $LONGDATETIME$   额外信息: $SERVICEOUTPUT$" $CONTACTPAGER$
      }
 

4.添加联系人:/usr/local/icinga/etc/objects/contacts.cfg

内容如下:

define contact{
        contact_name              nagios1             
        use                            generic-contact        
        alias                           nagiosAdmin           
        email                          hanyun.fang@51auto.com  
        pager xxxxxxxxxxx
}

definecontactgroup{
       contactgroup_name       admins
       alias                  Icinga Administrators
       members                nagios1
        }
 

5.修改模板文件:/usr/local/icinga/etc/objects/templates.cfg

内容如下:

define contact{
       name                           generic-contact        
       service_notification_period     24x7               
       host_notification_period        24x7                  
       service_notification_options    w,u,c,r,f,s         
       host_notification_options       d,u,r,f,s             
      
 service_notification_commands  notify-service-by-email,notify-service-by-nagiossms           host_notification_commands     notify-host-by-email   
       register                       0                      
        }
 

参考文档:http://wenku.baidu.com/view/69f9a22e0066f5335a812127.html

/usr/local/icinga/bin/icinga -v   /usr/local/icinga/etc/icinga.cfg           #检查配置文件 

优化icinganginx结合icinga安装

安装开始:

1、安装FCGI模块

#yum install -y perl-devel

#wgethttp://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz

#tar xvzf FCGI-0.73.tar.gz

#cd FCGI-0.73

#perl Makefile.PL

#make

#make install

2、安装FCGI-ProcManager模块

#wgethttp://mirrors.ustc.edu.cn/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz

#tar xvzf FCGI-ProcManager-0.19.tar.gz

#cd FCGI-ProcManager-0.19

#perl Makefile.PL

#make && make install

3、安装IOIO::ALL模块

#wgethttp://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz

#tar zxvf IO-1.25.tar.gz

#cd IO-1.25

#perl Makefile.PL

#make

#make install

#wgethttp://cpan.metacpan.org/authors/id/I/IN/INGY/IO-All-0.44.tar.gz

#tar zxvf IO-All-0.41.tar.gz

#cd IO-All-0.41

#perl Makefile.PL

#make

#make install

4、下载Perl脚本

这个脚本的目的就是产生一个PERLFastCGI接口,让Nginx可以以CGI方式处理Perl

#wget http://www.chlinux.net/perl-fcgi.zip

#unzip perl-fcgi.zip

#cp perl-fcgi.pl /usr/local/nginx/       注:建议把这个脚本放在Nginx安装目录。

修改脚本权限

#chmod 755 /usr/local/nginx/perl-fcgi.pl

5、建立一个CGI启动/停止脚本

这个SHELL脚本只是为了方便管理上面的Perl脚本。脚本中的nobodynginx的运行用户,请据自己的实际情况调整。

注意事项:不能用root用户执行(会提示). 要用与Nginx相同身份的用户执行。否则可能会在Nginx Log中提示 Permision Denied

#vi /usr/local/nginx/start_perl_cgi.sh

#!/bin/bash

#set -x

dir=/usr/local/nginx/

stop ()

{

#pkill -f $dir/perl-fcgi.pl

kill $(cat $dir/logs/perl-fcgi.pid)

rm $dir/logs/perl-fcgi.pid 2>/dev/null

rm $dir/logs/perl-fcgi.sock 2>/dev/null

echo “stop perl-fcgi done”

}

start ()

{

rm $dir/now_start_perl_fcgi.sh2>/dev/null

chown www.www $dir/logs

echo “$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log-pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock”>>$dir/now_start_perl_fcgi.sh

chown www.www $dir/now_start_perl_fcgi.sh

chmod u+x $dir/now_start_perl_fcgi.sh

sudo -u nobody $dir/now_start_perl_fcgi.sh

echo “start perl-fcgi done”

}

case $1 in

stop)

stop

;;

start)

start

;;

restart)

stop

start

;;

esac

保存退出

修改SHELL脚本权限

#chmod 755/usr/local/nginx/start_perl_cgi.sh

启动脚本

#/usr/local/nginx/start_perl_cgi.sh start

正常情况下在/usr/local/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的步聚。 

6.Nginx配置文件修改:

server

  {

      listen 80;

      server_name monitor.51auto.com;

      index index.html index.htm index.php;

      root /usr/local/icinga/share;

      error_log  /tmp/error.log  info;

      auth_basic  "icingaAccess";

      auth_basic_user_file /usr/local/icinga/etc/htpasswd.users; 

       large_client_header_buffers 4 16k;

       client_max_body_size 300m;

       client_body_buffer_size 128k;

       proxy_connect_timeout 600;

       proxy_read_timeout 600;

       proxy_send_timeout 600;

       proxy_buffer_size 64k;

       proxy_buffers   32 32k;

       proxy_busy_buffers_size 128k;

       proxy_temp_file_write_size 128k; 

     location ~ .*\.(php|php5)?$

    {

   #fastcgi_pass unix:/tmp/php-cgi.sock;

    fastcgi_pass 127.0.0.1:9000;

    fastcgi_index index.php;

    include        fastcgi_params;

    include fastcgi.conf;

    }

    location ~ .*\.(cgi|pl)?$

    {gzip off;

root /usr/local/icinga/sbin;

rewrite ^/icinga/cgi-bin/(.*)\.cgi /$1.cgibreak;

fastcgi_passunix:/usr/local/nginx/logs/perl-fcgi.sock;

fastcgi_param SCRIPT_FILENAME/usr/local/icinga/sbin$fastcgi_script_name;

fastcgi_index index.cgi;

fastcgi_read_timeout 60;

fastcgi_buffers 32 8k;

fastcgi_param REMOTE_USER $remote_user;

include fastcgi.conf;

 auth_basic "icinga Access";

auth_basic_user_file/usr/local/icinga/etc/htpasswd.users;

location /icinga

{

alias /usr/local/icinga/share;

auth_basic "icinga access";

auth_basic_user_file/usr/local/icinga/etc/htpasswd.users;

}

}

重新载入nginx配置文件:

#/etc/init.d/nginx reload

重启icinga

#/etc/init.d/icinga restart

访问icinga

http://monitor.51auto.com/icinga


本文出自 “hanyun.fang” 博客,转载请与作者联系!

icinga实战安装部署及短信报警脚本

标签:icinga nagios sms

原文地址:http://hanyun.blog.51cto.com/1060170/1600830

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!