237 lines
7.2 KiB
Python
237 lines
7.2 KiB
Python
#!/usr/bin/python -u
|
|
|
|
'''
|
|
------------------------------------------------------------------------
|
|
Type: Python 3.x script
|
|
Author: Milan Toman (milan.v.toman@gmail.com)
|
|
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
|
|
------------------------------------------------------------------------
|
|
'''
|
|
# mandatory
|
|
import requests
|
|
import sys
|
|
import os
|
|
import re
|
|
import textwrap
|
|
import argparse
|
|
import time
|
|
import datetime
|
|
import simplejson as json
|
|
import sqlite3
|
|
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")
|
|
|
|
global _hostname
|
|
_hostname = socket.gethostname()
|
|
|
|
'''
|
|
------------------------------------------------------------------------
|
|
SQLite def
|
|
------------------------------------------------------------------------
|
|
'''
|
|
_SQLiteDB = '/var/lib/plutonium/status.db'
|
|
|
|
'''
|
|
------------------------------------------------------------------------
|
|
Define variables
|
|
------------------------------------------------------------------------
|
|
'''
|
|
_VERSION = 2.0
|
|
_NAME = u"System stats collector"
|
|
|
|
|
|
'''
|
|
------------------------------------------------------------------------
|
|
Generic, standalone functions
|
|
------------------------------------------------------------------------
|
|
'''
|
|
|
|
# Obvious shit, set up the client class
|
|
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"
|
|
|
|
def construct(self, DB, query):
|
|
""" Takes values in a writes them to influxdb
|
|
|
|
requires: list(connector): connector with ticks to be written
|
|
at once
|
|
str(measurement): the measurement ID to be written
|
|
dict(fields): fields to be written in one tick
|
|
dict(tags): tags to be written with the fields
|
|
|
|
returns: list(result_connector)
|
|
"""
|
|
conn = sqlite3.connect(db_file)
|
|
c = conn.cursor()
|
|
c.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
return result_connector
|
|
|
|
class Stats(object):
|
|
def __init__(self):
|
|
__name__ = "Database writer class, SQLite"
|
|
|
|
|
|
|
|
'''
|
|
------------------------------------------------------------------------
|
|
Main
|
|
------------------------------------------------------------------------
|
|
'''
|
|
if '__main__':
|
|
status_writer = DBwriter()
|
|
while True:
|
|
averaged_cpu = psutil.cpu_percent()
|
|
mem_consumption = psutil.virtual_memory()[2]
|
|
disk_usage = psutil.disk_usage('/')[3]
|
|
print(psutil)
|
|
# CPU stats
|
|
for timeout in range(1,15):
|
|
if timeout == 1:
|
|
averaged_cpu = (averaged_cpu + psutil.cpu_percent()) / 2
|
|
mem_consumption = psutil.virtual_memory()[2]
|
|
disk_usage = psutil.disk_usage('/')[3]
|
|
if_counters = psutil.net_io_counters(pernic=True)
|
|
interfaces = if_counters.keys()
|
|
|
|
wlan_counters = if_counters["wlan0"]
|
|
wlan_sent = wlan_counters[0]
|
|
wlan_recv = wlan_counters[1]
|
|
wlan_error_in = wlan_counters[4]
|
|
wlan_error_out = wlan_counters[5]
|
|
wlan_drop_in = wlan_counters[6]
|
|
wlan_drop_out = wlan_counters[7]
|
|
eth_counters = if_counters["eth0"]
|
|
eth_sent = eth_counters[0]
|
|
eth_recv = eth_counters[1]
|
|
eth_error_in = eth_counters[4]
|
|
eth_error_out = eth_counters[5]
|
|
eth_drop_in = eth_counters[6]
|
|
eth_drop_out = eth_counters[7]
|
|
else:
|
|
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,
|
|
"RasPI",
|
|
{"usage": float(averaged_cpu)},
|
|
{"type": "cpu"}
|
|
)
|
|
influx_status_write = status_writer.construct(
|
|
influx_status_write,
|
|
"RasPI",
|
|
{"usage": float(mem_consumption)},
|
|
{"type": "mem"}
|
|
)
|
|
influx_status_write = status_writer.construct(
|
|
influx_status_write,
|
|
"RasPI",
|
|
{"usage": float(disk_usage)},
|
|
{"type": "disk"}
|
|
)
|
|
influx_status_write = status_writer.construct(
|
|
influx_status_write,
|
|
"net",
|
|
{
|
|
"b_out": float(wlan_sent),
|
|
"b_in": float(wlan_recv),
|
|
"e_out": float(wlan_error_out),
|
|
"e_in": float(wlan_error_in),
|
|
"drop_out": float(wlan_drop_out),
|
|
"drop_in": float(wlan_drop_in),
|
|
},
|
|
{
|
|
"type": "wlan0",
|
|
"host": _hostname
|
|
}
|
|
)
|
|
influx_status_write = status_writer.construct(
|
|
influx_status_write,
|
|
"net",
|
|
{
|
|
"b_out": float(eth_sent),
|
|
"b_in": float(eth_recv),
|
|
"e_out": float(eth_error_out),
|
|
"e_in": float(eth_error_in),
|
|
"drop_out": float(eth_drop_out),
|
|
"drop_in": float(eth_drop_in),
|
|
},
|
|
{
|
|
"type": "eth0",
|
|
"host": _hostname
|
|
}
|
|
)
|
|
print("Writing values: {}".format(influx_status_write))
|
|
influx_status_client.write_points(influx_status_write)
|
|
influx_status_write = []
|
|
averaged_cpu = 0
|