Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ aws:
````

## Ruby
### FreeBSD 10 (ruby 2.2.5 / AWS SDK v2)
````
pkg install ruby rubygem-aws-sdk ca_root_nss git
git clone https://github.com/laurencegill/nagios-cloudwatch.git
````

### Ubuntu 14.04 LTS (ruby 1.9.3)

````
Expand Down Expand Up @@ -244,15 +250,19 @@ Examples of valid thresholds are:

You can list available metrics for your instance, your load balancer, etc, by using the --list-metrics parameter.

````
./check_cloudwatch.rb --namespace="AWS/EC2" -i <instance_id> --list-metrics
````

## Statistics window, and statistics period

The collection of AWS metrics is not done every minute. For example CPUUtilization is collected every 5 minutes. If you are asking for a window of 60 seconds and a period of 60 seconds, it is very likely that Cloudwatch will return an empty result set since there is no data to be presented for that period. This is a feature of the AWS Cloudwatch.

The workaround for this is to ask for a longer period, say 10 minutes or longer, to make sure you will get at least one metric in your result set.

````
./check_cloudwatch.rb --namespace="AWS/EC2" -i <instance_id> --metric="CPUUtilization" --window=600 --period=60
````



46 changes: 24 additions & 22 deletions check_cloudwatch.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/ruby
#!/usr/bin/env ruby

#============================================
# Script: check_ec2_meta_moniotor
# Author: Magnus Luebeck, magnus.luebeck@kmggroup.ch
Expand All @@ -25,7 +26,7 @@
# http://exchange.nagios.org/directory/Plugins/Operating-Systems/*-Virtual-Environments/Others/Check_AWS_CloudWatch_metrics/details
#============================================

%w[ rubygems getoptlong yaml aws-sdk pp time].each { |f| require f }
%w[ rubygems getoptlong yaml aws-sdk aws-sdk-core aws-sdk-resources pp time].each { |f| require f }
$stdout.sync = true

#============================================
Expand Down Expand Up @@ -281,7 +282,7 @@ def listMetrics(namespace, instance_id)

logIt("* Entering: #{thisMethod()}", DEBUG)

aws_api = AWS::CloudWatch.new()
aws_api = Aws::CloudWatch::Client.new()

case namespace
when AWS_NAMESPACE_EC2
Expand All @@ -307,7 +308,7 @@ def listMetrics(namespace, instance_id)
end

begin
metrics = aws_api.client.list_metrics({:namespace=> namespace, :dimensions =>dimensions}).data[:metrics]
metrics = aws_api.list_metrics({:namespace=> namespace, :dimensions =>dimensions}).data[:metrics]
rescue Exception => e
logIt("ERROR: - #{e.to_s}", NORMAL)
exit 1
Expand All @@ -331,10 +332,10 @@ def listMetrics(namespace, instance_id)
def listEC2Instances(noMonitoringTag, printTags)
$stderr.puts "* Entering: #{thisMethod()}" if $debug

aws_api = AWS::EC2.new()
aws_api = Aws::EC2::Client.new()

begin
response = aws_api.client.describe_instances
response = aws_api.describe_instances
rescue Exception => e
$stderr.puts "ERROR: - #{e.to_s}"
exit 1
Expand All @@ -346,7 +347,7 @@ def listEC2Instances(noMonitoringTag, printTags)
printTagArray=printTags.split(",")
end

instances = response[:reservation_set]
instances = response[:reservations]

#--- loop through all instances
instances.each do |instance|
Expand All @@ -361,14 +362,14 @@ def listEC2Instances(noMonitoringTag, printTags)

$stderr.puts curTags if $debug

instance[:instances_set].each do |curInstance|
instance[:instances].each do |curInstance|
instanceName = "nil"
noMonitoring = "nil"
instanceId = curInstance[:instance_id]
privateIpAddress = (curInstance[:private_ip_address].nil?) ? "nil" : curInstance[:private_ip_address]
availabilityZone = curInstance[:placement][:availability_zone]

curInstance[:tag_set].each do | item |
curInstance[:tags].each do | item |
case item[:key]
when 'Name'
instanceName = (item[:value].nil?) ? "nil" : item[:value].gsub(' ', '-')
Expand All @@ -387,7 +388,7 @@ def listEC2Instances(noMonitoringTag, printTags)
end

$stderr.puts curTags if $debug
printf "Name: %-35s Id: %-14s privateIp: %-18s State: %-10s Zone: %s", instanceName, instanceId, privateIpAddress, curInstance[:instance_state][:name], availabilityZone
printf "Name: %-35s Id: %-14s privateIp: %-18s State: %-10s Zone: %s", instanceName, instanceId, privateIpAddress, curInstance[:state][:name], availabilityZone

printTagArray.each do |printTag|
printf " %s: %s", printTag, (curTags[printTag] == "") ? "nil" : curTags[printTag]
Expand All @@ -403,10 +404,11 @@ def listEC2Instances(noMonitoringTag, printTags)
def listELBInstances(noMonitoringTag, printTags)
$stderr.puts "* Entering: #{thisMethod()}" if $debug

aws_api = AWS::ELB.new()
#aws_api = Aws::ELB::Client.new()
aws_api = Aws::ElasticLoadBalancing::Client.new()

begin
response = aws_api.client.describe_load_balancers
response = aws_api.describe_load_balancers
rescue Exception => e
$stderr.puts "ERROR: - #{e.to_s}"
exit 1
Expand Down Expand Up @@ -457,11 +459,11 @@ def EC2InstanceRunning(instanceId)
$stderr.puts "* Entering: #{thisMethod()}" if $debug
$stderr.puts " - Checking running state of #{instanceId}" if $debug

aws_api = AWS::EC2.new()
aws_api = Aws::EC2::Client.new()

#--- get the instance running state
begin
response = aws_api.client.describe_instances({:instance_ids => [ instanceId ]})[:reservation_set][0][:instances_set][0][:instance_state][:name]
response = aws_api.describe_instances({:instance_ids => [ instanceId ]})[:reservation_set][0][:instances_set][0][:instance_state][:name]
rescue Exception => e
$stderr.puts " - Instance id does not exist" if $debug
return "instance does not exist"
Expand Down Expand Up @@ -497,8 +499,8 @@ def getCloudwatchStatistics(namespace, metric, statistics, dimensions, window, p


begin
aws_api = AWS::CloudWatch.new()
metrics = aws_api.client.get_metric_statistics( params )
aws_api = Aws::CloudWatch::Client.new()
metrics = aws_api.get_metric_statistics( params )
rescue Exception => e
$stderr.puts "ERROR: Could not get cloudwatch stats: #{metric}"
$stderr.puts "ERROR: - #{e.to_s}"
Expand All @@ -522,7 +524,7 @@ def awsGetBilling(namespace)
$stderr.puts "* Entering: #{thisMethod()}" if $debug

#--- all billing is reported from us-east-1
AWS.config(:region=>'us-east-1')
Aws.config.update({:region=>'us-east-1'})

$stderr.puts " - Billing type: #{namespace.inspect}" if $debug
case namespace
Expand Down Expand Up @@ -884,15 +886,15 @@ def getCheckValue(statistics, output)
logIt("* Setting up namespace dimensions to #{dimensions.inspect}", DEBUG)


logIt("* AWS Config", DEBUG)
logIt("* Aws Config", DEBUG)

AWS.config(config["aws"]) unless config.nil?
Aws.config.update(config ["aws"]) unless config.nil?
#--- if --region was used
AWS.config(:region => regionOverride) unless regionOverride.to_s.empty?
Aws.config.update({:region => regionOverride}) unless regionOverride.to_s.empty?
#--- if --access_key was used
AWS.config(:access_key_id => accessKeyOverride) unless accessKeyOverride.to_s.empty?
Aws.config.update({:access_key_id => accessKeyOverride}) unless accessKeyOverride.to_s.empty?
#--- if --secret was used
AWS.config(:secret_access_key => secretKeyOverride) unless secretKeyOverride.to_s.empty?
Aws.config.update({:secret_access_key => secretKeyOverride}) unless secretKeyOverride.to_s.empty?

#--- list instances (--list-instances)
if (scriptAction == "list-instances")
Expand Down
10 changes: 5 additions & 5 deletions config.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ aws:
#======================
#--- authentication
#======================
access_key_id: YOUR_ACCESS_KEY
secret_access_key: YOUR_SECRET_KEY
:access_key_id: YOUR_ACCESS_KEY
:secret_access_key: YOUR_SECRET_KEY

#========================================
#--- default region, unless overridden on the command line
#========================================
#--- possible regions us-west-1 us-west-2 eu-west-1, etc...
region: us-west-2
:region: us-west-2

#======================
#--- Proxy config
#======================
#proxy_uri: http://user:passwd@IP:PORT
#:proxy_uri: http://user:passwd@IP:PORT

app:
#==============================================================
#--- noMonitoringTag: <string>
#--- name of the instance tag in EC2 console you want to use to
#--- exclude an instance from op5 monitoring
#==============================================================
noMonitoringTag: noMonitoring
:noMonitoringTag: noMonitoring