1
2
3
4
5
6
7
8
9
10
11
12 import sys
13 from optparse import OptionParser
14 from amqplib.client_0_8.connection import Connection
15 from contextlib import closing
16
17 import Globals
18 from Products.ZenUtils.GlobalConfig import getGlobalConfiguration
19
20 import logging
21 LOG = logging.getLogger("zen.qverify")
22
23 -def _addSetting(name, settings, globalSettings, default=None):
27
28 _ZEN_AMQP_SETTINGS = {
29 'amqphost': 'localhost',
30 'amqpport': 5672,
31 'amqpuser': 'zenoss',
32 'amqppassword': 'zenoss',
33 'amqpvhost': '/zenoss',
34 'amqpusessl': False,
35 'amqpadminport': 55672,
36 'amqpadminusessl': False,
37 }
38
40
43
45 zenSettings = {}
46 for setting, default in _ZEN_AMQP_SETTINGS.items():
47 _addSetting(setting, zenSettings, self._global_conf, default)
48 zenSettings.update(extraParams)
49 ssl = zenSettings.get('amqpadminusessl', False) in ('1', 'True', 'true', 1, True)
50 settings = {
51 'host': '%(amqphost)s:%(amqpadminport)s' % zenSettings,
52 'userid': '%(amqpuser)s' % zenSettings,
53 'password': '%(amqppassword)s' % zenSettings,
54 'virtual_host': '%(amqpvhost)s' % zenSettings,
55 'ssl': ssl,
56 }
57
58 for name, value in extraParams.items():
59 if name not in _ZEN_AMQP_SETTINGS:
60 settings[name] = value
61 return settings
62
64 zenSettings = {}
65 for setting, default in _ZEN_AMQP_SETTINGS.items():
66 _addSetting(setting, zenSettings, self._global_conf, default)
67 zenSettings.update(extraParams)
68 ssl = zenSettings.get('amqpusessl', False) in ('1', 'True', 'true', 1, True)
69 settings = {
70 'host': '%(amqphost)s:%(amqpport)s' % zenSettings,
71 'userid': '%(amqpuser)s' % zenSettings,
72 'password': '%(amqppassword)s' % zenSettings,
73 'virtual_host': '%(amqpvhost)s' % zenSettings,
74 'ssl': ssl,
75 }
76
77 for name, value in extraParams.items():
78 if name not in _ZEN_AMQP_SETTINGS:
79 settings[name] = value
80 return settings
81
85
89
91
92 - def __init__(self, verbose=False):
93 self._verbose = verbose
94 LOG.debug("Getting global conf")
95 self._global_conf = getGlobalConfiguration()
96
97 - def _get_setting(self, name):
98 val = self._global_conf.get(name, None)
99 if val is None:
100 print >> sys.stderr, "global.conf setting %s must be set." % name
101 sys.exit(1)
102 return val
103
104 - def verify(self, expected_version):
105 conn = None
106 rc = 1
107 try:
108
109 for setting in _ZEN_AMQP_SETTINGS:
110 self._get_setting(setting)
111 conn = ZenAmqp().getConnection()
112 server_version = conn.server_properties.get('version')
113 e_ver = tuple(int(v) for v in expected_version.split('.'))
114 s_ver = tuple(int(v) for v in server_version.split('.'))
115 if s_ver < e_ver:
116 print >> sys.stderr, "Server version: %s < Expected version: %s" % (
117 server_version, expected_version)
118 rc = 2
119 else:
120 if self._verbose:
121 print "Server version: %s" % server_version
122 rc = 0
123 finally:
124 if conn:
125 conn.close()
126 sys.exit(rc)
127
128 if __name__=="__main__":
129 usage = "%prog VERSION_NUMBER"
130 epilog = "Verifies connectivity with the amqp server configued in global.conf and " \
131 "checks if server version is >= VERSION_NUMBER. Returns exit code 1 if " \
132 "connection fails, 2 if server version < VERSION_NUMBER, and 0 if " \
133 "connection is OK and server version >= VERSION_NUMBER."
134 parser = OptionParser(usage=usage, epilog=epilog)
135 parser.add_option("--verbose", "-v", default=False, action='store_true')
136 (options, args) = parser.parse_args()
137
138 if len(args) != 1:
139 parser.print_help()
140 sys.exit(1)
141
142 main = Main(options.verbose)
143 main.verify(args[0])
144