Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Correctly grep and display the uptime, load average and amount of users?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Correctly grep and display the uptime, load average and amount of users?

RaymiiRaymii Member
edited September 2012 in General

Because of the new version of my status monitoring script I've been busy rewriting the client script a little bit. See here

Now I find it quite hard to reliably parse the output of the uptime command with only bash/sed/awk etc. I have a solution which works on my tests (iMac, Debian 6, RHEL 5). Maybe peoples can test it to see if they also get the correct output?

https://raymii.org/cms/p_Get_uptime_load_and_users_with_grep_sed_and_awk

Thanked by 2djvdorp bnmkl

Comments

  • bnmklbnmkl Member
    edited September 2012

    @Raymii:

    I prefer what you are doing (parsing uptime), but if you are having problems making it "cross-distro", you can try to get the info directly. I wrote these line on my machine only, but in theory they should work on all.

    USERS: ( 2 users )
    echo "$(who | cut -d ' ' -f1 | sort | uniq | wc -l) users"
    
    UPTIME: ( 5 days, 19:12:41 )
    upsecs=$(cat /proc/uptime | cut -d ' ' -f1); echo "$((`echo $upsecs | cut -d '.' -f1` (60*60*24))) days, $(date -d "`echo @$upsecs`" +%H:%M:%S)"
    
    LOAD AVERAGE: ( 0.03 0.02 0.00 )
    for avload in $(cat /proc/loadavg); do if [[ $avload =~ ^.*\\..*$ ]]; then echo -n  "$avload "; fi done
    

    You should be able to make my lines better with your awk skills.

    Thanked by 1djvdorp
  • corehostingcorehosting Member
    edited September 2012

    Only with awk ones tested on Linux and MAC
    uptime: uptime | awk '{sub(/\,/,"");print $3" "$4 }'
    users: uptime | awk '{gsub(/\,/,"");print $6" "$7 }'
    load: uptime | awk '{gsub(/\,/,"");print $10 }'

    Awk by default doesn't care too much of the number of spaces separating entries

  • @bnmkl the /proc filesystem is not available on every system I'm using.

    @corehosting the amount of variables differs if a host is up less then a day.

  • @Raymii said: @corehosting the amount of variables differs if a host is up less then a day.

    True... Don't have anything with less than a day to test that out :D

  • @corehosting said: True... Don't have anything with less than a day to test that out :D

    Reboot like a sir! Also that is mostly fixed with an extra [if...fi]...

  • @corehosting said: True... Don't have anything with less than a day to test that out :D

    root@sg:~# uptime | awk '{sub(/\,/,"");print $3" "$4 }'
    2:20 1

  • @William what do the commands I use give?:

    uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $2" "$3 }'
    
    uptime | grep -ohe '[0-9.*] user[s,]'
    
    uptime | grep -ohe 'load average[s:][: ].*' | awk '{ print $3 }'
    

    And, what OS?

  • [root@curse ~]# uptime | awk '{sub(/\,/,"");print $3" "$4 }'
    1:06 1
    [root@curse ~]# uptime | grep -ohe 'up .' | sed 's/,//g' | awk '{ print $2" "$3 }'
    1:06 1
    [root@curse ~]# uptime | grep -ohe '[0-9.
    ] user[s,]'
    1 user,
    [root@curse ~]# uptime | grep -ohe 'load average[s:][: ].*' | awk '{ print $3 }'
    0.00,

    EL6.3

  • @Corehosting thank you. CentOS/SL or real RHEL? My test on RHEL 6.3 gives me funky output after a reboot. Hope it is different tomorrow.

  • corehostingcorehosting Member
    edited September 2012

    @Raymii: SL to be precise, but the result should be the same
    Which version of procps ? So that we can compare.

  • @Raymii... "The /proc filesystem is not available on every system I'm using."

    Does that mean you are using some systems that have an alternate uptime command @Raymii? If so, then you must already have to test which system you are on to determine which command to use. Thus, I would still use /proc to cover "cross-distro" for the ones that do as a function, then however many (least) other functions to cover the "system-sphere".

    The last commands you gave did not work correctly. My uptime format is:

    uptime
     23:06:54 up 6 days,  2:34, load average: 0.00, 0.00, 0.00
    

    It does seem proven from your own words really that it can not be achieved by just passing uptime as it changes too much. Though I like your determination to keep trying! It is nice to have precise code, and sometimes, the code is more admirable than the application. :)

Sign In or Register to comment.