|
1 | 1 | #!/usr/bin/python
|
2 | 2 | import sys
|
3 |
| -import os |
4 |
| -import traceback |
5 | 3 | import argparse
|
6 |
| - |
7 | 4 | from twisted.python import log
|
8 | 5 | from twisted.internet import reactor
|
| 6 | +from twisted.internet.defer import inlineCallbacks |
9 | 7 |
|
10 |
| -from autobahn.websocket import connectWS |
11 |
| -from autobahn.wamp import WampClientFactory |
12 |
| -from autobahn.wamp import WampClientProtocol |
| 8 | +from autobahn.twisted.wamp import ApplicationSession |
13 | 9 |
|
14 | 10 | exit_code = 3
|
15 | 11 | exit_message = 'UNKNOWN - Unable to get info for socket connections'
|
| 12 | +topic = 'notifications.1' |
16 | 13 |
|
17 |
| -class NagiosMonitor(WampClientProtocol): |
18 | 14 |
|
19 |
| - def onOpen(self): |
20 |
| - global exit_code |
21 |
| - global exit_message |
22 |
| - exit_code = 1 |
23 |
| - exit_message = 'Warning - Welcome message recieved, but no session' |
| 15 | +class Component(ApplicationSession): |
24 | 16 |
|
25 |
| - def onSessionOpen(self): |
26 |
| - global exit_code |
27 |
| - global exit_message |
28 |
| - exit_code = 0 |
29 |
| - exit_message = 'OK - Socket session fully established' |
30 |
| - self.dropConnection(self) |
31 |
| - reactor.stop() |
| 17 | + """ |
| 18 | + An application component that subscribes and receives events, |
| 19 | + and stop after having received 5 events. |
| 20 | + """ |
32 | 21 |
|
33 |
| - def clientConnectionFailed(self, connector, reason): |
| 22 | + @inlineCallbacks |
| 23 | + def onJoin(self, details): |
34 | 24 | global exit_code
|
35 | 25 | global exit_message
|
36 |
| - exit_code = 2 |
37 |
| - exit_message = "CRITICAL - Unable to connect to socket server" |
38 |
| - reactor.stop() |
39 |
| - |
| 26 | + global topic |
| 27 | + exit_code = 1 |
| 28 | + exit_message = 'Warning - Connected, but no subscription made' |
| 29 | + |
| 30 | + def on_event(msg): |
| 31 | + print("Got event: {}".format(msg)) |
| 32 | + |
| 33 | + try: |
| 34 | + yield self.subscribe(on_event, topic) |
| 35 | + exit_code = 0 |
| 36 | + exit_message = 'OK - Socket session fully established' |
| 37 | + reactor.stop() |
| 38 | + except Exception: |
| 39 | + exit_code = 1 |
| 40 | + exit_message = 'Warning - Connected, but no subscription made' |
| 41 | + |
| 42 | + def onLeave(self, details): |
| 43 | + """ |
| 44 | + There is a bug in authobahn.wamp.protocol.py that always calls |
| 45 | + disconnect() even if there is no longer a transport, so let's just |
| 46 | + override it. This way Nagios doesn't get 'Unhandled Error' as the |
| 47 | + exit message. |
| 48 | + """ |
| 49 | + if self._transport: |
| 50 | + self.disconnect() |
40 | 51 |
|
41 | 52 | if __name__ == '__main__':
|
| 53 | + parser = argparse.ArgumentParser( |
| 54 | + description='A Nagios plugin to monitor WAMP servers' |
| 55 | + ) |
42 | 56 |
|
43 |
| - parser = argparse.ArgumentParser(description='A Nagios plugin to monitor WAMP servers') |
44 | 57 | parser.add_argument('-H', '--host')
|
| 58 | + parser.add_argument('-R', '--realm') |
| 59 | + parser.add_argument('-T', '--topic') |
45 | 60 | parser.add_argument('--debug', action="store_true")
|
46 | 61 | parser.add_argument('-t', '--timeout', type=int)
|
47 | 62 | args = parser.parse_args()
|
48 | 63 |
|
49 |
| - host = 'wss://localhost:8080' |
50 |
| - if args.host != None: |
| 64 | + host = 'wss://localhost:8080/ws' |
| 65 | + if args.host is not None: |
51 | 66 | host = args.host
|
52 | 67 |
|
| 68 | + realm = 'realm1' |
| 69 | + if args.realm is not None: |
| 70 | + realm = args.realm |
| 71 | + |
| 72 | + topic = 'notifications.1' |
| 73 | + if args.topic is not None: |
| 74 | + topic = args.topic |
| 75 | + |
53 | 76 | debug = False
|
54 |
| - if args.debug != None: |
| 77 | + if args.debug is not None: |
55 | 78 | debug = args.debug
|
56 | 79 |
|
57 | 80 | timeout = 30
|
58 |
| - if args.timeout != None: |
| 81 | + if args.timeout is not None: |
59 | 82 | timeout = args.timeout
|
60 | 83 |
|
61 |
| - |
62 | 84 | try:
|
63 | 85 | if debug:
|
64 | 86 | log.startLogging(sys.stdout)
|
65 | 87 |
|
66 |
| - factory = WampClientFactory(host, debugWamp=debug) |
67 |
| - factory.protocol = NagiosMonitor |
68 |
| - connectWS(factory, timeout = timeout) |
69 |
| - reactor.run() |
| 88 | + from autobahn.twisted.wamp import ApplicationRunner |
| 89 | + runner = ApplicationRunner(host, realm) |
| 90 | + runner.run(Component) |
70 | 91 | except KeyboardInterrupt:
|
71 | 92 | print "Shutdown requested...exiting"
|
72 | 93 | except Exception:
|
73 |
| - traceback.print_exc(file=sys.stdout) |
| 94 | + exit_code = 2 |
| 95 | + exit_message = "CRITICAL - Unable to connect to socket server" |
74 | 96 |
|
75 | 97 | print exit_message
|
76 | 98 | sys.exit(exit_code)
|
0 commit comments