Commit 606df697 by Bach Dániel

add disk io metric

parent 5a251e2c
*.pyc
*.swp
[Client]
Debug = False
[Metrics]
cpuUsage = 5
cpuTimes = 5
memoryUsage = 5
userCount = 60
swapUsage = 30
systemBootTime = 120
packageTraffic = 10
dataTraffic = 10
[KVM]
cpuUsage = 5
memoryUsage = 5
networkUsage = 5
import logging import logging
import sys import sys
from src import cnfparse from src.client import Client
from src import client
from src.collectables import collectables
def main(): def main():
...@@ -16,10 +14,8 @@ def main(): ...@@ -16,10 +14,8 @@ def main():
if len(sys.argv) is not 2 and sys.argv[1] is not "run": if len(sys.argv) is not 2 and sys.argv[1] is not "run":
print("[ERROR] Command cannot be parsed. Exiting...") print("[ERROR] Command cannot be parsed. Exiting...")
return return
configuration, metrics = cnfparse.import_conf("config/client.conf")
cli = client.Client(configuration) Client().run()
metricCollectors = collectables.provide(metrics)
cli.run(metricCollectors)
if __name__ == "__main__": if __name__ == "__main__":
......
import sys
if sys.version_info < (3, 0):
import ConfigParser as configparser
else:
import configparser
def import_conf(path_to_file):
config = configparser.RawConfigParser(allow_no_value=False)
try:
config.read(path_to_file)
params = {}
metrics = {}
params["debugMode"] = config.get("Client", "Debug")
##
## Metrics
##
metrics["cpu.usage"] = int(config.get("Metrics", "cpuUsage"))
metrics["cpu.times"] = int(config.get("Metrics", "cpuTimes"))
metrics["memory.usage"] = int(config.get("Metrics", "memoryUsage"))
metrics["user.count"] = int(config.get("Metrics", "userCount"))
metrics["swap.usage"] = int(config.get("Metrics", "swapUsage"))
metrics["system.boot_time"] = int(config.get("Metrics",
"systemBootTime"))
metrics["network"] = int(config.get("Metrics", "dataTraffic"))
##
## Params
##
params["kvmCpuUsage"] = int(config.get("KVM", "cpuUsage"))
params["kvmMemoryUsage"] = int(config.get("KVM", "memoryUsage"))
params["kvmNetworkUsage"] = int(config.get("KVM", "networkUsage"))
except configparser.NoSectionError:
print("[ERROR] Config file contains error! "
"Reason: Missing section.")
raise
except configparser.ParsingError:
print("[ERROR] Config file contains error! "
"Reason: Cannot parse.")
raise
except configparser.MissingSectionHeaderError:
print("[ERROR] Config file contains error! "
"Reason: Missing section-header.")
raise
return params, metrics
from metrics import std
class collectables:
__collectables = {
std.cpu.usage.name: [std.cpu.usage],
std.cpu.times.name: [std.cpu.times],
std.memory.usage.name: [std.memory.usage],
std.swap.usage.name: [std.swap.usage],
std.user.count.name: [std.user.count],
std.network.packages_sent.name: [std.network.packages_sent],
std.network.packages_received.name: [std.network.packages_received],
std.network.bytes_sent.name: [std.network.bytes_sent],
std.network.bytes_received.name: [std.network.bytes_received],
std.system.boot_time.name: [std.system.boot_time],
"network": [std.network.bytes_sent, std.network.bytes_received,
std.network.packages_sent, std.network.packages_received],
}
@staticmethod
def list_keys():
return list(collectables.__collectables.keys())
@staticmethod
def list_metrics_to_key(key):
return collectables.__collectables[key]
@staticmethod
def list_metrics_name_to_key(key):
return [x.name for x in collectables.__collectables[key]]
@staticmethod
def provide(requests=[]):
reqs = []
for requests, value in requests.items():
if value > 0:
reqs.append([requests, value])
collectors = []
for request in reqs:
for item in collectables.__collectables[request[0]]:
collectors.append([item.harvest, request[1]])
return collectors
@staticmethod
def provideAll():
return collectables.provide(collectables.list_keys())
#!/usr/bin/python
import psutil as ps
import collections
#############################################################################
Metrics = collections.namedtuple("Metrics", ["name", "value"])
class Collection(object):
class Group(object):
class Metric(object):
name = "unknown"
collector_function = 0
collector_function_arguments = {}
collector_function_result_attr = ""
@classmethod
def harvest(cls):
query = cls.collector_function.im_func(
**cls.collector_function_arguments)
if ((isinstance(query, list)) or (isinstance(query, dict))):
return Metrics(cls.name,
query[cls.collector_function_result_attr])
elif (isinstance(query, tuple)):
return Metrics(cls.name, query.__getattribute__(
cls.collector_function_result_attr))
else:
return Metrics(cls.name, query)
##############################################################################
class std(Collection):
class cpu(Collection.Group):
class usage(Collection.Group.Metric):
name = "cpu.usage"
collector_function = ps.cpu_percent
collector_function_arguments = {'interval': 0.0}
class times(Collection.Group.Metric):
name = "cpu.times"
@classmethod
def harvest(cls):
return Metrics(cls.name,
ps.cpu_times().user + ps.cpu_times().system)
class memory(Collection.Group):
class usage(Collection.Group.Metric):
name = "memory.usage"
collector_function = ps.virtual_memory
collector_function_result_attr = "percent"
class swap(Collection.Group):
class usage(Collection.Group.Metric):
name = "swap.usage"
collector_function = ps.swap_memory
collector_function_result_attr = "percent"
class user(Collection.Group):
class count(Collection.Group.Metric):
name = "user.count"
@classmethod
def harvest(cls):
return Metrics(cls.name, len(ps.get_users()))
class network(Collection.Group):
class packages_sent(Collection.Group.Metric):
name = "network.packets_sent"
collector_function = ps.network_io_counters
collector_function_result_attr = "packets_sent"
class packages_received(Collection.Group.Metric):
name = "network.packets_recv"
collector_function = ps.network_io_counters
collector_function_result_attr = "packets_recv"
class bytes_sent(Collection.Group.Metric):
name = "network.bytes_sent"
collector_function = ps.network_io_counters
collector_function_result_attr = "bytes_sent"
class bytes_received(Collection.Group.Metric):
name = "network.bytes_received"
collector_function = ps.network_io_counters
collector_function_result_attr = "bytes_recv"
class system(Collection.Group):
class boot_time(Collection.Group.Metric):
name = "system.boot_time"
collector_function = ps.get_boot_time
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