User Tools

Site Tools


server_stats

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
server_stats [2015/05/10 14:35] luke7858server_stats [2024/05/23 07:26] (current) – external edit 127.0.0.1
Line 3: Line 3:
 <sxh bash> <sxh bash>
 #!/bin/bash #!/bin/bash
 +#####################################
 +##Release:
 +##Version 1.3
 +##Author: Luke Shirnia
 +####################################
 +#version 1.3 release notes:
 +#added total CPU usage for top process and all its threads
 +#added server uptime from /proc/uptime 
 +#load average now pulled from /proc/loadavg rather than 'w' (for compatibility)
 +#replaced 'free -m' with values from /proc/meminfo (for compatibility)
 +#####################################
 +#Version 1.2 Update Notes:
 +#Ram usage now in MB
 +#removed 'bc' commands and replaced with awk for compatibility reasons
 ##################################### #####################################
 #####Colours###### #####Colours######
Line 9: Line 23:
 RED=$ESC_SEQ"31;01m" RED=$ESC_SEQ"31;01m"
 RESET=$ESC_SEQ"39;49;00m" RESET=$ESC_SEQ"39;49;00m"
 +BLUE=$ESC_SEQ"34;01m"
 #####Underline/bold##### #####Underline/bold#####
 BOLD=$ESC_SEQ"\033[1m" BOLD=$ESC_SEQ"\033[1m"
 bold=$(tput bold) bold=$(tput bold)
 UNDERLINE=$ESC_SEQ"\033[4m" UNDERLINE=$ESC_SEQ"\033[4m"
-#NONE=$ESC_SEQ"\033[00m" 
 ##################################### #####################################
 neat="############################################" neat="############################################"
 ##################################### #####################################
 #Load Average #Load Average
-# -o = +loadnow=$( cut /proc/loadavg -f1 -d\ #this gets the 1 min load average from /proc/loadavg 
-# -h = +load15=$( cut /proc/loadavg -f3 -d\ ) #gets the 15 min load average from /proc/loadavg
-# -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 #Ram Usage
-free=$(free -m | grep -ohe '-/+ buffers/cache[: ].*' | awk '{ print $}'); +totalc=$( grep MemTotal /proc/meminfo | awk '{print $2 / 1024 }'#gets the total available ram from /proc/meminfo 
-used=$(free -m | grep -ohe '-/buffers/cache[: ].*' | awk '{ print $3 }');+usedc=$( ps -Fe --sort:-rss --no-headers | awk '{totalram += $6}END{print totalram 1024}
 +free=$( printf "%.2f\n" $( echo - | awk -v t=$totalc -v u=$usedc '{print t - u }')
 +total=$( printf "%0.2f\n" $totalc ) 
 +used=$( printf "%0.2f\n" $usedc ) 
 topramprocess=$(ps -Fe --sort:-rss --no-headers | head -1 | awk '{print $11}') topramprocess=$(ps -Fe --sort:-rss --no-headers | head -1 | awk '{print $11}')
 topramrss=$(ps -Fe --sort:-rss --no-headers | head -1 | awk '{print $6}') topramrss=$(ps -Fe --sort:-rss --no-headers | head -1 | awk '{print $6}')
  
 #CPU Stuff #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}' | sed 's/[][]//g' ) 
-topprocess=$(ps -eo user,pcpu,pid,cmd --sort:-pcpu --no-headers | head -1 | awk '{print $4}'+topcpu=$(ps -eo user,pcpu,pid,cmd --sort:-pcpu --no-headers | head -1 | awk '{print $2}'
-#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') +serveruptime=$(awk '{print int($1/86400)"days "int($1%86400/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime)
-#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 $neat
-#echo -e $bold "bold test" $RESET 
  
 #####Load Average##### #####Load Average#####
 +echo ""
 +echo "Server Uptime: "$serveruptime
 echo "" echo ""
 #1 min load check #1 min load check
-loadcheck=$(echo $loadnow '<0.80 | bc -l )+loadcheck=$(echo - | awk -v lnow=$loadnow '{print ( lnow < 0.80 )}')
     if [ "$loadcheck" -ge 1 ]; then     if [ "$loadcheck" -ge 1 ]; then
-        echo -e "Load Average: Current$GREEN "$loadnow$RESET +       echo -e "Load Average: Current$GREEN "$loadnow$RESET 
-    elif [ "$loadnow" -le 0 ]; then +    elif [ "$loadcheck" -le 0 ]; then 
-        echo -e "Load Average: Current$RED "$loadnow$RESET+       echo -e "Load Average: Current$RED "$loadnow$RESET
     fi     fi
  
 #15 min load check #15 min load check
-loadcheck15=$(echo $load15 '<0.80 | bc -l +loadcheck15test=$(echo - | awk -v l15=$load15 '{print ( l15 < 0.80 )}'
-    if [ "$loadcheck15" -ge 1 ]; then +    if [ "$loadcheck15test" -ge 1 ]; then 
-        echo -e "Load Average: 15min Average$GREEN "$loadnow$RESET +    echo -e "Load Average: 15min Average$GREEN "$loadnow$RESET 
-    elif [ "$loadcheck15" -le 0 ]; then+    elif [ "$loadcheck15test" -le 0 ]; then
         echo -e "Load Average: 15min Average$RED "$loadnow$RESET         echo -e "Load Average: 15min Average$RED "$loadnow$RESET
     fi     fi
  
 +#####RAM#####
 echo "" echo ""
 +echo ""
 +echo "###RAM usage###"
  
- +echo -e "Free Ram: "$free"MB" 
-#####RAM##### +
-echo -e "Free Ram: "$free"MB"+
 echo "Used RAM: "$used"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" $(echo | awk -v u=$used -v f=$free '{ print 100 - ( / ( ) ) * 100 }' )) 
-#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 ""
 echo $ramtest"% ram left" echo $ramtest"% ram left"
         if [ $ramtest -gt 20 ]; then         if [ $ramtest -gt 20 ]; then
Line 79: Line 93:
             echo -e "RAM WARNING:$RED CRITICAL STATE!!$RESET"             echo -e "RAM WARNING:$RED CRITICAL STATE!!$RESET"
         fi         fi
-echo "" +echo ""  
-echo "TOP RAM CONSUMER: ""$topramprocess" +
-echo "It's RAM Usage (RSS): "$topramrss" bytes"+
  
 +topramMB=$( printf "%.2f\n" $(echo - | awk -v tp=$topramrss '{print tp / 1024}'))
 +ramtotalprocesses=$(ps -Fe --sort:-rss | grep -v grep | grep -ic $topramprocess)
 +
 +echo -e "TOP RAM CONSUMER: ""$BLUE$topramprocess$RESET"
 +echo "RAM Usage (RSS): $topramMB MB"
 +echo "Total Number of RAM Processes: $ramtotalprocesses"
 +
 +
 +totalRAMall=$( printf "%.2f" $(ps -Fe --sort:-rss --no-headers | grep -v grep | grep $topramrss | awk '{total += $6}END{print total / 1024 }'))
 +rampercentage=$( printf "%.2f\n" $( echo - | awk -v t=$total -v tra=$totalRAMall '{ print tra / t }'))
 +
 +
 +#command below checks total ram usage of top RAM consumer and ALL of its threads and compares to total RAM avaiable. If it is higher than the threshold of 70% then a warning is produced.
 +totalramcheck=$(echo - | awk -v ramp=$rampercentage '{print ( ramp < 70 )}')
 +if [ "$totalramcheck" -ge 1 ]; then
 +    echo -e "Total RAM Usage for all $BLUE$topramprocess$RESET processes = " $GREEN$totalRAMall" MB"$RESET
 +    echo -e $GREEN$rampercentage$RESET"% used by "$topramprocess
 +elif [ "$totalramcheck" -le 0 ]; then
 +    echo -e "Total RAM usage for all $BLUE$topramprocess$RESET processes = " $RED$totalRAMall" MB"$RESET
 +    echo -e $RED$rampercentage$RESET"% used by "$topramprocess
 +fi
  
 #####CPU##### #####CPU#####
 echo "" echo ""
-echo "---CPU usage--- +echo "
-echo "Top Process: " $topprocess +echo "###CPU usage###
-#old command with 'hack' to minus 1 threads=$(($(ps afx | grep -ic "$topprocess") -1 )) +echo -e "Top Process: " $BLUE$topprocess$RESET
-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 +cpuchecktest=$( echo - | awk -v topcpu=$topcpu '{print ( topcpu < 50 )}' ) 
-#ps afx | grep /usr/sbin/httpd | grep -v grep | awk '{print $4}'+    if [ "$cpuchecktest" -ge 1 ]; then 
 +        echo -e "CPU % for SINGLE Top Process = " "$GREEN$topcpu$RESET" 
 +    elif [ "$cpuchecktest" -le 0 ]; then 
 +        echo -e "CPU % for SINGLE Top Process = " "$RED$topcpu$RESET" 
 +    fi 
 +     
 +threads=$( ps afx | grep -v grep | grep -ci $topprocess ) 
 +echo -e "number of processes this is running: $UNDERLINE$threads$RESET"
  
-#threadstest=$($threads )+#add all of the processes up and then print the total: 
-echo "number of processes this is running:  $threads"+totalcpu=$( ps aux | grep $topprocess | grep -v grep | awk '{total += $3}END{print total}' )
  
-cpucheck=$(echo $topcpu '<' 0.50 | bc -l + 
-    if [ "$cpucheck" -ge 1 ]; then +totalcpuchecktest=$( echo - | awk -v totalcpu=$totalcpu '{print ( totalcpu 70 )}') 
-        echo -e "cpu % for process = " "$GREEN$topcpu$RESET" +    if [ "$totalcpuchecktest" -ge 1 ]; then 
-    elif [ "$cpucheck" -le 0 ]; then +        echo -e "Total CPU % for $BLUE$topprocess$RESET = " "$GREEN$totalcpu$RESET" 
-        echo -e "cpu % for process = " "$RED$topcpu$RESET"+    elif [ "$totalcpuchecktest" -le 0 ]; then 
 +        echo -e "Total CPU % for $BLUE$topprocess$RESET = " "$RED$totalcpu$RESET"
     fi     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 ""
-#echo "$threads $threadstest" 
 echo $neat echo $neat
- 
 ####################################### #######################################
- 
 </sxh> </sxh>
server_stats.1431268526.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