Commit df42b9eb authored by Joe Lyons's avatar Joe Lyons

add x2go clean sessions script

parent 1734890c
#!/usr/bin/python3
# Search for x2go sessions and terminate depending how old that are
import subprocess
import time
import pwd
import sys
from systemd import journal
stale_age = 10
# Set session fields for x2golistsessions
session_fields = ["agent_pid",
"session_id",
"port",
"host",
"state",
"init_time",
"session_cookie",
"client_ip",
"gr_port",
"snd_port",
"last_time",
"user",
"age_in_secs",
"sshfs_port"]
# Get session data
stdoutdata = subprocess.getoutput("x2golistsessions_root")
for sessions in stdoutdata.splitlines():
# Collect session data
session = sessions.split('|')
session_obj = zip(session_fields, session)
session = dict(session_obj)
# Work out last time connected
last_time = time.mktime(time.strptime(session['last_time'], '%Y-%m-%dT%H:%M:%S'))
# How old is the session
now = time.time()
session_time = now - last_time
session_time_min = int(session_time / 60)
session_time_hour = int(session_time_min / 60)
session_time_day = int(session_time_hour / 24)
# If suspended and old than specified age, terminate.
if session['state'] == "S":
if session_time_day > stale_age:
print("Terminating %s. Suspended for %i days" % (session['user'], session_time_day))
# Log
journal.send('Terminating x2go session %s for user %s. It was suspended for %i days.' % (session['session_id'],session['user'],session_time_day), SYSLOG_IDENTIFIER='x2go_clean_sessions')
output = subprocess.run(["sudo", "-u", str(session['user']), "/usr/bin/x2goterminate-session", session['session_id']])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment