Initialcommit
commit
a2080b8864
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
#Installationsskript für pi_watch.py
|
||||||
|
import toml
|
||||||
|
import os
|
||||||
|
import getpass
|
||||||
|
#Benutzereingaben abfragen
|
||||||
|
config = dict()
|
||||||
|
config["ip"] = input("IP Adresse eingeben: ")
|
||||||
|
config["port"] = int(input("Port: "))
|
||||||
|
config["user"] = input("Benutzername: ")
|
||||||
|
config["pw"] = getpass.getpass()
|
||||||
|
config["DB"] = input("Datenbankname: ")
|
||||||
|
config["record"] = input("Recordname: ")
|
||||||
|
config["intervall"] = float(input("Abfrageintervall in Sekunden eingeben: "))
|
||||||
|
eingabe = input("Netzwerkschnittstelle auswählen, Mehrfachnennung per , trennen: ")
|
||||||
|
config["netzwerk"] = eingabe.split(",")
|
||||||
|
config["name"] = input("Name für Tag eingeben: ")
|
||||||
|
config["nummer"] = input("Nummmer für Tag eingeben: ")
|
||||||
|
config["pi"] = input("0 für VServer, 1 für Pi eingeben: ")
|
||||||
|
print("Sollten Sie keine ssh_auth.toml Datei besitzen, und dem Paket liegt keine ssh_auth.py bei, bitte den Entwickler kontaktieren")
|
||||||
|
config["pfad_zu_ssh_auth"] = input("Pfad und Datei zu ssh_auth.toml: ")
|
||||||
|
#Eingaben im toml Format speichern
|
||||||
|
ausgabe = config
|
||||||
|
ausgabe = toml.dumps(ausgabe)
|
||||||
|
with open("conf.toml", "w") as file:
|
||||||
|
file.write(ausgabe)
|
||||||
|
print("Konfigurationsdatei erstellt, Einrichtung beendet")
|
||||||
|
|
@ -0,0 +1,120 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
# # # # # # # # # #
|
||||||
|
#Imports
|
||||||
|
# # # # # # # # # #
|
||||||
|
import psutil
|
||||||
|
import subprocess
|
||||||
|
import toml
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from sshtunnel import SSHTunnelForwarder
|
||||||
|
import sqlalchemy
|
||||||
|
|
||||||
|
|
||||||
|
def load_config():
|
||||||
|
configfile = os.path.join(PFAD, "conf.toml")
|
||||||
|
with open(configfile) as conffile:
|
||||||
|
config = toml.loads(conffile.read())
|
||||||
|
config["ssh"] = {}
|
||||||
|
with open(config["pfad_zu_ssh_auth"]) as confsshfile:
|
||||||
|
config["ssh"] = toml.loads(confsshfile.read())
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
# # # # # # # # # #
|
||||||
|
# Config
|
||||||
|
# # # # # # # # # #
|
||||||
|
PFAD = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
CONFIG = load_config()
|
||||||
|
|
||||||
|
|
||||||
|
class PGHandler:
|
||||||
|
def __init__(self, port):
|
||||||
|
self.pguser = CONFIG["pguser"]
|
||||||
|
self.pgpw = CONFIG["pgpw"]
|
||||||
|
self.port = port
|
||||||
|
self.db = CONFIG["DB"]
|
||||||
|
self.engine = sqlalchemy.create_engine('postgresql+psycopg2://{pguser}:{pgpw}@localhost:{port}/{db}'.format(
|
||||||
|
pguser=self.pguser, pgpw=self.pgpw, port=self.port, db=self.db))
|
||||||
|
|
||||||
|
def check_spalten(self, daten):
|
||||||
|
for key in daten.keys():
|
||||||
|
print(key)
|
||||||
|
|
||||||
|
def daten_schreiben(self, daten):
|
||||||
|
with self.engine.begin() as conn:
|
||||||
|
values = [CONFIG["name"]]
|
||||||
|
spalte_liste = ["name"]
|
||||||
|
values_var = ""
|
||||||
|
for spalte, wert in daten.items():
|
||||||
|
values.append(wert)
|
||||||
|
spalte_liste.append(spalte)
|
||||||
|
values_var = ', '.join(['%s'] * (len(spalte_liste)))
|
||||||
|
spalten = ", ".join(spalte_liste)
|
||||||
|
sql = "INSERT INTO pi_watch ({spalten}) VALUES ({values_var})".format(spalten=spalten,
|
||||||
|
values_var=values_var)
|
||||||
|
data = values
|
||||||
|
conn.execute(sql, data)
|
||||||
|
|
||||||
|
|
||||||
|
# # # # # # # # # #
|
||||||
|
# Funktionen
|
||||||
|
# # # # # # # # # #
|
||||||
|
def system_daten_erfassen():
|
||||||
|
daten = {"cpu_percent": psutil.cpu_percent()}
|
||||||
|
abfrage = psutil.cpu_freq()
|
||||||
|
daten["cpufreq_current"] = abfrage.current
|
||||||
|
|
||||||
|
abfrage = psutil.virtual_memory()
|
||||||
|
daten["vmemory_total"] = abfrage.total
|
||||||
|
daten["vmemory_available"] = abfrage.available
|
||||||
|
daten["vmemory_percent"] = abfrage.percent
|
||||||
|
daten["vmemory_used"] = abfrage.used
|
||||||
|
daten["vmemory_free"] = abfrage.free
|
||||||
|
|
||||||
|
abfrage = psutil.swap_memory()
|
||||||
|
daten["swapmemory_total"] = abfrage.total
|
||||||
|
daten["swapmemory_used"] = abfrage.used
|
||||||
|
daten["swapmemory_free"] = abfrage.free
|
||||||
|
daten["swapmemory_percent"] = abfrage.percent
|
||||||
|
|
||||||
|
abfrage = psutil.disk_usage('/')
|
||||||
|
daten["disk_total"] = abfrage.total
|
||||||
|
daten["disk_used"] = abfrage.used
|
||||||
|
daten["disk_free"] = abfrage.free
|
||||||
|
daten["disk_percent"] = abfrage.percent
|
||||||
|
|
||||||
|
abfrage = psutil.net_io_counters(pernic=True)
|
||||||
|
for schnittstelle in CONFIG["netzwerk"]:
|
||||||
|
daten[schnittstelle + "_bytes_sent"] = abfrage[schnittstelle].bytes_sent
|
||||||
|
daten[schnittstelle + "_bytes_recv"] = abfrage[schnittstelle].bytes_recv
|
||||||
|
daten[schnittstelle + "_packets_sent"] = abfrage[schnittstelle].packets_sent
|
||||||
|
daten[schnittstelle + "_packets_recv"] = abfrage[schnittstelle].packets_recv
|
||||||
|
daten[schnittstelle + "_errin"] = abfrage[schnittstelle].errin
|
||||||
|
daten[schnittstelle + "_errout"] = abfrage[schnittstelle].errout
|
||||||
|
daten[schnittstelle + "_dropin"] = abfrage[schnittstelle].dropin
|
||||||
|
daten[schnittstelle + "_dropout"] = abfrage[schnittstelle].dropout
|
||||||
|
|
||||||
|
if CONFIG["pi"]:
|
||||||
|
daten["cpu_temp"] = subprocess.Popen(["vcgencmd", "measure_temp"], stdout=subprocess.PIPE).stdout.read()
|
||||||
|
daten["cpu_temp"] = daten["cpu_temp"].decode("utf-8").strip("temp='C\n")
|
||||||
|
daten["cpu_spannung"] = subprocess.Popen(["vcgencmd", "measure_volts"], stdout=subprocess.PIPE).stdout.read()
|
||||||
|
daten["cpu_spannung"] = daten["cpu_spannung"].decode("utf-8").strip("volt=V\n")
|
||||||
|
|
||||||
|
return daten
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with SSHTunnelForwarder(
|
||||||
|
(CONFIG["ssh"]["ip_server"], CONFIG["ssh"]["ssh_port"]), ssh_username=CONFIG["ssh"]["user"],
|
||||||
|
ssh_password=CONFIG["ssh"]["pw"], remote_bind_address=('127.0.0.1', CONFIG["pgport"])) as server:
|
||||||
|
pg_handler = PGHandler(server.local_bind_port)
|
||||||
|
while True:
|
||||||
|
daten = system_daten_erfassen()
|
||||||
|
pg_handler.daten_schreiben(daten)
|
||||||
|
time.sleep(CONFIG["intervall"])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue