2012-10-19 18:20:52 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('--verbose',
|
|
|
|
dest='verbose',
|
|
|
|
default=False,
|
|
|
|
action='store_true')
|
|
|
|
parser.add_option('--site',
|
|
|
|
dest='site',
|
2012-10-27 17:36:55 +02:00
|
|
|
default="https://humbughq.com",
|
2012-10-19 18:20:52 +02:00
|
|
|
action='store')
|
2012-11-08 19:16:34 +01:00
|
|
|
parser.add_option('--root-path',
|
|
|
|
dest='root_path',
|
|
|
|
default="/home/humbug",
|
|
|
|
action='store')
|
2012-10-19 18:20:52 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2012-11-08 19:16:34 +01:00
|
|
|
sys.path[:0] = [os.path.join(options.root_path, "python-zephyr"),
|
|
|
|
os.path.join(options.root_path, "python-zephyr/build/lib.linux-x86_64-2.6/"),
|
|
|
|
options.root_path]
|
|
|
|
|
2012-10-19 18:20:52 +02:00
|
|
|
mit_user = 'tabbott/extra@ATHENA.MIT.EDU'
|
|
|
|
humbug_user = 'tabbott/extra@mit.edu'
|
|
|
|
|
|
|
|
hzkey1 = random.getrandbits(32)
|
|
|
|
hzkey2 = random.getrandbits(32)
|
2012-11-15 20:42:45 +01:00
|
|
|
zhkey1 = random.getrandbits(32)
|
|
|
|
zhkey2 = random.getrandbits(32)
|
2012-10-19 18:20:52 +02:00
|
|
|
|
|
|
|
sys.path.append(".")
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
import api.common
|
|
|
|
humbug_client = api.common.HumbugAPI(email=humbug_user,
|
|
|
|
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
verbose=True,
|
2012-10-23 16:59:57 +02:00
|
|
|
client="test: Humbug API",
|
2012-10-19 18:20:52 +02:00
|
|
|
site=options.site)
|
|
|
|
|
2012-11-15 20:24:38 +01:00
|
|
|
def print_status_and_exit(status):
|
|
|
|
# The output of this script is used by Nagios. Various outputs,
|
|
|
|
# e.g. true success and punting due to a SERVNAK, result in a
|
|
|
|
# non-alert case, so to give us something unambiguous to check in
|
|
|
|
# Nagios, print the exit status.
|
|
|
|
print status
|
|
|
|
sys.exit(status)
|
|
|
|
|
2012-10-19 18:20:52 +02:00
|
|
|
def print_zephyr(notice):
|
|
|
|
print notice.cls, notice.instance, notice.sender, notice.message.split('\0')[1]
|
|
|
|
|
2012-10-19 19:10:28 +02:00
|
|
|
def print_humbug(message):
|
|
|
|
if message['type'] == "stream":
|
|
|
|
print message["type"], message['display_recipient'], message['subject'], \
|
|
|
|
message['sender_email'], message['content']
|
|
|
|
else:
|
|
|
|
print message["type"], message['sender_email'], \
|
|
|
|
message['display_recipient'], message['content']
|
|
|
|
|
2012-11-26 22:29:44 +01:00
|
|
|
max_message_id = humbug_client.get_profile()['max_message_id']
|
|
|
|
|
2012-10-19 18:20:52 +02:00
|
|
|
child_pid = os.fork()
|
|
|
|
if child_pid == 0:
|
|
|
|
# Run the humbug => zephyr mirror in the child
|
2012-11-26 22:29:44 +01:00
|
|
|
time.sleep(5)
|
2012-11-15 20:24:38 +01:00
|
|
|
result = humbug_client.send_message({
|
2012-11-08 00:38:21 +01:00
|
|
|
"type": "private",
|
2012-10-19 18:20:52 +02:00
|
|
|
"content": str(hzkey1),
|
2012-11-14 23:21:46 +01:00
|
|
|
"to": humbug_user,
|
2012-11-15 20:24:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if result["result"] != "success":
|
|
|
|
print "key1 send error:"
|
|
|
|
print result
|
|
|
|
|
|
|
|
result = humbug_client.send_message({
|
2012-10-19 18:20:52 +02:00
|
|
|
"type": "stream",
|
|
|
|
"subject": "test",
|
|
|
|
"content": str(hzkey2),
|
2012-11-14 23:21:46 +01:00
|
|
|
"to": "tabbott-nagios-test",
|
2012-11-15 20:24:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if result["result"] != "success":
|
|
|
|
print "key2 send error:"
|
|
|
|
print result
|
|
|
|
|
2012-11-08 19:39:09 +01:00
|
|
|
if options.verbose:
|
|
|
|
print "Sent Humbug messages!"
|
2012-10-19 18:20:52 +02:00
|
|
|
|
|
|
|
import zephyr
|
2012-11-15 20:24:38 +01:00
|
|
|
try:
|
|
|
|
zephyr.init()
|
|
|
|
except IOError, e:
|
|
|
|
if "SERVNAK received" in e:
|
|
|
|
print "SERVNAK received, punting rest of test"
|
|
|
|
print_status_and_exit(0)
|
|
|
|
|
2012-10-19 18:20:52 +02:00
|
|
|
zsig = "Timothy Good Abbott"
|
|
|
|
|
2012-10-19 19:47:05 +02:00
|
|
|
zeph = zephyr.ZNotice(sender=mit_user, auth=True, recipient=mit_user,
|
2012-10-19 18:20:52 +02:00
|
|
|
cls="message", instance="personal")
|
|
|
|
zeph.setmessage("%s\0%s" % (zsig, zhkey1))
|
|
|
|
zeph.send()
|
|
|
|
|
2012-10-19 19:47:05 +02:00
|
|
|
zeph = zephyr.ZNotice(sender=mit_user, auth=True,
|
2012-10-19 18:20:52 +02:00
|
|
|
cls="tabbott-nagios-test", instance="test")
|
|
|
|
zeph.setmessage("%s\0%s" % (zsig, zhkey2))
|
|
|
|
zeph.send()
|
2012-11-08 19:39:09 +01:00
|
|
|
if options.verbose:
|
|
|
|
print "Sent Zephyr messages!"
|
2012-10-19 18:20:52 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
failed = False
|
|
|
|
import zephyr
|
2012-11-15 20:24:38 +01:00
|
|
|
try:
|
|
|
|
zephyr.init()
|
2012-11-26 22:29:44 +01:00
|
|
|
zephyr._z.subAll([('message', 'personal', 'tabbott/extra@ATHENA.MIT.EDU'),
|
|
|
|
('tabbott-nagios-test', '*', '*')])
|
2012-11-15 20:24:38 +01:00
|
|
|
except IOError, e:
|
|
|
|
if "SERVNAK received" in e:
|
|
|
|
print "SERVNAK received, punting rest of test"
|
|
|
|
print_status_and_exit(0)
|
|
|
|
|
2012-11-20 21:13:29 +01:00
|
|
|
time.sleep(20)
|
2012-11-08 19:39:09 +01:00
|
|
|
if options.verbose:
|
|
|
|
print "Receiving messages!"
|
2012-10-19 18:20:52 +02:00
|
|
|
notices = []
|
|
|
|
while True:
|
|
|
|
notice = zephyr.receive(block=False)
|
|
|
|
if notice is None:
|
|
|
|
break
|
|
|
|
if notice.opcode != "":
|
|
|
|
continue
|
|
|
|
notices.append(notice)
|
|
|
|
if len(notices) != 4:
|
|
|
|
print "humbug=>zephyr: Got wrong number of messages back!"
|
|
|
|
failed = True
|
2012-11-08 19:16:28 +01:00
|
|
|
elif (set(notice.message.split('\0')[1] for notice in notices) !=
|
|
|
|
set([str(hzkey1), str(hzkey2), str(zhkey1), str(zhkey2)])):
|
2012-10-19 18:20:52 +02:00
|
|
|
print "humbug=>zephyr: Didn't get back right values!"
|
|
|
|
failed = True
|
|
|
|
if failed:
|
|
|
|
for notice in notices:
|
|
|
|
print_zephyr(notice)
|
|
|
|
|
|
|
|
messages = humbug_client.get_messages({'first': '0',
|
|
|
|
'last': str(max_message_id),
|
|
|
|
'server_generation': '0'})['messages']
|
|
|
|
if len(messages) != 4:
|
|
|
|
print "zephyr=>humbug: Didn't get exactly 4 messages!"
|
2012-10-19 19:10:28 +02:00
|
|
|
for message in messages:
|
|
|
|
print_humbug(message)
|
2012-10-19 18:20:52 +02:00
|
|
|
failed = True
|
2012-11-08 19:16:28 +01:00
|
|
|
elif (set(message["content"] for message in messages) !=
|
|
|
|
set([str(hzkey1), str(hzkey2), str(zhkey1), str(zhkey2)])):
|
2012-10-19 18:20:52 +02:00
|
|
|
print "zephyr=>humbug: Didn't get back right values!"
|
2012-10-19 19:10:28 +02:00
|
|
|
for message in messages:
|
|
|
|
print_humbug(message)
|
2012-10-19 18:20:52 +02:00
|
|
|
failed = True
|
|
|
|
|
|
|
|
if failed:
|
2012-11-15 20:42:45 +01:00
|
|
|
print "original keys:", hzkey1, hzkey2, zhkey1, zhkey2
|
2012-11-15 20:24:38 +01:00
|
|
|
print_status_and_exit(1)
|
2012-10-19 18:20:52 +02:00
|
|
|
|
|
|
|
print "Success!"
|
2012-11-15 20:24:38 +01:00
|
|
|
print_status_and_exit(0)
|