Last active
April 3, 2018 07:07
-
-
Save naga41/5652659 to your computer and use it in GitHub Desktop.
Script for registering aws billing data to zabbix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/usr/bin/env ruby | |
require 'aws-sdk' | |
@cw_client = AWS::CloudWatch.new( | |
:access_key_id => "YOUR_ACCESS_KEY" | |
:secret_access_key => "YOUR_SECRET_KEY", | |
:cloud_watch_endpoint => 'monitoring.us-east-1.amazonaws.com', | |
).client | |
def get_billing(dimentions) | |
statistics = @cw_client.get_metric_statistics({ | |
:namespace => 'AWS/Billing', | |
:metric_name => 'EstimatedCharges', | |
:statistics => ['Maximum'], | |
:dimensions => dimentions, | |
:start_time => (Time.now - 4 * 60 * 60).iso8601, | |
:end_time => Time.now.iso8601, | |
:period => 60, | |
}) | |
billing_data = <<-EOS | |
#{dimentions.first[:value]} \ | |
#{statistics[:datapoints].first[:timestamp].to_i} \ | |
#{statistics[:datapoints].first[:maximum]} | |
EOS | |
return billing_data | |
rescue | |
return "#{dimentions.first[:value]} #{(Time.now - 4 * 60 * 60).to_i} 0.0\n" | |
end | |
### Main | |
ZABBIX_SERVER_URL = "YOUR_ZABBIX_SERVER_URL" | |
ZABBIX_SERVER_PORT = 10051 | |
TARGET_SERVER_NAME = "YOUR_TARGET_SERVER_NAME" | |
ZABBIX_SENDER_PATH = "/usr/bin/zabbix_sender" | |
# extract billing dimentions | |
billing_metrics = @cw_client.list_metrics(:namespace => 'AWS/Billing')[:metrics] | |
billing_dimentions = billing_metrics.map { |metrics| metrics[:dimensions] } | |
# format to zabbix sender style | |
send_data = billing_dimentions.map {|dimention| "#{TARGET_SERVER_NAME} #{get_billing(dimention)}"} | |
formated_send_data = send_data.join("\s") | |
system("echo \"#{formated_send_data}\"") | |
system("echo \"#{formated_send_data}\" | #{ZABBIX_SENDER_PATH} -vv -z #{ZABBIX_SERVER_URL} -p #{ZABBIX_SERVER_PORT} -T -i -") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment