From 3f050960f37b2d20cedf826db0c059fa786a74b4 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:01:30 +0100 Subject: [PATCH] [LABI] tema04 xml reader added --- 1ano/2semestre/labi/tema04/datafiles/conf.xml | 13 ++++++++ .../labi/tema04/datafiles/monitor.csv | 7 +++++ 1ano/2semestre/labi/tema04/src/csv_monitor.py | 30 +++++++++++++++++++ 1ano/2semestre/labi/tema04/src/xml_reader.py | 18 +++++++++++ 4 files changed, 68 insertions(+) create mode 100644 1ano/2semestre/labi/tema04/datafiles/conf.xml create mode 100644 1ano/2semestre/labi/tema04/datafiles/monitor.csv create mode 100644 1ano/2semestre/labi/tema04/src/csv_monitor.py create mode 100644 1ano/2semestre/labi/tema04/src/xml_reader.py diff --git a/1ano/2semestre/labi/tema04/datafiles/conf.xml b/1ano/2semestre/labi/tema04/datafiles/conf.xml new file mode 100644 index 0000000..b8154d0 --- /dev/null +++ b/1ano/2semestre/labi/tema04/datafiles/conf.xml @@ -0,0 +1,13 @@ + + + 1 + + + + + + + + + + \ No newline at end of file diff --git a/1ano/2semestre/labi/tema04/datafiles/monitor.csv b/1ano/2semestre/labi/tema04/datafiles/monitor.csv new file mode 100644 index 0000000..376e6a4 --- /dev/null +++ b/1ano/2semestre/labi/tema04/datafiles/monitor.csv @@ -0,0 +1,7 @@ +time,cpu,ram,network +1679769801.7018511,20.0,,38943828 +1679769802.702516,28.0,,38946821 +1679769803.7031012,7.4,,38948575 +1679769804.7034643,11.8,,38949179 +1679769805.7041101,8.0,,38951189 +1679769806.704759,7.1,,38951668 diff --git a/1ano/2semestre/labi/tema04/src/csv_monitor.py b/1ano/2semestre/labi/tema04/src/csv_monitor.py new file mode 100644 index 0000000..e81d39b --- /dev/null +++ b/1ano/2semestre/labi/tema04/src/csv_monitor.py @@ -0,0 +1,30 @@ +import csv +import time +import psutil +from lxml import etree + + +def main(args=None): + xml = etree.parse('../datafiles/conf.xml') + root = xml.getroot() + + delimiter = root.findall("./output/csv")[0].attrib["separator"] + monitor_cpu = root.findall("./monitor/cpu")[0].attrib["active"] + monitor_ram = root.findall("./monitor/ram")[0].attrib["active"] + monitor_network = root.findall("./monitor/network")[0].attrib["active"] + + with open("../datafiles/monitor.csv", "w") as f: + writer = csv.DictWriter(f, delimiter=delimiter, fieldnames=["time", "cpu", "ram", "network"]) + + writer.writeheader() + + while True: + cpu = psutil.cpu_percent() if monitor_cpu == "true" else "" + ram = psutil.virtual_memory().percent if monitor_ram == "true" else "" + network = psutil.net_io_counters().bytes_sent if monitor_network == "true" else "" + writer.writerow({"time": time.time(), "cpu": cpu, "ram": ram, "network": network}) + time.sleep(1) + + +if __name__ == '__main__': + main() diff --git a/1ano/2semestre/labi/tema04/src/xml_reader.py b/1ano/2semestre/labi/tema04/src/xml_reader.py new file mode 100644 index 0000000..89339cf --- /dev/null +++ b/1ano/2semestre/labi/tema04/src/xml_reader.py @@ -0,0 +1,18 @@ +from lxml import etree + + +def main(args=None): + xml = etree.parse('../datafiles/conf.xml') + root = xml.getroot() + print(root.tag) + print_childs(root) + + +def print_childs(root): + for child in root: + print(child.tag, child.attrib, child.text) + print_childs(child) + + +if __name__ == '__main__': + main()