User Tools

Site Tools


server_stats

This is an old revision of the document!


—Work in progress—

#!/bin/bash
#####################################
#####Colours######
ESC_SEQ="\x1b["
GREEN=$ESC_SEQ"32;01m"
RED=$ESC_SEQ"31;01m"
RESET=$ESC_SEQ"39;49;00m"
#####Underline/bold#####
BOLD=$ESC_SEQ"\033[1m"
bold=$(tput bold)
UNDERLINE=$ESC_SEQ"\033[4m"
#NONE=$ESC_SEQ"\033[00m"
#####################################
neat="############################################"
#####################################
#Load Average
# -o =
# -h =
# -e = pattern list. Specify one or more patters to be used during the search for input
#load2=$(w | grep -ohe 'load average[s:][: ].*' | awk '{ print $5 }');
loadnow=$(uptime | grep -ohe 'load average[s:][: ].*' | awk '{ print $3 }' | sed 's/,//');
load15=$(uptime | grep -ohe 'load average[s:][: ].*' | awk '{ print $5 }');

#Ram Usage
free=$(free -m | grep -ohe '-/+ buffers/cache[: ].*' | awk '{ print $4 }');
used=$(free -m | grep -ohe '-/+ buffers/cache[: ].*' | awk '{ print $3 }');
topramprocess=$(ps -Fe --sort:-rss --no-headers | head -1 | awk '{print $11}')
topramrss=$(ps -Fe --sort:-rss --no-headers | head -1 | awk '{print $6}')

#CPU Stuff
# old command topprocess=$(ps -eo user,pcpu,pid,cmd --no-headers | sort -r -k2 | head -1 | awk '{print $4}')
topprocess=$(ps -eo user,pcpu,pid,cmd --sort:-pcpu --no-headers | head -1 | awk '{print $4}')
#topcpu=$(ps -eo user,pcpu,pid,cmd --no-headers | sort -r -k2 | head -1 | awk '{print $2}')
topcpu=$(ps -eo user,pcpu,pid,cmd --sort:-pcpu --no-headers | head -1 | awk '{print $2}' | sed 's/[][]//g')
#sed to remove [ ]
# ps -eo user,pcpu,pid,cmd --no-headers | sort -r -k2 | head -1 | awk '{print $4}' | sed 's/[][]//g'
#######################################

echo $neat
#echo -e $bold "bold test" $RESET

#####Load Average#####
echo ""
#1 min load check
loadcheck=$(echo $loadnow '<' 0.80 | bc -l )
    if [ "$loadcheck" -ge 1 ]; then
        echo -e "Load Average: Current$GREEN "$loadnow$RESET
    elif [ "$loadnow" -le 0 ]; then
        echo -e "Load Average: Current$RED "$loadnow$RESET
    fi

#15 min load check
loadcheck15=$(echo $load15 '<' 0.80 | bc -l )
    if [ "$loadcheck15" -ge 1 ]; then
        echo -e "Load Average: 15min Average$GREEN "$loadnow$RESET
    elif [ "$loadcheck15" -le 0 ]; then
        echo -e "Load Average: 15min Average$RED "$loadnow$RESET
    fi

echo ""


#####RAM#####
echo -e "Free Ram: "$free"MB"
echo "Used RAM: "$used"MB"
#bc - the -l option will load the standard math library and default the scale to 20
#ramtest=$( printf "%.0f\n" $(bc -l <<< "100 - ($used / ( $free + $used) ) * 100 ") )
ramtest=$( printf "%.0f\n" $(bc -l <<< "100 - ($used / ( $free + $used) ) * 100 ") )
#ramtest=9
echo $ramtest"% ram left"
        if [ $ramtest -gt 20 ]; then
            echo -e "RAM Usage:$GREEN Acceptable$RESET"
        elif [ $ramtest -le 20 ] && [ $ramtest -gt 10 ] ; then
            echo -e "RAM ALERT:$RED Low!$RESET "
        elif [ $ramtest -le 10 ]; then
            echo -e "RAM WARNING:$RED CRITICAL STATE!!$RESET"
        fi
echo ""
echo "TOP RAM CONSUMER: ""$topramprocess"
echo "It's RAM Usage (RSS): "$topramrss" bytes"


#####CPU#####
echo ""
echo "---CPU usage--- "
echo "Top Process: " $topprocess
#old command with 'hack' to minus 1 threads=$(($(ps afx | grep -ic "$topprocess") -1 ))
threads=$( ps afx | grep -v grep | grep -ic "$topprocess" )

#command below can be used to exclude grep from ps results. You can then add up all the values for totals etc
#ps afx | grep /usr/sbin/httpd | grep -v grep | awk '{print $4}'

#threadstest=$(( $threads - 1 ))
echo "number of processes this is running:  $threads"

cpucheck=$(echo $topcpu '<' 0.50 | bc -l )
    if [ "$cpucheck" -ge 1 ]; then
        echo -e "cpu % for process = " "$GREEN$topcpu$RESET"
    elif [ "$cpucheck" -le 0 ]; then
        echo -e "cpu % for process = " "$RED$topcpu$RESET"
    fi

#add all of the processes up and then print them:
#ps aux | grep /usr/sbin/httpd | grep -v grep | awk '{total += $3}END{print total}'
#echo "cpu % for top process = " "$topcpu"
echo "cpu usage for all $thread processes ="
echo ""
#echo "$threads $threadstest"
echo $neat

#######################################

server_stats.1431268519.txt.gz · Last modified: 2024/05/23 07:26 (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki