Get current logged in user in linux system

1,381 views
Skip to first unread message

dibya ranjan mishra

unread,
Aug 20, 2018, 3:11:13 AM8/20/18
to Prometheus Users
How to get current logged in user(s) in a linux system using prometheus node exporter

Ben Kochie

unread,
Aug 20, 2018, 4:35:34 AM8/20/18
to dibyami...@gmail.com, Prometheus Users
This sounds like a job for a textfile exporter. There are several examples[0] in the node_exporter repo.


On Mon, Aug 20, 2018 at 9:11 AM dibya ranjan mishra <dibyami...@gmail.com> wrote:
How to get current logged in user(s) in a linux system using prometheus node exporter

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To post to this group, send email to promethe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/90f9a1d5-c301-4915-bc6e-a89ba632f26c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dibya ranjan mishra

unread,
Aug 20, 2018, 5:02:20 AM8/20/18
to Prometheus Users
Ya I have done that
using the following command
 lastlog -b 0 -t 100 |awk '{if (NR!=1){$2=$3=""; print $0}}


I have setup a cron job and able to write into a .prom file
 file contains list of users and their login info
file content:
usr1   Mon Aug 20 12:00:09 +0530 2018
but when I am trying to run node exporter it is showing  
text format parsing error in line 1: expected float as value, got "Mon"  source="textfile.go:212"

Christian Hoffmann

unread,
Aug 20, 2018, 3:52:40 PM8/20/18
to dibya ranjan mishra, Prometheus Users
On 8/20/18 11:02 AM, dibya ranjan mishra wrote:
> I have setup a cron job and able to write into a .prom file
>  file contains list of users and their login info
> file content:
> usr1   Mon Aug 20 12:00:09 +0530 2018
This does not look like the Prometheus metrics format, which would be
required for the textfile collector to work.

You probably would want something like:
lastlog{user="usr1"} 1234567 [Unix timestamp of the given Date]

Further reading/examples:
https://github.com/prometheus/node_exporter/tree/master/text_collector_examples
https://www.robustperception.io/quick-sensor-metrics-with-the-textfile-collector
https://prometheus.io/docs/concepts/data_model/

You should probably also be wary of putting high-cardinality labels such
as usernames into your metrics. If this is about tens of users, it's
probably ok. If the number of users is potentially unlimited, you may
run into problems.

> but when I am trying to run node exporter it is showing  
> text format parsing error in line 1: expected float as value, got "Mon" 
> source="textfile.go:212"
If the above example is the actual content of the file, this is likely
the issue here. If not, have you tried comparing the cron-generated
.prom file with your expected output?

Running the script via cron may influence environment variables. If I
were to guess, your parsing might rely on a non-English local while the
cronjob runs with LC_ALL=C and as such English, which would break the
awk-based parsing.

Kind regards,
Christian

dibya ranjan mishra

unread,
Aug 20, 2018, 10:29:42 PM8/20/18
to Prometheus Users
Thank you so much for replying
No, The users won't be much maximum 10-15.
Currently I am parsing using awk and the format that I am storing the data is

users{usr1:"time as string", usr2:"time as string"} 1

When I query users in prometheus server, it is returning the above string.
Is there any better way of doing this?

Nicholas Capo

unread,
Aug 21, 2018, 10:50:36 AM8/21/18
to dibya ranjan mishra, Prometheus Users
You need `=` instead of `:`, try this format:

    users{usr1="time as string", usr2="time as string"} 1

Nicholas

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To post to this group, send email to promethe...@googlegroups.com.

dibya ranjan mishra

unread,
Aug 24, 2018, 11:13:26 AM8/24/18
to Prometheus Users
Hi Nicholas,
How to show this list of users in grafana dashboard ?
Reply all
Reply to author
Forward
0 new messages