From edbc83cb4205808dc4bddea2d1b6dfce6c9dc8eb Mon Sep 17 00:00:00 2001 From: Florian Rupp Date: Tue, 18 Apr 2023 22:39:21 +0200 Subject: [PATCH] Added support for local sessions Local sessions (not over SSH) are identified by 'localhost' --- prometheus-ssh-exporter.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/prometheus-ssh-exporter.py b/prometheus-ssh-exporter.py index 278c417..c79e747 100644 --- a/prometheus-ssh-exporter.py +++ b/prometheus-ssh-exporter.py @@ -38,14 +38,17 @@ def get_utmp_data() -> list[Session]: Returns a list of User Objects The function uses the utmp library. The utmp file contains information about ALL currently logged in users, including local users (not SSH sessions). We filter out the local users by checking if the remote IP address - is empty. + is empty and set the hostname for the local sessions to "localhost". """ users : list[Session] = [] with open('/var/run/utmp', 'rb') as fd: buffer = fd.read() for record in utmp.read(buffer): - if record.type == utmp.UTmpRecordType.user_process and record.host != '': - users.append(Session(record.user, record.line, record.host, record.sec)) + if record.type == utmp.UTmpRecordType.user_process: + if record.host != '': + users.append(Session(record.user, record.line, record.host, record.sec)) + else: + users.append(Session(record.user, record.line, 'localhost', record.sec)) return users