Skip to content

Commit

Permalink
Merge pull request #223 from riemann/erlang-ruby
Browse files Browse the repository at this point in the history
Move all extra tool classes in dedicated files
  • Loading branch information
jamtur01 authored Aug 9, 2022
2 parents d427740 + 54acc53 commit b5270be
Show file tree
Hide file tree
Showing 44 changed files with 2,094 additions and 2,224 deletions.
2 changes: 1 addition & 1 deletion tools/riemann-aws/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gemspec = Gem::Specification.new do |s|
s.add_runtime_dependency 'fog-aws', '~> 3.14', '>= 3.14.0'
s.add_runtime_dependency 'json', '~> 2.6', '>=2.6.2'

s.files = FileList['bin/*', 'LICENSE', 'README.md'].to_a
s.files = FileList['bin/*', 'lib/**/*.rb', 'LICENSE', 'README.md'].to_a
s.executables |= Dir.entries('bin/')

s.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
Expand Down
89 changes: 2 additions & 87 deletions tools/riemann-aws/bin/riemann-aws-billing
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,6 @@

Process.setproctitle($PROGRAM_NAME)

require 'riemann/tools'
require 'riemann/tools/aws/billing'

$0 = __FILE__

module Riemann
module Tools
class AWSBilling
include Riemann::Tools
require 'fog/aws'

opt :fog_credentials_file, 'Fog credentials file', type: String
opt :fog_credential, 'Fog credentials to use', type: String

opt :access_key, 'AWS access key', type: String
opt :secret_key, 'Secret access key', type: String
opt :services, 'AWS services: AmazonEC2 AmazonS3 AWSDataTransfer', type: :strings, multi: true,
default: %w[AmazonEC2 AmazonS3 AWSDataTransfer]

opt :time_start, 'Start time in seconds of the metrics period (2hrs ago default)', type: Integer,
default: 7200
opt :time_end, 'End time in seconds of the metrics period ', type: Integer, default: 60

def initialize
if options[:fog_credentials_file]
Fog.credentials_path = opts[:fog_credentials_file]
Fog.credential = opts[:fog_credential].to_sym
@cloudwatch = Fog::AWS::CloudWatch.new
else
creds = if opts.key?('secret_key') && opts.key?('access_key')
{
aws_secret_access_key: opts[:secret_key],
aws_access_key_id: opts[:access_key],
}
else
{ use_iam_profile: true }
end
@cloudwatch = Fog::AWS::CloudWatch.new(creds)
end
@start_time = (Time.now.utc - opts[:time_start]).iso8601
@end_time = (Time.now.utc - opts[:time_end]).iso8601
end

def tick
opts[:services].each do |service|
data = @cloudwatch.get_metric_statistics({
'Statistics' => ['Maximum'],
'StartTime' => @start_time,
'EndTime' => @end_time,
'Period' => 3600,
'Unit' => 'None',
'MetricName' => 'EstimatedCharges',
'Namespace' => 'AWS/Billing',
'Dimensions' => [
{
'Name' => 'ServiceName',
'Value' => service,
},
{
'Name' => 'Currency',
'Value' => 'USD',
},
],
}).body['GetMetricStatisticsResult']['Datapoints']

data.each do |metrics|
name = "AWScloudwatch.Billing.#{service}"
value = metrics['Maximum']
timestamp = metrics['Timestamp'].to_i

event = {
host: nil,
service: name,
time: timestamp,
description: "AWS Estimate Charges for #{service}",
tags: ['aws_billing'],
state: 'ok',
metric: value,
}

report event
end
end
end
end
end
end

Riemann::Tools::AWSBilling.run
Riemann::Tools::AWS::Billing.run
64 changes: 2 additions & 62 deletions tools/riemann-aws/bin/riemann-aws-rds-status
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,6 @@

Process.setproctitle($PROGRAM_NAME)

require 'riemann/tools'
require 'riemann/tools/aws/rds_status'

$0 = __FILE__ # Let's not expose our AWS keys in the process list

module Riemann
module Tools
class AWS
include Riemann::Tools
require 'fog/aws'
require 'date'
require 'time'
require 'json'

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
abort 'FATAL: specify a DB instance name, see --help for usage' unless opts[:dbinstance_identifier]
creds = if opts[:access_key] && opts[:secret_key]
{
aws_access_key_id: opts[:access_key],
aws_secret_access_key: opts[:secret_key],
}
else
{ use_iam_profile: true }
end
creds['region'] = opts[:region]
@cloudwatch = Fog::AWS::CloudWatch.new(creds)
end

def tick
time = Time.new
%w[DatabaseConnections FreeableMemory FreeStorageSpace NetworkReceiveThroughput
NetworkTransmitThroughput ReadThroughput CPUUtilization].each do |metric|
result = @cloudwatch.get_metric_statistics(
'Namespace' => 'AWS/RDS',
'MetricName' => metric.to_s,
'Statistics' => 'Average',
'Dimensions' => [{ 'Name' => 'DBInstanceIdentifier', 'Value' => opts[:dbinstance_identifier].to_s }],
'StartTime' => (time - 120).to_time.iso8601,
'EndTime' => time.to_time.iso8601, 'Period' => 60,
)
metrics_result = result.data[:body]['GetMetricStatisticsResult']
next unless metrics_result['Datapoints'].length.positive?

datapoint = metrics_result['Datapoints'][0]
ev = {
metric: datapoint['Average'],
service: "#{opts[:dbinstance_identifier]}.#{metric} (#{datapoint['Unit']})",
description: JSON.dump(metrics_result),
state: 'ok',
ttl: 300,
}

report ev
end
end
end
end
end

Riemann::Tools::AWS.run
Riemann::Tools::AWS::RDSStatus.run
46 changes: 2 additions & 44 deletions tools/riemann-aws/bin/riemann-aws-sqs-status
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,6 @@

Process.setproctitle($PROGRAM_NAME)

require 'riemann/tools'
require 'riemann/tools/aws/sqs_status'

$0 = __FILE__ # Let's not expose our AWS keys in the process list

module Riemann
module Tools
class AWS
include Riemann::Tools
require 'fog/aws'

opt :access_key, 'AWS access key', type: String
opt :secret_key, 'Secret access key', type: String
opt :region, 'AWS region', type: String, default: 'us-east-1'
opt :queue, 'SQS Queue name', type: String
def initialize
creds = if opts.key?('access_key') && opts.key?('secret_key')
{
aws_access_key_id: opts[:access_key],
aws_secret_access_key: opts[:secret_key],
}
else
{ use_iam_profile: true }
end
creds['region'] = opts[:region]
@sqs = Fog::AWS::SQS.new(creds)
response = @sqs.list_queues({ 'QueueNamePrefix' => opts[:queue] })
@queue_url = response[:body]['QueueUrls'].first
end

def tick
response = @sqs.get_queue_attributes(@queue_url, 'All')
%w[ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible].each do |attr|
msg = {
metric: response[:body]['Attributes'][attr],
service: "#{opts[:queue]} #{attr}",
state: 'ok',
}
report msg
end
end
end
end
end

Riemann::Tools::AWS.run
Riemann::Tools::AWS::SQSStatus.run
75 changes: 2 additions & 73 deletions tools/riemann-aws/bin/riemann-aws-status
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,6 @@

Process.setproctitle($PROGRAM_NAME)

require 'riemann/tools'
require 'riemann/tools/aws/status'

$0 = __FILE__ # Let's not expose our AWS keys in the process list

module Riemann
module Tools
class AWS
include Riemann::Tools
require 'fog/aws'
require 'date'

opt :fog_credentials_file, 'Fog credentials file', type: String
opt :fog_credential, 'Fog credentials to use', type: String
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 :retirement_critical, 'Number of days before retirement. Defaults to 2', default: 2
opt :event_warning, 'Number of days before event. Defaults to nil (i.e. when the event appears)', default: nil

def initialize
if options[:fog_credentials_file]
Fog.credentials_path = options[:fog_credentials_file]
Fog.credential = options[:fog_credential].to_sym
@compute = Fog::AWS::Compute.new
else
@compute = if options[:access_key] && options[:secret_key]
Fog::AWS::Compute.new({
access_key_key_id: options[:access_key],
secret_key_access_key: options[:secret_key],
region: options[:region],
})
else
Fog::AWS::Compute.new({
use_iam_profile: true,
region: options[:region],
})
end
end
end

def tick
hosts = @compute.servers.select { |s| s.state == 'running' }

hosts.each do |host, host_status|
host_status['eventsSet'].each do |event|
before, _after = %w[notBefore notAfter].map { |k| Date.parse event[k].to_s if event[k] }

ev = {
host: host,
service: 'aws_instance_status',
description: "#{event['code']}\n\nstart #{event['notBefore']}\nend #{event['notAfter']}\n\n#{event['description']}",
state: 'ok',
ttl: 300,
}

ev2 = if (event['code'] == 'instance-retirement') &&
(Date.today >= before - opts[:retirement_critical])
{ state: 'critical' }
elsif opts[:event_warning] && (Date.today >= before - opts[:event_warning])
{ state: 'warning' }
else
{ state: 'warning' }
end

report ev.merge(ev2)
end
end
end
end
end
end

Riemann::Tools::AWS.run
Riemann::Tools::AWSStatus.run
Loading

0 comments on commit b5270be

Please sign in to comment.