Class: ZabbixCloudwatch::GetCloudwatchMetric
- Inherits:
-
Object
- Object
- ZabbixCloudwatch::GetCloudwatchMetric
- Defined in:
- lib/zabbix-cloudwatch.rb
Defined Under Namespace
Classes: AwsAccessKeyMissingException, AwsSecretKeyMissingException, BadAWSAccessKeysException, DimensionArgumentMissingException, MetricnameArgumentMissingException, MonitoringTypeArgumentException, NamespaceArgumentMissingException, StatisticTypeArgumentException
Instance Attribute Summary collapse
-
#aws ⇒ Object
Returns the value of attribute aws.
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#options ⇒ Object
Returns the value of attribute options.
-
#period ⇒ Object
Returns the value of attribute period.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#statistic ⇒ Object
Returns the value of attribute statistic.
Instance Method Summary collapse
- #get_aws_options ⇒ Object
-
#initialize(options) ⇒ GetCloudwatchMetric
constructor
A new instance of GetCloudwatchMetric.
- #run! ⇒ Object
- #set_statistic ⇒ Object
- #set_time_range ⇒ Object
- #test_aws_connectivity ⇒ Object
- #time_five_minutes_ago ⇒ Object
- #time_now ⇒ Object
- #time_one_minute_ago ⇒ Object
Constructor Details
#initialize(options) ⇒ GetCloudwatchMetric
Returns a new instance of GetCloudwatchMetric.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/zabbix-cloudwatch.rb', line 17 def initialize self. = usage if .key?"help" raise NamespaceArgumentMissingException unless .key?"namespace" raise MetricnameArgumentMissingException unless .key?"metricname" raise DimensionArgumentMissingException unless .key?"dimension-name" raise DimensionArgumentMissingException unless .key?"dimension-value" self.aws = AWS::CloudWatch.new().client test_aws_connectivity set_time_range set_statistic end |
Instance Attribute Details
#aws ⇒ Object
Returns the value of attribute aws.
15 16 17 |
# File 'lib/zabbix-cloudwatch.rb', line 15 def aws @aws end |
#end_time ⇒ Object
Returns the value of attribute end_time.
15 16 17 |
# File 'lib/zabbix-cloudwatch.rb', line 15 def end_time @end_time end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/zabbix-cloudwatch.rb', line 15 def @options end |
#period ⇒ Object
Returns the value of attribute period.
15 16 17 |
# File 'lib/zabbix-cloudwatch.rb', line 15 def period @period end |
#start_time ⇒ Object
Returns the value of attribute start_time.
15 16 17 |
# File 'lib/zabbix-cloudwatch.rb', line 15 def start_time @start_time end |
#statistic ⇒ Object
Returns the value of attribute statistic.
15 16 17 |
# File 'lib/zabbix-cloudwatch.rb', line 15 def statistic @statistic end |
Instance Method Details
#get_aws_options ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/zabbix-cloudwatch.rb', line 30 def raise AwsAccessKeyMissingException unless .key?"aws-access-key" raise AwsSecretKeyMissingException unless .key?"aws-secret-key" if .key?"aws-region" && ['aws-region'] != '' region = ["aws-region"] else region = 'us-east-1' end puts .inspect {:access_key_id => ["aws-access-key"], :secret_access_key => ["aws-secret-key"], :region => region} end |
#run! ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/zabbix-cloudwatch.rb', line 89 def run! ret = aws.get_metric_statistics({ :namespace => ["namespace"], :metric_name => ["metricname"], :dimensions => [{:name => ["dimension-name"],:value => ["dimension-value"]}], :period => period, :start_time => start_time, :end_time => end_time, :statistics => [statistic]}) begin symbol = statistic.downcase.to_sym symbol = :sample_count if symbol == :samplecount puts ret[:datapoints][0][symbol] rescue exit 1 end end |
#set_statistic ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/zabbix-cloudwatch.rb', line 42 def set_statistic unless .key?"statistic" self.statistic = "Average" else if ["statistic"] =~ /Minimum|Maximum|Average|Sum|SampleCount/ self.statistic = ["statistic"] else raise StatisticTypeArgumentException, "Statistic type must be one of: Minimum, Maximum, Average, Sum, SampleCount. " end end end |
#set_time_range ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/zabbix-cloudwatch.rb', line 54 def set_time_range unless .key?"monitoring-type" detailed = false else if ["monitoring-type"] =~ /detailed|basic/ detailed = true if ["monitoring-type"] == 'detailed' detailed = false if ["monitoring-type"] == 'basic' else raise MonitoringTypeArgumentException, "Monitoring type must be either 'detailed' or 'basic'. " end end if detailed self.start_time = time_one_minute_ago self.period = 60 else self.start_time = time_five_minutes_ago self.period = 360 end self.end_time = time_now end |
#test_aws_connectivity ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/zabbix-cloudwatch.rb', line 107 def test_aws_connectivity begin aws.describe_alarms(:max_records => 1) rescue raise BadAWSAccessKeysException, <<-EOF You cannot access AWS due to one of the following reasons: - The AWS keys provided do not have access to Cloudwatch - your server is not synced with NTP - The Region setting is missing or incorrect EOF end end |
#time_five_minutes_ago ⇒ Object
84 85 86 87 |
# File 'lib/zabbix-cloudwatch.rb', line 84 def time_five_minutes_ago # Not really 5 minutes ago, but adds a bit of buffer for amazon's silliness (Time.now - (60*7+30)).utc.iso8601 end |
#time_now ⇒ Object
75 76 77 |
# File 'lib/zabbix-cloudwatch.rb', line 75 def time_now Time.now.utc.iso8601 end |
#time_one_minute_ago ⇒ Object
79 80 81 82 |
# File 'lib/zabbix-cloudwatch.rb', line 79 def time_one_minute_ago # Not really 1 minute ago, but adds a bit of buffer for amazon's silliness (Time.now - 90).utc.iso8601 end |