stfsy / prometheus-what-active-users-exporter

Prometheus exporter that scrapes currently logged in users on unix systems
MIT License
33 stars 4 forks source link

Exported metrics do no include IP #48

Closed danielmez closed 1 year ago

danielmez commented 2 years ago

Hi. I tried building the latest version of the code and running it. I can not see the IP for the user in the exported metric label although I saw a reference to it in the code.

 13:08:23 up 181 days, 18:16,  2 users,  load average: 0.48, 0.28, 0.18
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
dme      pts/0    192.168.1.109    12:31   22.00s  0.50s  0.02s sshd: dme [priv]

dme@dme-dev:~$ curl -v 127.0.0.1:9839/metrics

# HELP what_up Health and status of the exporter
# TYPE what_up gauge
what_up{version="0.6.0"} 1
# HELP what_user_sessions_currently_active Currently logged in users
# TYPE what_user_sessions_currently_active gauge
what_user_sessions_currently_active{user="dme"} 1
stfsy commented 2 years ago

hey @danielmez thanks for reaching out! 👋

I'm currently at the gym. Will get back to you Iater today.

stfsy commented 2 years ago

hi @danielmez I can confirm the IP was never exposed.

The exporter does collect the IP here https://github.com/stfsy/prometheus-what-active-users-exporter/blob/master/lib/index.js#L83. However the actual value of Gauge (metric) what_user_sessions_currently_active is always only the sum of sessions per user. not each individual session: https://github.com/stfsy/prometheus-what-active-users-exporter/blob/master/lib/index.js#L59.

It's tricky because at some point we want the metric to be reset, but we still want the exporter to collect each login. And we also want to give Grafana a chance to see the metric and trigger an alarm. So the exporter stores each user session for a configurable amount of time https://github.com/stfsy/prometheus-what-active-users-exporter/blob/master/lib/index.js#L24. To track each session the exporter creates a hash value of each session and only these hash values are considered for the metric. https://github.com/stfsy/prometheus-what-active-users-exporter/blob/master/lib/index.js#L98. So by the time Prometheus would collect the metrics from the exporter, the exporter is not aware of the IPs anymore.

What do you think @danielmez? Is it important enough for you to create a PR? Or for us two to work on the topic?

danielmez commented 2 years ago

Hi,

Thanks for the quick response. The current format is useful for displaying on a dashboard but not so useful for creating a potential alert. I assume you check each hash an remove it from the exported metrics after the configured retention period. I'd like to get a little more info into prometheus and I think it's just a matter of reorganizing the data you already collect. I'd leave the session counting to be computed in prometheus with a sum or count by the username and IP address exported as labels and instead of the value I'd put the value of Login@ in unix time or something Grafana can understand.

For example: what_user_sessions_currently_active{user="dme",ip="1.2.3.4",tty="pts/0"} unixtime(12:31) what_user_sessions_currently_active{user="dme",ip="1.2.3.4",tty="pts/1"} unixtime(12:35) what_user_sessions_currently_active{user="dme",ip="5.6.7.8",tty="pts/3"} unixtime(12:37) what_user_sessions_currently_active{user="abc",ip="1.1.1.1",tty="pts/2"} unixtime(12:36)

In this format I could quickly get the number of sessions for a user by matching the name, or more in depth by matching name and IP. I'm not really familiar with nodjs, if you can point me to the bit of code that adds the label value and timeseries value I can try to alter it.

stfsy commented 2 years ago

@danielmez

The code that collects the values for the labels and the metric's value is here: https://github.com/stfsy/prometheus-what-active-users-exporter/blob/master/lib/index.js#L57-L61.

The definition of the labels is here: https://github.com/stfsy/prometheus-what-active-users-exporter/blob/master/lib/index.js#L52

We'd also need a way to distinguish currently tracked and obsolete/old sessions. Currently this is done via hashing parts of the output of the w command: https://github.com/stfsy/prometheus-what-active-users-exporter/blob/master/lib/index.js#L98

stfsy commented 1 year ago

@danielmez it has been a while. I published a new release a few minutes ago. It includes a new metric which has the IP a as label 😊

what_each_session_currently_active{user="pip3",ip="192.168.2.107",tty="pts/0"} 1

Would this serve your use case?