Commit ec91e24f by Gregory Nagy

pep8 conventions

parent 3faa2b8f
...@@ -13,6 +13,15 @@ logging.basicConfig() ...@@ -13,6 +13,15 @@ logging.basicConfig()
class Client: class Client:
env_config = {
"host": "GRAPHITE_HOST",
"port": "GRAPHITE_PORT",
"amqp_user": "GRAPHITE_AMQP_USER",
"amqp_pass": "GRAPHITE_AMQP_PASSWORD",
"amqp_queue": "GRAPHITE_AMQP_QUEUE",
"amqp_vhost": "GRAPHITE_AMQP_VHOST",
}
def __init__(self, config): def __init__(self, config):
""" """
Constructor of the client class that is responsible for handling the Constructor of the client class that is responsible for handling the
...@@ -29,40 +38,42 @@ class Client: ...@@ -29,40 +38,42 @@ class Client:
""" """
hostname = socket.gethostname().split('.') hostname = socket.gethostname().split('.')
hostname.reverse() hostname.reverse()
self.name = "circle." + ".".join(hostname) separator = '.'
if os.getenv("GRAPHITE_SERVER_ADDRESS") is "": self.name = 'circle.%(host)s' % {'host': separator.join(hostname)}
print("GRAPHITE_SERVER_ADDRESS cannot be found in environmental " self.server_address = str(os.getenv(self.env_config['host']))
"variables" self.server_port = int(os.getenv(self.env_config['port']))
self.amqp_user = str(os.getenv(self.env_config['amqp_user']))
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 "":
print(('%(host)s cannot be found in environmental variables.')
% {'host': self.env_config['host']}
) )
self.valid = False self.valid = False
return return
if os.getenv("GRAPHITE_SERVER_PORT") is "": if self.server_port is "":
print("GRAPHITE_SERVER_PORT cannot be found in environmental " print(('%(port)s cannot be found in environmental variables. ')
"variables. (AMQP standard is: 5672" % {'port': self.env_config['port']}
) )
self.valid = False self.valid = False
return return
if os.getenv("GRAPHITE_AMQP_USER") is "" or os.getenv( if self.amqp_user is "" or self.amqp_pass is "":
"GRAPHITE_AMQP_PASSWORD") is "": print(('%(user)s or %(pass)s cannot be '
print("GRAPHITE_AMQP_USER or GRAPHITE_AMQP_PASSWORD cannot be " 'found in environmental variables.')
"found in environmental variables. (AMQP standard is: " % {'user': self.env_config['amqp_user'],
"guest-guest)" 'pass': self.env_config['amqp_pass']}
) )
self.valid = False self.valid = False
return return
if os.getenv("GRAPHITE_AMQP_QUEUE") is "" or os.getenv( if self.amqp_queue is "" or self.amqp_vhost is "":
"GRAPHITE_AMQP_VHOST") is "": print(('%(queue)s or %(vhost)s cannot be '
print("GRAPHITE_AMQP_QUEUE or GRAPHITE_AMQP_VHOST cannot be " 'found in environmental variables.')
"found in environmental variables." % {'queue': self.env_config['amqp_queue'],
'vhost': self.env_config['amqp_vhost']}
) )
self.valid = False self.valid = False
return return
self.server_address = str(os.getenv("GRAPHITE_SERVER_ADDRESS"))
self.server_port = int(os.getenv("GRAPHITE_SERVER_PORT"))
self.amqp_user = str(os.getenv("GRAPHITE_AMQP_USER"))
self.amqp_pass = str(os.getenv("GRAPHITE_AMQP_PASSWORD"))
self.amqp_queue = str(os.getenv("GRAPHITE_AMQP_QUEUE"))
self.amqp_vhost = str(os.getenv("GRAPHITE_AMQP_VHOST"))
self.debugMode = config["debugMode"] self.debugMode = config["debugMode"]
self.kvmCPU = int(config["kvmCpuUsage"]) self.kvmCPU = int(config["kvmCpuUsage"])
self.kvmMem = int(config["kvmMemoryUsage"]) self.kvmMem = int(config["kvmMemoryUsage"])
...@@ -70,7 +81,7 @@ class Client: ...@@ -70,7 +81,7 @@ class Client:
self.beat = 1 self.beat = 1
self.valid = True self.valid = True
def __connect(self): def connect(self):
""" """
This method creates the connection to the queue of the graphite This method creates the connection to the queue of the graphite
server using the environmental variables given in the constructor. server using the environmental variables given in the constructor.
...@@ -86,17 +97,17 @@ class Client: ...@@ -86,17 +97,17 @@ class Client:
self.channel = self.connection.channel() self.channel = self.connection.channel()
return True return True
except RuntimeError: except RuntimeError:
print ("[ERROR] Cannot connect to the server. " print ('[ERROR] Cannot connect to the server. '
"Parameters could be wrong." 'Parameters could be wrong.'
) )
return False return False
except: except:
print ("[ERROR] Cannot connect to the server. There is no one " print ('[ERROR] Cannot connect to the server. There is no one '
"listening on the other side." 'listening on the other side.'
) )
return False return False
def __disconnect(self): def disconnect(self):
""" """
Break up the connection to the graphite server. If something went Break up the connection to the graphite server. If something went
wrong while disconnecting it simply cut the connection up. wrong while disconnecting it simply cut the connection up.
...@@ -105,11 +116,11 @@ class Client: ...@@ -105,11 +116,11 @@ class Client:
self.channel.close() self.channel.close()
self.connection.close() self.connection.close()
except RuntimeError: except RuntimeError:
print("[ERROR] An error has occured while disconnecting from the " print('[ERROR] An error has occured '
"server." 'while disconnecting from the server.'
) )
def __send(self, message): def send(self, message):
""" """
Send the message given in the parameters given in the message Send the message given in the parameters given in the message
parameter. This function expects that the graphite server want the parameter. This function expects that the graphite server want the
...@@ -121,12 +132,12 @@ class Client: ...@@ -121,12 +132,12 @@ class Client:
routing_key='', body="\n".join(message)) routing_key='', body="\n".join(message))
return True return True
except: except:
print("[ERROR] An error has occured while sending metrics to the " print('[ERROR] An error has occured '
"server." 'while sending metrics to the server.'
) )
return False return False
def __collectFromNode(self, metricCollectors): def collect_node(self, metricCollectors):
""" """
It harvests the given metrics in the metricCollectors list. This list It harvests the given metrics in the metricCollectors list. This list
should be provided by the collectables modul. It is important that should be provided by the collectables modul. It is important that
...@@ -134,15 +145,18 @@ class Client: ...@@ -134,15 +145,18 @@ class Client:
""" """
metrics = [] metrics = []
for collector in metricCollectors: for collector in metricCollectors:
if (self.beat % collector[1]) is 0: collector_function = collector[0]
stat = collector[0]() phase = collector[1]
metrics.append((self.name + "." + if (self.beat % phase) is 0:
stat.name + " %d" % (stat.value) stat = collector_function()
+ " %d" % (time.time()) metrics.append(('%(hostname)s.%(name)s %(value)f %(time)d') %
)) {'hostname': self.name,
'name': stat.name,
'value': stat.value,
'time': time.time()})
return metrics return metrics
def __collectFromVMs(self): def collect_vms(self):
""" """
This method is used for fetching the kvm processes running on the This method is used for fetching the kvm processes running on the
node and using the cmdline parameters calculates different types of node and using the cmdline parameters calculates different types of
...@@ -154,8 +168,6 @@ class Client: ...@@ -154,8 +168,6 @@ class Client:
for entry in procList: for entry in procList:
try: try:
entry_name = entry.name entry_name = entry.name
except psutil._error.NoSuchProcess:
entry_name = ""
if entry_name in "kvm": if entry_name in "kvm":
cmdLine = entry.as_dict()["cmdline"] cmdLine = entry.as_dict()["cmdline"]
search = [cmd_param_index for cmd_param_index, cmd_param in search = [cmd_param_index for cmd_param_index, cmd_param in
...@@ -199,35 +211,37 @@ class Client: ...@@ -199,35 +211,37 @@ class Client:
if vm[0] in iname: if vm[0] in iname:
metrics.append( metrics.append(
('vm.%(name)s.network.packets_sent_%(interface)s ' ('vm.%(name)s.network.packets_sent_%(interface)s '
'%(data)f %(time)d') % { '%(data)f %(time)d') %
'name': vm[0], {'name': vm[0],
'interface': iname, 'interface': iname,
'time': time.time(), 'time': time.time(),
'data': interfaces_list[iname].packets_sent}) 'data': interfaces_list[iname].packets_sent})
metrics.append( metrics.append(
('vm.%(name)s.network.packets_recv_%(interface)s ' ('vm.%(name)s.network.packets_recv_%(interface)s '
'%(data)f %(time)d') % { '%(data)f %(time)d') %
'name': vm[0], {'name': vm[0],
'interface': iname, 'interface': iname,
'time': time.time(), 'time': time.time(),
'data': interfaces_list[iname].packets_recv}) 'data': interfaces_list[iname].packets_recv})
metrics.append( metrics.append(
('vm.%(name)s.network.bytes_sent_%(interface)s ' ('vm.%(name)s.network.bytes_sent_%(interface)s '
'%(data)f %(time)d') % { '%(data)f %(time)d') %
'name': vm[0], {'name': vm[0],
'interface': iname, 'interface': iname,
'time': time.time(), 'time': time.time(),
'data': interfaces_list[iname].bytes_sent}) 'data': interfaces_list[iname].bytes_sent})
metrics.append( metrics.append(
('vm.%(name)s.network.bytes_recv_%(interface)s ' ('vm.%(name)s.network.bytes_recv_%(interface)s '
'%(data)f %(time)d') % { '%(data)f %(time)d') %
'name': vm[0], {'name': vm[0],
'interface': iname, 'interface': iname,
'time': time.time(), 'time': time.time(),
'data': interfaces_list[iname].bytes_recv}) 'data': interfaces_list[iname].bytes_recv})
except psutil.NoSuchProcess:
print('[ERROR LOG] Process lost.')
return metrics return metrics
def getMaxFrequency(self, metricCollectors=[]): def get_frequency(self, metricCollectors=[]):
""" """
""" """
items = metricCollectors + [["kvmCpuUsage", self.kvmMem], [ items = metricCollectors + [["kvmCpuUsage", self.kvmMem], [
...@@ -238,7 +252,7 @@ class Client: ...@@ -238,7 +252,7 @@ class Client:
max = item[1] max = item[1]
return max return max
def startReporting(self, metricCollectors=[]): def run(self, metricCollectors=[]):
""" """
Call this method to start reporting to the server, it needs the Call this method to start reporting to the server, it needs the
metricCollectors parameter that should be provided by the collectables metricCollectors parameter that should be provided by the collectables
...@@ -247,20 +261,20 @@ class Client: ...@@ -247,20 +261,20 @@ class Client:
if self.valid is False: if self.valid is False:
print("[ERROR] The client cannot be started.") print("[ERROR] The client cannot be started.")
raise RuntimeError raise RuntimeError
if self.__connect() is False: if self.connect() is False:
hostdata = self.server_address + ':' + self.server_port
print("[ERROR] An error has occured while connecting to the " print("[ERROR] An error has occured while connecting to the "
"server on %s." "server on %(host)s."
% (self.server_address + ":" + str(self.server_port))) % {'host': hostdata})
else: else:
print("[SUCCESS] Connection established to %s on port %s. \ print('[SUCCESS] Connection established to %(host)s:%(port)s.'
Clientname: %s" % {'host': self.server_address,
% (self.server_address, self.server_port, 'port': self.server_port})
self.name))
try: try:
maxFrequency = self.getMaxFrequency(metricCollectors) maxFrequency = self.get_frequency(metricCollectors)
while True: while True:
nodeMetrics = self.__collectFromNode(metricCollectors) nodeMetrics = self.collect_node(metricCollectors)
vmMetrics = self.__collectFromVMs() vmMetrics = self.collect_vms()
metrics = nodeMetrics + vmMetrics metrics = nodeMetrics + vmMetrics
if self.debugMode == "True": if self.debugMode == "True":
print(metrics) print(metrics)
...@@ -274,4 +288,4 @@ class Client: ...@@ -274,4 +288,4 @@ class Client:
except KeyboardInterrupt: except KeyboardInterrupt:
print("[x] Reporting has stopped by the user. Exiting...") print("[x] Reporting has stopped by the user. Exiting...")
finally: finally:
self.__disconnect() self.disconnect()
import ConfigParser as configparser import sys
if sys.version_info < (3, 0):
import ConfigParser as configparser
else:
import configparser
def importConf(path_to_file): def import_conf(path_to_file):
config = configparser.RawConfigParser(allow_no_value=False) config = configparser.RawConfigParser(allow_no_value=False)
try: try:
config.read(path_to_file) config.read(path_to_file)
params = {} params = {}
metrics = {} metrics = {}
params["debugMode"] = config.get("Client", "Debug") params["debugMode"] = config.get("Client", "Debug")
##
## Metrics
##
metrics["cpu.usage"] = int(config.get("Metrics", "cpuUsage")) metrics["cpu.usage"] = int(config.get("Metrics", "cpuUsage"))
metrics["cpu.times"] = int(config.get("Metrics", "cpuTimes")) metrics["cpu.times"] = int(config.get("Metrics", "cpuTimes"))
metrics["memory.usage"] = int(config.get("Metrics", "memoryUsage")) metrics["memory.usage"] = int(config.get("Metrics", "memoryUsage"))
...@@ -16,17 +23,23 @@ def importConf(path_to_file): ...@@ -16,17 +23,23 @@ def importConf(path_to_file):
metrics["system.boot_time"] = int(config.get("Metrics", metrics["system.boot_time"] = int(config.get("Metrics",
"systemBootTime")) "systemBootTime"))
metrics["network"] = int(config.get("Metrics", "dataTraffic")) metrics["network"] = int(config.get("Metrics", "dataTraffic"))
##
## Params
##
params["kvmCpuUsage"] = int(config.get("KVM", "cpuUsage")) params["kvmCpuUsage"] = int(config.get("KVM", "cpuUsage"))
params["kvmMemoryUsage"] = int(config.get("KVM", "memoryUsage")) params["kvmMemoryUsage"] = int(config.get("KVM", "memoryUsage"))
params["kvmNetworkUsage"] = int(config.get("KVM", "networkUsage")) params["kvmNetworkUsage"] = int(config.get("KVM", "networkUsage"))
except configparser.NoSectionError: except configparser.NoSectionError:
print("Config file contains error! Reason: Missing section.") print("[ERROR] Config file contains error! "
"Reason: Missing section.")
raise raise
except configparser.ParsingError: except configparser.ParsingError:
print("Config file contains error! Reason: Cannot parse.") print("[ERROR] Config file contains error! "
"Reason: Cannot parse.")
raise raise
except configparser.MissingSectionHeaderError: except configparser.MissingSectionHeaderError:
print("Config file contains error! Reason: Missing section-header.") print("[ERROR] Config file contains error! "
"Reason: Missing section-header.")
raise raise
return params, metrics return params, metrics
...@@ -18,20 +18,19 @@ class collectables: ...@@ -18,20 +18,19 @@ class collectables:
} }
@staticmethod @staticmethod
def listKeys(): def list_keys():
return list(collectables.__collectables.keys()) return list(collectables.__collectables.keys())
@staticmethod @staticmethod
def listMetricsToKey(key): def list_metrics_to_key(key):
return collectables.__collectables[key] return collectables.__collectables[key]
@staticmethod @staticmethod
def listMetricsNameToKey(key): def list_metrics_name_to_key(key):
return [x.name for x in collectables.__collectables[key]] return [x.name for x in collectables.__collectables[key]]
@staticmethod @staticmethod
def provide(requests=[]): def provide(requests=[]):
#valid_keys = collectables.listKeys()
reqs = [] reqs = []
for requests, value in requests.items(): for requests, value in requests.items():
if value > 0: if value > 0:
...@@ -44,4 +43,4 @@ class collectables: ...@@ -44,4 +43,4 @@ class collectables:
@staticmethod @staticmethod
def provideAll(): def provideAll():
return collectables.provide(collectables.listKeys()) return collectables.provide(collectables.list_keys())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment