2323import cherrypy
2424import json
2525import re
26- import argparse
2726import logging .handlers
2827import sys
2928import socket
3433from queryHandler .Topo import Topo
3534from queryHandler import SensorConfig
3635from __version__ import __version__
36+ from messages import ERR , MSG
37+ from confParser import parse_cmd_args , findCertFile , findKeyFile
3738from collections import defaultdict
3839from 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
7643class 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-
626577def 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):
654605def 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 :
0 commit comments