最近一直在用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基本上没有语法错误就成功了




转载于:https://blog.51cto.com/raytech/1684078

利用Nagios + CloudWatch API 监控 Amazon 实例相关推荐

  1. python批量爬取小网格区域坐标系_Python爬虫实例_利用百度地图API批量获取城市所有的POI点...

    上篇关于爬虫的文章,我们讲解了如何运用Python的requests及BeautifuiSoup模块来完成静态网页的爬取,总结过程,网页爬虫本质就两步: 1.设置请求参数(url,headers,co ...

  2. html约束验证的例子,HTML5利用约束验证API来检查表单的输入数据的代码实例

    HTML5对于表单有着极大程度的优化,无论是语义,小部件,还是数据格式的验证.我猜你肯定会以浏览器兼容作为借口不愿意使用这些"新功能",但这绝不应该成为使你停滞不前的原因,况且还有 ...

  3. 百度地图利用位置提供服务器,利用百度地图API获取当前位置信息的实例

    利用百度地图API可以做很多事情,个人感觉最核心也是最基础的就是定位功能了.这里分享一个制作的JS可以实现登录网页后定位: var map; var gpsPoint; var baiduPoint; ...

  4. python高德地图api调用实例_Python 利用高德地图api实现经纬度与地址的批量转换...

    我们都知道,可以使用高德地图api实现经纬度与地址的转换.那么,当我们有很多个地址与经纬度,需要批量转换的时候,应该怎么办呢? 在这里,选用高德Web服务的API,其中的地址/逆地址编码,可以实现经纬 ...

  5. (转)Nagios 配置及监控

    Nagios 配置及监控 原文:http://blog.csdn.net/linuxlsq/article/details/52606824 Nagios 监控 在互联网日益发展的今天,监控的重要性已 ...

  6. 用户领域 API 监控和代码注入检测

    原文地址:Userland API Monitoring and Code Injection Detection 原文作者:dtm 译文出自:掘金翻译计划 本文永久链接:github.com/xit ...

  7. WIN32:API串口通讯实例教程

    WIN32 API串口通讯实例教程   第一节   实现串口通讯的函数及串口编程简介   API函数不仅提供了打开和读写通讯端口的操作方法,还提供了名目繁多的函数以支持对串行通讯的各种操作.常用函数及 ...

  8. 【zz】免费的午餐——编程利用Google日历API发短信、Email

    免费的午餐--编程利用Google日历API发短信.Email Google日历是相当优秀的日程管理工具,可以方便的组织.分享各类日程,Google还提供了贴心而免费的手机短信.Email提示功能,真 ...

  9. 有道接口php,php有道翻译api调用方法实例,php有道api实例

    php有道翻译api调用方法实例,php有道api实例 本文实例讲述了php有道翻译api调用方法,这里我们利用了file_get_contents函数直接读取由api返回的数据进行处理,分享给大家供 ...

最新文章

  1. zz[as3 hack技术]垃圾回收机强制执行
  2. 大数据技术基础_【基础】大数据技术栈介绍
  3. 【C语言】C语言实现面向对象编程之多态
  4. 如何修改Fiori Launchpad里Tile计数调用的时间间隔
  5. Linux查看c语言组件进程,Linux下查看进程IO工具iopp
  6. Hadoop集群下进行集成测试的小策略
  7. 【java笔记】TCP通信程序
  8. bzoj 1032: [JSOI2007]祖码Zuma(区间DP)
  9. 如何用c#来制作一个activeX控件或类似的东西
  10. 企业安全管理的内外合规之ISO27001标准详解
  11. 城域网100G宽带传输设备实现客户接入链路告警
  12. latex添加代码注释_在代码中添加注释:好的,坏的和丑陋的。
  13. 将一个 iOS 设备变成 iBeacon
  14. 辽宁计算机专业大学排名及分数线,辽宁一本大学排名及分数线2021
  15. 动画制作后期那点事——调色
  16. 【硬核干货】Teleport Ultra扒站教程 (附汉化破解版资源下载链接)
  17. URI中有关@符号的一些猥琐idea
  18. Vultr新用户充值送50刀
  19. 蓝桥杯历年省赛JAVA-B组真题汇总及题目详解
  20. windows修改默认端口3389

热门文章

  1. 报错解决:usr/bin/ld: output.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when maki
  2. mysql用户的创建和授权_MySQL用户创建和授权
  3. adf的主要功能之一是_浅析高性能纤维纸基功能材料
  4. java随机产生100个大小写字母_Java生成固定长度的随机字符串(以大小写字母和数字)...
  5. java 泛型解耦,T-MVP:泛型深度解耦下的MVP大瘦身
  6. mysql中的EXPLAIN
  7. 两个形状不同的长方形周长_借助思维导图玩转小学阶段三种不同计算图形周长的方法!...
  8. python数据库pymysql_Python——数据库04 Python操作MySQL pymysql模块使用,python,04python,MySQLpymysql...
  9. 国内好用的5款软件测试管理工具
  10. ollections 库中常用的 4 个数据结构