nagios 虽然是一个比较不错的监控软件,不过因为其和zabbix相比,API功能几乎就是空白,而在很多企业中仍然以nagios为主要监控工具。在大批量需要自动化监控的环境中,没有API的nagios 监控自动化被严重制肘。不过东西是死的,人是灵活的。本篇就结合一个现网环境要求,通过php写一个简单的更新配置nagios配置文件并可以自动加载配置的API 功能来实现主机的基本监控块可以实现自动添加。在大批量部署时,可以再结合puppt、cfengine、saltstack等自动化工具完成。

一、updatecfg.php文件

nagios更新配置文件的API文件:

 1<?php $spwd=$_POST["key"];
 2if ($spwd != '31d860f7-6f7f-48d3-97b3-8407d5083f34') {
 3        exit('Wrong key!');
 4}
 5$iid=$_POST["iid"];
 6$pip=$_POST["pip"];
 7$hostname=$_POST["hostname"];
 8if ($iid==""||$pip==""||$hostname=="") {
 9    echo "Some args is null!";
10    echo "iid: $iid, pip: $pip, hostname: $hostname";
11    exit;
12}
13$dir="/etc/nagios3/servers/";
14$file="${dir}/${hostname}.cfg";
15if (file_exists("${file}")) {
16    echo "Config file exist! Changing config file...";
17    $ofile=$file;
18    $wfile=$file;
19}else {
20    echo "Config file not exist! Generating config file...";
21    $ofile="${dir}/TEMPLATE";
22    $wfile=$file;
23}
24$ofp=fopen($ofile,'r');
25$ncont="";
26while (!feof($ofp))
27{
28    $buffer=fgets($ofp,4096);
29    $buffer=preg_replace("/(s+host_name)s+.*/","$1ttt$hostname",$buffer);
30    $buffer=preg_replace("/(s+alias)s+.*/","$1ttt$iid",$buffer);
31    $buffer=preg_replace("/(s+address)s+.*/","$1ttt$pip",$buffer);
32#    echo "$buffer<br/?>n";
33    $ncont.=$buffer;
34}
35fclose($ofp);
36$wfp=fopen($wfile,'w');
37fwrite($wfp,$ncont);
38fclose($wfp);
39echo exec("/etc/init.d/nagios3 reload");
40echo "Updata config file success!";
41?>

原理很简单,这里定义三个需要传入的变量---主机名、别名、IP ,并且在接受client post过来的参数值时,需要先比对一个KEY值,如果KEY值通过就通过模板文件,进行参数替换,并将新的文件保存的相应的路径。最后reload配置文件生效(reload的好处就是,既然配置文件中有错误,其照样能将解析正常的配置文件,不影响整个监控)。

二、client post脚本

这里使用的shell 脚本,内容为:

1#!/bin/bash
2key="31d860f7-6f7f-48d3-97b3-8407d5083f34"
3hostname=$(curl -s http://169.254.169.254/latest/user-data | json.sh | grep -m1 '["hostname"]' | awk '{print $2}' | sed 's/"//g')
4pip=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
5iid=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
6postdata="hostname=${hostname}&pip=${pip}&iid=${iid}&key=${key}"
7curl -u "nagios:abc123" -sk -d ${postdata} http://nagios.361way.com/nagios3/cgi-bin/updatecfg.php

注:由于我这里是AWS主机,通过上面的URL可以取到本机的三个值(实际非AWS主机,可以通过shell 语句获取本机信息) 。同样,可以通过调用ec2metadata命令取值,如下:

 1# ec2metadata
 2ami-id: ami-3b879652
 3ami-launch-index: 0
 4ami-manifest-path: (unknown)
 5ancestor-ami-ids: unavailable
 6availability-zone: us-east-1a
 7block-device-mapping: ami
 8ebs1
 9ephemeral0
10root
11instance-action: none
12instance-id: i-05b3e152
13instance-type: c3.xlarge
14local-hostname: ip-10-19-255-21.ec2.internal
15local-ipv4: 10.19.255.21
16kernel-id: aki-88aa75e1
17mac: unavailable
18profile: default-paravirtual
19product-codes: unavailable
20public-hostname: ec2-58-85-121-145.compute-1.amazonaws.com
21public-ipv4: 58.85.121.145
22//不要打我ssh 公钥的主意,IP是伪造的,key也是伪造的,嘿嘿
23public-keys: ['ssh-rsa YDUNjq8WYsDh685BSofB4v4Kq+CjsXs3QF+5lERjVjet0PBwibk/Gs0tG3oBE1+HqMtiZaOTeifehnxMQUn4RsFCLqfGy US_E_VPC']
24ramdisk-id: unavailable
25reserveration-id: unavailable
26security-groups: vpc_norules
27user-data: {"hostname":"AMZ-IAD-Zabbix-255-21"}

三、TEMPLATE文件

在updatecfg.php文件中调用的TEMPLATE文件内容如下:

 1#### host define
 2define host{
 3    use                     generic-host,host-pnp4nagios
 4        host_name               DY-HOSTNAME
 5        alias                   INSTANCEID
 6        address                 IPADDRESS
 7}
 8#### common service
 9define service{
10    use                             hour-service,service-pnp4nagios
11        host_name                       DY-HOSTNAME
12        service_description             Disk Space
13        check_command                   check_nrpe!check_disk!check_disk -w 20% -c 10%
14}
15define service{
16    use                             generic-service,service-pnp4nagios
17        host_name                       DY-HOSTNAME
18        service_description             Current Users
19        check_command                   check_nrpe!check_users!check_users -w 8 -c 10
20}
21define service{
22    use                             minute-service,service-pnp4nagios
23        host_name                       DY-HOSTNAME
24        service_description             Total Processes
25        check_command                   check_nrpe!check_procs!check_perf check_procs -w 250 -c 400
26}
27define service{
28    use                             minute-service,service-pnp4nagios
29        host_name                       DY-HOSTNAME
30        service_description             Zombie Processes
31        check_command                   check_nrpe!check_procs!check_perf check_procs -w 5 -c 10 -s Z
32}
33define service{
34    use                             minute-service,service-pnp4nagios
35        host_name                       DY-HOSTNAME
36        service_description             CF-Execd Processes
37        check_command                   check_nrpe!check_procs!check_perf check_procs -w 1: -c 1: -C cf-execd
38}
39define service{
40    use                             minute-service,service-pnp4nagios
41        host_name                       DY-HOSTNAME
42        service_description             Current Load
43        check_command                   check_nrpe!check_load!check_load -w 5.0,5.0,5.0 -c 8.0,8.0,8.0
44}
45define service{
46    use                             minute-service
47        host_name                       DY-HOSTNAME
48        service_description             SSH
49        check_command                   check_ssh
50}
51define service{
52    use                             minute-service,service-pnp4nagios
53        host_name                       DY-HOSTNAME
54        service_description             CPU Usage
55        check_command                   check_nrpe!check_cpu!check_cpu -w 80 -c 90
56}
57define service{
58    use                             minute-service,service-pnp4nagios
59        host_name                       DY-HOSTNAME
60        service_description             Open FD
61        check_command                   check_nrpe!check_open_fds!check_open_fds -W 80 -C 90
62}
63define service{
64    use                             minute-service,service-pnp4nagios
65        host_name                       DY-HOSTNAME
66        service_description             Context Switches
67        check_command                   check_nrpe!check_context_switches!check_context_switches -w 20000 -c 25000
68}
69define service{
70    use                             minute-service,service-pnp4nagios
71        host_name                       DY-HOSTNAME
72        service_description             Eth0 Traffic
73        check_command                   check_nrpe!check_iftraffic!check_iftraffic -i eth0 -b 100 -u m
74}
75define service{
76    use                             minute-service,service-pnp4nagios
77        host_name                       DY-HOSTNAME
78        service_description             Memory Usage
79        check_command                   check_nrpe!check_mem!check_mem  -w 90 -c 95
80}
81#### unique service

以上代码已上传至本人github站点上。