aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWojciech Kosior <kwojtus@protonmail.com>2020-06-17 14:24:24 +0200
committerWojciech Kosior <kwojtus@protonmail.com>2020-06-17 14:24:24 +0200
commit1017dfb223bc857a2327dbad90b8c68e8664d3fe (patch)
treea852725839bf5dc72965e3d3c98da6b4ebf656ee /src
parent7caef553b4061b6179c2757cb3bbd4e17e892cf7 (diff)
download0tdns-1017dfb223bc857a2327dbad90b8c68e8664d3fe.tar.gz
0tdns-1017dfb223bc857a2327dbad90b8c68e8664d3fe.zip
prepend log message with an hour
Diffstat (limited to 'src')
-rwxr-xr-xsrc/hourly.py4
-rwxr-xr-xsrc/perform_queries.py3
-rw-r--r--src/ztdnslib.py10
3 files changed, 15 insertions, 2 deletions
diff --git a/src/hourly.py b/src/hourly.py
index 4fd30a6..ecf1f10 100755
--- a/src/hourly.py
+++ b/src/hourly.py
@@ -10,7 +10,7 @@ import psycopg2
# our own module used by several scripts in the project
from ztdnslib import start_db_connection, \
- get_default_host_address, get_ztdns_config, log
+ get_default_host_address, get_ztdns_config, log, set_loghour
wrapper = '/var/lib/0tdns/vpn_wrapper.sh'
perform_queries = '/var/lib/0tdns/perform_queries.py'
@@ -251,6 +251,8 @@ def do_hourly_work(hour):
# round down to an hour - this datetime format is one
# of the formats accepted by postgres
hour = strftime('%Y-%m-%d %H:00%z', gmtime())
+set_loghour(hour) # log() function will now prepend messages with hour
+
if not lock_on_file():
log('Failed trying to run for {}; {} exists'.format(hour, lockfile))
else:
diff --git a/src/perform_queries.py b/src/perform_queries.py
index c11ff1a..4e22f1b 100755
--- a/src/perform_queries.py
+++ b/src/perform_queries.py
@@ -7,7 +7,7 @@ import unbound
import psycopg2
# our own module used by several scripts in the project
-from ztdnslib import start_db_connection, get_ztdns_config, log
+from ztdnslib import start_db_connection, get_ztdns_config, log, set_loghour
class dns_queries:
def __init__(self, dns_IP, dns_id, services):
@@ -122,6 +122,7 @@ def resolve_call_back(mydata, status, result):
dups = False
hour = argv[1]
+set_loghour(hour) # log() function will now prepend messages with hour
vpn_id = argv[2]
config = get_ztdns_config()
diff --git a/src/ztdnslib.py b/src/ztdnslib.py
index dc54602..8ac0169 100644
--- a/src/ztdnslib.py
+++ b/src/ztdnslib.py
@@ -2,6 +2,7 @@ import yaml
import psycopg2
import os
import fcntl
+from time import gmtime, strftime
db_config_path = '/etc/0tdns/db_connection_config.yml'
logfile = '/var/log/0tdns.log'
@@ -28,7 +29,16 @@ def get_default_host_address(remote_address):
s.close()
return hostaddr
+loghour = None
+
+def set_loghour(hour):
+ global loghour
+ loghour = hour
+
def log(msg):
+ msg = '[{}] {}'.format(strftime('%H:%M', gmtime()), msg)
+ if loghour:
+ msg = '[{}]{}'.format(loghour, msg)
msg = bytearray(msg + '\n', "UTF-8")
fd = os.open(logfile, os.O_APPEND | os.O_WRONLY | os.O_CREAT)
try: