-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a complete rewrite of the monitor to support WAMP v2 and the …
…latest autobahn libraries. Documentation and manifest updated as well.
- Loading branch information
Camron Levanger
committed
May 19, 2015
1 parent
b09c5a9
commit 3064f13
Showing
4 changed files
with
72 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,98 @@ | ||
#!/usr/bin/python | ||
import sys | ||
import os | ||
import traceback | ||
import argparse | ||
|
||
from twisted.python import log | ||
from twisted.internet import reactor | ||
from twisted.internet.defer import inlineCallbacks | ||
|
||
from autobahn.websocket import connectWS | ||
from autobahn.wamp import WampClientFactory | ||
from autobahn.wamp import WampClientProtocol | ||
from autobahn.twisted.wamp import ApplicationSession | ||
|
||
exit_code = 3 | ||
exit_message = 'UNKNOWN - Unable to get info for socket connections' | ||
topic = 'notifications.1' | ||
|
||
class NagiosMonitor(WampClientProtocol): | ||
|
||
def onOpen(self): | ||
global exit_code | ||
global exit_message | ||
exit_code = 1 | ||
exit_message = 'Warning - Welcome message recieved, but no session' | ||
class Component(ApplicationSession): | ||
|
||
def onSessionOpen(self): | ||
global exit_code | ||
global exit_message | ||
exit_code = 0 | ||
exit_message = 'OK - Socket session fully established' | ||
self.dropConnection(self) | ||
reactor.stop() | ||
""" | ||
An application component that subscribes and receives events, | ||
and stop after having received 5 events. | ||
""" | ||
|
||
def clientConnectionFailed(self, connector, reason): | ||
@inlineCallbacks | ||
def onJoin(self, details): | ||
global exit_code | ||
global exit_message | ||
exit_code = 2 | ||
exit_message = "CRITICAL - Unable to connect to socket server" | ||
reactor.stop() | ||
|
||
global topic | ||
exit_code = 1 | ||
exit_message = 'Warning - Connected, but no subscription made' | ||
|
||
def on_event(msg): | ||
print("Got event: {}".format(msg)) | ||
|
||
try: | ||
yield self.subscribe(on_event, topic) | ||
exit_code = 0 | ||
exit_message = 'OK - Socket session fully established' | ||
reactor.stop() | ||
except Exception: | ||
exit_code = 1 | ||
exit_message = 'Warning - Connected, but no subscription made' | ||
|
||
def onLeave(self, details): | ||
""" | ||
There is a bug in authobahn.wamp.protocol.py that always calls | ||
disconnect() even if there is no longer a transport, so let's just | ||
override it. This way Nagios doesn't get 'Unhandled Error' as the | ||
exit message. | ||
""" | ||
if self._transport: | ||
self.disconnect() | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description='A Nagios plugin to monitor WAMP servers' | ||
) | ||
|
||
parser = argparse.ArgumentParser(description='A Nagios plugin to monitor WAMP servers') | ||
parser.add_argument('-H', '--host') | ||
parser.add_argument('-R', '--realm') | ||
parser.add_argument('-T', '--topic') | ||
parser.add_argument('--debug', action="store_true") | ||
parser.add_argument('-t', '--timeout', type=int) | ||
args = parser.parse_args() | ||
|
||
host = 'wss://localhost:8080' | ||
if args.host != None: | ||
host = 'wss://localhost:8080/ws' | ||
if args.host is not None: | ||
host = args.host | ||
|
||
realm = 'realm1' | ||
if args.realm is not None: | ||
realm = args.realm | ||
|
||
topic = 'notifications.1' | ||
if args.topic is not None: | ||
topic = args.topic | ||
|
||
debug = False | ||
if args.debug != None: | ||
if args.debug is not None: | ||
debug = args.debug | ||
|
||
timeout = 30 | ||
if args.timeout != None: | ||
if args.timeout is not None: | ||
timeout = args.timeout | ||
|
||
|
||
try: | ||
if debug: | ||
log.startLogging(sys.stdout) | ||
|
||
factory = WampClientFactory(host, debugWamp=debug) | ||
factory.protocol = NagiosMonitor | ||
connectWS(factory, timeout = timeout) | ||
reactor.run() | ||
from autobahn.twisted.wamp import ApplicationRunner | ||
runner = ApplicationRunner(host, realm) | ||
runner.run(Component) | ||
except KeyboardInterrupt: | ||
print "Shutdown requested...exiting" | ||
except Exception: | ||
traceback.print_exc(file=sys.stdout) | ||
exit_code = 2 | ||
exit_message = "CRITICAL - Unable to connect to socket server" | ||
|
||
print exit_message | ||
sys.exit(exit_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Twisted==13.2.0 | ||
autobahn==0.6.4 | ||
pyOpenSSL==0.13.1 | ||
Twisted==15.1.0 | ||
autobahn[twisted]==0.10.2 | ||
pyOpenSSL==0.15.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters