You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
import datetime
|
|
import os
|
|
import subprocess
|
|
import time
|
|
|
|
import psutil
|
|
import toml
|
|
from sshtunnel import SSHTunnelForwarder
|
|
|
|
from db_modell import db, PiWatch, create_table
|
|
from peewee import PostgresqlDatabase
|
|
|
|
|
|
def load_config():
|
|
configfile = os.path.join(SKRIPTPFAD, "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
|
|
|
|
|
|
SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__))
|
|
CONFIG = load_config()
|
|
MESSINTERVALL = datetime.timedelta(seconds=CONFIG["messintervall"])
|
|
SENDEINTERVALL = datetime.timedelta(seconds=CONFIG["sendeintervall"])
|
|
|
|
|
|
def system_daten_erfassen(now):
|
|
daten = {"ts": now, "name": "hofei", "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
|
|
|
|
if CONFIG["pi"]:
|
|
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():
|
|
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__":
|
|
main()
|