Skip to content

Commit 3064f13

Browse files
author
Camron Levanger
committed
This is a complete rewrite of the monitor to support WAMP v2 and the latest autobahn libraries.
Documentation and manifest updated as well.
1 parent b09c5a9 commit 3064f13

File tree

4 files changed

+72
-48
lines changed

4 files changed

+72
-48
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
nagios_wss
22
==========
33

4-
A Nagios Web Socket Monitoring Plugin
4+
A Nagios WAMP V2 Web Socket Monitoring Plugin
55
-------------------------------------
6+
###If you need WAMP V1 monitoring checkout my older releases (v1.0.0)
67

78
A Nagios NRPE script that allows you to monitor a web socket server using
8-
the WAMP protocol. While it is fairly easy with Nagios to monitor whether
9+
the *WAMP V2* protocol. While it is fairly easy with Nagios to monitor whether
910
or not your server proccess is running, unfortunately that can't tell us
1011
if that server is available and accepting connections.
1112

@@ -32,14 +33,13 @@ and finally set proper permissions.
3233
git clone git@github.com:camronlevanger/nagios-wss.git
3334
cd nagios-wss
3435
pip install -r requirements.txt
35-
chmod +x check_wss_conn.py
3636

3737
Then restart the NRPE service, on Ubuntu that looks like:
3838

3939
service nagios-nrpe-server restart
4040

4141
####Install Using Pip
42-
Coming Soon
42+
Coming Soon(er or later)
4343

4444
###Add nagios-wss Command to NRPE Client
4545
Now we need to edit our nrpe.cfg file and add our new plugin command.
@@ -59,9 +59,11 @@ To pass args to the command make sure you set in nrpe.cfg
5959

6060
dont_blame_nrpe=1
6161

62-
And then the host arg is
62+
###Important arguments:
6363

64-
-H host_uri
64+
+ -H host_uri | The url you use to connect to the socket server - This defaults to wss://localhost:8080/ws
65+
+ -R realm | The WAMP realm your server is listening on - defaults to realm1
66+
+ -T topic | The name of the topic you want the monitor to attempt subscribing to - defaults to notifications.1
6567

6668
###Add nagios-wss to Nagios Checks on Your Nagios Monitoring Server
6769
Define a new service for nagios-wss

check_wss_conn.py

100644100755
Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,98 @@
11
#!/usr/bin/python
22
import sys
3-
import os
4-
import traceback
53
import argparse
6-
74
from twisted.python import log
85
from twisted.internet import reactor
6+
from twisted.internet.defer import inlineCallbacks
97

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
139

1410
exit_code = 3
1511
exit_message = 'UNKNOWN - Unable to get info for socket connections'
12+
topic = 'notifications.1'
1613

17-
class NagiosMonitor(WampClientProtocol):
1814

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):
2416

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+
"""
3221

33-
def clientConnectionFailed(self, connector, reason):
22+
@inlineCallbacks
23+
def onJoin(self, details):
3424
global exit_code
3525
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()
4051

4152
if __name__ == '__main__':
53+
parser = argparse.ArgumentParser(
54+
description='A Nagios plugin to monitor WAMP servers'
55+
)
4256

43-
parser = argparse.ArgumentParser(description='A Nagios plugin to monitor WAMP servers')
4457
parser.add_argument('-H', '--host')
58+
parser.add_argument('-R', '--realm')
59+
parser.add_argument('-T', '--topic')
4560
parser.add_argument('--debug', action="store_true")
4661
parser.add_argument('-t', '--timeout', type=int)
4762
args = parser.parse_args()
4863

49-
host = 'wss://localhost:8080'
50-
if args.host != None:
64+
host = 'wss://localhost:8080/ws'
65+
if args.host is not None:
5166
host = args.host
5267

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+
5376
debug = False
54-
if args.debug != None:
77+
if args.debug is not None:
5578
debug = args.debug
5679

5780
timeout = 30
58-
if args.timeout != None:
81+
if args.timeout is not None:
5982
timeout = args.timeout
6083

61-
6284
try:
6385
if debug:
6486
log.startLogging(sys.stdout)
6587

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)
7091
except KeyboardInterrupt:
7192
print "Shutdown requested...exiting"
7293
except Exception:
73-
traceback.print_exc(file=sys.stdout)
94+
exit_code = 2
95+
exit_message = "CRITICAL - Unable to connect to socket server"
7496

7597
print exit_message
7698
sys.exit(exit_code)

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Twisted==13.2.0
2-
autobahn==0.6.4
3-
pyOpenSSL==0.13.1
1+
Twisted==15.1.0
2+
autobahn[twisted]==0.10.2
3+
pyOpenSSL==0.15.1

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
# Versions should comply with PEP440. For a discussion on single-sourcing
1515
# the version across setup.py and the project code, see
1616
# http://packaging.python.org/en/latest/tutorial.html#version
17-
version='0.1.0',
17+
version='2.0.0',
1818

19-
description='Monitor WAMP servers with Nagios.',
19+
description='Monitor WAMP V2 servers with Nagios.',
2020
long_description=long_description,
2121

2222
# The project's main homepage.
@@ -35,7 +35,7 @@
3535
# 3 - Alpha
3636
# 4 - Beta
3737
# 5 - Production/Stable
38-
'Development Status :: 4 - Beta',
38+
'Development Status :: 3 - Alpha',
3939

4040
# Indicate who your project is intended for
4141
'Intended Audience :: Developers',
@@ -62,7 +62,7 @@
6262
# project is installed. For an analysis of "install_requires" vs pip's
6363
# requirements files see:
6464
# https://packaging.python.org/en/latest/technical.html#install-requires-vs-requirements-files
65-
install_requires=['Twisted==13.2.0', 'autobahn==0.6.4', 'pyOpenSSL==0.13.1'],
65+
install_requires=['Twisted==15.1.0', 'autobahn[twisted]==0.10.2', 'pyOpenSSL==0.15.1'],
6666

6767
# To provide executable scripts, use entry points in preference to the
6868
# "scripts" keyword. Entry points provide cross-platform support and allow

0 commit comments

Comments
 (0)