forked from HariSekhon/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_aws_ec2_instance_states.py
executable file
·153 lines (132 loc) · 5.45 KB
/
check_aws_ec2_instance_states.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2020-01-08 14:55:35 +0000 (Wed, 08 Jan 2020)
#
# https://github.com/harisekhon/nagios-plugins
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/harisekhon
#
"""
Nagios Plugin to check AWS EC2 instance states, raising warnings for non-running states
Checks warning thresholds for each status type (aggregates stopped / stopping / shutting-down states)
Uses the Boto python library, read here for the list of ways to configure your AWS credentials:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html
See also DevOps Python Tools and DevOps Bash Tools repos which have more similar AWS tools
- https://github.com/harisekhon/devops-python-tools
- https://github.com/harisekhon/devops-bash-tools
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import logging
import os
import sys
import traceback
from collections import OrderedDict
import boto3
srcdir = os.path.abspath(os.path.dirname(__file__))
libdir = os.path.join(srcdir, 'pylib')
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import log, jsonpp
from harisekhon import NagiosPlugin
except ImportError:
print(traceback.format_exc(), end='')
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.2.0'
class CheckAwsEC2InstanceStates(NagiosPlugin):
def __init__(self):
# Python 2.x
super(CheckAwsEC2InstanceStates, self).__init__()
# Python 3.x
# super().__init__()
self.instance_count = 0
self.thresholds = {}
self.msg = 'AWS EC2 states msg not defined'
self.ok()
def add_options(self):
self.add_opt('-r', '--max-running', default=None, type=int,
help="Max instances in running state (default: None)")
self.add_opt('-p', '--max-pending', default=0, type=int,
help="Max instances in pending state (default: 0)")
self.add_opt('-s', '--max-stopped', default=0, type=int,
help="Max instances in stopped / stopping / shutting-down state (default: 0)")
self.add_opt('-T', '--max-terminated', default=0, type=int,
help="Max instances in terminated state (default: 0)")
def process_args(self):
self.no_args()
self.thresholds = {
'running': self.get_opt('max_running'),
'pending': self.get_opt('max_pending'),
'stopped': self.get_opt('max_stopped'),
'terminated': self.get_opt('max_terminated'),
}
def run(self):
log.info('testing AWS API call')
# there isn't really a .ping() type API endpoint so just connect to IAM and list users
ec2 = boto3.client('ec2')
#instances = ec2.describe_instances()
describe_instances = ec2.get_paginator('describe_instances')
statuses = OrderedDict(
[
('running', 0),
('terminated', 0),
('stopped', 0),
('stopping', 0),
('shutting-down', 0),
]
)
flatten = lambda _: [item for sublist in _ for item in sublist]
# this might time out if there are a lot of EC2 instances
for instances_response in describe_instances.paginate():
if log.isEnabledFor(logging.DEBUG):
log.debug('\n\n%s', jsonpp(instances_response))
instances = flatten([_['Instances'] for _ in instances_response['Reservations']])
for instance in instances:
self.instance_count += 1
#if log.isEnabledFor(logging.DEBUG):
# log.debug('\n\n%s', instance)
statuses[instance['State']['Name']] = statuses.get(instance['State']['Name'], 0) + 1
self.msg = 'AWS EC2 instance total = {}'.format(self.instance_count)
self.check_statuses(statuses)
def check_statuses(self, statuses):
stopped_bundle = ['stopped', 'stopping', 'shutting-down']
thresholds = self.thresholds
for status in statuses:
self.msg += ', {} = {}'.format(status, statuses[status])
if status in thresholds:
if thresholds[status] is None:
continue
elif statuses[status] > thresholds[status]:
self.msg_warn()
elif status in stopped_bundle:
total_stopped = 0
for _ in stopped_bundle:
total_stopped += statuses[_]
if total_stopped > thresholds['stopped']:
self.warning()
else:
self.msg_warn()
self.msg += ' | total={}'.format(self.instance_count)
for status in statuses:
self.msg += ' {}={}'.format(status, statuses[status])
if status in thresholds:
if thresholds[status] is not None:
self.msg += ';{}'.format(thresholds[status])
elif status in stopped_bundle:
self.msg += ';{}'.format(thresholds['stopped'])
def msg_warn(self):
self.warning()
self.msg += ' (!)'
if __name__ == '__main__':
CheckAwsEC2InstanceStates().main()