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 time
import os
import sys
import rrdtool
@ -140,10 +141,14 @@ class UpdateRRD:
info = rrdtool.info(self.rrdfile)
if self.toupdate['timestamp'] is None:
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.')
self.toupdate = self.freshdict.copy()
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:
rrdtool.update(
self.rrdfile,
@ -156,6 +161,8 @@ class UpdateRRD:
logging.debug('Updated rrdfile')
return True
except rrdtool.OperationalError as error:
if str(error) == 'could not lock RRD':
return False
logging.error(str(error))
return False
@ -173,13 +180,24 @@ class UpdateRRD:
self.toupdate['loc-net'] += input_dict['loc-net']
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('Difference is ' + str(input_dict['timestamp'] - self.toupdate['timestamp']) + ' seconds')
else:
logging.error('Not sure what to do here? ' + str(input_dict) + str(self.toupdate))
def main():
rrdfile = 'test.rrd'
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:
try:
client.connect()
@ -193,13 +211,16 @@ def main():
try:
retval = handle(client.recv(), client)
if retval:
loops = 0
if retval == True:
loops = 0
pass
else:
rrdupdater.add(retval)
else:
rrdupdater.push()
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:
logging.error('No data in 60 seconds. We expect a ping/pong every 30. Lost connection, probably')
loops = 0