tlu/tlumon/tlumon

176 lines
6.4 KiB
Plaintext
Raw Normal View History

2021-09-23 06:00:20 +00:00
#!/usr/bin/env python3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import Gio
2021-09-23 10:54:33 +00:00
from gi.repository import GLib
2021-09-23 07:08:49 +00:00
import os
2021-09-23 06:00:20 +00:00
import time
import threading
import logging
import subprocess
import psutil
import socket
2021-09-23 07:08:49 +00:00
import yaml
2021-09-23 06:00:20 +00:00
2021-09-23 11:02:32 +00:00
LOGLEVEL=logging.WARNING
2021-09-23 06:00:20 +00:00
2021-09-23 11:02:32 +00:00
SLEEPTIMER=5
2021-09-23 06:00:20 +00:00
class Main:
def __init__(self):
2021-09-23 07:08:49 +00:00
scriptpath = os.path.dirname(os.path.realpath(__file__))
logging.debug("Executing from %s", scriptpath)
2021-09-23 06:00:20 +00:00
self.builder = Gtk.Builder()
2021-09-23 07:08:49 +00:00
self.builder.add_from_file(f"{scriptpath}/main.glade")
2021-09-23 06:00:20 +00:00
self.builder.connect_signals(self)
self.windowMain = self.builder.get_object("MainWindow")
self.DialogChangeNIC = self.builder.get_object("dlg_nic_name")
#load nics to combobox
nics = Gtk.ListStore(str)
for interface in psutil.net_if_addrs().keys():
if interface != "lo":
nics.append([interface])
self.combobox = self.builder.get_object("cbox_nics")
self.combobox.set_model(nics)
renderer_text = Gtk.CellRendererText()
self.combobox.pack_start(renderer_text, True)
self.combobox.add_attribute(renderer_text, "text", 0)
self.windowMain.connect("delete-event", Gtk.main_quit)
self.windowMain.show()
self.icon_ok = Gio.ThemedIcon(name="gtk-yes")
self.icon_not_ok = Gio.ThemedIcon(name="gtk-no")
2021-09-23 07:08:49 +00:00
yamlpath = scriptpath.replace("tlumon","salt/pillars")
try:
with open(f"{yamlpath}/network.sls", "r") as f:
logging.debug("Reading global config %s", f"{yamlpath}/network.sls")
config = yaml.safe_load(f)
except FileNotFoundError as e:
logging.error("Couldn't read global config: %s", e)
try:
with open(f"{yamlpath}/local.sls", "r") as f:
2021-09-23 07:19:39 +00:00
localconfig = yaml.safe_load(f)
2021-09-23 07:08:49 +00:00
except FileNotFoundError as e:
logging.debug("Couldn't read local config: %s", e)
localconfig = {}
print(config)
try:
2021-10-25 08:33:09 +00:00
self.lan_nic = localconfig["network"]["bridge"]
2021-09-23 07:08:49 +00:00
except KeyError:
2021-10-25 08:33:09 +00:00
self.lan_nic = config["network"]["bridge"]
2021-09-23 07:08:49 +00:00
try:
2021-10-25 08:33:09 +00:00
self.wan_nic = localconfig["network"]["external"]
2021-09-23 07:08:49 +00:00
except KeyError:
2021-10-25 08:33:09 +00:00
self.wan_nic = config["network"]["external"]
2021-09-23 07:08:49 +00:00
2021-09-23 06:00:20 +00:00
self.builder.get_object("lbl_wan").set_text(f"wan ({self.wan_nic})")
self.builder.get_object("lbl_lan").set_text(f"lan ({self.lan_nic})")
self.service_checks = [
"nginx",
"rmt-server",
"chronyd",
"rmt-server-sync.timer",
"rmt-server-mirror.timer",
"rmt-server-systems-scc-sync.timer",
2021-09-23 10:54:33 +00:00
"atftpd.socket",
2021-10-12 08:15:14 +00:00
"dnsmasq",
2021-10-14 14:39:51 +00:00
"hostapd",
2021-10-25 08:33:39 +00:00
"firewalld",
"nfsserver",
"sshd",
2021-11-06 09:11:32 +00:00
"rancher",
"registry-container",
2021-09-23 06:00:20 +00:00
"mariadb"
]
2021-09-23 10:54:33 +00:00
timer_thread = threading.Thread(target=self.update)
timer_thread.daemon = True
timer_thread.start()
2021-09-23 06:00:20 +00:00
def update(self):
2021-09-23 10:54:33 +00:00
while True:
logging.debug("Update triggered")
GLib.idle_add(self.check_service)
GLib.idle_add(self.check_ip)
time.sleep(SLEEPTIMER)
2021-09-23 06:00:20 +00:00
def check_service(self):
for service in self.service_checks:
logging.debug("Checking status of service %s", service)
image = self.builder.get_object("img_service_"+service)
result = subprocess.run(["systemctl", "is-active", service, "--quiet"])
if result.returncode == 0:
logging.debug("%s is running", service)
2021-09-23 10:54:33 +00:00
image.set_from_gicon(self.icon_ok, Gtk.IconSize.BUTTON)
2021-09-23 06:00:20 +00:00
else:
logging.debug("%s is not running, exit code %i",service, result.returncode)
2021-09-23 10:54:33 +00:00
image.set_from_gicon(self.icon_not_ok, Gtk.IconSize.BUTTON)
2021-09-23 06:00:20 +00:00
def check_ip(self):
#checking LAN network
2021-09-23 07:17:22 +00:00
logging.debug("Checking IP for LAN: %s", self.lan_nic)
2021-09-23 06:00:20 +00:00
image = self.builder.get_object("img_ip_lan")
text = self.builder.get_object("lbl_lan_status")
ip = list(self.get_ip_address(self.lan_nic))
if ip:
logging.debug("found : %s", " ".join(ip))
image.set_from_gicon(self.icon_ok, Gtk.IconSize.DIALOG)
text.set_text(" ".join(ip))
else:
logging.debug("no IP found")
image.set_from_gicon(self.icon_not_ok, Gtk.IconSize.DIALOG)
text.set_text("N/A")
2021-09-23 07:17:22 +00:00
logging.debug("Checking IP for WAN: %s", self.wan_nic)
2021-09-23 06:00:20 +00:00
ip = list(self.get_ip_address(self.wan_nic))
image = self.builder.get_object("img_ip_wan")
text = self.builder.get_object("lbl_wan_status")
2021-09-23 07:17:22 +00:00
ip = list(self.get_ip_address(self.wan_nic))
2021-09-23 06:00:20 +00:00
if ip:
logging.debug("found : %s", " ".join(ip))
image.set_from_gicon(self.icon_ok, Gtk.IconSize.DIALOG)
text.set_text(" ".join(ip))
else:
logging.debug("no IP found")
image.set_from_gicon(self.icon_not_ok, Gtk.IconSize.DIALOG)
text.set_text("N/A")
def get_ip_address(self, interface):
for snic in psutil.net_if_addrs()[interface]:
if snic.family == socket.AF_INET:
yield (snic.address)
def change_nic(self, widget):
button = Gtk.Buildable.get_name(widget)
logging.debug("pressed %s", button)
response = self.DialogChangeNIC.run()
self.DialogChangeNIC.hide()
if response == Gtk.ResponseType.OK:
logging.debug("Ok was pressed")
tree_iter = self.combobox.get_active_iter()
if tree_iter is not None:
model = self.combobox.get_model()
name = model[tree_iter][:2][0]
if button == "btn_change_wan_nic":
self.wan_nic = namn
self.builder.get_object("lbl_wan").set_text(f"wan ({self.wan_nic})")
if button == "btn_change_lan_nic":
self.lan_nic = name
self.builder.get_object("lbl_lan").set_text(f"lan ({self.lan_nic})")
self.check_ip()
if __name__ == '__main__':
logging.basicConfig(format='%(levelname)s:\t%(message)s', level=LOGLEVEL)
main = Main()
Gtk.main()