Add host/ip and port as arguments, be more verbose about error situations when it comes to timestamps, and handle-ish rrd-locking errors

This commit is contained in:
2024-04-16 00:29:33 +02:00
parent 99a2a2ec47
commit 6e0b7e3e70

View File

@@ -4,6 +4,7 @@ import socket
import logging import logging
import time import time
import os import os
import sys
import rrdtool import rrdtool
@@ -140,10 +141,14 @@ class UpdateRRD:
info = rrdtool.info(self.rrdfile) info = rrdtool.info(self.rrdfile)
if self.toupdate['timestamp'] is None: if self.toupdate['timestamp'] is None:
return False return False
if info['last_update'] >= self.toupdate['timestamp']: if info['last_update'] > self.toupdate['timestamp']:
logging.error('Trying to update when rrdfile is newer than our timestamp. Ignoring line and resetting.') logging.error('Trying to update when rrdfile is newer than our timestamp. Ignoring line and resetting.')
self.toupdate = self.freshdict.copy() self.toupdate = self.freshdict.copy()
return False return False
elif info['last_update'] == self.toupdate['timestamp']:
logging.error('last update and toupdate timestamp are the same, this should not happen')
self.toupdate = self.freshdict.copy()
return False
try: try:
rrdtool.update( rrdtool.update(
self.rrdfile, self.rrdfile,
@@ -156,6 +161,8 @@ class UpdateRRD:
logging.debug('Updated rrdfile') logging.debug('Updated rrdfile')
return True return True
except rrdtool.OperationalError as error: except rrdtool.OperationalError as error:
if str(error) == 'could not lock RRD':
return False
logging.error(str(error)) logging.error(str(error))
return False return False
@@ -173,13 +180,24 @@ class UpdateRRD:
self.toupdate['loc-net'] += input_dict['loc-net'] self.toupdate['loc-net'] += input_dict['loc-net']
elif input_dict['timestamp'] < self.toupdate['timestamp']: elif input_dict['timestamp'] < self.toupdate['timestamp']:
logging.error('Newly fetched data is older than what we have in the queue already. Passing.') logging.error('Newly fetched data is older than what we have in the queue already. Passing.')
logging.error('Difference is ' + str(input_dict['timestamp'] - self.toupdate['timestamp']) + ' seconds')
else: else:
logging.error('Not sure what to do here? ' + str(input_dict) + str(self.toupdate)) logging.error('Not sure what to do here? ' + str(input_dict) + str(self.toupdate))
def main(): def main():
rrdfile = 'test.rrd' rrdfile = 'test.rrd'
rrdupdater = UpdateRRD(rrdfile) rrdupdater = UpdateRRD(rrdfile)
client = routerstats_client('127.0.0.1', 9999) try:
client_host = sys.argv[1]
except IndexError:
logging.error('Need hostname/ip as first argument')
try:
client_port = int(sys.argv[2])
except (ValueError, IndexError):
#We're fine on our own
client_port = 9999
client = routerstats_client(client_host, client_port)
while True: while True:
try: try:
client.connect() client.connect()
@@ -193,13 +211,16 @@ def main():
try: try:
retval = handle(client.recv(), client) retval = handle(client.recv(), client)
if retval: if retval:
if retval == True:
loops = 0 loops = 0
if retval == True:
pass
else: else:
rrdupdater.add(retval) rrdupdater.add(retval)
else: else:
rrdupdater.push()
loops += 1 loops += 1
if loops >= 5:
#Want to wait until at least 5 secs have passed since last actual data fetch..
rrdupdater.push()
if loops >= 60: if loops >= 60:
logging.error('No data in 60 seconds. We expect a ping/pong every 30. Lost connection, probably') logging.error('No data in 60 seconds. We expect a ping/pong every 30. Lost connection, probably')
loops = 0 loops = 0