-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_amqp1.0
executable file
·140 lines (122 loc) · 4.67 KB
/
check_amqp1.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# vim: set fileencoding=utf-8
# Copyright (c) 2015-, IT Services, Stockholm University
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of Stockholm University nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from proton import Message, SSLDomain, Url
from proton.utils import BlockingConnection
from pynag.Plugins import PluginHelper,ok,warning,critical,unknown
import logging
import os.path
import time
## Create the plugin option
np = PluginHelper()
# Configure logging
# FIXME Use logging.config.fileConfig() instead?
logging.basicConfig()
log = logging.getLogger(os.path.basename(__file__))
## Command line arguments
np.parser.add_option("-H", "--hostname",
help="Hostname to connect to (default: %default)",
default="localhost",
)
np.parser.add_option("-P", "--port",
help="Port to connect with",
type="int",
)
np.parser.add_option("-u", "--username",
help="Username to connect with (default: %default)",
default="system",
)
np.parser.add_option("-p", "--password",
help="Password to connect with (default: %default)",
default="manager",
)
np.parser.add_option("-s", "--ssl",
help="Use SSL when connecting (default: %default)",
default=False,
action="store_true",
)
np.parser.add_option("-c", "--cafile",
help="CA to verify against when using SSL (default: %default)",
default="/etc/ssl/certs/ca-certificates.crt",
)
np.parse_arguments()
# Set loglevel based on verbose
if np.options.verbose:
log.setLevel(logging.DEBUG)
# Santiy checks
# Change the port if not set and we're using SSL
if np.options.ssl and not np.options.port:
log.debug("Silently changing to SSL port since SSL is requested but the default SSL port number isn't used.")
np.options.port = 5671
# Ensure we have a CAfile if SSL is enabled (and that it exists)
if np.options.ssl and (not np.options.cafile or not os.path.isfile(np.options.cafile)):
np.exit(exit_code=critical, summary="SSL is enabled but option 'cafile' wasn't set or it doesn't exist (%s)" % (np.options.cafile))
try:
# Set SSL settings
ssl_domain = SSLDomain(SSLDomain.MODE_CLIENT)
ssl_domain.set_trusted_ca_db(np.options.cafile)
# FIXME Make this configurable. VERIFY_PEER_NAME is useful
ssl_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
url = Url()
url.host = np.options.hostname
url.port = np.options.port
url.username = np.options.username
url.scheme = "amqp%s" % ("s" if np.options.ssl else "")
url.path = "aliveness-test"
log.debug("Connecting to '%r'" % (url))
conn = BlockingConnection(
password=np.options.password,
url=url,
)
log.debug("Creating consumer")
receiver = conn.create_receiver(url.path)
log.debug("Creating producer")
sender = conn.create_sender(url.path)
sent_msg = Message(
body="Faxa mig goda kakor.",
# Test the durable/persistent storage
durable=True,
creation_time=int(time.time()),
ttl=60,
)
log.debug("Created message: %r" % (sent_msg))
log.debug("Sending message")
sender.send(sent_msg)
recv_msg = receiver.receive(timeout=np.options.timeout)
log.debug("Received message: %r" % (recv_msg))
receiver.accept()
conn.close()
if recv_msg:
np.add_status(ok)
np.add_summary("Received message %r" % (recv_msg))
except Exception as e:
np.add_status(critical)
np.add_summary('%s: %r' % (e.__class__.__name__, e))
np.exit()