Skip to content

Commit f81d442

Browse files
committed
internal code restructuring
-move the text messages to a separate file -move argparser to a separate module -rename the 'keyPath' cmd param to the 'tlsKeyPath'
1 parent 6a01353 commit f81d442

File tree

6 files changed

+142
-87
lines changed

6 files changed

+142
-87
lines changed

example_deployment_scripts/bridge_deployment/bridge-deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ spec:
5555
env:
5656
- name: SERVER
5757
value: $(IBM_SPECTRUM_SCALE_PERF_QUERY_SERVICE_HOST)
58-
- name: KEYPATH
58+
- name: TLSKEYPATH
5959
value: /opt/registry/certs
6060
- name: PORT
6161
value: "8443"

source/Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ ENV PORT=$HTTPPORT
2424
RUN echo "the HTTP/S port is set to $PORT"
2525

2626
ARG CERTPATH
27-
ENV KEYPATH=$CERTPATH
28-
RUN if [ -z "$KEYPATH" ] && [ "$PORT" -eq 8443 ]; then echo "KEYPATH FOR SSL CONNECTION NOT SET - ERROR"; exit 1; else echo "PASS"; fi
29-
RUN echo "the ssl certificates path is set to $KEYPATH"
27+
ENV TLSKEYPATH=$CERTPATH
28+
RUN if [ -z "$TLSKEYPATH" ] && [ "$PORT" -eq 8443 ]; then echo "TLSKEYPATH FOR SSL CONNECTION NOT SET - ERROR"; exit 1; else echo "PASS"; fi
29+
RUN echo "the ssl certificates path is set to $TLSKEYPATH"
3030

3131
ARG PMCOLLECTORIP=0.0.0.0
3232
ENV SERVER=$PMCOLLECTORIP
@@ -44,10 +44,10 @@ RUN echo "$(pwd)"
4444
RUN touch $LOGPATH
4545
RUN echo "log path: $(dirname $LOGPATH)" >> $LOGPATH
4646
RUN echo "pmcollector_server: $SERVER" >> $LOGPATH
47-
RUN echo "ssl certificates location: $KEYPATH" >> $LOGPATH
47+
RUN echo "ssl certificates location: $TLSKEYPATH" >> $LOGPATH
4848
RUN echo "HTTP/S port: $PORT" >> $LOGPATH
4949

50-
CMD ["sh", "-c", "python3 zimonGrafanaIntf.py -c 10 -s $SERVER -p $PORT -k $KEYPATH"]
50+
CMD ["sh", "-c", "python3 zimonGrafanaIntf.py -c 10 -s $SERVER -p $PORT -t $TLSKEYPATH"]
5151

5252
EXPOSE 4242 8443
5353

source/confParser.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''
2+
##############################################################################
3+
# Copyright 2021 IBM Corp.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
##############################################################################
17+
18+
Created on Feb 15, 2021
19+
20+
@author: HWASSMAN
21+
'''
22+
23+
import argparse
24+
import os
25+
import logging.handlers
26+
from messages import ERR, MSG
27+
28+
29+
def findKeyFile(path):
30+
for name in ["privkey.pem", "tls.key"]:
31+
for root, dirs, files in os.walk(path):
32+
if name in files:
33+
return name
34+
return None
35+
36+
def findCertFile(path):
37+
for name in ["cert.pem", "tls.crt"]:
38+
for root, dirs, files in os.walk(path):
39+
if name in files:
40+
return name
41+
return None
42+
43+
44+
def parse_cmd_args(argv):
45+
'''parse input parameters'''
46+
47+
parser = argparse.ArgumentParser('python zimonGrafanaIntf.py')
48+
parser.add_argument('-s', '--server', action="store", default='localhost',
49+
help='Host name or ip address of the ZIMon collector (Default: 127.0.0.1) \
50+
NOTE: Per default ZIMon does not accept queries from remote machines. \
51+
To run the bridge from outside of the ZIMon collector, you need to modify ZIMon queryinterface settings (\'ZIMonCollector.cfg\')')
52+
parser.add_argument('-P', '--serverPort', action="store", type=int, default=9084, help='ZIMon collector port number (Default: 9084)')
53+
parser.add_argument('-l', '--logFile', action="store", default="./logs/zserver.log", help='location of the log file (Default: ./logs/zserver.log')
54+
parser.add_argument('-c', '--logLevel', action="store", type=int, default=logging.INFO, help='log level 10 (DEBUG), 20 (INFO), 30 (WARN), 40 (ERROR) (Default: 20)')
55+
parser.add_argument('-p', '--port', action="store", type=int, choices=[4242, 8443], default=4242, help='port number listening on for HTTP(S) connections (Default: 4242)')
56+
parser.add_argument('-t', '--tlsKeyPath', action="store", help='Directory path of tls privkey.pem and cert.pem file location (Required only for HTTPS port 8443)')
57+
58+
args = parser.parse_args(argv)
59+
60+
if args.port == 8443 and not args.tlsKeyPath:
61+
return None, MSG['MissingParm']
62+
elif args.port == 8443 and not os.path.exists(args.tlsKeyPath):
63+
return None, MSG['KeyPathError']
64+
elif args.port == 8443:
65+
certFile = findCertFile(args.tlsKeyPath)
66+
keyFile = findKeyFile(args.tlsKeyPath)
67+
if (not certFile) or (not keyFile):
68+
return None, MSG['CertError']
69+
70+
return args, ''

source/messages.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
##############################################################################
3+
# Copyright 2021 IBM Corp.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
##############################################################################
17+
18+
Created on Feb 15, 2021
19+
20+
@author: HWASSMAN
21+
'''
22+
23+
24+
ERR = {400: 'Bad Request',
25+
404: 'Not Found',
26+
500: 'Internal Server Error. Please check logs for more details.'}
27+
28+
MSG = {'IntError': 'Server internal error occurred. Reason: {}',
29+
'sysStart': 'Initial cherryPy server engine start have been invoked. Python version: {}, cherryPy version: {}.',
30+
'MissingParm': 'Missing mandatory parameters, quitting',
31+
'KeyPathError': 'KeyPath directory not found, quitting',
32+
'CertError': 'Missing certificates in tht specified keyPath directory, quitting',
33+
'CollectorErr': 'Failed to initialize connection to pmcollector, quitting',
34+
'MetaError': 'Metadata could not be retrieved. Check log file for more details, quitting',
35+
'MetaSuccess': 'Successfully retrieved MetaData',
36+
'QueryError': 'Query request could not be proceed. Reason: {}',
37+
'SearchErr': 'Search for {} did cause exception: {}',
38+
'LookupErr': 'Lookup for metric {} did not return any results',
39+
'FilterByErr': 'No component entry found for the specified \'filterby\' attribute',
40+
'GroupByErr': 'In the current setup the group aggregation \'groupby\' is not possible.',
41+
'MetricErr': 'Metric {0} cannot be found. Please check if the corresponding sensor is configured',
42+
'InconsistentParams': 'Received parameters {} inconsistent with request parameters {}',
43+
'SensorDisabled': 'Sensor for metric {} is disabled',
44+
'NoData': 'Empty results received', # Please check the pmcollector is properly configured and running.
45+
'BucketsizeChange': 'Based on requested downsample value: {} the bucketsize will be set: {}',
46+
'BucketsizeToPeriod': 'Bucketsize will be set to sensors period: {}',
47+
'ReceivedQuery': 'Received query request for query:{}, start:{}, end:{}',
48+
'RunQuery': 'Execute zimon query: {}',
49+
'AttrNotValid': 'Invalid attribute:{}',
50+
'AllowedAttrValues': 'For attribute {} applicable values:{}',
51+
'ReceivAttrValues': 'Received {}:{}',
52+
'TimerInfo': 'Processing {} took {} seconds',
53+
'Query2port': 'For better bridge performance multithreaded port {} will be used',
54+
'CollectorConnInfo': 'Connection to the collector server established successfully',
55+
'BridgeVersionInfo': ' *** IBM Spectrum Scale bridge for Grafana - Version: {} ***'
56+
}

source/zimonGrafanaIntf.py

Lines changed: 9 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import cherrypy
2424
import json
2525
import re
26-
import argparse
2726
import logging.handlers
2827
import sys
2928
import socket
@@ -34,44 +33,12 @@
3433
from queryHandler.Topo import Topo
3534
from queryHandler import SensorConfig
3635
from __version__ import __version__
36+
from messages import ERR, MSG
37+
from confParser import parse_cmd_args, findCertFile, findKeyFile
3738
from collections import defaultdict
3839
from timeit import default_timer as timer
3940

4041

41-
ERR = {400: 'Bad Request',
42-
404: 'Not Found',
43-
500: 'Internal Server Error. Please check logs for more details.'}
44-
45-
MSG = {'IntError': 'Server internal error occurred. Reason: {}',
46-
'sysStart': 'Initial cherryPy server engine start have been invoked. Python version: {}, cherryPy version: {}.',
47-
'MissingParm': 'Missing mandatory parameters, quitting',
48-
'KeyPathError': 'KeyPath directory not found, quitting',
49-
'CertError': 'Missing certificates in tht specified keyPath directory, quitting',
50-
'CollectorErr': 'Failed to initialize connection to pmcollector, quitting',
51-
'MetaError': 'Metadata could not be retrieved. Check log file for more details, quitting',
52-
'MetaSuccess': 'Successfully retrieved MetaData',
53-
'QueryError': 'Query request could not be proceed. Reason: {}',
54-
'SearchErr': 'Search for {} did cause exception: {}',
55-
'LookupErr': 'Lookup for metric {} did not return any results',
56-
'FilterByErr': 'No component entry found for the specified \'filterby\' attribute',
57-
'GroupByErr': 'In the current setup the group aggregation \'groupby\' is not possible.',
58-
'MetricErr': 'Metric {0} cannot be found. Please check if the corresponding sensor is configured',
59-
'InconsistentParams': 'Received parameters {} inconsistent with request parameters {}',
60-
'SensorDisabled': 'Sensor for metric {} is disabled',
61-
'NoData': 'Empty results received', # Please check the pmcollector is properly configured and running.
62-
'BucketsizeChange': 'Based on requested downsample value: {} the bucketsize will be set: {}',
63-
'BucketsizeToPeriod': 'Bucketsize will be set to sensors period: {}',
64-
'ReceivedQuery': 'Received query request for query:{}, start:{}, end:{}',
65-
'RunQuery': 'Execute zimon query: {}',
66-
'AttrNotValid': 'Invalid attribute:{}',
67-
'AllowedAttrValues': 'For attribute {} applicable values:{}',
68-
'ReceivAttrValues': 'Received {}:{}',
69-
'TimerInfo': 'Processing {} took {} seconds',
70-
'Query2port': 'For better bridge performance multithreaded port {} will be used',
71-
'CollectorConnInfo': 'Connection to the collector server established successfully',
72-
'BridgeVersionInfo': 'IBM Spectrum Scale bridge for Grafana - Version: {}'
73-
}
74-
7542

7643
class MetadataHandler():
7744

@@ -607,22 +574,6 @@ def validateCollectorConf(args, logger):
607574
logger.info(MSG['Query2port'].format(args.serverPort))
608575

609576

610-
def findKeyFile(path):
611-
for name in ["privkey.pem", "tls.key"]:
612-
for root, dirs, files in os.walk(path):
613-
if name in files:
614-
return name
615-
return None
616-
617-
618-
def findCertFile(path):
619-
for name in ["cert.pem", "tls.crt"]:
620-
for root, dirs, files in os.walk(path):
621-
if name in files:
622-
return name
623-
return None
624-
625-
626577
def updateCherrypyConf(args):
627578

628579
path, folder = os.path.split(args.logFile)
@@ -642,9 +593,9 @@ def updateCherrypyConf(args):
642593
cherrypy.config.update(customconf)
643594

644595

645-
def updateCherrypySslConf(args, certFile, keyFile):
646-
certPath = os.path.join(args.keyPath, certFile)
647-
keyPath = os.path.join(args.keyPath, keyFile)
596+
def updateCherrypySslConf(args):
597+
certPath = os.path.join(args.tlsKeyPath, findCertFile(args.tlsKeyPath))
598+
keyPath = os.path.join(args.tlsKeyPath, findKeyFile(args.tlsKeyPath))
648599
sslConfig = {'global': {'server.ssl_module': 'builtin',
649600
'server.ssl_certificate': certPath,
650601
'server.ssl_private_key': keyPath}}
@@ -654,31 +605,9 @@ def updateCherrypySslConf(args, certFile, keyFile):
654605
def main(argv):
655606

656607
# parse input arguments
657-
parser = argparse.ArgumentParser('python zimonGrafanaIntf.py')
658-
parser.add_argument('-s', '--server', action="store", default='localhost',
659-
help='Host name or ip address of the ZIMon collector (Default: 127.0.0.1) \
660-
NOTE: Per default ZIMon does not accept queries from remote machines. \
661-
To run the bridge from outside of the ZIMon collector, you need to modify ZIMon queryinterface settings (\'ZIMonCollector.cfg\')')
662-
parser.add_argument('-P', '--serverPort', action="store", type=int, default=9084, help='ZIMon collector port number (Default: 9084)')
663-
parser.add_argument('-l', '--logFile', action="store", default="./logs/zserver.log", help='location of the log file (Default: ./logs/zserver.log')
664-
parser.add_argument('-c', '--logLevel', action="store", type=int, default=logging.INFO, help='log level 10 (DEBUG), 20 (INFO), 30 (WARN), 40 (ERROR) (Default: 20)')
665-
parser.add_argument('-p', '--port', action="store", type=int, default=4242, help='port number to listen on (Default: 4242)')
666-
parser.add_argument('-k', '--keyPath', action="store", help='Directory path of privkey.pem and cert.pem file location(Required only for HTTPS port 8443)')
667-
668-
args = parser.parse_args(argv)
669-
670-
if args.port == 8443 and not args.keyPath:
671-
print(MSG['MissingParm'])
672-
return
673-
elif args.port == 8443 and not os.path.exists(args.keyPath):
674-
print(MSG['KeyPathError'])
675-
return
676-
elif args.port == 8443:
677-
certFile = findCertFile(args.keyPath)
678-
keyFile = findKeyFile(args.keyPath)
679-
if (not certFile) or (not keyFile):
680-
print(MSG['CertError'])
681-
return
608+
args, msg = parse_cmd_args(argv)
609+
if not args:
610+
print(msg)
682611

683612
# prepare the logger
684613
logger = configureLogging(args.logFile, args.logLevel)
@@ -687,7 +616,7 @@ def main(argv):
687616
# prepare cherrypy server configuration
688617
updateCherrypyConf(args)
689618
if args.port == 8443:
690-
updateCherrypySslConf(args, certFile, keyFile)
619+
updateCherrypySslConf(args)
691620

692621
# prepare metadata
693622
try:

tests/test_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_case04():
2222

2323

2424
def test_case05():
25-
result = os.system('python ./source/zimonGrafanaIntf.py --port 8443 --keyPath "/tmp"')
25+
result = os.system('python ./source/zimonGrafanaIntf.py --port 8443 --tlsKeyPath "/tmp"')
2626
assert result == 0
2727

2828

0 commit comments

Comments
 (0)