Change to Peewee

This commit is contained in:
Hofei
2019-11-06 23:02:09 +01:00
parent a2080b8864
commit c2ebbc2c9c
2 changed files with 80 additions and 71 deletions
+38
View File
@@ -0,0 +1,38 @@
import peewee
db = peewee.Proxy()
class BaseModel(peewee.Model):
class Meta:
database = db
class PiWatch(BaseModel):
ts = peewee.DateTimeField(primary_key=True)
name = peewee.TextField()
cpu_percent = peewee.FloatField()
cpufreq_current = peewee.FloatField()
cpu_temp = peewee.FloatField()
cpu_spannung = peewee.FloatField()
vmemory_total = peewee.FloatField()
vmemory_available = peewee.FloatField()
vmemory_percent = peewee.FloatField()
vmemory_used = peewee.FloatField()
vmemory_free = peewee.FloatField()
swapmemory_total = peewee.FloatField()
swapmemory_used = peewee.FloatField()
swapmemory_free = peewee.FloatField()
swapmemory_percent = peewee.FloatField()
disk_total = peewee.FloatField()
disk_used = peewee.FloatField()
disk_free = peewee.FloatField()
disk_percent = peewee.FloatField()
class Meta:
table_name = "pi_watch"
def create_table():
db.create_tables([PiWatch])
+42 -71
View File
@@ -1,19 +1,18 @@
#!/usr/bin/python3 import datetime
# # # # # # # # # #
#Imports
# # # # # # # # # #
import psutil
import subprocess
import toml
import os import os
import subprocess
import time import time
import psutil
import toml
from sshtunnel import SSHTunnelForwarder from sshtunnel import SSHTunnelForwarder
import sqlalchemy
from db_modell import db, PiWatch, create_table
from peewee import PostgresqlDatabase
def load_config(): def load_config():
configfile = os.path.join(PFAD, "conf.toml") configfile = os.path.join(SKRIPTPFAD, "conf.toml")
with open(configfile) as conffile: with open(configfile) as conffile:
config = toml.loads(conffile.read()) config = toml.loads(conffile.read())
config["ssh"] = {} config["ssh"] = {}
@@ -22,47 +21,14 @@ def load_config():
return config return config
# # # # # # # # # # SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__))
# Config
# # # # # # # # # #
PFAD = os.path.abspath(os.path.dirname(__file__))
CONFIG = load_config() CONFIG = load_config()
MESSINTERVALL = datetime.timedelta(seconds=CONFIG["messintervall"])
SENDEINTERVALL = datetime.timedelta(seconds=CONFIG["sendeintervall"])
class PGHandler: def system_daten_erfassen(now):
def __init__(self, port): daten = {"ts": now, "name": "hofei", "cpu_percent": psutil.cpu_percent()}
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() abfrage = psutil.cpu_freq()
daten["cpufreq_current"] = abfrage.current daten["cpufreq_current"] = abfrage.current
@@ -84,36 +50,41 @@ def system_daten_erfassen():
daten["disk_used"] = abfrage.used daten["disk_used"] = abfrage.used
daten["disk_free"] = abfrage.free daten["disk_free"] = abfrage.free
daten["disk_percent"] = abfrage.percent 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"]: if CONFIG["pi"]:
daten["cpu_temp"] = subprocess.Popen(["vcgencmd", "measure_temp"], stdout=subprocess.PIPE).stdout.read() 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_temp"] = cpu_temp.decode("utf-8").strip("temp='C\n")
daten["cpu_spannung"] = subprocess.Popen(["vcgencmd", "measure_volts"], stdout=subprocess.PIPE).stdout.read() 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") daten["cpu_spannung"] = cpu_spannung.decode("utf-8").strip("volt=V\n")
return daten return daten
def main(): def main():
with SSHTunnelForwarder( zwischenspeicher = []
(CONFIG["ssh"]["ip_server"], CONFIG["ssh"]["ssh_port"]), ssh_username=CONFIG["ssh"]["user"], letzte_messung = datetime.datetime.now() - MESSINTERVALL
ssh_password=CONFIG["ssh"]["pw"], remote_bind_address=('127.0.0.1', CONFIG["pgport"])) as server: letzter_upload = datetime.datetime.now() - SENDEINTERVALL
pg_handler = PGHandler(server.local_bind_port) while True:
while True: now = datetime.datetime.now()
daten = system_daten_erfassen()
pg_handler.daten_schreiben(daten) if (now - letzte_messung) > MESSINTERVALL:
time.sleep(CONFIG["intervall"]) 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__": if __name__ == "__main__":