def __init__(self):
        self.db_sensors = sqlite3.connect(dashconfig['db-sensors-path'])
        self.db_power = sqlite3.connect(dashconfig['db-power-path'])

        self.sensors = dashconfig['sensors-id']
        self.power_backlog = []
        self.power_backlog_days = []

        self.slave_sensors = DashboardSlave("sensors-backlog")
        self.slave_power_backlog = DashboardSlave("power-backlog")
        self.slave_power_backlog_days = DashboardSlave("power-backlog-days")
Beispiel #2
0
    def __init__(self):
        self.sensors_last = {}
        self.sensors_id = dashconfig['sensors-id']
        self.sensors_dht_last = {}

        self.app = Sanic(__name__)

        self.db_sensors = sqlite3.connect(dashconfig['db-sensors-path'])
        self.db_power = sqlite3.connect(dashconfig['db-power-path'])

        self.power = {
            1000: {
                'timestamp': 0,
                'value': 0,
            }
        }

        self.slave_sensors = DashboardSlave("sensors")
        self.slave_sensors_dht = DashboardSlave("sensors-dht")
        self.slave_power = DashboardSlave("power")
class DashboardBacklog():
    def __init__(self):
        self.db_sensors = sqlite3.connect(dashconfig['db-sensors-path'])
        self.db_power = sqlite3.connect(dashconfig['db-power-path'])

        self.sensors = dashconfig['sensors-id']
        self.power_backlog = []
        self.power_backlog_days = []

        self.slave_sensors = DashboardSlave("sensors-backlog")
        self.slave_power_backlog = DashboardSlave("power-backlog")
        self.slave_power_backlog_days = DashboardSlave("power-backlog-days")

    def power_backlog_fetch(self):
        print("[+] power backlogger: fetching 24h")

        yesterday = datetime.datetime.fromtimestamp(time.time() - 86400)
        yesterday = yesterday.replace(minute=0, second=0)
        limit = int(time.mktime(yesterday.timetuple())) + 3600

        # Today
        cursor = self.db_power.cursor()
        '''
        cursor.execute("""
            select byhour, sum(val) from (
                select strftime('%m-%d %Hh', timestamp, 'unixepoch', 'localtime') byhour, avg(value) val, phase
                from power where timestamp > ? group by byhour, phase
            ) group by byhour;
        """, (limit,))
        '''
        cursor.execute("""
            select strftime('%m-%d %Hh', timestamp, 'unixepoch', 'localtime') byhour, avg(value) val
            from power where timestamp > ? and phase = 2 group by byhour
        """, (limit,))

        backlog = cursor.fetchall()
        parsed = []

        for values in backlog:
            parsed.append([values[0][6:], values[1]])


        self.slave_power_backlog.set(parsed)
        self.slave_power_backlog.publish()

        # 30 days backlog
        print("[+] power backlogger: fetching 30 days")
        cursor = self.db_power.cursor()
        '''
        cursor.execute("""
            -- select sum of hours, per day
            select strftime('%Y-%m-%d', byhour) byday, sum(av) from (
                -- select full average per hour (sum phases)
                select byhour, sum(av) av from (
                    -- select average per hour, per phase
                    select strftime('%Y-%m-%d %H:00:00', timestamp, 'unixepoch', 'localtime') byhour, avg(value) av, phase
                    from power where date(timestamp, 'unixepoch') > date('now', '-30 days') group by byhour, phase
                ) group by byhour
            ) group by byday;
        """)
        cursor.execute("""
            -- select sum of hours, per day
            select strftime('%Y-%m-%d', byhour) byday, sum(av) from (
                -- select average per hour
                select strftime('%Y-%m-%d %H:00:00', timestamp, 'unixepoch', 'localtime') byhour, avg(value) av
                from power where date(timestamp, 'unixepoch') > date('now', '-30 days') and phase = 2 group by byhour
            ) group by byday;
        """)
        '''
        cursor.execute("""
            select strftime('%Y-%m-%d', byhour) byday, phase, sum(av) from (
                select strftime('%Y-%m-%d %H:00:00', timestamp, 'unixepoch', 'localtime') byhour, avg(value) av, phase
                from power where date(timestamp, 'unixepoch') > date('now', '-30 days') group by byhour, phase
            ) group by byday, phase;
        """)

        backlog = cursor.fetchall()

        self.slave_power_backlog_days.set(backlog)
        self.slave_power_backlog_days.publish()

    def sensors_backlog(self, id):
        limit = 600

        cursor = self.db_sensors.cursor()
        rows = (id, limit)
        cursor.execute("select timestamp, value from sensors where id=? order by timestamp desc limit ?", rows)

        backlog = cursor.fetchall()

        array = []

        for entry in backlog:
            array.append([entry[0] * 1000, entry[1]])

        return array

    def run(self):
        while True:
            for name in self.sensors:
                print("[+] sensors backlog: fetching [%s]" % name)
                self.slave_sensors.set({"id": name, "serie": self.sensors_backlog(name)})
                self.slave_sensors.publish()

            self.power_backlog_fetch()

            time.sleep(5 * 60)
Beispiel #4
0
import json
import time
import redis
from config import dashconfig
from dashboard import DashboardSlave

r = redis.Redis()
slave = DashboardSlave("dnsquery")


class BindRedisStats:
    def __init__(self, target="127.0.0.1"):
        self.read = redis.Redis(target)
        self.write = redis.Redis(target)

        self.reader = self.read.pubsub()
        self.reader.subscribe("bind-query")

        self.excludes = ["10.241.0.252", "10.241.0.253"]
        self.lastline = ""

    def process(self, message):
        client = message.split()
        host = client[2].split("#")

        hostaddr = host[0]
        query = client[5]

        # ignore RIPE
        if host[0] in self.excludes:
            return None
Beispiel #5
0
from modules.lapac import *
from config import dashconfig
from dashboard import DashboardSlave

slave = DashboardSlave("wireless")

monitor = LAPACMonitor(dashconfig['lapac-address'],
                       dashconfig['lapac-username'],
                       dashconfig['lapac-password'])

while True:
    print("[+] wireless: updating")

    response = monitor.allclients()
    print("[+] wireless: %d clients found" % len(monitor.clients))

    slave.set(monitor.clients)
    slave.publish()
    slave.sleep(2)
Beispiel #6
0
import requests
from config import dashconfig
from dashboard import DashboardSlave

slave = DashboardSlave("rtinfo")

while True:
    print("[+] rtinfo: fetching")

    try:
        response = requests.get(dashconfig['rtinfo-endpoint'])
        slave.set(response.json())

        print("[+] rtinfo: %d hosts found" % len(slave.payload['rtinfo']))

        slave.publish()

    except Exception as e:
        print(e)

    slave.sleep(1)
Beispiel #7
0
import requests
import time
from config import dashconfig
from dashboard import DashboardSlave

slave = DashboardSlave("weather")

while True:
    print("[+] weather information: fetching")

    response = requests.get(dashconfig['weather-station'], timeout=3).json()
    slave.set(response)

    if not response:
        slave.set({
            "temp": "-",
            "press": "-",
            "hum": "-",
            "dew": "-",
            "wind": 0,
            "uv": "-",
            "widir": "-",
            "gust": None,
            "solar": "-"
        })

    response = requests.get(dashconfig['weather-rain'], timeout=3).json()
    if not response:
        continue

    slave.payload['rain90min'] = response['rain_90min']
Beispiel #8
0
class DashboardSensors():
    def __init__(self):
        self.sensors_last = {}
        self.sensors_id = dashconfig['sensors-id']
        self.sensors_dht_last = {}

        self.app = Sanic(__name__)

        self.db_sensors = sqlite3.connect(dashconfig['db-sensors-path'])
        self.db_power = sqlite3.connect(dashconfig['db-power-path'])

        self.power = {
            1000: {
                'timestamp': 0,
                'value': 0,
            }
        }

        self.slave_sensors = DashboardSlave("sensors")
        self.slave_sensors_dht = DashboardSlave("sensors-dht")
        self.slave_power = DashboardSlave("power")

    def httpd_routes(self, app):
        @app.route("/sensors/<name>/<timestamp>/<value>")
        async def httpd_routes_index(request, name, timestamp, value):
            print("[+] sensors: %s (%s): value: %s" % (name, timestamp, value))

            cursor = self.db_sensors.cursor()
            rows = (name, int(timestamp), float(value))
            cursor.execute(
                "INSERT INTO sensors (id, timestamp, value) VALUES (?, ?, ?)",
                rows)
            self.db_sensors.commit()

            self.sensors_last[name] = {
                'id': name,
                'timestamp': int(timestamp),
                'value': float(value),
            }

            self.slave_sensors.set(self.sensors_last)
            self.slave_sensors.publish()
            """
            # pushing chart
            temp = {"id": name, "serie": self.sensors_backlog(name)}
            await self.wsbroadcast("sensors-backlog", temp)
            """

            return sanicjson({})

        @app.route("/sensors-dht/<name>/<timestamp>/<temperature>/<humidity>")
        async def httpd_routes_dht(request, name, timestamp, temperature,
                                   humidity):
            print("[+] sensors: %s (%s): temperature: %s, humidity: %s" %
                  (name, timestamp, temperature, humidity))

            cursor = self.db_sensors.cursor()
            rows = (name, int(timestamp), float(temperature), float(humidity))
            cursor.execute(
                "INSERT INTO dht (id, timestamp, temp, hum) VALUES (?, ?, ?, ?)",
                rows)
            self.db_sensors.commit()

            self.sensors_dht_last[name] = {
                'id': name,
                'timestamp': int(timestamp),
                'temperature': float(temperature),
                'humidity': float(humidity),
            }

            self.slave_sensors_dht.set(self.sensors_dht_last)
            self.slave_sensors_dht.publish()
            """
            # pushing chart
            temp = {"id": name, "serie": self.sensors_backlog(name)}
            await self.wsbroadcast("sensors-backlog", temp)
            """

            return sanicjson({})

        @app.route("/power/<timestamp>/<value>")
        async def httpd_routes_power(request, timestamp, value):
            print("[+] power: %s watt at %s" % (value, timestamp))

            cursor = self.db_power.cursor()
            rows = (int(timestamp), float(value))
            cursor.execute(
                "INSERT OR IGNORE INTO power (timestamp, value, phase) VALUES (?, ?, 1000)",
                rows)
            self.db_power.commit()

            self.power["1000"] = {
                'timestamp': int(timestamp),
                'value': float(value),
            }

            self.slave_power.set(self.power)
            self.slave_power.publish()

            # pushing chart
            # temp = {"id": name, "serie": self.sensors_backlog(name)}
            # await self.wsbroadcast("sensors-backlog", temp)

            return sanicjson({})

        @app.route("/power/<timestamp>/<phase>/<value>")
        async def httpd_routes_power(request, timestamp, phase, value):
            print("[+] power: phase %s: %s watt at %s" %
                  (phase, value, timestamp))

            cursor = self.db_power.cursor()
            rows = (int(timestamp), float(value), int(phase))
            cursor.execute(
                "INSERT OR IGNORE INTO power (timestamp, value, phase) VALUES (?, ?, ?)",
                rows)
            self.db_power.commit()

            self.power[phase] = {
                'timestamp': int(timestamp),
                'value': float(value),
            }

            self.slave_power.set(self.power)
            self.slave_power.publish()

            # pushing chart
            # temp = {"id": name, "serie": self.sensors_backlog(name)}
            # await self.wsbroadcast("sensors-backlog", temp)

            return sanicjson({})

    def run(self):
        loop = asyncio.get_event_loop()

        self.httpd_routes(self.app)
        httpd = self.app.create_server(host=dashconfig['http-listen-addr'],
                                       port=dashconfig['http-listen-port'])
        asyncio.ensure_future(httpd, loop=loop)

        #
        # main loop, let's run everything together
        #
        loop.run_forever()
Beispiel #9
0
import json
import time
import redis
from config import dashconfig
from dashboard import DashboardSlave

r = redis.Redis()
slave = DashboardSlave("devices")

while True:
    print("[+] local (dhcp) devices checker: updating")

    devices = {}

    dhclients = r.keys('dhcp-*')
    for client in dhclients:
        data = r.get(client)
        if not data:
            continue

        payload = data.decode('utf-8')
        keyname = client.decode('utf-8')[5:]

        devices[keyname] = json.loads(payload)

    clients = r.keys('traffic-*')
    for client in clients:
        payload = r.get(client).decode('utf-8')
        live = json.loads(payload)

        # ignore inactive client
Beispiel #10
0
from modules.netuse import *
from config import dashconfig
from dashboard import DashboardSlave

pings = dashconfig['ping-targets']
ping = RootPing()

slave = DashboardSlave("ping")
payload = {}

while True:
    for target in pings:
        print("[+] ping checker: pinging %s" % target)

        value = ping.ping_host(pings[target]['target'])
        pings[target]['value'] = value
        print("[+] ping checker: %s, %s" % (target, value))

        slave.set({"name": target, "data": pings[target]})
        slave.publish()

    slave.sleep(2)

Beispiel #11
0
from modules.voolevels import *
from config import dashconfig
from dashboard import DashboardSlave

slave = DashboardSlave("docsis-levels")

while True:
    print("[+] voo levels: fetching new values")

    levels = SignalLevel(dashconfig['voo-address'], dashconfig['voo-password'])

    try:
        response = levels.fetch()
        if response:
            slave.set({'up': levels.upstream, 'down': levels.downstream})

            docsisaverage = 0.0
            for ups in slave.payload['up']:
                docsisaverage += ups['txpower']

            print("[+] voo levels: upstream ~%.2f dBmV" %
                  (docsisaverage / len(slave.payload['up'])))
            slave.publish()

    except Exception as e:
        print(e)

    slave.sleep(10)