Skip to content

Commit 6309962

Browse files
authored
Merge pull request #129 from Helene/opentsdb
Check port settings before bind opentsdb api application
2 parents 72c3faf + afd8d38 commit 6309962

File tree

3 files changed

+77
-45
lines changed

3 files changed

+77
-45
lines changed

source/confParser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def checkTLSsettings(args):
4545
return True, ''
4646

4747

48+
def checkApplicationPort(args):
49+
if not args.get('port') or not args.get('apiKeyValue'):
50+
return False, MSG['MissingParm']
51+
return True, ''
52+
53+
4854
def checkAPIsettings(args):
4955
if not args.get('apiKeyName') or not args.get('apiKeyValue'):
5056
return False, MSG['MissingParm']
@@ -70,6 +76,8 @@ def getSettings(argv):
7076
settings = args
7177
else:
7278
return None, msg
79+
# check application port
80+
valid, msg = checkApplicationPort(settings)
7381
# check API key settings
7482
valid, msg = checkAPIsettings(settings)
7583
if not valid:

source/zimonGrafanaIntf.py

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ def setup_cherrypy_logging(args):
7979

8080
def updateCherrypyConf(args):
8181

82-
globalConfig = {'global': {'server.socket_port': args.get('port'),
83-
# default error response
84-
'error_page.default': format_default_error_page,
85-
# unexpected errors
82+
globalConfig = {'global': {'error_page.default': format_default_error_page,
8683
'request.error_response': handle_error},
8784
}
8885

@@ -91,6 +88,20 @@ def updateCherrypyConf(args):
9188
dirname, filename = os.path.split(os.path.abspath(__file__))
9289
customconf = os.path.join(dirname, 'mycherrypy.conf')
9390
cherrypy.config.update(customconf)
91+
cherrypy.server.unsubscribe()
92+
93+
94+
def bind_opentsdb_server(args):
95+
opentsdb_server = cherrypy._cpserver.Server()
96+
opentsdb_server.socket_port = args.get('port')
97+
opentsdb_server._socket_host = '0.0.0.0'
98+
if args.get('protocol') == "https":
99+
certPath = os.path.join(args.get('tlsKeyPath'), args.get('tlsCertFile'))
100+
keyPath = os.path.join(args.get('tlsKeyPath'), args.get('tlsKeyFile'))
101+
opentsdb_server.ssl_module = 'builtin'
102+
opentsdb_server.ssl_certificate = certPath
103+
opentsdb_server.ssl_private_key = keyPath
104+
opentsdb_server.subscribe()
94105

95106

96107
def updateCherrypySslConf(args):
@@ -164,8 +175,6 @@ def main(argv):
164175

165176
# prepare cherrypy server configuration
166177
updateCherrypyConf(args)
167-
if args.get('protocol') == "https":
168-
updateCherrypySslConf(args)
169178

170179
# prepare metadata
171180
try:
@@ -203,44 +212,47 @@ def main(argv):
203212
logger.error("ZiMon sensor configuration file not found")
204213
return
205214

206-
api = OpenTsdbApi(logger, mdHandler)
207-
cherrypy.tree.mount(api, '/api/query',
208-
{'/':
209-
{'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
210-
'request.body.processors': {'application/x-www-form-urlencoded': processFormJSON}
211-
}
212-
}
213-
)
214-
# query for metric name (openTSDB: zimon extension returns keys as well)
215-
cherrypy.tree.mount(api, '/api/suggest',
216-
{'/':
217-
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
218-
}
219-
)
220-
# query for tag name and value, given a metric (openTSDB)
221-
cherrypy.tree.mount(api, '/api/search/lookup',
222-
{'/':
223-
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
224-
}
225-
)
226-
# query to force update of metadata (zimon feature)
227-
cherrypy.tree.mount(api, '/api/update',
228-
{'/':
229-
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
230-
}
231-
)
232-
# query for list of aggregators (openTSDB)
233-
cherrypy.tree.mount(api, '/api/aggregators',
234-
{'/':
235-
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
236-
}
237-
)
238-
# query for list of filters (openTSDB)
239-
cherrypy.tree.mount(api, '/api/config/filters',
240-
{'/':
241-
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
242-
}
243-
)
215+
if args.get('port'):
216+
bind_opentsdb_server(args)
217+
api = OpenTsdbApi(logger, mdHandler)
218+
219+
cherrypy.tree.mount(api, '/api/query',
220+
{'/':
221+
{'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
222+
'request.body.processors': {'application/x-www-form-urlencoded': processFormJSON}
223+
}
224+
}
225+
)
226+
# query for metric name (openTSDB: zimon extension returns keys as well)
227+
cherrypy.tree.mount(api, '/api/suggest',
228+
{'/':
229+
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
230+
}
231+
)
232+
# query for tag name and value, given a metric (openTSDB)
233+
cherrypy.tree.mount(api, '/api/search/lookup',
234+
{'/':
235+
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
236+
}
237+
)
238+
# query to force update of metadata (zimon feature)
239+
cherrypy.tree.mount(api, '/api/update',
240+
{'/':
241+
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
242+
}
243+
)
244+
# query for list of aggregators (openTSDB)
245+
cherrypy.tree.mount(api, '/api/aggregators',
246+
{'/':
247+
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
248+
}
249+
)
250+
# query for list of filters (openTSDB)
251+
cherrypy.tree.mount(api, '/api/config/filters',
252+
{'/':
253+
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
254+
}
255+
)
244256

245257
try:
246258
files_to_watch = SensorConfig.get_config_paths()

tests/test_params.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from source.confParser import ConfigManager, merge_defaults_and_args, parse_cmd_args, checkCAsettings
1+
from source.confParser import ConfigManager, merge_defaults_and_args, parse_cmd_args, checkCAsettings, checkApplicationPort
22
from source.__version__ import __version__ as version
33
from nose2.tools.decorators import with_setup
44

@@ -133,3 +133,15 @@ def test_case12():
133133
assert isinstance(result.get('caCertPath'), str)
134134
assert valid == False
135135
assert len(msg) > 0
136+
137+
138+
@with_setup(my_setup)
139+
def test_case13():
140+
x = a.copy()
141+
del x['port']
142+
result = merge_defaults_and_args(x, b)
143+
valid, msg = checkApplicationPort(result)
144+
assert len(result.keys()) > 0
145+
assert 'port' not in result.keys()
146+
assert valid == False
147+
assert len(msg) > 0

0 commit comments

Comments
 (0)