-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
182 lines (144 loc) · 6.43 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
"""
awslimitchecker/docs/examples/check_aws_limits.py
awslimitchecker example Python wrapper - see README.rst for information
The latest version of this package is available at:
<https://github.com/jantman/awslimitchecker>
################################################################################
Copyright 2015-2018 Jason Antman <[email protected]>
This file is part of awslimitchecker, also known as awslimitchecker.
awslimitchecker is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
awslimitchecker is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with awslimitchecker. If not, see <http://www.gnu.org/licenses/>.
The Copyright and Authors attributions contained herein may not be removed or
otherwise altered, except to add the Author attribution of a contributor to
this work. (Additional Terms pursuant to Section 7b of the AGPL v3)
################################################################################
While not legally required, I sincerely request that anyone who finds
bugs please submit them at <https://github.com/jantman/awslimitchecker> or
to me via email, and that you send any contributions or improvements
either as a pull request on GitHub, or to me via email.
################################################################################
AUTHORS:
Jason Antman <[email protected]> <http://www.jasonantman.com>
################################################################################
"""
import logging
from awslimitchecker.checker import AwsLimitChecker
# BEGIN configuration for thresholds and limit overrides
from util import send_mail
AWS_LIMIT_OVERRIDES = {
# 'EC2': {
# 'Running On-Demand EC2 instances': 400,
# },
}
AWS_THRESHOLD_OVERRIDES = {
# 'Lambda': {
# 'Total Code Size (MiB)': {'warning': {'percent': 85}},
# },
}
# END configuration for thresholds and limit overrides
EMAIL_SUBJECT_WARNING = "awslimitchecker - warning"
EMAIL_SUBJECT_CRITICAL = "awslimitchecker - critical"
EMAIL_TO_WARNING = "[email protected]"
EMAIL_TO_CRITICAL = "[email protected]"
logger = logging.getLogger(__name__)
class CheckAWSLimits(object):
"""check AWS usage against service limits"""
def check_limits(self, verbose=False):
"""
Run the actual usage and limit check, with overrides.
see: http://awslimitchecker.readthedocs.org/en/latest/python_usage.html#ci-deployment-checks
"""
# instantiate the class
checker = AwsLimitChecker()
# set your overrides
checker.set_threshold_overrides(AWS_THRESHOLD_OVERRIDES)
checker.set_limit_overrides(AWS_LIMIT_OVERRIDES)
print("Checking AWS resource usage; WARNING threshold {w}% of "
"limit, CRITICAL threshold {c}% of limit".format(
w=checker.warning_threshold,
c=checker.critical_threshold))
# check usage against thresholds
# if we didn't support verbose output, we could just iterate the return
# value of this to be a bit more efficient.
checker.check_thresholds()
# save state for exit code and summary
warnings = []
criticals = []
# iterate the results
for service, svc_limits in sorted(checker.get_limits().items()):
for limit_name, limit in sorted(svc_limits.items()):
have_alarms = False
# check warnings and criticals for each Limit
for warn in limit.get_warnings():
warnings.append("{service} '{limit_name}' usage "
"({u}) exceeds warning threshold "
"(limit={l})".format(
service=service,
limit_name=limit_name,
u=str(warn),
l=limit.get_limit(),
))
have_alarms = True
for crit in limit.get_criticals():
criticals.append("{service} '{limit_name}' usage "
"({u}) exceeds critical threshold"
" (limit={l})".format(
service=service,
limit_name=limit_name,
u=str(crit),
l=limit.get_limit(),
))
have_alarms = True
if not have_alarms and verbose:
print("{service} '{limit_name}' OK: {u} (limit={l})".format(
service=service,
limit_name=limit_name,
u=limit.get_current_usage_str(),
l=limit.get_limit()
))
if verbose:
print("\n\n")
return (warnings, criticals)
def run(self, verbose=False):
"""
Main entry point.
"""
warnings, criticals = self.check_limits(verbose=verbose)
# output
if len(warnings) > 0:
print("\nWARNING:\n")
for w in warnings:
print(w)
if len(criticals) > 0:
print("\nCRITICAL:\n")
for c in criticals:
print(c)
# alerts
if len(warnings) > 0:
body = '\n'.join(warnings)
send_mail(EMAIL_SUBJECT_WARNING, body, [EMAIL_TO_WARNING])
if len(criticals) > 0:
body = '\n'.join(criticals)
send_mail(EMAIL_SUBJECT_CRITICAL, body, [EMAIL_TO_CRITICAL])
# summary
if len(warnings) > 0 or len(criticals) > 0:
print("\n{c} limit(s) above CRITICAL threshold; {w} limit(s) above "
"WARNING threshold".format(c=len(criticals), w=len(warnings)))
else:
print("All limits are within thresholds.")
def handler(event, _):
# print("new lambda run, event:" + str(event))
checker = CheckAWSLimits()
checker.run(verbose=True)
if __name__ == "__main__":
print("new CLI run")
handler(None, None)