最近一直在用AWS的EC2做些项目,发现AWS里面的所有东西都有一个叫CloudWatch的在监控。不过问题这个东西做得实在有些简单,无法类似Nagios那样。
于是在网上找了一下发现在国外有一个用Ruby做的脚本可以直接与Nagios整合。参考网站在这里:
http://maglub.github.io/nagios-cloudwatch/
其它安装文件可以在此下载:http://down.51cto.com/data/2081648
不过我的Nagios server是Centos 6.4装的,所以在安装AWS Ruby API就出现了一些麻烦。下面我简单的解释一下如何安装ruby 1.9 +aws API
· 擦除系统的自带Ruby
yum erase rubyruby-libs ruby-mode ruby-rdoc ruby-irb ruby-ri ruby-docs
安装依赖包
yum -y install zlib-devel curl-devel openssl-devel httpd-devel apr-devel apr-util-devel mysql-devel openssl-devel gcc gcc-c++ make autoconf readline-devel expat-devel gettext-devel
· 安装 Node
tar -zxfnode-v0.6.6.tar.gz
cd node-v0.6.6
./configure
make
make install
· 安装yaml
tar -zxf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure --prefix=/usr/local
make
make install
tar -zxf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure --prefix=/usr/local
make
make install
· 安装 Ruby
tar -zxfruby-1.9.3-p0.tar.gz
cd ruby-1.9.3-p0
./configure --prefix=/usr/local--enable-shared --enable-pthread --disable-install-doc--with-opt-dir=/usr/local/lib
make
make install
安装1.9版本在Centos6 会在make的时候报错,需要替换ext/openssl下的ossl_pkey_ec.c文件。
· 安装 Rails
gem install rails
· 安装 nokogiri
yum installruby-devel \
gcc make \
libxml2 libxml2-devellibxslt libxslt-devel
gem install nokogiri-- --with-xml2-lib=/usr/lib64 --with-xml2-include=/usr/include/libxml2/--with-xslt-lib=/usr/lib64 --with-xslt-include=/usr/include/libxslt/
· 安装 AWS SDK
gem install aws-sdk -v 1.64.0
注意这里的SDK必须要1.6的版本,如果是新的2.0版本ruby脚本跑不了
· 下载 Nagios-CloudWatch script
wget -O nagios-cloudwatch.latest.tar.gzhttps://github.com/maglub/nagios-cloudwatch/tarball/master
tar xvzfnagios-cloudwatch.latest.tar.gz
cp -r maglub-nagios-cloudwatch-32780a0/ /usr/local/nagios/cwruby
chown –R nagios. /usr/local/nagios/cwruby
· 配置 Nagios-CloudWatch 脚本的配置文件config.yml
输入如下类容:
aws:
#======================
#--- authentication
#======================
access_key_id: XXXXXXXXXX
secret_access_key:XXXX/XXXX/XXXXX
#========================================
#--- default region, unless overridden on thecommand line
#========================================
#--- possible regions us-west-1 us-west-2eu-west-1, etc...
region: ap-southeast-1
#======================
#--- Proxy config
#======================
#proxy_uri: http://user:passwd@IP:PORT
在这里一定要输入正确的access_key_id,secret_access_key与 region 否脚本会报错。
./check_cloudwatch.rb --ec2 --list-instances
命令输出将是所有实例的运行情况。
· 将脚本与Nagios整合
1.创建 cloud check command:
进入 /usr/local/nagios/etc/objects 目录然后编辑 commands.cfg 文件增加下面的语句:
define command {
command_name check_cloudwatch_CPUU
command_line/usr/local/nagios/cwruby/check_cloudwatch.rb --ec2 -i $ARG1$ --metric="CPUUtilization" --window=600--period=600 -w $ARG2$ -c $ARG3$
}
2.创建 cloud-server 在 templates.cfg 文件
编辑 templates.cfg 文件增加下面语句:
define host{
name cloud-server ; The name of this host template
use generic-host ; This template inherits other values from the generic-host template
check_period 24x7 ; By default, Linux hosts are checked round the clock
check_interval 3 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-cloud-alive ; Default command to check cloud hosts
notification_period 24x7 ; Linux admins hate to be woken up, so we only notify during the day
; Note that the notification_period variable is being overridden from
; the value that is inherited from the generic-host template!
notification_interval 10 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
在此注意check_command 的check-cloud-alive ,这个需要自己建立一个新的脚本去检查EC2的实例是否running,否则将报告服务器DOWN,该脚本与check_cloudwatch.rb放在同一个目录下,代码如下:
#!/bin/bash
#Written by Ray
#This Nagios plugin can be check your EC2 instance status
CLOUD_CHECK=/usr/local/nagios/cwruby/check_cloudwatch.rb
find_instance()
{
for status in "`$CLOUD_CHECK --ec2 --list-instances |awk ‘{print $4,$8}‘`"
do
a=($(echo "$status"))
len=`echo ${#a[@]}`
j=0
k=0
for((i=0;i<len;i++))
do
if [[ $i -eq 0 ]]
then
id=`echo ${a[0]}`
state=`echo ${a[1]}`
fi
j=`expr $j + 2`
k=`expr $j + 1`
id=`echo ${a[$j]}`
state=`echo ${a[$k]}`
if [[ "$id" == "$1" ]]
then
if [[ "$state" == "running" ]]
then
echo "OK - $id $state" 2>&1
return 0
else
echo "DOWN - $id $state" 2>&1
return 1
fi
fi
done
done
}
然后赋予该脚本的则行权限:
chmod +x cloud_check.sh
chown nagios. cloud_check.sh
再次编辑 commands.cfg文件添加:
define command{
command_name check-cloud-alive
command_line /usr/local/nagios/cwruby/cloud_check.sh $HOSTALIAS$
}
3.在objects 目录配置一个新的cfg文件
新的cfg文件内容如下:
define host{
use cloud-server
host_name ICG_Web_Server_TONY
alias i-06b7f3ca
address ec2-52-74-33-192.ap-southeast-1.compute.amazonaws.com
a
}
define service{
usegeneric-service
host_name Web_Server_CLOUD
service_description CPU Usage
check_command check_cloudwatch_CPUU!i-06b7f3ca!10!90
}
然后reload一下nagios基本上没有语法错误就成功了
本文出自 “技术博客” 博客,请务必保留此出处http://raytech.blog.51cto.com/7602157/1684078
利用Nagios + CloudWatch API 监控 AWS
原文地址:http://raytech.blog.51cto.com/7602157/1684078