Skip to content

Commit f698063

Browse files
authored
Merge pull request #15 from IBM/v6.1
internal code restructuring
2 parents 14ab811 + ee520c1 commit f698063

File tree

6 files changed

+145
-90
lines changed

6 files changed

+145
-90
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 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+
37+
def findCertFile(path):
38+
for name in ["cert.pem", "tls.crt"]:
39+
for root, dirs, files in os.walk(path):
40+
if name in files:
41+
return name
42+
return None
43+
44+
45+
def parse_cmd_args(argv):
46+
'''parse input parameters'''
47+
48+
parser = argparse.ArgumentParser('python zimonGrafanaIntf.py')
49+
parser.add_argument('-s', '--server', action="store", default='localhost',
50+
help='Host name or ip address of the ZIMon collector (Default: 127.0.0.1) \
51+
NOTE: Per default ZIMon does not accept queries from remote machines. \
52+
To run the bridge from outside of the ZIMon collector, you need to modify ZIMon queryinterface settings (\'ZIMonCollector.cfg\')')
53+
parser.add_argument('-P', '--serverPort', action="store", type=int, default=9084, help='ZIMon collector port number (Default: 9084)')
54+
parser.add_argument('-l', '--logFile', action="store", default="./logs/zserver.log", help='location of the log file (Default: ./logs/zserver.log')
55+
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)')
56+
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)')
57+
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)')
58+
59+
args = parser.parse_args(argv)
60+
61+
if args.port == 8443 and not args.tlsKeyPath:
62+
return None, MSG['MissingParm']
63+
elif args.port == 8443 and not os.path.exists(args.tlsKeyPath):
64+
return None, MSG['KeyPathError']
65+
elif args.port == 8443:
66+
certFile = findCertFile(args.tlsKeyPath)
67+
keyFile = findKeyFile(args.tlsKeyPath)
68+
if (not certFile) or (not keyFile):
69+
return None, MSG['CertError']
70+
71+
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 & 81 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,45 +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-
75-
7642
class MetadataHandler():
7743

7844
def __init__(self, logger, server, port=9084):
@@ -607,22 +573,6 @@ def validateCollectorConf(args, logger):
607573
logger.info(MSG['Query2port'].format(args.serverPort))
608574

609575

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-
626576
def updateCherrypyConf(args):
627577

628578
path, folder = os.path.split(args.logFile)
@@ -642,9 +592,9 @@ def updateCherrypyConf(args):
642592
cherrypy.config.update(customconf)
643593

644594

645-
def updateCherrypySslConf(args, certFile, keyFile):
646-
certPath = os.path.join(args.keyPath, certFile)
647-
keyPath = os.path.join(args.keyPath, keyFile)
595+
def updateCherrypySslConf(args):
596+
certPath = os.path.join(args.tlsKeyPath, findCertFile(args.tlsKeyPath))
597+
keyPath = os.path.join(args.tlsKeyPath, findKeyFile(args.tlsKeyPath))
648598
sslConfig = {'global': {'server.ssl_module': 'builtin',
649599
'server.ssl_certificate': certPath,
650600
'server.ssl_private_key': keyPath}}
@@ -654,31 +604,9 @@ def updateCherrypySslConf(args, certFile, keyFile):
654604
def main(argv):
655605

656606
# 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
607+
args, msg = parse_cmd_args(argv)
608+
if not args:
609+
print(msg)
682610

683611
# prepare the logger
684612
logger = configureLogging(args.logFile, args.logLevel)
@@ -687,7 +615,7 @@ def main(argv):
687615
# prepare cherrypy server configuration
688616
updateCherrypyConf(args)
689617
if args.port == 8443:
690-
updateCherrypySslConf(args, certFile, keyFile)
618+
updateCherrypySslConf(args)
691619

692620
# prepare metadata
693621
try:

tests/test_params.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ def test_case03():
1818

1919
def test_case04():
2020
result = os.system("python ./source/zimonGrafanaIntf.py --port 8443")
21-
assert result == 0
21+
assert result > 0
2222

2323

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

2828

2929
def test_case06():

0 commit comments

Comments
 (0)