使用getopt处理Python警告和关键参数

时间:2015-03-13 15:04:03

标签: amazon-web-services nagios

我正在尝试编写一个脚本来警告某些警告和关键级别。到目前为止,无论输出如何,它都显示正常。它忽略了警告和批评。我需要改变什么?

./ check_sqs -p prefix -q queue -s xxx -a xxx -w 5 -c 10

确定:深度= 33; | Prefix_Queue = 33

for o, a in opts:
    if o in ('-q', '--queue'):
        queue_name = a
    if o in ('-a', '--access_key'):
        access_key = a
    if o in ('-s', '--secret_key'):
        secret_key = a
    if o in ('-p', '--prefix'):
        prefix = a
    if o in ('-w', '--warning'):
        warning = a
    if o in ('-c', '--critical'):
        critical = a

if access_key == "_none_":
    c = SQSConnection()
else:
    c = SQSConnection(access_key, secret_key)

cnt = 0
if queue_name != '':
    q = c.get_queue(queue_name)
    if q == None:
        print "ERROR: Invalid queue name " + queue_name
        sys.exit(3)
    cnt = q.count()
    if cnt >= critical:
        print 'Critical: depth =', str(cnt) + ';|', q.id.split('/')[2] + "=" + str(cnt)
        sys.exit(2)
    if cnt >= warning:
        print 'Warning: depth =', str(cnt) + ';|', q.id.split('/')[2] + "=" + str(cnt)
        sys.exit(1)
    if isinstance(cnt, int) and cnt >= 0:
        print 'OK: depth =', str(cnt) + ';|', q.id.split('/')[2] + "=" + str(cnt)
        sys.exit(0)
    else:
        print 'Unknown: depth =', str(cnt) + ';|', q.id.split('/')[2] + "=" + str(cnt)
        sys.exit(3)

rs = c.get_all_queues()
resp = ''
for q in rs:
    name = q.id.split('/')[2]
    if name.startswith(prefix):
        cnt = cnt + q.count()
        resp = resp + name + "=" + str(q.count()) + " "
print 'OK: depth =', str(cnt) + ';|', resp
sys.exit(0)

1 个答案:

答案 0 :(得分:0)

谢谢,这非常有帮助。严重和警告是字符串,这就是比较不起作用的原因。将它们改成int就成了伎俩。

critical = int(严重) warning = int(警告)