From 977465d6a0265ae6087b59fc4d6e62ab39891754 Mon Sep 17 00:00:00 2001 From: Gregory Nagy Date: Mon, 24 Feb 2014 13:41:13 +0100 Subject: [PATCH] Fixtures --- manage.py | 12 ++++++------ src/client.py | 37 +++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/manage.py b/manage.py index af796ae..3bfdd95 100644 --- a/manage.py +++ b/manage.py @@ -1,19 +1,19 @@ import sys -from src import cnfparse, client, collectables +from src import cnfparse +from src import client +from src.collectables import collectables def main(): if len(sys.argv) < 2: print("usage: manage.py run [options]") - if len(sys.argv) is not 2 and sys.argv[1] is not "run": print("[ERROR] Command cannot be parsed. Exiting...") return - - configuration, metrics = cnfparse.importConf("config/client.conf") + configuration, metrics = cnfparse.import_conf("config/client.conf") cli = client.Client(configuration) - cli.startReporting( - metricCollectors=collectables.collectables.provide(metrics)) + metricCollectors = collectables.provide(metrics) + cli.run(metricCollectors) if __name__ == "__main__": diff --git a/src/client.py b/src/client.py index 24373f6..23b102b 100644 --- a/src/client.py +++ b/src/client.py @@ -46,34 +46,38 @@ class Client: self.amqp_pass = str(os.getenv(self.env_config['amqp_pass'])) self.amqp_queue = str(os.getenv(self.env_config['amqp_queue'])) self.amqp_vhost = str(os.getenv(self.env_config['amqp_vhost'])) - if self.server_address is "": + host_check = self.__check_envvar(self.server_address) + port_check = self.__check_envvar(self.server_port) + amqp_pass_check = self.__check_envvar(self.amqp_pass) + amqp_user_check = self.__check_envvar(self.amqp_user) + amqp_queue_check = self.__check_envvar(self.amqp_queue) + amqp_vhost_check = self.__check_envvar(self.amqp_vhost) + if host_check: print(('%(host)s cannot be found in environmental variables.') % {'host': self.env_config['host']} ) - self.valid = False - return - if self.server_port is "": + raise RuntimeError + if port_check: print(('%(port)s cannot be found in environmental variables. ') % {'port': self.env_config['port']} ) - self.valid = False - return - if self.amqp_user is "" or self.amqp_pass is "": + raise RuntimeError + if amqp_user_check or amqp_pass_check: print(('%(user)s or %(pass)s cannot be ' 'found in environmental variables.') % {'user': self.env_config['amqp_user'], 'pass': self.env_config['amqp_pass']} ) - self.valid = False - return - if self.amqp_queue is "" or self.amqp_vhost is "": + raise RuntimeError + amqp_pass_check = self.__check_envvar(self.amqp_pass) + amqp_user_check = self.__check_envvar(self.amqp_user) + if amqp_vhost_check or amqp_queue_check: print(('%(queue)s or %(vhost)s cannot be ' 'found in environmental variables.') % {'queue': self.env_config['amqp_queue'], 'vhost': self.env_config['amqp_vhost']} ) - self.valid = False - return + raise RuntimeError self.debugMode = config["debugMode"] self.kvmCPU = int(config["kvmCpuUsage"]) self.kvmMem = int(config["kvmMemoryUsage"]) @@ -81,6 +85,9 @@ class Client: self.beat = 1 self.valid = True + def __check_envvar(variable): + return variable == "None" or variable == "" + def connect(self): """ This method creates the connection to the queue of the graphite @@ -258,14 +265,12 @@ class Client: metricCollectors parameter that should be provided by the collectables modul to work properly. """ - if self.valid is False: - print("[ERROR] The client cannot be started.") - raise RuntimeError if self.connect() is False: - hostdata = self.server_address + ':' + self.server_port + hostdata = self.server_address + ':' + str(self.server_port) print("[ERROR] An error has occured while connecting to the " "server on %(host)s." % {'host': hostdata}) + return else: print('[SUCCESS] Connection established to %(host)s:%(port)s.' % {'host': self.server_address, -- libgit2 0.26.0