Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for cpu-balance on RDS #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]
- Add check-rds-cpu_balance.rb to check cpu-balanced for RDS instance

### Added
- ruby 2.4 testing (@majormoses)

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@

**check-kms-key.rb**

**check-rds-cpu_balance.rb**

**check-rds-events.rb**

**check-rds-pending.rb**
Expand Down
99 changes: 99 additions & 0 deletions bin/check-rds-cpu_balance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#! /usr/bin/env ruby
#
# check-rds-cpu_balance
#
# DESCRIPTION:
# This plugin retrieves the value of the cpu balance for RDS instance
#
# OUTPUT:
# plain-text
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: aws-sdk
# gem: sensu-plugin
#
# USAGE:
# ./check-rds-cpu_balance -c 20
#
# NOTES:
# Based on check-ec2-cpu_balance.rb script (Shane Starcher)
#
# LICENSE:
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#

require 'sensu-plugins-aws'
require 'sensu-plugin/check/cli'
require 'aws-sdk'

class RDSCpuBalance < Sensu::Plugin::Check::CLI
include Common

option :critical,
description: 'Trigger a critical when value is below VALUE',
short: '-c VALUE',
long: '--critical VALUE',
proc: proc(&:to_f),
required: true

option :warning,
description: 'Trigger a warning when value is below VALUE',
short: '-w VALUE',
long: '--warning VALUE',
proc: proc(&:to_f)

option :aws_region,
short: '-r R',
long: '--region REGION',
description: 'AWS region',
default: 'us-east-1'

def data(instance)
client = Aws::CloudWatch::Client.new
stats = 'Average'
period = 60
resp = client.get_metric_statistics(
namespace: 'AWS/RDS',
metric_name: 'CPUCreditBalance',
dimensions: [{
name: 'DBInstanceIdentifier',
value: instance
}],
start_time: Time.now - period * 10,
end_time: Time.now,
period: period,
statistics: [stats]
)

return resp.datapoints.first.send(stats.downcase) unless resp.datapoints.first.nil?
end

def run
rds = Aws::RDS::Client.new
instances = rds.describe_db_instances

messages = "\n"
level = 0
instances.db_instances.each do |db_instance|
next unless db_instance.db_instance_class.start_with? 'db.t2.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like this broken out as a param that we can check against a regex. My motivation is to not have to rewrite the plugin when t3's come out. It should ideally take a comma separated list and generate an array of regexes to do. Minimally a single regex exposed seems like the min requirement to not make this throw away when amazon bumps families. This unblocks user when it does break without waiting for a hotfix and release.

id = db_instance.db_instance_identifier
result = data id
unless result.nil?
if result < config[:critical]
level = 2
messages << "#{id} is below critical threshold [#{result} < #{config[:critical]}]\n"
elsif config[:warning] && result < config[:warning]
level = 1 if level == 0
messages << "#{id} is below warning threshold [#{result} < #{config[:warning]}]\n"
end
end
end
ok messages if level == 0
warning messages if level == 1
critical messages if level == 2
end
end