From 2f2d9f8337c32ba5c8263bd66dfb51bcf4e1b7a4 Mon Sep 17 00:00:00 2001 From: peterneubauer Date: Mon, 23 Feb 2015 14:28:41 +0100 Subject: [PATCH 1/6] adding RDS monitoring --- bin/riemann-aws-rds-status | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 bin/riemann-aws-rds-status diff --git a/bin/riemann-aws-rds-status b/bin/riemann-aws-rds-status new file mode 100755 index 00000000..a2574047 --- /dev/null +++ b/bin/riemann-aws-rds-status @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +require 'rubygems' +require 'fog' +require 'date' +require 'time' +require 'json' + +require File.expand_path('../../lib/riemann/tools', __FILE__) + +$0 = __FILE__ # Let's not expose our AWS keys in the process list + +class Riemann::Tools::AWS + include Riemann::Tools + + opt :access_key, "AWS access key", :type => String + opt :secret_key, "Secret access key", :type => String + opt :region, "AWS region", :type => String, :default => 'eu-west-1' + opt :dbinstance_identifier, "DBInstanceIdentifier", :type => String + + def initialize + @cloudwatch = Fog::AWS::CloudWatch.new(:aws_access_key_id => opts[:access_key], + :aws_secret_access_key => opts[:secret_key], + :region => opts[:region]) + end + + def tick + time = Time.new + ['DatabaseConnections', 'FreeableMemory','FreeStorageSpace', 'NetworkReceiveThroughput','NetworkTransmitThroughput','ReadThroughput','CPUUtilization'].each do |metric| + puts metric + result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-60).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60}) + metricsResult = result.data[:body]['GetMetricStatisticsResult'] + puts JSON.dump(metricsResult) + ev = {:metric => metricsResult['Datapoints'][0]['Average'], + :service => metric, + :description => JSON.dump(metricsResult), + :state => "ok", + :ttl => 300} + + + report ev + end + end +end + +Riemann::Tools::AWS.run From ae688be6f49549ad2d52635a6225857b19e24526 Mon Sep 17 00:00:00 2001 From: peterneubauer Date: Mon, 23 Feb 2015 14:29:04 +0100 Subject: [PATCH 2/6] removing debug --- bin/riemann-aws-rds-status | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/riemann-aws-rds-status b/bin/riemann-aws-rds-status index a2574047..48f81314 100755 --- a/bin/riemann-aws-rds-status +++ b/bin/riemann-aws-rds-status @@ -26,7 +26,6 @@ class Riemann::Tools::AWS def tick time = Time.new ['DatabaseConnections', 'FreeableMemory','FreeStorageSpace', 'NetworkReceiveThroughput','NetworkTransmitThroughput','ReadThroughput','CPUUtilization'].each do |metric| - puts metric result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-60).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60}) metricsResult = result.data[:body]['GetMetricStatisticsResult'] puts JSON.dump(metricsResult) From 28fbed20935dbcce74c417fea020e2e06e88ba64 Mon Sep 17 00:00:00 2001 From: peterneubauer Date: Mon, 23 Feb 2015 14:28:41 +0100 Subject: [PATCH 3/6] adding RDS monitoring --- bin/riemann-aws-rds-status | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 bin/riemann-aws-rds-status diff --git a/bin/riemann-aws-rds-status b/bin/riemann-aws-rds-status new file mode 100755 index 00000000..a2574047 --- /dev/null +++ b/bin/riemann-aws-rds-status @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +require 'rubygems' +require 'fog' +require 'date' +require 'time' +require 'json' + +require File.expand_path('../../lib/riemann/tools', __FILE__) + +$0 = __FILE__ # Let's not expose our AWS keys in the process list + +class Riemann::Tools::AWS + include Riemann::Tools + + opt :access_key, "AWS access key", :type => String + opt :secret_key, "Secret access key", :type => String + opt :region, "AWS region", :type => String, :default => 'eu-west-1' + opt :dbinstance_identifier, "DBInstanceIdentifier", :type => String + + def initialize + @cloudwatch = Fog::AWS::CloudWatch.new(:aws_access_key_id => opts[:access_key], + :aws_secret_access_key => opts[:secret_key], + :region => opts[:region]) + end + + def tick + time = Time.new + ['DatabaseConnections', 'FreeableMemory','FreeStorageSpace', 'NetworkReceiveThroughput','NetworkTransmitThroughput','ReadThroughput','CPUUtilization'].each do |metric| + puts metric + result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-60).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60}) + metricsResult = result.data[:body]['GetMetricStatisticsResult'] + puts JSON.dump(metricsResult) + ev = {:metric => metricsResult['Datapoints'][0]['Average'], + :service => metric, + :description => JSON.dump(metricsResult), + :state => "ok", + :ttl => 300} + + + report ev + end + end +end + +Riemann::Tools::AWS.run From 1738f7769202b5c93b89a2ee69a1790423db685e Mon Sep 17 00:00:00 2001 From: peterneubauer Date: Mon, 23 Feb 2015 14:29:04 +0100 Subject: [PATCH 4/6] removing debug --- bin/riemann-aws-rds-status | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/riemann-aws-rds-status b/bin/riemann-aws-rds-status index a2574047..48f81314 100755 --- a/bin/riemann-aws-rds-status +++ b/bin/riemann-aws-rds-status @@ -26,7 +26,6 @@ class Riemann::Tools::AWS def tick time = Time.new ['DatabaseConnections', 'FreeableMemory','FreeStorageSpace', 'NetworkReceiveThroughput','NetworkTransmitThroughput','ReadThroughput','CPUUtilization'].each do |metric| - puts metric result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-60).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60}) metricsResult = result.data[:body]['GetMetricStatisticsResult'] puts JSON.dump(metricsResult) From f878f2a6efca2bf93bba1857b433aa7000076126 Mon Sep 17 00:00:00 2001 From: peterneubauer Date: Mon, 23 Feb 2015 14:34:56 +0100 Subject: [PATCH 5/6] better service description --- bin/riemann-aws-rds-status | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/riemann-aws-rds-status b/bin/riemann-aws-rds-status index 48f81314..6202c8c9 100755 --- a/bin/riemann-aws-rds-status +++ b/bin/riemann-aws-rds-status @@ -29,8 +29,9 @@ class Riemann::Tools::AWS result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-60).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60}) metricsResult = result.data[:body]['GetMetricStatisticsResult'] puts JSON.dump(metricsResult) - ev = {:metric => metricsResult['Datapoints'][0]['Average'], - :service => metric, + datapoint = metricsResult['Datapoints'][0] + ev = {:metric => datapoint['Average'], + :service => "#{metric} (#{datapoint['Unit']})", :description => JSON.dump(metricsResult), :state => "ok", :ttl => 300} From 4535521093c065752c1aab039526efade6a0b20a Mon Sep 17 00:00:00 2001 From: peterneubauer Date: Mon, 23 Feb 2015 16:34:51 +0100 Subject: [PATCH 6/6] more robust parsing --- bin/riemann-aws-rds-status | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bin/riemann-aws-rds-status b/bin/riemann-aws-rds-status index 6202c8c9..4c7da49e 100755 --- a/bin/riemann-aws-rds-status +++ b/bin/riemann-aws-rds-status @@ -25,19 +25,22 @@ class Riemann::Tools::AWS def tick time = Time.new - ['DatabaseConnections', 'FreeableMemory','FreeStorageSpace', 'NetworkReceiveThroughput','NetworkTransmitThroughput','ReadThroughput','CPUUtilization'].each do |metric| - result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-60).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60}) + ['DatabaseConnections', 'FreeableMemory', 'FreeStorageSpace', 'NetworkReceiveThroughput', 'NetworkTransmitThroughput', 'ReadThroughput', 'CPUUtilization'].each do |metric| + result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-120).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60}) metricsResult = result.data[:body]['GetMetricStatisticsResult'] puts JSON.dump(metricsResult) - datapoint = metricsResult['Datapoints'][0] - ev = {:metric => datapoint['Average'], - :service => "#{metric} (#{datapoint['Unit']})", - :description => JSON.dump(metricsResult), - :state => "ok", - :ttl => 300} + if (metricsResult['Datapoints'].length>0) + datapoint = metricsResult['Datapoints'][0] + ev = {:metric => datapoint['Average'], + :service => "#{opts[:dbinstance_identifier]}.#{metric} (#{datapoint['Unit']})", + :description => JSON.dump(metricsResult), + :state => "ok", + :ttl => 300} - report ev + report ev + end + end end end