-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_sqs_queue.py
executable file
·50 lines (41 loc) · 1.43 KB
/
check_sqs_queue.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
#!/usr/bin/env python
from boto3.session import Session
import click
import sys
@click.command()
@click.argument('queue', type = str)
@click.argument('profile', type = str)
@click.option('--region', '-r', default = 'us-west-1', help = 'AWS region.',
type = str)
@click.option('--warning', '-w', default = 10, help='Warning threshold.',
type = int)
@click.option('--critical', '-c', default = 20, help='Critical threshold.',
type = int)
def check_sqs_queue(queue,profile,region,warning,critical):
"""Icinga plugin for checking number of messages in specified SQS queue.
ARGUMENTS:
QUEUE AWS SQS queue name.
PROFILE AWS profile name configured in file ~/.aws/credentials.
"""
try:
count = get_q_count(queue,profile,region)
except Exception:
print 'CRITICAL: Invalid AWS attributes'
sys.exit(2)
if count < warning:
print 'Queue OK: "%s" contains %s messages' % (queue, count)
sys.exit(0)
elif count >= critical:
print 'Queue CRITICAL: "%s" contains %s messages' % (queue, count)
sys.exit(2)
else:
print 'Queue WARNING: "%s" contains %s messages' % (queue, count)
sys.exit(1)
def get_q_count(queue,profile,region):
"""Get approximate number of messages in SQS queue."""
con = Session(profile_name = profile, region_name = region)
sqs = con.resource('sqs')
q_res = sqs.get_queue_by_name(QueueName = queue)
return int(q_res.attributes['ApproximateNumberOfMessages'])
if __name__ == '__main__':
check_sqs_queue()