Commit 19408133 by Nagy Gergő

Refactored config file parser and client

parent c5cc5350
...@@ -14,11 +14,11 @@ def main(): ...@@ -14,11 +14,11 @@ def main():
""") """)
if (len(sys.argv)==2 and sys.argv[1]=="run"): if (len(sys.argv)==2 and sys.argv[1]=="run"):
configuration = cnfparse.importConf("config/client.conf") configuration, metrics = cnfparse.importConf("config/client.conf")
cli = client.Client(configuration) cli = client.Client(configuration)
cli.startReporting(metricCollectors = cli.startReporting(metricCollectors =
collectables.collectables.provideAll()) collectables.collectables.provide(metrics))
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -5,6 +5,7 @@ def importConf(path_to_file): ...@@ -5,6 +5,7 @@ def importConf(path_to_file):
try: try:
config.read(path_to_file) config.read(path_to_file)
params = {} params = {}
metrics = {}
params["frequency"] = config.get("Client" , "Frequency") params["frequency"] = config.get("Client" , "Frequency")
params["debugMode"] = config.get("Client" , "Debug") params["debugMode"] = config.get("Client" , "Debug")
params["server_address"] = config.get("Server" , "Address") params["server_address"] = config.get("Server" , "Address")
...@@ -12,13 +13,13 @@ def importConf(path_to_file): ...@@ -12,13 +13,13 @@ def importConf(path_to_file):
params["amqp_queue"] = config.get("AMQP" , "Queue") params["amqp_queue"] = config.get("AMQP" , "Queue")
params["amqp_user"] = config.get("AMQP" , "User") params["amqp_user"] = config.get("AMQP" , "User")
params["amqp_pass"] = config.get("AMQP" , "Pass") params["amqp_pass"] = config.get("AMQP" , "Pass")
params["cpu.usage"] = config.get("Metrics", "cpuUsage") metrics["cpu.usage"] = config.get("Metrics", "cpuUsage")
params["memory.usage"] = config.get("Metrics", "memoryUsage") metrics["memory.usage"] = config.get("Metrics", "memoryUsage")
params["user.count"] = config.get("Metrics", "userCount") metrics["user.count"] = config.get("Metrics", "userCount")
params["swap.usage"] = config.get("Metrics", "swapUsage") metrics["swap.usage"] = config.get("Metrics", "swapUsage")
params["system.boot_time"]= config.get("Metrics", "systemBootTime") metrics["system.boot_time"]= config.get("Metrics", "systemBootTime")
params["package.traffic"] = config.get("Metrics", "packageTraffic") metrics["package.traffic"] = config.get("Metrics", "packageTraffic")
params["data.traffic"] = config.get("Metrics", "dataTraffic") metrics["data.traffic"] = config.get("Metrics", "dataTraffic")
except configparser.NoSectionError: except configparser.NoSectionError:
print("Config file contains error! Reason: Missing section.") print("Config file contains error! Reason: Missing section.")
raise raise
...@@ -29,6 +30,6 @@ def importConf(path_to_file): ...@@ -29,6 +30,6 @@ def importConf(path_to_file):
print("Config file contains error! Reason: Missing section-header.") print("Config file contains error! Reason: Missing section-header.")
raise raise
return params return params, metrics
from metrics import * 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.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],
...@@ -27,10 +28,12 @@ class collectables: ...@@ -27,10 +28,12 @@ class collectables:
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()
reqs = [request for request, value in requests.items() if value=="True"]
collectors = [] collectors = []
for request in requests: for request in reqs:
for item in collectables.__collectables[request]: for item in collectables.__collectables[request]:
collectors.append(item.harvest) collectors.append(item.harvest)
seen = set() seen = set()
......
...@@ -51,10 +51,10 @@ class std (Collection): ...@@ -51,10 +51,10 @@ class std (Collection):
class user (Collection.Group): class user (Collection.Group):
class count (Collection.Group.Metric): class count (Collection.Group.Metric):
name = "user.count"
@staticmethod @staticmethod
def harvest(): def harvest():
return Metrics("user.count", len(ps.get_users())) return Metrics(std.user.count.name, len(ps.get_users()))
class network (Collection.Group): class network (Collection.Group):
......
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