利用Nagios调用Python程序发布报警
来自:网络
安装完进入系统更新应用 yum update 安装php及http服务器 yum install httpd php 安装nagios所需的库 yum install gcc glibc glibc-common make gd gd-devel net-snmp 安装命令行下载软件 yum install wget 安装vim文本编辑软件(个人喜好) nagios安装 yum install vim 新建用户:nagios useradd nagios 新建用户组:nagcmd groupadd nagcmd 将用户加入用户组 usermod -G nagcmd nagios usermod -G nagcmd apache 在/root目录下创建nagios文件夹 mkdir ~/nagios 进入/root/nagios文件夹 cd ~/nagios 下载nagios程序及nagios增加插件 wget [ /sourceforge/nagios/nagios-4.0.8.tar.gz]sourceforge/nagios/nagios-4.0.8.tar.gz wget [ /download/nagios-plugins-2.0.3.tar.gz]download/nagios-plugins-2.0.3.tar.gz 解压nagios增加插件程序包及增加插件 tar zxvf nagios-4.0.8.tar.gz tar zxvf nagios-plugins-2.0.3.tar.gz 进入/root/nagios/nagios-4.0.8文件夹 cd nagios-4.0.8 编译安装nagios-4.0.8 ./configure --with-command-group=nagcmd make all make install make install-init make install-commandmode make install-config 编辑nagios的contacts.cfg文件(存放设置接收报警的用户的,此布最后修改) vim /usr/local/nagios/etc/objects/contacts.cfg ################################ 华丽分割线O(∩_∩)O~ ######################## define contact{ contact_name caiyuanbo ; Short name of user use generic-contact ; Inherit default values from generic-contact template (defined above) alias caiyuanbo ; Full name of user 后续要对应微信公众账号中通讯录的用户名 email ;这里设置邮箱 pager 15000000000 ;这里是我后来添加的字段,如果要在commands.cfg配置文件中获取这个字段信息就使用 $CONTACTPAGER$ 表示(既是contact中的pager字段)。 } define contact{ contact_name luojinping use generic-contact alias luojinping email pager 13400000000 } define contactgroup{ contactgroup_name admins alias Nagios Administrators members caiyuanbo members luojinping } ################################ 华丽分割线O(∩_∩)O~ ################### make install-webconf 设置nagios登录用户nagiosadmin的密码 htpasswd -s -c /usr/local/nagios/etc/htpasswd.users nagiosadmin 注:关于nagios监控-多用户管理,以下文章不错 liqfyiyi/article/details/8778671 进入/root/nagios/nagios-plugins-2.0.3文件夹 cd ~/nagios/nagios-plugins-2.0.3 安装nagios增加插件 make make install 使用下面的命令核对nagios配置文件是否有错误,最好每次修改nagios配置文件都测试下。 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg chkconfig --add nagios chkconfig --level 35 nagios on systemctl start nagios.service 注:关于nagios在Centos7.0上的安装方法我参照的是下面这个链接的文章(洋文的) articles/vYFv2i7 启用http服务 systemctl start httpd.service 设置httpd开机启动 chkconfig httpd on systemctl enable httpd 修改Centos 7的防火墙,使其http服务可被访问。 查看防火墙所在网络区域,默认为public firewall-cmd --get-default-zone 查看public所允许访问的服务 firewall-cmd --zone=public --list-services 添加运行访问http服务 firewall-cmd --permanent --zone=public --add-service=http 删除不需要的服务(可选) firewall-cmd --permanent --zone=public --remove-service=dhcpv6-client 重新加载防火墙 firewall-cmd --reload 查看public所允许访问的服务,验证是否修改成功 firewall-cmd --zone=public --list-services 配置sendEmail发送nagios邮件报警 到官网下载sendEmail安装包 menu/Software/SendEmail/ 解压sendEmail程序包(这里下载后放在我统一/root目录下) cd /root tar zxvf sendEmail-v1.56.tar.gz 进入解压出来后的目录 cd sendEmail-v1.56 复制sendEmail程序至/usr/local/bin cp sendEmail /usr/local/bin 查看/usr/local/bin/sendEmail的权限,是否普通用户可读可执行 ll /usr/local/bin/sendEmail 这里可尝试用sendEmail发送邮件 /usr/local/bin/sendEmail -f -t -s smtp.126.com -u "这里设置主题" -xu fasongzhe -xp fasongzhedemima 解释: -f 表示发送者的邮箱 -t 表示接收者的邮箱 -s 表示SMTP服务器的域名或者ip -u 表示邮件的主题 -xu 表示SMTP验证的用户名 -xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别) -m 表示邮件的内容 如果你不带-m参数的话,就会提示你自行输入 Reading message body from STDIN because the ‘-m’ option was not used. If you are manually typing in a message: - First line must be received within 60 seconds. - End manual input with a CTRL-D on its own line 输入完成后使用CTRL-D来结束 既然nagios要使用sendEmail来发警告邮件,那么就要修改commands.cfg中关于发邮件的命令的定义,我们现在来修改notify-by-email这个命令,如下 ################################ 华丽分割线O(∩_∩)O~ #################### # 'notify-host-by-email' command definition define command{ command_name notify-host-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f -t $CONTACTEMAIL$ -s smtp.126.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTNAME$ $HOSTADDRESS$ is $HOSTSTATE$ **" -xu fasongzhe -xp fasongzhedemima } # 'notify-service-by-email' command definition define command{ command_name notify-service-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\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 -f -t $CONTACTEMAIL$ -s smtp.126.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$ $HOSTADDRESS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu fasongzhe -xp fasongzhedemima } ################################ 华丽分割线O(∩_∩)O~ ######################## 修改templates.cfg文件,修改报警方式为邮件报警 vim /usr/local/nagios/etc/objects/templates.cfg ################################ 华丽分割线O(∩_∩)O~ ############################# define contact{ name generic-contact ; The name of this contact template service_notification_period 24x7 ; service notifications can be sent anytime 开源监控软件 host_notification_period 24x7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events service_notification_commands notify-service-by-email ; send service notifications via email host_notification_commands notify-host-by-email ; send host notifications via email register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE! } ################################ 华丽分割线O(∩_∩)O~ ########################### 注:关于邮件报警配置我参考的是以下这个链接的文章 665033/381310/ 微信报警配置 首先注册个微信公共平台账号 1、基本信息---->2、邮箱激活 [ /cgi-bin/readtemplate?t=register/step1_tmpl&lang=zh_CN]cgi-bin/readtemplate?t=register/step1_tmpl&lang=zh_CN ---->3、选择类型 选择“企业号” ---->4、信息登记---->5、公众号信息 (注:“我选的是“其他组织”,可以在百度查下自己公司的“组织机构代码”) 登录微信公共平台进行配置 打开通讯录添加成员 打开应用中心添加应用 查看并记住发送报警使用的应用的ID 点击设置号自己的账号信息,点击“权限管理”右边的“管理”按键进入权限管理页面,新建管理组(名字自己定) 根据添加管理员(内部) 根据实际情况修改“通讯录权限”、“应用权限”、“敏感接口权限”的配置。 在该页面最下方有个开发者凭据,这个是客户端与服务器建立连接需要用到的。 简单的使用nagios发微信报警主要用到微信API以下两个格动作: 1、建立连接:获取AccessToken; 2、发送消息。 可在微信企业号接口调试工具的页面测试下。 1、发送请求地址,获取AccessToken 2、发送请求地址,发送消息 其中的body部分可以这样定义: { "touser": "这里设置公众平台通讯录中的用户名,可用@all代表所有用户", "msgtype": "text", "agentid": "0", "text": { "content": "发送的内容,稍后会用python发送请求地址,暂不能发送中文" }, "safe":"0" } 详细接口信息可参考微信官方接口文档介绍 [ /wiki/index.php?title=%E9%A6%96%E9%A1%B5]wiki/index.php?title=%E9%A6%96%E9%A1%B5 安装Python-3.4 科普下: 有些人在linux下使用python发送微信连接时会报urllib2.URLError: <urlopen error unknown url type: https>这个错误,是因为python没有SSL模块,需重新编译安装python。步骤如下 安装openssl与openssl-devel包 yum install openssl yum install openssl-devel 到官网下载Python3的最新版 [ /ftp/python/3.4.3/Python-3.4.3.tgz]ftp/python/3.4.3/Python-3.4.3.tgz 解压Python安装包(我默认放在/root下) cd /root tar xzvf Python-3.4.3.tgz 进入python源代码文件夹,进入Modules文件夹,修改Setup.dist,使其支持发送https请求。 nagios配置 cd /root/Python-3.4.3/Modules
vim Setup.dist 将 # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: #SSL=/usr/local/ssl #_ssl _ssl.c \ # -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ # -L$(SSL)/lib -lssl -lcrypto 修改为 # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto 编译安装,设置安装目录为/usr/local/python-3.4,以免与系统已安装的python2冲突。 ./configure --prefix=/usr/local/python-3.4 make make install #############################这部分有兴趣可以弄,可忽略################### 为方便后续测试可在PATH中添加python-3.4的运行路径,以后在终端可直接输入命令python3.4执行文件。 vim /etc/profile 在文档最后,添加: export PATH="/usr/local/python-3.4/bin:$PATH" 保存,退出,然后运行 source /etc/profile 可验证是否修改无误。 ################################ 华丽分割线O(∩_∩)O~ ######################### 安装完成后开始编辑python脚本调用微信公众平台。 我将编辑好的脚本放在/usr/local/nagios/python目录下 mkdir /usr/local/nagios/python cd /usr/local/nagios/python 编辑主机报警调用脚本 vim notify-host-by-weixin.py 代码如下: ################################ 华丽分割线O(∩_∩)O~ ######################### import urllib.request import json import sys #以上是导入模块 #创建获取AccessToken的方法 def gettoken(corp_id,corp_secret): gettoken_url = 'cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret try: token_file = urllib.request.urlopen(gettoken_url) except urllib.error.HTTPError as e: print(e.code) print(e.read().decode("utf8")) token_data = token_file.read().decode('utf-8') token_json = json.loads(token_data) token_json.keys() token = token_json['access_token'] return token #这里是发送消息的方法 def senddata(access_token,notify_str): send_url = 'cgi-bin/message/send?access_token=' + access_token #我传入的参数是一段字符串每个信息用separator连起来,只要再用字符串的split("separator")方法分开信息就可以了。 notifydata = notify_str.split("separator") user = notifydata[0] cationtype = notifydata[1] name = notifydata[2] state = notifydata[3] address = notifydata[4] output = notifydata[5] datatime = notifydata[6] content = '[擦汗[擦汗 Nagios警报 [擦汗[擦汗 \n\n类型: ' + cationtype + '\n主机名: ' + name + '\n状态: ' + state + '\nIP地址: ' + address + '\n[猪头日志: ' + output + '\n\n[瓢虫时间: ' + datatime + '\n' send_values = { "touser":user, "msgtype":"text", "agentid":"0", "text":{ "content":content }, "safe":"0" } send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8') #设置为非ascii解析,使其支持中文 send_request = urllib.request.Request(send_url, send_data) response = urllib.request.urlopen(send_request) #这个是返回微信公共平台的信息,调试时比较有用 msg = response.read() return msg default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) #我编辑的脚本是要获取nagios传入的一段参数的(字符串),下面这条代码是获取执行脚本后获取的第一个参数(经测试nagios只能传入一个参数进python,所以把所有包括用户名跟报警主机报警信息放进一个字符串里) notifystr = str(sys.argv[1]) corpid = '这里输入你的微信公众平台corpid' corpsecret = '这里输入你的微信公众平台corpsecret' accesstoken = gettoken(corpid,corpsecret) msg = senddata(accesstoken,notifystr) print(msg) ################################ 华丽分割线O(∩_∩)O~ ############################ 编辑服务报警调用脚本 vim notify-service-by-weixin.py 代码如下: ################################ 华丽分割线O(∩_∩)O~ ######################### import urllib.request import json import sys def gettoken(corp_id,corp_secret): gettoken_url = 'cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret try: token_file = urllib.request.urlopen(gettoken_url) 监控软件 except urllib.error.HTTPError as e:
print(e.code) print(e.read().decode("utf8")) token_data = token_file.read().decode('utf-8') token_json = json.loads(token_data) token_json.keys() token = token_json['access_token'] return token def senddata(access_token,notify_str): send_url = 'cgi-bin/message/send?access_token=' + access_token notifydata = notify_str.split("separator") user = notifydata[0] cationtype = notifydata[1] desc = notifydata[2] alias = notifydata[3] address = notifydata[4] state = notifydata[5] datatime = notifydata[6] output = notifydata[7] content ='[擦汗][擦汗] Nagios警报 [擦汗][擦汗] \n\n类型: ' + cationtype + '\n\n服务名: ' + desc + '\n主机名: ' + alias + '\nIP地址: ' + address + '\n状态: ' + state + '\n\n[瓢虫时间: ' + datatime + '\n\n[猪头日志:\n\n' + output + '\n' send_values = { "touser":user, "msgtype":"text", "agentid":"0", "text":{ "content":content }, "safe":"0" } send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8') send_request = urllib.request.Request(send_url, send_data) response = urllib.request.urlopen(send_request) msg = response.read() return msg default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) notifystr = str(sys.argv[1]) corpid = '这里输入你的微信公众平台corpid' corpsecret = '这里输入你的微信公众平台corpsecret' accesstoken = gettoken(corpid,corpsecret) msg = senddata(accesstoken,notifystr) print(msg) ################################ 华丽分割线O(∩_∩)O~ ############################# 编辑好python程序后可以使用以下命令测试,主机报警示例(将下面命令中文改为英文): /usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/notify-host-by-weixin.py "微信通讯录中的用户名separator时间标题separator主机名separator主机状态separator主机地址separator主机输出信息separator时间" 成功后会有 b'{"errcode":0,"errmsg":"ok"}' 的提示。 接下来开始对接nagios报警 然后定义发送微信的命令,修改commands.cfg文件,定义主机报警命令notify-host-by-weixin,及服务报警命令notify-service-by-weixin。 vim /usr/local/nagios/etc/objects/commands.cfg 在文档最后面添加一下信息即可。 ############################################################################## # weixin # ############################################################################## define command{ command_name notify-host-by-weixin command_line /usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/notify-host-by-weixin.py "$CONTACTALIAS$separator$NOTIFICATIONTYPE$separator$HOSTNAME$separator$HOSTSTATE$separator$HOSTADDRESS$separator$HOSTOUTPUT$separator$LONGDATETIME$" } define command{ command_name notify-service-by-weixin command_line /usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/notify-service-by-weixin.py "$CONTACTALIAS$separator$NOTIFICATIONTYPE$separator$SERVICEDESC$separator$HOSTALIAS$separator$HOSTADDRESS$separator$SERVICESTATE$separator$LONGDATETIME$separator$SERVICEOUTPUT$" } 注:这部分我参考了这个网页中的部分汉化资料,还是比较有用的,介绍配置文件很详细,可以去看看。 mchina/archive/2013/02/20/2883404.html nagios实施
修改templates.cfg文件,添加微信报警(这里我删除了邮件报警,因为没必要了,要添加也可以,在邮件报警后面添加个逗号和微信报警的命令就行了) vim /usr/local/nagios/etc/objects/templates.cfg define contact{ name generic-contact ; The name of this contact template service_notification_period 24x7 ; service notifications can be sent anytime host_notification_period 24x7 ; host notifications can be sent anytime service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events service_notification_commands notify-service-by-weixin host_notification_commands notify-host-by-weixin register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE! } 修改templates.cfg文件添加监控设备类别的模板 # Define a template for switches that we can reuse define host{ name generic-switch ; The name of this host template use generic-host ; Inherit default values from the generic-host template check_period 24x7 ; By default, switches are monitored round the clock check_interval 1 ; Switches are checked every 5 minutes 检测设备的时间间隔 retry_interval 1 ; Schedule host check retries at 1 minute intervals 确认是否故障再次检测的时间间隔 max_check_attempts 1 ; Check each switch 10 times (max) check_command check-host-alive ; Default command to check if routers are "alive" notification_period 24x7 ; Send notifications at any time notification_interval 30 ; Resend notifications every 30 minutes 设置为0表示只报一次警,这里设置为30分钟报一次 notification_options d,r ; Only send notifications for specific host states contact_groups admins ; Notifications get sent to the admins by default register 0 ; DONT REGISTER THIS - ITS JUST A TEMPLATE } #下面是在添加汇聚交换机模板,继承了generic-switch,这个是自己定义的,也可以使用默认的例如generic-switch # Define Convergence_Layer_Switches define host{ name Convergence_Layer_Switches #新建模板的名字 use generic-switch #继承默认的generic-switch模板 #这里是定义map中绘制的拓扑中显示的图标,可以自己定义。然后存放在/usr/local/nagios/share/images/logos目录 } # Define Access_Layer_Switches define host{ name Access_Layer_Switches use generic-switch statusmap_image Access_Layer_Switches.png nagios培训 }
注:关于MAP绘图我参考的是这个链接 nagios-map/2457.html 修改nagios.cfg修改主机配置文件的读取目录,使用额外的文件保存主机信息(方便管理)。 vim /usr/local/nagios/etc/nagios.cfg # You can specify individual object config files as shown below: cfg_file=/usr/local/nagios/etc/objects/commands.cfg cfg_file=/usr/local/nagios/etc/objects/contacts.cfg cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg cfg_file=/usr/local/nagios/etc/objects/templates.cfg # Definitions for monitoring the local (Linux) host #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #在这里我将默认的本机检测关掉取消了。 # Definitions for monitoring a Windows machine #cfg_file=/usr/local/nagios/etc/objects/windows.cfg # Definitions for monitoring a router/switch #cfg_file=/usr/local/nagios/etc/objects/switch.cfg # Definitions for monitoring a network printer #cfg_file=/usr/local/nagios/etc/objects/printer.cfg # You can also tell Nagios to process all config files (with a .cfg # extension) in a particular directory by using the cfg_dir # directive as shown below: #cfg_dir=/usr/local/nagios/etc/servers #cfg_dir=/usr/local/nagios/etc/printers #cfg_dir=/usr/local/nagios/etc/switches #cfg_dir=/usr/local/nagios/etc/routers cfg_dir=/usr/local/nagios/etc/campus/south #添加了一个读取配置文件的目录,想放哪自己决定,以后将主机的信息放在/usr/local/nagios/etc/campus/south这个目录便可读取。 创建文件夹/usr/local/nagios/etc/campus/south mkdir /usr/local/nagios/etc/campus mkdir /usr/local/nagios/etc/campus/south 创建主机配置文件(名字随便取) cd /usr/local/nagios/etc/campus/south vim switches.cfg #定义主机,我下面监控的是汇聚交换机 define host{ use Convergence_Layer_Switches #是使用的templates.cfg中的Convergence_Layer_Switches(这个是我自己定义的) host_name S5-103_1F-RG-3760-68 #主机名 alias 192.168.1.202 #别名 address 192.168.1.222 #IP地址 hostgroups S-103 #隶属于哪个主机组 } #定义主机,我下面监控的是连接汇聚交换机的接入层交换机 define host{ use Access_Layer_Switches host_name S_103_2F_RG_S2952A alias 192.168.1.3 address 192.168.1.3 parents S5-103_1F-RG-3760-68 #上一级节点名字,填写清楚,在nagios的map页面的绘图才会清晰显示结构 hostgroups S-103 } define host{ use Access_Layer_Switches host_name S_103_3F_RG_S2952A alias 192.168.1.4 address 192.168.1.4 parents S5-103_1F-RG-3760-68 hostgroups S-103 } #创建主机组 define hostgroup{ hostgroup_name S-103 #主机组名 alias S-103-PeiXunGongYu #主机组别名 } #定义监控方式这里是ping define service{ use generic-service hostgroup_name S-103 service_description PING check_command check_ping!200.0,20%!600.0,60% normal_check_interval 5 retry_check_interval 1 } 核对nagios配置文件是否有错误 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 没有报错就重启nagios服务 systemctl start nagios.service 效果图:
想做Nagios, Zabbix,Cacti,iTop各种交流的,可以进入开源监控工具Nagios交流 QQ群号 :476809427