fix datetime in cleanup.py #2
36
calls.py
36
calls.py
|
@ -22,6 +22,7 @@ class EjabberdApiCalls(EjabberdApi):
|
|||
|
||||
except AttributeError:
|
||||
# emtpy response or None obj
|
||||
log.warning("nodename not found on split")
|
||||
raise SystemExit(17)
|
||||
|
||||
except IndexError:
|
||||
|
@ -50,6 +51,7 @@ class EjabberdApiCalls(EjabberdApi):
|
|||
tmp = ver_str.findall(status)[0]
|
||||
# raise SystemExit code 17 if no status message is received
|
||||
except TypeError:
|
||||
log.warning("ver sting not parsed")
|
||||
raise SystemExit(17)
|
||||
|
||||
# return parsed version string
|
||||
|
@ -93,25 +95,39 @@ class EjabberdApiCalls(EjabberdApi):
|
|||
|
||||
def fetch_s2s_in(self):
|
||||
result = self.cmd("incoming_s2s_number", {})
|
||||
if "s2s_incoming" not in result:
|
||||
return result
|
||||
return result["s2s_incoming"]
|
||||
if isinstance(result, dict):
|
||||
if "s2s_incoming" in result:
|
||||
return result["s2s_incoming"]
|
||||
log.warning("fetch_s2s_in: error empty result " + str(result))
|
||||
return 0
|
||||
return result
|
||||
|
||||
def fetch_s2s_out(self):
|
||||
result = self.cmd("outgoing_s2s_number", {})
|
||||
if "s2s_outgoing" not in result:
|
||||
return result
|
||||
return result["s2s_outgoing"]
|
||||
if isinstance(result, dict):
|
||||
if "s2s_outgoing" in result:
|
||||
return result["s2s_outgoing"]
|
||||
log.warning("fetch_s2s_out: error empty result " + str(result))
|
||||
return 0
|
||||
return result
|
||||
|
||||
def fetch_uptime(self):
|
||||
result = self.cmd("stats", {"name": "uptimeseconds"})
|
||||
if "stat" in result:
|
||||
return result["stat"]
|
||||
if isinstance(result, dict):
|
||||
if "stat" in result:
|
||||
return result["stat"]
|
||||
log.warning("uptime: error empty result " + str(result))
|
||||
return 0
|
||||
return result
|
||||
|
||||
def fetch_processes(self):
|
||||
result = self.cmd("stats", {"name": "processes"})
|
||||
if "stat" in result:
|
||||
return result["stat"]
|
||||
if isinstance(result, dict):
|
||||
if "stat" in result:
|
||||
return result["stat"]
|
||||
log.warning("processes: error empty result " + str(result))
|
||||
return 0
|
||||
return result
|
||||
|
||||
def fetch_registered_count(self, vhost=None):
|
||||
if vhost is None:
|
||||
|
|
20
cleanup.py
20
cleanup.py
|
@ -16,6 +16,9 @@ class EjabberdCleanup(EjabberdApiCalls):
|
|||
self.delete_not_login = True
|
||||
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=""):
|
||||
if self.dry:
|
||||
logging.warning(f"{user}@{host}: dry delete : {reason}")
|
||||
|
@ -44,13 +47,24 @@ class EjabberdCleanup(EjabberdApiCalls):
|
|||
except ValueError as err:
|
||||
logging.error(f"{user}@{host}: not able to parse '{last_stamp}': {err}")
|
||||
return
|
||||
if lastdate is not None and lastdate - datetime.datetime.now() > datetime.timedelta(
|
||||
days=self.offline_since_days
|
||||
):
|
||||
if lastdate is None:
|
||||
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}")
|
||||
return
|
||||
else:
|
||||
logging.debug(f"{user}@{host}: keep @ {lastdate}")
|
||||
return
|
||||
logging.debug(f"{user}@{host}: keep")
|
||||
|
||||
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():
|
||||
if host in self.ignore_hosts:
|
||||
continue
|
||||
|
|
Loading…
Reference in New Issue