Learn about Centmin Mod LEMP Stack today
Become a Member

Sysadmin Using psacct to find total ssh user log in time

Discussion in 'System Administration' started by eva2000, Sep 9, 2016.

  1. eva2000

    eva2000 Administrator Staff Member

    29,053
    6,594
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,790
    Local Time:
    4:59 PM
    Nginx 1.13.x
    MariaDB 5.5
    Was curious about sshd users total logged in time and while you can use the command last to find info for each, converting to a tallied total is a bit trickier as last command it differently.

    On virtualbox guest CentOS 7 instance using last command grep filter on root user only. Crash is just me powering down virtualbox guest instance without proper shutdown :)

    Code (Text):
    last -F | grep root
    root     pts/1        192.168.0.1    Thu Sep  8 18:28:23 2016   still logged in                   
    root     pts/0        192.168.0.1    Thu Sep  8 18:18:54 2016 - Thu Sep  8 18:28:38 2016  (00:09) 
    root     pts/0        192.168.0.1    Mon Sep  5 18:43:33 2016 - crash                    (2+20:33) 
    root     pts/0        192.168.0.1    Thu Aug 11 11:30:53 2016 - crash                    (25+07:11)
    root     pts/0        192.168.0.1    Tue Apr 12 09:39:32 2016 - down                      (00:02) 
    root     pts/0        192.168.0.1    Tue Apr 12 09:32:18 2016 - crash                     (00:07) 
    root     pts/0        192.168.0.1    Mon Dec 21 16:58:50 2015 - down                      (00:05) 
    root     tty1                          Tue Jul  8 03:32:38 2014 - Tue Jul  8 03:33:43 2014  (00:01) 


    So used psacct command instead.


    install psacct
    Code (Text):
    yum -y install psacct
    service psacct start
    chkconfig psacct on
    

    Get login times for root user via grep of the daily per user totals
    Code (Text):
     ac -pd | grep root
            root                                 0.02
            root                                 0.10
            root                                 0.04
            root                                12.49
            root                                 5.27
            root                                 2.88

    Totaling the root user login times from the 2nd column via awk = ~20.84 hrs worth
    Code (Text):
    ac -pd | awk '/root/ {t+=$2} END {print t}'
    20.98

    But seems ac command has an easier way just typing ac is enough :D
    Code (Text):
    ac root
            total       20.98
    

     
    Last edited: Sep 9, 2016
    • Informative Informative x 1