This is everyday fare for Nagios plug-ins. If you are using something else, you will have to work out what behaviour the monitoring system requires of your plug-in. Nagios has a documented interface for what is written to standard output and what the plug-in's exit status should be.
One place to start with developing a Nagios plug-in is where people have already done some of the job. See, for example, Hari Sekhon's Nagios plug-in collection, where there is a check_users.sh
script in the "older" section that looks at the active logins table but does not impose the rules that you want imposed. It's possible that someone else has developed something to do what you want already. There are numerous variants on and augmentations to the Nagios-supplied check_users
, such as this, written by people over the years. Some of them are in Nagios Exchange.
You can see M. Sekhon's script constructing a $userlist
string, by parsing the output of the who
command. It's actually just one line. What you want is some more complex processing of the output of the who
command than that, which you will have to write.
… Or a program in a programming language that can actually directly read the login database, rather than the somewhat rickety mechanism of parsing the human-readable (not intended as machine-parseable) output of the who
/w
command, and do the fairly trivial output generation and exit status calculation. Nagios plug-ins do not have to be written in shell script, and parsing the human-readable outputs of tools back into a machine-processable form is a notorious source of bugs on Unices and Linux operating systems. Combine that with the fact that you are going to be involving another notorious source of bugs, date/time arithmetic using human-readable forms, and the result is that shell script is going to be very fragile here.
Further reading
last
is the wrong command. The questioner wants to look at the active login sessions, not the log of login events. http://jdebp.uk./FGA/unix-login-database.html – JdeBP Jan 30 '20 at 12:05last
has the advantage of immediately giving the (recent) session duration. – RudiC Jan 30 '20 at 12:13