fix datetime in cleanup.py #2

Manually merged
sum7 merged 2 commits from fix-datetime into master 2021-04-17 17:53:42 +02:00
2 changed files with 43 additions and 13 deletions

View File

@ -22,6 +22,7 @@ class EjabberdApiCalls(EjabberdApi):
except AttributeError: except AttributeError:
# emtpy response or None obj # emtpy response or None obj
log.warning("nodename not found on split")
raise SystemExit(17) raise SystemExit(17)
except IndexError: except IndexError:
@ -50,6 +51,7 @@ class EjabberdApiCalls(EjabberdApi):
tmp = ver_str.findall(status)[0] tmp = ver_str.findall(status)[0]
# raise SystemExit code 17 if no status message is received # raise SystemExit code 17 if no status message is received
except TypeError: except TypeError:
log.warning("ver sting not parsed")
raise SystemExit(17) raise SystemExit(17)
# return parsed version string # return parsed version string
@ -93,25 +95,39 @@ class EjabberdApiCalls(EjabberdApi):
def fetch_s2s_in(self): def fetch_s2s_in(self):
result = self.cmd("incoming_s2s_number", {}) result = self.cmd("incoming_s2s_number", {})
if "s2s_incoming" not in result: if isinstance(result, dict):
return result if "s2s_incoming" in result:
return result["s2s_incoming"] return result["s2s_incoming"]
log.warning("fetch_s2s_in: error empty result " + str(result))
return 0
return result
def fetch_s2s_out(self): def fetch_s2s_out(self):
result = self.cmd("outgoing_s2s_number", {}) result = self.cmd("outgoing_s2s_number", {})
if "s2s_outgoing" not in result: if isinstance(result, dict):
return result if "s2s_outgoing" in result:
return result["s2s_outgoing"] return result["s2s_outgoing"]
log.warning("fetch_s2s_out: error empty result " + str(result))
return 0
return result
def fetch_uptime(self): def fetch_uptime(self):
result = self.cmd("stats", {"name": "uptimeseconds"}) result = self.cmd("stats", {"name": "uptimeseconds"})
if isinstance(result, dict):
if "stat" in result: if "stat" in result:
return result["stat"] return result["stat"]
log.warning("uptime: error empty result " + str(result))
return 0
return result
def fetch_processes(self): def fetch_processes(self):
result = self.cmd("stats", {"name": "processes"}) result = self.cmd("stats", {"name": "processes"})
if isinstance(result, dict):
if "stat" in result: if "stat" in result:
return result["stat"] return result["stat"]
log.warning("processes: error empty result " + str(result))
return 0
return result
def fetch_registered_count(self, vhost=None): def fetch_registered_count(self, vhost=None):
if vhost is None: if vhost is None:

View File

@ -16,6 +16,9 @@ class EjabberdCleanup(EjabberdApiCalls):
self.delete_not_login = True self.delete_not_login = True
self.offline_since_days = None self.offline_since_days = None
def calc_time_before(self):
return datetime.datetime.now() - datetime.timedelta(days=self.offline_since_days);
def delete_user(self, host, user, reason=""): def delete_user(self, host, user, reason=""):
if self.dry: if self.dry:
logging.warning(f"{user}@{host}: dry delete : {reason}") logging.warning(f"{user}@{host}: dry delete : {reason}")
@ -44,13 +47,24 @@ class EjabberdCleanup(EjabberdApiCalls):
except ValueError as err: except ValueError as err:
logging.error(f"{user}@{host}: not able to parse '{last_stamp}': {err}") logging.error(f"{user}@{host}: not able to parse '{last_stamp}': {err}")
return return
if lastdate is not None and lastdate - datetime.datetime.now() > datetime.timedelta( if lastdate is None:
days=self.offline_since_days logging.error(f"{user}@{host}: not able to parse '{last_stamp}'")
): return
elif lastdate < self.calc_time_before():
self.delete_user(host, user, f"last seen @ {lastdate}") self.delete_user(host, user, f"last seen @ {lastdate}")
return return
else:
logging.debug(f"{user}@{host}: keep @ {lastdate}")
return
logging.debug(f"{user}@{host}: keep")
def run(self): def run(self):
logging.debug("run with:")
logging.debug(f"dry: {self.dry}")
logging.debug(f"skip by roster: {self.skip_by_roster}")
logging.debug(f"delete not login: {self.delete_not_login}")
logging.debug(f"offline since days: {self.offline_since_days}")
for host in self.fetch_vhosts(): for host in self.fetch_vhosts():
if host in self.ignore_hosts: if host in self.ignore_hosts:
continue continue