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'
|
|
|
|
|
|
|
|
zhkey1 = random.getrandbits(32)
|
|
|
|
hzkey1 = random.getrandbits(32)
|
|
|
|
zhkey2 = random.getrandbits(32)
|
|
|
|
hzkey2 = random.getrandbits(32)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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-10-19 18:20:52 +02:00
|
|
|
child_pid = os.fork()
|
|
|
|
if child_pid == 0:
|
|
|
|
# Run the humbug => zephyr mirror in the child
|
|
|
|
time.sleep(3)
|
|
|
|
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),
|
|
|
|
"recipient": humbug_user,
|
|
|
|
});
|
|
|
|
time.sleep(0.2)
|
|
|
|
humbug_client.send_message({
|
|
|
|
"type": "stream",
|
|
|
|
"subject": "test",
|
|
|
|
"content": str(hzkey2),
|
|
|
|
"stream": "tabbott-nagios-test",
|
|
|
|
});
|
2012-11-08 19:39:09 +01:00
|
|
|
if options.verbose:
|
|
|
|
print "Sent Humbug messages!"
|
2012-10-19 18:20:52 +02:00
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
import zephyr
|
|
|
|
zephyr.init()
|
|
|
|
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()
|
|
|
|
time.sleep(0.2)
|
|
|
|
|
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
|
|
|
|
zephyr.init()
|
|
|
|
subs = zephyr.Subscriptions()
|
|
|
|
subs.add(('message', 'personal', 'tabbott/extra@ATHENA.MIT.EDU'))
|
|
|
|
subs.add(('tabbott-nagios-test', '*', '*'))
|
|
|
|
|
2012-11-07 19:18:10 +01:00
|
|
|
max_message_id = humbug_client.get_profile()['max_message_id']
|
2012-10-19 18:20:52 +02:00
|
|
|
|
|
|
|
time.sleep(10)
|
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:
|
|
|
|
print "original keys:", zhkey1, zhkey2, hzkey1, hzkey2
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
print "Success!"
|
|
|
|
sys.exit(0)
|