Commit 2c5ae1f7 by Gregory Nagy

Client.conf file KVM section added.

client.py pip8 formated
cnfparse.py pip8 formated
collectables.py fixed bug and pip formated
parent 743a2c72
...@@ -9,3 +9,7 @@ swapUsage = 30 ...@@ -9,3 +9,7 @@ swapUsage = 30
systemBootTime = 120 systemBootTime = 120
packageTraffic = 10 packageTraffic = 10
dataTraffic = 10 dataTraffic = 10
[KVM]
cpuUsage =5
memoryUsage = 5
#!/usr/bin/python #!/usr/bin/python
# import platform
# import collections
import time import time
import socket import socket
import pika import pika
...@@ -14,7 +12,6 @@ logging.basicConfig() ...@@ -14,7 +12,6 @@ logging.basicConfig()
class Client: class Client:
def __init__(self, config): def __init__(self, config):
""" """
Constructor of the client requires a configuration provided by cnfparse Constructor of the client requires a configuration provided by cnfparse
...@@ -75,38 +72,38 @@ class Client: ...@@ -75,38 +72,38 @@ class Client:
metrics.append((self.name + "." + metrics.append((self.name + "." +
stat.name + " %d" % (stat.value) stat.name + " %d" % (stat.value)
+ " %d" % (time.time()) + " %d" % (time.time())
)) ))
return metrics return metrics
def __collectFromVMs(self): def __collectFromVMs(self):
metrics = [] metrics = []
running_vms = [] running_vms = []
for entry in psutil.get_process_list(): for entry in psutil.get_process_list():
if entry.name in "kvm": if entry.name in "kvm":
search = [cmd_param_index for cmd_param_index, cmd_param in search = [cmd_param_index for cmd_param_index, cmd_param in
enumerate(entry.as_dict()["cmdline"]) enumerate(entry.as_dict()["cmdline"])
if cmd_param == "-name"] if cmd_param == "-name"]
memory = [cmd_param_index for cmd_param_index, cmd_param in memory = [cmd_param_index for cmd_param_index, cmd_param in
enumerate(entry.as_dict()["cmdline"]) enumerate(entry.as_dict()["cmdline"])
if cmd_param == "-m"] if cmd_param == "-m"]
running_vms.append([entry.as_dict()["cmdline"][search[0]+1], running_vms.append([entry.as_dict()["cmdline"][search[0] + 1],
entry.pid, entry.pid,
int(entry.as_dict()["cmdline"][memory[0]+1])]) int(entry.as_dict()["cmdline"][memory[0] + 1])])
for vm in running_vms: for vm in running_vms:
vm_proc = psutil.Process(vm[1]) vm_proc = psutil.Process(vm[1])
metrics.append((self.name + "." + "kvm." + metrics.append((self.name + "." + "kvm." +
vm[0] + "." + "memory.usage." + vm[0] + "." + "memory.usage." +
" %d" % (vm_proc.get_memory_percent()/100*vm[2]) " %d" % (vm_proc.get_memory_percent() / 100 * vm[2])
+ " %d" % (time.time()) + " %d" % (time.time())
)) ))
metrics.append((self.name + "." + "kvm." + metrics.append((self.name + "." + "kvm." +
vm[0] + "." + "cpu.usage" + vm[0] + "." + "cpu.usage" +
" %d" % (vm_proc.get_cpu_times().system + " %d" % (vm_proc.get_cpu_times().system +
vm_proc.get_cpu_times().user) vm_proc.get_cpu_times().user)
+ " %d" % (time.time()) + " %d" % (time.time())
)) ))
return metrics return metrics
def getMaxFrequency(self, metricCollectors=[]): def getMaxFrequency(self, metricCollectors=[]):
max = metricCollectors[0][1] max = metricCollectors[0][1]
for item in metricCollectors: for item in metricCollectors:
......
...@@ -15,8 +15,8 @@ def importConf(path_to_file): ...@@ -15,8 +15,8 @@ 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"))
metrics["kvmCpuUsage"] = int(config.get("KVM", "cpuUsage")) params["kvmCpuUsage"] = int(config.get("KVM", "cpuUsage"))
metrics["kvmMemoryUsage"] = int(config.get("KVM", "memoryUsage")) params["kvmMemoryUsage"] = int(config.get("KVM", "memoryUsage"))
except configparser.NoSectionError: except configparser.NoSectionError:
print("Config file contains error! Reason: Missing section.") print("Config file contains error! Reason: Missing section.")
raise raise
......
...@@ -2,46 +2,45 @@ from metrics import * ...@@ -2,46 +2,45 @@ from metrics import *
class collectables: class collectables:
__collectables = {
__collectables = { std.cpu.usage.name: [std.cpu.usage],
std.cpu.usage.name: [std.cpu.usage], std.memory.usage.name: [std.memory.usage],
std.memory.usage.name: [std.memory.usage], std.swap.usage.name: [std.swap.usage],
std.swap.usage.name: [std.swap.usage], std.user.count.name: [std.user.count],
std.user.count.name: [std.user.count], std.network.packages_sent.name: [std.network.packages_sent],
std.network.packages_sent.name: [std.network.packages_sent], std.network.packages_received.name: [std.network.packages_received],
std.network.packages_received.name: [std.network.packages_received], std.network.bytes_sent.name: [std.network.bytes_sent],
std.network.bytes_sent.name: [std.network.bytes_sent], std.network.bytes_received.name: [std.network.bytes_received],
std.network.bytes_received.name: [std.network.bytes_received], std.system.boot_time.name: [std.system.boot_time],
std.system.boot_time.name: [std.system.boot_time], "network": [std.network.bytes_sent, std.network.bytes_received,
"network": [std.network.bytes_sent, std.network.bytes_received, std.network.packages_sent, std.network.packages_received],
std.network.packages_sent, std.network.packages_received], }
}
@staticmethod
@staticmethod def listKeys():
def listKeys(): return list(collectables.__collectables.keys())
return list(collectables.__collectables.keys())
@staticmethod
@staticmethod def listMetricsToKey(key):
def listMetricsToKey(key): return collectables.__collectables[key]
return collectables.__collectables[key]
@staticmethod
@staticmethod def listMetricsNameToKey(key):
def listMetricsNameToKey(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()
#valid_keys = collectables.listKeys() reqs = []
reqs = [] for requests, value in requests.items():
for requests, value in requests.items(): if value > 0:
if value>0: reqs.append([requests, value])
reqs.append([requests, value]) collectors = []
collectors = [] for request in reqs:
for request in reqs: for item in collectables.__collectables[request[0]]:
for item in collectables.__collectables[request[0]]: collectors.append([item.harvest, request[1]])
collectors.append([item.harvest, request[1]]) return collectors
return collectors
@staticmethod
@staticmethod def provideAll():
def provideAll(): return collectables.provide(collectables.listKeys())
return collectables.provide(collectables.listKeys())
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