|
|
|
@ -1,19 +1,18 @@
|
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
import datetime
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
# # # # # # # # # #
|
|
|
|
|
#Imports
|
|
|
|
|
# # # # # # # # # #
|
|
|
|
|
import psutil
|
|
|
|
|
import subprocess
|
|
|
|
|
import toml
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
from sshtunnel import SSHTunnelForwarder
|
|
|
|
|
import sqlalchemy
|
|
|
|
|
|
|
|
|
|
from db_modell import db, PiWatch, create_table
|
|
|
|
|
from peewee import PostgresqlDatabase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_config():
|
|
|
|
|
configfile = os.path.join(PFAD, "conf.toml")
|
|
|
|
|
configfile = os.path.join(SKRIPTPFAD, "conf.toml")
|
|
|
|
|
with open(configfile) as conffile:
|
|
|
|
|
config = toml.loads(conffile.read())
|
|
|
|
|
config["ssh"] = {}
|
|
|
|
@ -22,47 +21,14 @@ def load_config():
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # # # # # # # # #
|
|
|
|
|
# Config
|
|
|
|
|
# # # # # # # # # #
|
|
|
|
|
PFAD = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
CONFIG = load_config()
|
|
|
|
|
MESSINTERVALL = datetime.timedelta(seconds=CONFIG["messintervall"])
|
|
|
|
|
SENDEINTERVALL = datetime.timedelta(seconds=CONFIG["sendeintervall"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()}
|
|
|
|
|
def system_daten_erfassen(now):
|
|
|
|
|
daten = {"ts": now, "name": "hofei", "cpu_percent": psutil.cpu_percent()}
|
|
|
|
|
abfrage = psutil.cpu_freq()
|
|
|
|
|
daten["cpufreq_current"] = abfrage.current
|
|
|
|
|
|
|
|
|
@ -84,36 +50,41 @@ def system_daten_erfassen():
|
|
|
|
|
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")
|
|
|
|
|
cpu_temp = subprocess.Popen(["vcgencmd", "measure_temp"], stdout=subprocess.PIPE).stdout.read()
|
|
|
|
|
daten["cpu_temp"] = cpu_temp.decode("utf-8").strip("temp='C\n")
|
|
|
|
|
cpu_spannung = subprocess.Popen(["vcgencmd", "measure_volts"], stdout=subprocess.PIPE).stdout.read()
|
|
|
|
|
daten["cpu_spannung"] = 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"])
|
|
|
|
|
zwischenspeicher = []
|
|
|
|
|
letzte_messung = datetime.datetime.now() - MESSINTERVALL
|
|
|
|
|
letzter_upload = datetime.datetime.now() - SENDEINTERVALL
|
|
|
|
|
while True:
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
|
|
|
|
|
if (now - letzte_messung) > MESSINTERVALL:
|
|
|
|
|
daten = system_daten_erfassen(now)
|
|
|
|
|
zwischenspeicher.append(daten)
|
|
|
|
|
letzte_messung = now
|
|
|
|
|
|
|
|
|
|
if (now - letzter_upload) > SENDEINTERVALL:
|
|
|
|
|
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:
|
|
|
|
|
db.initialize(PostgresqlDatabase(CONFIG["db"],
|
|
|
|
|
user=CONFIG["pguser"], password=CONFIG["pgpw"],
|
|
|
|
|
host="127.0.0.1",
|
|
|
|
|
port=server.local_bind_port))
|
|
|
|
|
create_table()
|
|
|
|
|
PiWatch.insert_many(zwischenspeicher).execute()
|
|
|
|
|
zwischenspeicher = []
|
|
|
|
|
letzter_upload = now
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|