DEV Community

decentralizuj
decentralizuj

Posted on • Updated on

Get server stats with bash

This is script to get server stats printed in terminal and saved as txt output (or not). I will not explain each command, but for total beginners I will say that commands are piped with I, so take each command and use with -h in terminal to get more info.

# example to get help for awk
awk -h

# commands are piped with |
# get free memory with 'free'
# output it to 'tail' to print last line
free -m | tail -n 1 
Enter fullscreen mode Exit fullscreen mode

So here's the full script, you can remove # at last 2 lines to save output and/or show notification on desktop.

#!/usr/bin/env bash

#  Define Output Log-File (or not)

OUTPUT_LOG=~/sys-log.txt

#  Define Colors

  R="\e[0m" #reset
  U="\e[4m" #underline
  Y="\e[33m" #yellow
  RED="\e[37m" #light red

#  Format Date and Time

DATE=$(date +%m/%d/%Y)
TIME=$(date +%k:%M:%S)
DAY=$(date +%A)

#  User Logged In

USER=$(whoami)

#  System Uptime

UPTIME_S=$(uptime -s)
UPTIME_P=$(uptime -p)
UPTIME=$(uptime -s && uptime -p)

#  Free Memory

FREE=$(free -m | head -n 2 | tail -n 1 | awk {'print $4'})

#  Free Swap Memory

SWAP=$(free -m | tail -n 1 | awk {'print $3'})

#  CPU Idle

CPU=$(vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | awk '{print $15}')

#  Free Disk Space

SPACE=$(df -h / | awk '{ a = $4 } END { print a }')

#  Define banner to print

  banner() {

    echo ----------------------------------------------------------------------
    echo -e "$RED" DAY: "$R"$DAY "$RED"'\t'DATE: "$R"$DATE "$RED"'\t'TIME: "$R"$TIME"$R"
    echo ----------------------------------------------------------------------
}

#  Define nice-print stats

  print_stats() {

    echo -e "$R"WELCOME:"$R"'\t|\t'"$Y"$USER

    echo -e "$R"UPTIME: "$R"'\t|\t'"$Y"$UPTIME

    echo -e "$R"FREE MEMORY:"$R"'\t|\t'"$Y"$FREE MB

    echo -e "$R"FREE SWAP:"$R"'\t|\t'"$Y"$SWAP MB

    echo -e "$R"CPU IDLE:"$R"'\t|\t'"$Y"$CPU %

    echo -e "$R"DISK SPACE:"$R"'\t|\t'"$Y"$SPACE
}

#  Define One-Line output log

  output() {

    echo "$DATE | $TIME | $USER | $UPTIME_S | $UPTIME_P | $FREE MB | $SWAP MB | $CPU % | $SPACE"
}

### ### ###

#  Execute Defined Functions

  clear && banner           # Print Banner

  print_stats               # Print Stats

#  output >> $OUTPUT_LOG     # Save Output (or not)

#  Display Desktop Notification

#  ALERT=$(notify-send -u normal "Free Space :  $SPACE   |   CPU USAGE :  $CPU %  |   Free Mem :  $FREE MB") && echo -e $ALERT
Enter fullscreen mode Exit fullscreen mode

SysData Preview

Top comments (2)

Collapse
 
goodevilgenius profile image
Dan Jones

You've got a few possible improvements in here.

Here's what I found.

free -m | head -n 2 | tail -n 1 | awk {'print $4'}

# could be, like you use in CPU idle
free -m | sed -n 2p | awk '{print $4}'

# but more simply
free -m | awk 'NR == 2 {print $4}'
Enter fullscreen mode Exit fullscreen mode
free -m | tail -n 1 | awk {'print $3'}

# like you do with free space
free -m | awk 'END {print $3}'
Enter fullscreen mode Exit fullscreen mode
df -h / | awk '{ a = $4 } END { print a }'

# You don't need to store a variable here
df -h / | awk 'END { print $4 }'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
decentralizuj profile image
decentralizuj

Thanks on pointing that out, I'll edit script with your improvements.