summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvuko <vuko@hackerspace.pl>2020-06-14 13:44:41 +0200
committervuko <vuko@hackerspace.pl>2020-06-14 13:45:21 +0200
commitf1c43b149ee6ed28c55f6014939cdf130f522885 (patch)
treeb082e08b3c994f7395f6d50974200068e15d648a
parentf99b6c92efa1d13fe04d3b3e449ad07282682010 (diff)
downloadcheckinator-f1c43b149ee6ed28c55f6014939cdf130f522885.tar.gz
checkinator-f1c43b149ee6ed28c55f6014939cdf130f522885.tar.bz2
checkinator-f1c43b149ee6ed28c55f6014939cdf130f522885.zip
fix time parsing, remove LEASE_OFFSET
-rw-r--r--at/cmd.py18
-rw-r--r--at/dhcp.py9
-rw-r--r--config.dist.yaml1
-rw-r--r--run.py3
4 files changed, 22 insertions, 9 deletions
diff --git a/at/cmd.py b/at/cmd.py
new file mode 100644
index 0000000..d2a0bb0
--- /dev/null
+++ b/at/cmd.py
@@ -0,0 +1,18 @@
+import argparse
+from pathlib import Path
+from at.dhcp import parse_isc_dhcpd_leases
+from time import time
+parser = argparse.ArgumentParser()
+parser.add_argument("leases", type=Path, help="leases file")
+parser.add_argument("--timeout", type=int, default=None, help="timeout in minutes")
+
+def list():
+ args = parser.parse_args()
+
+ with open(args.leases) as f:
+ offset, devices = parse_isc_dhcpd_leases(f)
+ if args.timeout is not None:
+ devices.purge_stale(args.timeout * 60)
+ print("Found devices:")
+ for device in devices._devices.values():
+ print(device._replace(atime = time() - device.atime))
diff --git a/at/dhcp.py b/at/dhcp.py
index 63eba35..bee2ace 100644
--- a/at/dhcp.py
+++ b/at/dhcp.py
@@ -6,7 +6,7 @@ import logging
from typing import Tuple, List, Optional, NamedTuple
from time import sleep, time, mktime
-from datetime import datetime
+from datetime import datetime, timezone
logger = logging.getLogger(__name__)
@@ -54,10 +54,9 @@ class ActiveDevices:
return updated
class Updater(threading.Thread):
- def __init__(self, timeout, lease_offset=0, logger=logger, *a, **kw):
+ def __init__(self, timeout, logger=logger, *a, **kw):
self.timeout = timeout
self.lock = threading.Lock()
- self.lease_offset = lease_offset
self.logger = logger
self.active = ActiveDevices()
threading.Thread.__init__(self, *a, **kw)
@@ -78,8 +77,6 @@ class Updater(threading.Thread):
def update(self, devices: ActiveDevices):
for device in devices._devices.values():
- if device.atime is not None:
- device = device._replace(atime = device.atime - self.lease_offset)
with self.lock:
changed = self.active.add(device)
if changed:
@@ -213,7 +210,7 @@ def parse_isc_dhcpd_leases(leases_file: io.TextIOBase) -> Tuple[int, ActiveDevic
if(field == 'starts'):
dt = datetime.strptime(' '.join(cmd[2:]),
'%Y/%m/%d %H:%M:%S;')
- atime = mktime(dt.utctimetuple())
+ atime = dt.replace(tzinfo=timezone.utc).timestamp()
if(field == 'client-hostname'):
name = cmd[1][1:-2]
if(field == 'hardware'):
diff --git a/config.dist.yaml b/config.dist.yaml
index de8b8a2..2582168 100644
--- a/config.dist.yaml
+++ b/config.dist.yaml
@@ -2,7 +2,6 @@ DB: 'at.db'
DEBUG: false
CAP_FILE: './dhcp-cap'
LEASE_FILE: './dhcpd.leases'
-LEASE_OFFSET: -3600
TIMEOUT: 1500
WIKI_URL: 'https://wiki.hackerspace.pl/people:%(login)s:start'
diff --git a/run.py b/run.py
index 2f9a25b..2b037c4 100644
--- a/run.py
+++ b/run.py
@@ -7,8 +7,7 @@ import yaml
config = yaml.safe_load(Path('config.yaml').read_text())
-updater = DhcpdUpdater(
- config['LEASE_FILE'], config['TIMEOUT'], config['LEASE_OFFSET'])
+updater = DhcpdUpdater(config['LEASE_FILE'], config['TIMEOUT'])
updater.start()
app = at.web.app(Path(__file__).parent, updater, config)