sensu-plugins-aws-boutetnico 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE +22 -0
  4. data/README.md +333 -0
  5. data/bin/check-alb-target-group-health.rb +100 -0
  6. data/bin/check-asg-instances-created.rb +129 -0
  7. data/bin/check-asg-instances-inservice.rb +109 -0
  8. data/bin/check-autoscaling-cpucredits.rb +160 -0
  9. data/bin/check-beanstalk-elb-metric.rb +123 -0
  10. data/bin/check-beanstalk-health.rb +123 -0
  11. data/bin/check-certificate-expiry.rb +123 -0
  12. data/bin/check-cloudfront-tag.rb +70 -0
  13. data/bin/check-cloudwatch-alarm.rb +102 -0
  14. data/bin/check-cloudwatch-alarms.rb +89 -0
  15. data/bin/check-cloudwatch-composite-metric.rb +199 -0
  16. data/bin/check-cloudwatch-metric.rb +123 -0
  17. data/bin/check-configservice-rules.rb +76 -0
  18. data/bin/check-direct-connect-virtual-interfaces.rb +84 -0
  19. data/bin/check-dynamodb-capacity.rb +194 -0
  20. data/bin/check-dynamodb-throttle.rb +188 -0
  21. data/bin/check-ebs-burst-limit.rb +143 -0
  22. data/bin/check-ebs-snapshots.rb +104 -0
  23. data/bin/check-ec2-cpu_balance.rb +139 -0
  24. data/bin/check-ec2-filter.rb +190 -0
  25. data/bin/check-ec2-network.rb +133 -0
  26. data/bin/check-ecs-service-health.rb +155 -0
  27. data/bin/check-efs-metric.rb +145 -0
  28. data/bin/check-eip-allocation.rb +64 -0
  29. data/bin/check-elasticache-failover.rb +113 -0
  30. data/bin/check-elb-certs.rb +132 -0
  31. data/bin/check-elb-health-fog.rb +114 -0
  32. data/bin/check-elb-health-sdk.rb +176 -0
  33. data/bin/check-elb-health.rb +116 -0
  34. data/bin/check-elb-instances-inservice.rb +103 -0
  35. data/bin/check-elb-latency.rb +166 -0
  36. data/bin/check-elb-nodes.rb +133 -0
  37. data/bin/check-elb-sum-requests.rb +157 -0
  38. data/bin/check-emr-cluster.rb +144 -0
  39. data/bin/check-emr-steps.rb +90 -0
  40. data/bin/check-eni-status.rb +110 -0
  41. data/bin/check-expiring-reservations.rb +117 -0
  42. data/bin/check-instance-events.rb +154 -0
  43. data/bin/check-instance-health.rb +108 -0
  44. data/bin/check-instance-reachability.rb +107 -0
  45. data/bin/check-instances-count.rb +94 -0
  46. data/bin/check-kms-key.rb +73 -0
  47. data/bin/check-rds-events.rb +141 -0
  48. data/bin/check-rds-pending.rb +91 -0
  49. data/bin/check-rds.rb +382 -0
  50. data/bin/check-redshift-events.rb +108 -0
  51. data/bin/check-reserved-instances.rb +80 -0
  52. data/bin/check-route.rb +122 -0
  53. data/bin/check-route53-domain-expiration.rb +78 -0
  54. data/bin/check-s3-bucket-visibility.rb +176 -0
  55. data/bin/check-s3-bucket.rb +86 -0
  56. data/bin/check-s3-object.rb +205 -0
  57. data/bin/check-s3-tag.rb +70 -0
  58. data/bin/check-sensu-client.rb +184 -0
  59. data/bin/check-ses-limit.rb +89 -0
  60. data/bin/check-ses-statistics.rb +149 -0
  61. data/bin/check-sns-subscriptions.rb +52 -0
  62. data/bin/check-sqs-messages.rb +168 -0
  63. data/bin/check-subnet-ip-consumption.rb +234 -0
  64. data/bin/check-trustedadvisor-service-limits.rb +90 -0
  65. data/bin/check-vpc-nameservers.rb +87 -0
  66. data/bin/check-vpc-vpn.rb +98 -0
  67. data/bin/handler-ec2_node.rb +241 -0
  68. data/bin/handler-scale-asg-down.rb +131 -0
  69. data/bin/handler-scale-asg-up.rb +131 -0
  70. data/bin/handler-ses.rb +107 -0
  71. data/bin/handler-sns.rb +64 -0
  72. data/bin/metrics-asg.rb +156 -0
  73. data/bin/metrics-autoscaling-instance-count.rb +101 -0
  74. data/bin/metrics-billing.rb +97 -0
  75. data/bin/metrics-cloudfront.rb +159 -0
  76. data/bin/metrics-ec2-count.rb +137 -0
  77. data/bin/metrics-ec2-filter.rb +97 -0
  78. data/bin/metrics-elasticache.rb +166 -0
  79. data/bin/metrics-elb.rb +169 -0
  80. data/bin/metrics-emr-steps.rb +82 -0
  81. data/bin/metrics-rds.rb +153 -0
  82. data/bin/metrics-reservation-utilization.rb +84 -0
  83. data/bin/metrics-s3.rb +107 -0
  84. data/bin/metrics-ses.rb +62 -0
  85. data/bin/metrics-sqs.rb +98 -0
  86. data/bin/metrics-waf.rb +111 -0
  87. data/lib/sensu-plugins-aws.rb +4 -0
  88. data/lib/sensu-plugins-aws/cloudwatch-common.rb +92 -0
  89. data/lib/sensu-plugins-aws/common.rb +35 -0
  90. data/lib/sensu-plugins-aws/filter.rb +47 -0
  91. data/lib/sensu-plugins-aws/version.rb +8 -0
  92. metadata +456 -0
@@ -0,0 +1,104 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # check-ebs-snapshots
4
+ #
5
+ # DESCRIPTION:
6
+ # Check EC2 Attached Volumes for Snapshots. Only for Volumes with a Name tag.
7
+ #
8
+ # OUTPUT:
9
+ # plain-text
10
+ #
11
+ # PLATFORMS:
12
+ # Linux
13
+ #
14
+ # DEPENDENCIES:
15
+ # gem: aws-sdk
16
+ # gem: sensu-plugin
17
+ #
18
+ # USAGE:
19
+ # ./check-ebs-snapshots.rb -r ${you_region}
20
+ # ./check-ebs-snapshots.rb -r ${you_region} -p 1
21
+ # ./check-ebs-snapshots.rb -r ${you_region} -p -i
22
+ # ./check-ebs-snapshots.rb -r ${you_region} -i
23
+ #
24
+ # NOTES:
25
+ # When using -i flag any volume that has a tag-key of "IGNORE_BACKUP" will
26
+ # be ignored.
27
+ #
28
+ # LICENSE:
29
+ # Shane Starcher <shane.starcher@gmail.com>
30
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
31
+ # for details.
32
+ #
33
+
34
+ require 'sensu-plugin/check/cli'
35
+ require 'sensu-plugins-aws'
36
+ require 'aws-sdk'
37
+
38
+ class CheckEbsSnapshots < Sensu::Plugin::Check::CLI
39
+ include Common
40
+ option :period,
41
+ short: '-p N',
42
+ long: '--period Days',
43
+ default: 7,
44
+ description: 'Length in time to alert on missing snapshots'
45
+
46
+ option :aws_region,
47
+ short: '-r R',
48
+ long: '--region REGION',
49
+ description: 'AWS region',
50
+ default: 'us-east-1'
51
+
52
+ option :check_ignored,
53
+ short: '-i',
54
+ long: '--ignore',
55
+ description: 'mark as true to ignore volumes with an IGNORE_BACKUP tag',
56
+ default: false,
57
+ boolean: true
58
+
59
+ def run
60
+ errors = []
61
+ @ec2 = Aws::EC2::Client.new
62
+
63
+ volumes = @ec2.describe_volumes(
64
+ filters: [
65
+ {
66
+ name: 'attachment.status',
67
+ values: ['attached']
68
+ },
69
+ {
70
+ name: 'tag-key',
71
+ values: ['Name']
72
+ }
73
+ ]
74
+ )
75
+ volumes[:volumes].each do |volume|
76
+ tags = volume[:tags].map { |a| Hash[*a] }.reduce(:merge) || {}
77
+ next if config[:check_ignored] && tags.key?('IGNORE_BACKUP')
78
+ snapshots = @ec2.describe_snapshots(
79
+ filters: [
80
+ {
81
+ name: 'volume-id',
82
+ values: [volume[:volume_id]]
83
+ }
84
+ ]
85
+ )
86
+
87
+ sorted_times = snapshots[:snapshots].sort_by { |i| i[:start_time].to_i }
88
+ if !sorted_times.empty?
89
+ latest_snapshot = sorted_times[-1][:start_time]
90
+ if (Date.today - config[:period].to_i).to_time > latest_snapshot
91
+ errors << "#{tags['Name']} latest snapshot is #{latest_snapshot} for #{volume[:volume_id]}"
92
+ end
93
+ else
94
+ errors << " #{tags['Name']} has no snapshots for #{volume[:volume_id]}"
95
+ end
96
+ end
97
+
98
+ if errors.empty?
99
+ ok
100
+ else
101
+ warning errors.join("\n")
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,139 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # check-ec2-cpu_balance
4
+ #
5
+ # DESCRIPTION:
6
+ # This plugin retrieves the value of the cpu balance for all servers
7
+ #
8
+ # OUTPUT:
9
+ # plain-text
10
+ #
11
+ # PLATFORMS:
12
+ # Linux
13
+ #
14
+ # DEPENDENCIES:
15
+ # gem: aws-sdk
16
+ # gem: sensu-plugin
17
+ #
18
+ # USAGE:
19
+ # ./check-ec2-cpu_balance -c 20
20
+ # ./check-ec2-cpu_balance -w 25 -c 20
21
+ # ./check-ec2-cpu_balance -c 20 -t 'Name'
22
+ # ./check-ec2-cpu_balance -c 20 -t 'Name' -F "{name:tag-value,values:[infrastructure]}"
23
+ #
24
+ # NOTES:
25
+ #
26
+ # LICENSE:
27
+ # Shane Starcher
28
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
29
+ # for details.
30
+ #
31
+
32
+ require 'sensu-plugins-aws'
33
+ require 'sensu-plugin/check/cli'
34
+ require 'aws-sdk'
35
+ require 'sensu-plugins-aws/filter'
36
+
37
+ class EC2CpuBalance < Sensu::Plugin::Check::CLI
38
+ include Common
39
+ include Filter
40
+
41
+ option :critical,
42
+ description: 'Trigger a critical when value is below VALUE',
43
+ short: '-c VALUE',
44
+ long: '--critical VALUE',
45
+ proc: proc(&:to_f),
46
+ required: true
47
+
48
+ option :warning,
49
+ description: 'Trigger a warning when value is below VALUE',
50
+ short: '-w VALUE',
51
+ long: '--warning VALUE',
52
+ proc: proc(&:to_f)
53
+
54
+ option :aws_region,
55
+ short: '-r R',
56
+ long: '--region REGION',
57
+ description: 'AWS region',
58
+ default: 'us-east-1'
59
+
60
+ option :tag,
61
+ description: 'Add instance TAG value to warn/critical message.',
62
+ short: '-t TAG',
63
+ long: '--tag TAG'
64
+
65
+ option :instance_families,
66
+ description: 'List of burstable instance families to check. Default to t2,t3',
67
+ short: '-f t2,t3',
68
+ long: '--instance-families t2,t3',
69
+ proc: proc { |x| x.split(',') },
70
+ default: %w[t2 t3]
71
+
72
+ option :filter,
73
+ short: '-F FILTER',
74
+ long: '--filter FILTER',
75
+ description: 'String representation of the filter to apply',
76
+ default: '{}'
77
+
78
+ def data(instance)
79
+ client = Aws::CloudWatch::Client.new
80
+ stats = 'Average'
81
+ period = 60
82
+ resp = client.get_metric_statistics(
83
+ namespace: 'AWS/EC2',
84
+ metric_name: 'CPUCreditBalance',
85
+ dimensions: [
86
+ {
87
+ name: 'InstanceId',
88
+ value: instance
89
+ }
90
+ ],
91
+ start_time: Time.now - period * 10,
92
+ end_time: Time.now,
93
+ period: period,
94
+ statistics: [stats]
95
+ )
96
+
97
+ return resp.datapoints.first.send(stats.downcase) unless resp.datapoints.first.nil?
98
+ end
99
+
100
+ def instance_tag(instance, tag_name)
101
+ tag = instance.tags.select { |t| t.key == tag_name }.first
102
+ tag.nil? ? '' : tag.value
103
+ end
104
+
105
+ def run
106
+ filters = Filter.parse(config[:filter])
107
+ filters.push(
108
+ name: 'instance-state-name',
109
+ values: ['running']
110
+ )
111
+ ec2 = Aws::EC2::Client.new
112
+ instances = ec2.describe_instances(
113
+ filters: filters
114
+ )
115
+
116
+ messages = "\n"
117
+ level = 0
118
+ instances.reservations.each do |reservation|
119
+ reservation.instances.each do |instance|
120
+ next unless instance.instance_type.start_with?(*config[:instance_families])
121
+ id = instance.instance_id
122
+ result = data id
123
+ tag = config[:tag] ? " (#{instance_tag(instance, config[:tag])})" : ''
124
+ unless result.nil?
125
+ if result < config[:critical]
126
+ level = 2
127
+ messages << "#{id}#{tag} is below critical threshold [#{result} < #{config[:critical]}]\n"
128
+ elsif config[:warning] && result < config[:warning]
129
+ level = 1 if level.zero?
130
+ messages << "#{id}#{tag} is below warning threshold [#{result} < #{config[:warning]}]\n"
131
+ end
132
+ end
133
+ end
134
+ end
135
+ ok messages if level.zero?
136
+ warning messages if level == 1
137
+ critical messages if level == 2
138
+ end
139
+ end
@@ -0,0 +1,190 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # check-ec2-filter
4
+ #
5
+ # DESCRIPTION:
6
+ # This plugin retrieves EC2 instances matching a given filter and
7
+ # returns the number matched. Warning and Critical thresholds may be set as needed.
8
+ # Thresholds may be compared to the count using [equal, not, greater, less] operators.
9
+ #
10
+ # OUTPUT:
11
+ # plain-text
12
+ #
13
+ # PLATFORMS:
14
+ # Linux
15
+ #
16
+ # DEPENDENCIES:
17
+ # gem: aws-sdk
18
+ # gem: sensu-plugin
19
+ #
20
+ # USAGE:
21
+ # ./check-ec2-filter.rb -w 20 -f "{name:tag-value,values:[infrastructure]}"
22
+ # ./check-ec2-filter.rb -w 10 -c 5 -o less -f "{name:tag-value,values:[infrastructure]} {name:instance-state-name,values:[running]}"
23
+ #
24
+ # NOTES:
25
+ #
26
+ # LICENSE:
27
+ # Justin McCarty
28
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
29
+ # for details.
30
+ #
31
+
32
+ require 'sensu-plugin/check/cli'
33
+ require 'aws-sdk'
34
+ require 'sensu-plugins-aws/filter'
35
+
36
+ class EC2Filter < Sensu::Plugin::Check::CLI
37
+ include Filter
38
+ option :aws_access_key,
39
+ short: '-a AWS_ACCESS_KEY',
40
+ long: '--aws-access-key AWS_ACCESS_KEY',
41
+ description: "AWS Access Key. Either set ENV['AWS_ACCESS_KEY'] or provide it as an option",
42
+ default: ENV['AWS_ACCESS_KEY']
43
+
44
+ option :aws_secret_access_key,
45
+ short: '-k AWS_SECRET_KEY',
46
+ long: '--aws-secret-access-key AWS_SECRET_KEY',
47
+ description: "AWS Secret Access Key. Either set ENV['AWS_SECRET_KEY'] or provide it as an option",
48
+ default: ENV['AWS_SECRET_KEY']
49
+
50
+ option :aws_region,
51
+ short: '-r AWS_REGION',
52
+ long: '--aws-region REGION',
53
+ description: 'AWS Region (defaults to us-east-1).',
54
+ default: 'us-east-1'
55
+
56
+ option :name,
57
+ description: 'Filter naming scheme, text to prepend to metric',
58
+ short: '-n NAME',
59
+ long: '--name NAME',
60
+ default: ''
61
+
62
+ option :filter,
63
+ short: '-f FILTER',
64
+ long: '--filter FILTER',
65
+ description: 'String representation of the filter to apply',
66
+ default: '{}'
67
+
68
+ option :warning,
69
+ description: 'Warning threshold for filter',
70
+ short: '-w COUNT',
71
+ long: '--warning COUNT'
72
+
73
+ option :critical,
74
+ description: 'Critical threshold for filter',
75
+ short: '-c COUNT',
76
+ long: '--critical COUNT'
77
+
78
+ option :exclude_tags,
79
+ short: '-e {<tag-key>:[VAL1, VAL2]} {<tag-key>:[VAL1, VAL2] }',
80
+ long: '--exclude_tags {<tag-key>:[VAL1, VAL2] } {<tag-key>:[VAL1, VAL2] }',
81
+ description: 'Tag Values to exclude by. Values treated as regex. Any matching value will result in exclusion.',
82
+ default: '{}'
83
+
84
+ option :compare,
85
+ description: 'Comparision operator for threshold: equal, not, greater, less',
86
+ short: '-o OPERATION',
87
+ long: '--operator OPERATION',
88
+ default: 'equal'
89
+
90
+ option :detailed_message,
91
+ short: '-d',
92
+ long: '--detailed-message',
93
+ boolean: true,
94
+ default: false
95
+
96
+ option :min_running_secs,
97
+ long: '--min-running-secs SECONDS',
98
+ default: nil
99
+
100
+ def aws_config
101
+ { access_key_id: config[:aws_access_key],
102
+ secret_access_key: config[:aws_secret_access_key],
103
+ region: config[:aws_region] }
104
+ end
105
+
106
+ def convert_operator
107
+ op = ->(c, t) { c == t }
108
+
109
+ if config[:compare] == 'greater'
110
+ op = ->(c, t) { c > t }
111
+ elsif config[:compare] == 'less'
112
+ op = ->(c, t) { c < t }
113
+ elsif config[:compare] == 'not'
114
+ op = ->(c, t) { c != t }
115
+ end
116
+
117
+ op
118
+ end
119
+
120
+ def run
121
+ filter_list = config[:exclude_tags].split(/}\s?{/).map do |x|
122
+ x.gsub(/[{}]/, '')
123
+ end
124
+ filter_list = filter_list.map do |y|
125
+ _, h2, h3 = y.split(/(.*):(.*)/)
126
+ { h2 => h3 }
127
+ end.reduce(:merge)
128
+ filter_list.delete(nil)
129
+ filter_list.each { |x, y| filter_list[x] = y.strip.gsub(/[\[\]]/, '') }
130
+
131
+ client = Aws::EC2::Client.new aws_config
132
+
133
+ filter = Filter.parse(config[:filter])
134
+
135
+ options = if filter.empty?
136
+ {}
137
+ else
138
+ { filters: filter }
139
+ end
140
+
141
+ data = client.describe_instances(options)
142
+
143
+ aws_instances = Set.new
144
+ data.reservations.each do |r|
145
+ r.instances.each do |i|
146
+ aws_instances << {
147
+ id: i[:instance_id],
148
+ launch_time: i.launch_time,
149
+ tags: i.tags
150
+ }
151
+ end
152
+ end
153
+
154
+ aws_instances.delete_if do |instance|
155
+ instance[:tags].any? do |key|
156
+ filter_list.keys.include?(key.key) && filter_list[key.key].split(',').any? do |v|
157
+ key.value.match(/#{v.strip}/)
158
+ end
159
+ end
160
+ end
161
+
162
+ unless config[:min_running_secs].nil?
163
+ aws_instances.delete_if do |instance|
164
+ (Time.now.utc - instance[:launch_time]).to_i < config[:min_running_secs].to_i
165
+ end
166
+ end
167
+
168
+ count = aws_instances.count
169
+ op = convert_operator
170
+ message = "Current count: #{count}"
171
+ message += " - #{aws_instances.collect { |x| x[:id] }.join(',')}" if config[:detailed_message] && count > 0
172
+
173
+ unless config[:critical].nil?
174
+ if op.call count, config[:critical].to_i
175
+ critical message
176
+ end
177
+ end
178
+
179
+ unless config[:warning].nil?
180
+ if op.call count, config[:warning].to_i
181
+ warning message
182
+ end
183
+ end
184
+
185
+ ok message
186
+ rescue StandardError => e
187
+ puts "Error: exception: #{e}"
188
+ critical
189
+ end
190
+ end
@@ -0,0 +1,133 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # check-ec2-network
4
+ #
5
+ # DESCRIPTION:
6
+ # Check EC2 Network Metrics by CloudWatch API.
7
+ #
8
+ # OUTPUT:
9
+ # plain-text
10
+ #
11
+ # PLATFORMS:
12
+ # Linux
13
+ #
14
+ # DEPENDENCIES:
15
+ # gem: aws-sdk
16
+ # gem: sensu-plugin
17
+ #
18
+ # USAGE:
19
+ # ./check-ec2-network.rb -r ${you_region} -i ${your_instance_id} --warning-over 1000000 --critical-over 1500000
20
+ # ./check-ec2-network.rb -r ${you_region} -i ${your_instance_id} -d NetworkIn --warning-over 1000000 --critical-over 1500000
21
+ # ./check-ec2-network.rb -r ${you_region} -i ${your_instance_id} -d NetworkOut --warning-over 1000000 --critical-over 1500000
22
+ #
23
+ # NOTES:
24
+ #
25
+ # LICENSE:
26
+ # Yohei Kawahara <inokara@gmail.com>
27
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
28
+ # for details.
29
+ #
30
+
31
+ require 'sensu-plugin/check/cli'
32
+ require 'aws-sdk'
33
+
34
+ class CheckEc2Network < Sensu::Plugin::Check::CLI
35
+ option :aws_access_key,
36
+ short: '-a AWS_ACCESS_KEY',
37
+ long: '--aws-access-key AWS_ACCESS_KEY',
38
+ description: "AWS Access Key. Either set ENV['AWS_ACCESS_KEY'] or provide it as an option",
39
+ default: ENV['AWS_ACCESS_KEY']
40
+
41
+ option :aws_secret_access_key,
42
+ short: '-k AWS_SECRET_KEY',
43
+ long: '--aws-secret-access-key AWS_SECRET_KEY',
44
+ description: "AWS Secret Access Key. Either set ENV['AWS_SECRET_KEY'] or provide it as an option",
45
+ default: ENV['AWS_SECRET_KEY']
46
+
47
+ option :aws_region,
48
+ short: '-r AWS_REGION',
49
+ long: '--aws-region REGION',
50
+ description: 'AWS Region (defaults to us-east-1).',
51
+ default: 'us-east-1'
52
+
53
+ option :instance_id,
54
+ short: '-i instance-id',
55
+ long: '--instance-id instance-ids',
56
+ description: 'EC2 Instance ID to check.'
57
+
58
+ option :end_time,
59
+ short: '-t T',
60
+ long: '--end-time TIME',
61
+ default: Time.now,
62
+ description: 'CloudWatch metric statistics end time'
63
+
64
+ option :period,
65
+ short: '-p N',
66
+ long: '--period SECONDS',
67
+ default: 60,
68
+ description: 'CloudWatch metric statistics period'
69
+
70
+ option :direction,
71
+ short: '-d NetworkIn or NetworkOut',
72
+ long: '--direction NetworkIn or NetworkOut',
73
+ default: 'NetworkIn',
74
+ description: 'Select NetworkIn or NetworkOut'
75
+
76
+ %w[warning critical].each do |severity|
77
+ option :"#{severity}_over",
78
+ long: "--#{severity}-over COUNT",
79
+ description: "Trigger a #{severity} if network traffice is over specified Bytes"
80
+ end
81
+
82
+ def aws_config
83
+ { access_key_id: config[:aws_access_key],
84
+ secret_access_key: config[:aws_secret_access_key],
85
+ region: config[:aws_region] }
86
+ end
87
+
88
+ def ec2
89
+ @ec2 ||= Aws::EC2::Client.new aws_config
90
+ end
91
+
92
+ def cloud_watch
93
+ @cloud_watch ||= Aws::CloudWatch::Client.new aws_config
94
+ end
95
+
96
+ def network_metric(instance)
97
+ cloud_watch.get_metric_statistics(
98
+ namespace: 'AWS/EC2',
99
+ metric_name: config[:direction].to_s,
100
+ dimensions: [
101
+ {
102
+ name: 'InstanceId',
103
+ value: instance
104
+ }
105
+ ],
106
+ start_time: config[:end_time] - 300,
107
+ end_time: config[:end_time],
108
+ statistics: ['Average'],
109
+ period: config[:period],
110
+ unit: 'Bytes'
111
+ )
112
+ end
113
+
114
+ def latest_value(value)
115
+ value.datapoints[0][:average].to_f unless value.datapoints[0].nil?
116
+ end
117
+
118
+ def check_metric(instance)
119
+ metric = network_metric instance
120
+ latest_value metric unless metric.nil?
121
+ end
122
+
123
+ def run
124
+ metric_value = check_metric config[:instance_id]
125
+ if !metric_value.nil? && metric_value > config[:critical_over].to_f
126
+ critical "#{config[:direction]} at #{metric_value} Bytes"
127
+ elsif !metric_value.nil? && metric_value > config[:warning_over].to_f
128
+ warning "#{config[:direction]} at #{metric_value} Bytes"
129
+ else
130
+ ok "#{config[:direction]} at #{metric_value} Bytes"
131
+ end
132
+ end
133
+ end