2012-10-19 12:20:52 -04: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 11:36:55 -04:00
|
|
|
default="https://humbughq.com",
|
2012-10-19 12:20:52 -04:00
|
|
|
action='store')
|
2012-11-08 13:16:34 -05:00
|
|
|
parser.add_option('--root-path',
|
|
|
|
dest='root_path',
|
|
|
|
default="/home/humbug",
|
|
|
|
action='store')
|
2012-10-19 12:20:52 -04:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2012-11-08 13:16:34 -05: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 12:20:52 -04: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 14:42:45 -05:00
|
|
|
zhkey1 = random.getrandbits(32)
|
|
|
|
zhkey2 = random.getrandbits(32)
|
2012-10-19 12:20:52 -04: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 10:59:57 -04:00
|
|
|
client="test: Humbug API",
|
2012-10-19 12:20:52 -04:00
|
|
|
site=options.site)
|
|
|
|
|
2012-11-15 14:24:38 -05: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 12:20:52 -04:00
|
|
|
def print_zephyr(notice):
|
|
|
|
print notice.cls, notice.instance, notice.sender, notice.message.split('\0')[1]
|
|
|
|
|
2012-10-19 13:10:28 -04: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-10-19 12:20:52 -04:00
|
|
|
child_pid = os.fork()
|
|
|
|
if child_pid == 0:
|
|
|
|
# Run the humbug => zephyr mirror in the child
|
|
|
|
time.sleep(3)
|
2012-11-15 14:24:38 -05:00
|
|
|
result = humbug_client.send_message({
|
2012-11-07 18:38:21 -05:00
|
|
|
"type": "private",
|
2012-10-19 12:20:52 -04:00
|
|
|
"content": str(hzkey1),
|
2012-11-14 17:21:46 -05:00
|
|
|
"to": humbug_user,
|
2012-11-15 14:24:38 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if result["result"] != "success":
|
|
|
|
print "key1 send error:"
|
|
|
|
print result
|
|
|
|
|
2012-10-19 12:20:52 -04:00
|
|
|
time.sleep(0.2)
|
2012-11-15 14:24:38 -05:00
|
|
|
result = humbug_client.send_message({
|
2012-10-19 12:20:52 -04:00
|
|
|
"type": "stream",
|
|
|
|
"subject": "test",
|
|
|
|
"content": str(hzkey2),
|
2012-11-14 17:21:46 -05:00
|
|
|
"to": "tabbott-nagios-test",
|
2012-11-15 14:24:38 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if result["result"] != "success":
|
|
|
|
print "key2 send error:"
|
|
|
|
print result
|
|
|
|
|
2012-11-08 13:39:09 -05:00
|
|
|
if options.verbose:
|
|
|
|
print "Sent Humbug messages!"
|
2012-10-19 12:20:52 -04:00
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
import zephyr
|
2012-11-15 14:24:38 -05: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 12:20:52 -04:00
|
|
|
zsig = "Timothy Good Abbott"
|
|
|
|
|
2012-10-19 13:47:05 -04:00
|
|
|
zeph = zephyr.ZNotice(sender=mit_user, auth=True, recipient=mit_user,
|
2012-10-19 12:20:52 -04:00
|
|
|
cls="message", instance="personal")
|
|
|
|
zeph.setmessage("%s\0%s" % (zsig, zhkey1))
|
|
|
|
zeph.send()
|
|
|
|
time.sleep(0.2)
|
|
|
|
|
2012-10-19 13:47:05 -04:00
|
|
|
zeph = zephyr.ZNotice(sender=mit_user, auth=True,
|
2012-10-19 12:20:52 -04:00
|
|
|
cls="tabbott-nagios-test", instance="test")
|
|
|
|
zeph.setmessage("%s\0%s" % (zsig, zhkey2))
|
|
|
|
zeph.send()
|
2012-11-08 13:39:09 -05:00
|
|
|
if options.verbose:
|
|
|
|
print "Sent Zephyr messages!"
|
2012-10-19 12:20:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
failed = False
|
|
|
|
import zephyr
|
2012-11-15 14:24:38 -05:00
|
|
|
try:
|
|
|
|
zephyr.init()
|
2012-11-26 11:47:26 -05:00
|
|
|
subs = zephyr.Subscriptions()
|
|
|
|
subs.add(('message', 'personal', 'tabbott/extra@ATHENA.MIT.EDU'))
|
|
|
|
subs.add(('tabbott-nagios-test', '*', '*'))
|
2012-11-15 14:24:38 -05:00
|
|
|
except IOError, e:
|
|
|
|
if "SERVNAK received" in e:
|
|
|
|
print "SERVNAK received, punting rest of test"
|
|
|
|
print_status_and_exit(0)
|
|
|
|
|
2012-10-19 12:20:52 -04:00
|
|
|
|
2012-11-07 13:18:10 -05:00
|
|
|
max_message_id = humbug_client.get_profile()['max_message_id']
|
2012-10-19 12:20:52 -04:00
|
|
|
|
2012-11-20 15:13:29 -05:00
|
|
|
time.sleep(20)
|
2012-11-08 13:39:09 -05:00
|
|
|
if options.verbose:
|
|
|
|
print "Receiving messages!"
|
2012-10-19 12:20:52 -04: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 13:16:28 -05:00
|
|
|
elif (set(notice.message.split('\0')[1] for notice in notices) !=
|
|
|
|
set([str(hzkey1), str(hzkey2), str(zhkey1), str(zhkey2)])):
|
2012-10-19 12:20:52 -04: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 13:10:28 -04:00
|
|
|
for message in messages:
|
|
|
|
print_humbug(message)
|
2012-10-19 12:20:52 -04:00
|
|
|
failed = True
|
2012-11-08 13:16:28 -05:00
|
|
|
elif (set(message["content"] for message in messages) !=
|
|
|
|
set([str(hzkey1), str(hzkey2), str(zhkey1), str(zhkey2)])):
|
2012-10-19 12:20:52 -04:00
|
|
|
print "zephyr=>humbug: Didn't get back right values!"
|
2012-10-19 13:10:28 -04:00
|
|
|
for message in messages:
|
|
|
|
print_humbug(message)
|
2012-10-19 12:20:52 -04:00
|
|
|
failed = True
|
|
|
|
|
|
|
|
if failed:
|
2012-11-15 14:42:45 -05:00
|
|
|
print "original keys:", hzkey1, hzkey2, zhkey1, zhkey2
|
2012-11-15 14:24:38 -05:00
|
|
|
print_status_and_exit(1)
|
2012-10-19 12:20:52 -04:00
|
|
|
|
|
|
|
print "Success!"
|
2012-11-15 14:24:38 -05:00
|
|
|
print_status_and_exit(0)
|