working project, maybe

This commit is contained in:
2019-08-22 19:24:35 +02:00
parent 8689dd8c63
commit a454a4aac5
152 changed files with 43419 additions and 316 deletions

View File

@@ -7,37 +7,6 @@
Description: System stats
Version: 2.0 (SQLite)
SQLite SCHEMA:
DB status
TABLE raspi
----------------
t_stamp | usage | host | type
-----------------------------------------------
INT | INT | VARCHAR(50) | VARCHAR(20)
CREATE TABLE raspi(
t_stamp INT,
usage INT,
host VARCHAR(50),
type VARCHAR(20));
TABLE network
----------------
t_stamp | count | type | nic | host
------------------------------------------------------------
INT | INT | VARCHAR(20) | VARCHAR(20) | VARCHAR(50)
CREATE TABLE network(
t_stamp INT,
count INT,
type VARCHAR(20),
nic VARCHAR(20),
host VARCHAR(50));
------------------------------------------------------------------------
Import libraries
------------------------------------------------------------------------
@@ -52,39 +21,44 @@ import argparse
import time
import datetime
import simplejson as json
import sqlite3
import influxdb
import psutil
import socket
# optionally, future modules, locally available, I hate dependencies
from pprint import pprint
_SCRIPT_PATH = os.path.dirname(sys.argv[0])
#sys.path.append(_SCRIPT_PATH + "/home/pi/test/lib")
#print(_SCRIPT_PATH + "/lib")
_SCRIPT_PATH = os.path.dirname(os.path.abspath(sys.argv[0]))
sys.path.append(_SCRIPT_PATH + "/lib")
global _hostname
_hostname = socket.gethostname()
'''
------------------------------------------------------------------------
SQLite def
------------------------------------------------------------------------
'''
_SQLiteDB = '/var/lib/plutonium/status.db'
#import ventilLogger
'''
------------------------------------------------------------------------
--------------------------------------------------------------------------------
Define variables
------------------------------------------------------------------------
--------------------------------------------------------------------------------
'''
_VERSION = 2.0
_NAME = u"System stats collector"
_NAME = u"Vantage Vue Decoding shite"
_LOG_DIR = _SCRIPT_PATH + '/log/'
_LOG_FILE_ROOT = re.sub(u'./', '', sys.argv[0])
_LOG_FILE = _LOG_DIR + _LOG_FILE_ROOT + u'.log'
_DEBUG_FILE = _LOG_DIR + _LOG_FILE_ROOT + u'.dbg'
influx_status_write = []
influx_host = 'localhost'
influx_port = 8086
influx_user = 'pi'
influx_pwd = 'Ventil6996'
status_db = 'status'
'''
------------------------------------------------------------------------
--------------------------------------------------------------------------------
Generic, standalone functions
------------------------------------------------------------------------
--------------------------------------------------------------------------------
'''
# Obvious shit, set up the client class
@@ -92,35 +66,20 @@ influx_status_client = influxdb.client.InfluxDBClient(
influx_host, influx_port, influx_user, influx_pwd, status_db
)
def create_project(conn, project):
"""
Create a new project into the projects table
:param conn:
:param project:
:return: project id
"""
sql = ''' INSERT INTO wind(name,begin_date,end_date)
VALUES(?,?,?) '''
cur = conn.cursor()
cur.execute(sql, project)
return cur.lastrowid
'''
------------------------------------------------------------------------
--------------------------------------------------------------------------------
Classes
------------------------------------------------------------------------
--------------------------------------------------------------------------------
'''
class DBwriter(object):
def __init__(self):
__name__ = "Database writer class, SQLite"
__name__ = "Database writer class, Influx"
def construct(self, DB, query):
def construct(self, connector, measurement, fields, tags):
""" Takes values in a writes them to influxdb
requires: list(connector): connector with ticks to be written
requires: list(connector): connector with all ticks to be written
at once
str(measurement): the measurement ID to be written
dict(fields): fields to be written in one tick
@@ -128,26 +87,20 @@ class DBwriter(object):
returns: list(result_connector)
"""
conn = sqlite3.connect(db_file)
c = conn.cursor()
c.execute(query)
conn.commit()
conn.close()
result_connector = connector
result_connector.append({"measurement": measurement,
"fields": fields,
"tags": tags}
)
return result_connector
class Stats(object):
def __init__(self):
__name__ = "Database writer class, SQLite"
'''
------------------------------------------------------------------------
--------------------------------------------------------------------------------
Main
------------------------------------------------------------------------
--------------------------------------------------------------------------------
'''
if '__main__':
status_writer = DBwriter()
davis_writer = DBwriter()
while True:
averaged_cpu = psutil.cpu_percent()
mem_consumption = psutil.virtual_memory()[2]
@@ -180,25 +133,25 @@ if '__main__':
averaged_cpu = (averaged_cpu + psutil.cpu_percent()) / 2
time.sleep(1)
# Write the whole blob into Influx DB
influx_status_write = status_writer.construct(
influx_status_write = davis_writer.construct(
influx_status_write,
"RasPI",
{"usage": float(averaged_cpu)},
{"type": "cpu"}
)
influx_status_write = status_writer.construct(
influx_status_write = davis_writer.construct(
influx_status_write,
"RasPI",
{"usage": float(mem_consumption)},
{"type": "mem"}
)
influx_status_write = status_writer.construct(
influx_status_write = davis_writer.construct(
influx_status_write,
"RasPI",
{"usage": float(disk_usage)},
{"type": "disk"}
)
influx_status_write = status_writer.construct(
influx_status_write = davis_writer.construct(
influx_status_write,
"net",
{
@@ -214,7 +167,7 @@ if '__main__':
"host": _hostname
}
)
influx_status_write = status_writer.construct(
influx_status_write = davis_writer.construct(
influx_status_write,
"net",
{