Verified Commit ddc90cc5 authored by Martin Petráček's avatar Martin Petráček
Browse files

make archive_path configurable

parent c0d39f31
......@@ -14,18 +14,16 @@ import signal
import errno
import logging
__ARCHIVE_DB_PATH__ = "/srv/pakon/pakon-archive.db"
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
#logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
delimiter = '__uci__delimiter__'
#TODO: replace with uci bindings - once available
def uci_get(opt):
delimiter = '__uci__delimiter__'
chld = subprocess.Popen(['/sbin/uci', '-d', delimiter, '-q', 'get', opt],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = chld.communicate()
out = str(out.strip(), 'ascii')
out = out.strip().decode('ascii','ignore')
if out.find(delimiter) != -1:
return out.split(delimiter)
else:
......@@ -48,9 +46,8 @@ def uci_get_time(opt, default):
ret = int(text)
return ret
if not os.path.isfile(__ARCHIVE_DB_PATH__):
subprocess.call(['/usr/bin/python3', '/usr/libexec/pakon-light/create_db.py'])
con = sqlite3.connect(__ARCHIVE_DB_PATH__)
archive_path = uci_get('pakon.common.archive_path') or '/srv/pakon/pakon-archive.db'
con = sqlite3.connect(archive_path)
con.row_factory = sqlite3.Row
def squash(from_details, to_details, start, window):
......
......@@ -2,6 +2,19 @@
import os
import sqlite3
import subprocess
#TODO: replace with uci bindings - once available
def uci_get(opt):
delimiter = '__uci__delimiter__'
chld = subprocess.Popen(['/sbin/uci', '-d', delimiter, '-q', 'get', opt],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = chld.communicate()
out = out.strip().decode('ascii','ignore')
if out.find(delimiter) != -1:
return out.split(delimiter)
else:
return out
os.makedirs("/var/lib", exist_ok=True)
con = sqlite3.connect('/var/lib/pakon.db')
......@@ -13,8 +26,9 @@ c.execute('PRAGMA user_version=1')
con.commit()
con.close()
os.makedirs("/srv/pakon", exist_ok=True)
con = sqlite3.connect('/srv/pakon/pakon-archive.db')
archive_path = uci_get('pakon.common.archive_path') or '/srv/pakon/pakon-archive.db'
os.makedirs(os.path.dirname(os.path.abspath(archive_path)), exist_ok=True)
con = sqlite3.connect(archive_path)
c = con.cursor()
c.execute('CREATE TABLE IF NOT EXISTS traffic (start real, duration integer, details integer, src_mac text, src_ip text, src_port integer, dest_ip text, dest_port integer, proto text, app_proto text, bytes_send integer, bytes_received integer, app_hostname text)')
c.execute('CREATE INDEX IF NOT EXISTS traffic_lookup ON traffic(details, start, src_mac)')
......
......@@ -11,8 +11,21 @@ import errno
import re
import json
import glob
import subprocess
import socketserver
#TODO: replace with uci bindings - once available
def uci_get(opt):
delimiter = '__uci__delimiter__'
chld = subprocess.Popen(['/sbin/uci', '-d', delimiter, '-q', 'get', opt],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = chld.communicate()
out = out.strip().decode('ascii','ignore')
if out.find(delimiter) != -1:
return out.split(delimiter)
else:
return out
def build_filter(query):
now = time.time()
if "start" in query:
......@@ -64,9 +77,10 @@ def is_ignored(hostname):
return False
def query(query):
archive_path = uci_get('pakon.common.archive_path') or '/srv/pakon/pakon-archive.db'
con = sqlite3.connect('/var/lib/pakon.db')
c = con.cursor()
c.execute('ATTACH DATABASE "/srv/pakon/pakon-archive.db" AS archive')
c.execute('ATTACH DATABASE ? AS archive', (archive_path,))
try:
query = json.loads(query)
except ValueError:
......
......@@ -24,6 +24,7 @@ from cachetools import LRUCache, TTLCache
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
#logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
#TODO: replace with uci bindings - once available
def uci_get(opt):
delimiter = '__uci__delimiter__'
chld = subprocess.Popen(['/sbin/uci', '-d', delimiter, '-q', 'get', opt],
......@@ -242,8 +243,7 @@ def reload_replaces(signum, frame):
def main():
global allowed_interfaces, conntrack
if not os.path.isfile('/var/lib/pakon.db') or not os.path.isfile('/srv/pakon/pakon-archive.db'):
subprocess.call(['/usr/bin/python3', '/usr/libexec/pakon-light/create_db.py'])
archive_path = uci_get('pakon.common.archive_path') or '/srv/pakon/pakon-archive.db'
dns_cache.try_load()
con = sqlite3.connect('/var/lib/pakon.db')
c = con.cursor()
......@@ -253,9 +253,9 @@ def main():
con.commit()
except:
logging.debug('Error cleaning flow_id')
notify_new_devices=uci_get('pakon.monitor.notify_new_devices')
notify_new_devices = uci_get('pakon.common.notify_new_devices')
if notify_new_devices:
c.execute('ATTACH "/srv/pakon/pakon-archive.db" AS archive')
c.execute('ATTACH ? AS archive', (archive_path,))
for row in c.execute('SELECT DISTINCT(src_mac) FROM traffic UNION SELECT DISTINCT(src_mac) FROM archive.traffic'):
known_devices.add(row[0])
c.execute('DETACH archive')
......
config monitor 'monitor'
config common 'common'
option notify_new_devices 1
config archive 'archive'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment