User Tools

Site Tools


server_stats

This is an old revision of the document!


---Work in progress---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/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######
ESC_SEQ="\x1b["
GREEN=$ESC_SEQ"32;01m"
RED=$ESC_SEQ"31;01m"
RESET=$ESC_SEQ"39;49;00m"
BLUE=$ESC_SEQ"34;01m"
#####Underline/bold#####
BOLD=$ESC_SEQ"\033[1m"
bold=$(tput bold)
UNDERLINE=$ESC_SEQ"\033[4m"
#####################################
neat="############################################"
#####################################
#Load Average
loadnow=$( cut /proc/loadavg -f1 -d\ ) #this gets the 1 min load average from /proc/loadavg
load15=$( cut /proc/loadavg -f3 -d\ ) #gets the 15 min load average from /proc/loadavg
 
#Ram Usage
totalc=$( grep MemTotal /proc/meminfo | awk '{print $2 / 1024 }') #gets the total available ram from /proc/meminfo
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}')
topramrss=$(ps -Fe --sort:-rss --no-headers | head -1 | awk '{print $6}')
 
#CPU Stuff
topprocess=$(ps -eo user,pcpu,pid,cmd --sort:-pcpu --no-headers | head -1 | awk '{print $4}' | sed 's/[][]//g' )
topcpu=$(ps -eo user,pcpu,pid,cmd --sort:-pcpu --no-headers | head -1 | awk '{print $2}')
 
serveruptime=$(awk '{print int($1/86400)"days "int($1%86400/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime)
#######################################
 
echo $neat
 
#####Load Average#####
echo ""
echo "Server Uptime: "$serveruptime
echo ""
#1 min load check
loadcheck=$(echo - | awk -v lnow=$loadnow '{print ( lnow < 0.80 )}')
    if [ "$loadcheck" -ge 1 ]; then
       echo -e "Load Average: Current$GREEN "$loadnow$RESET
    elif [ "$loadcheck" -le 0 ]; then
       echo -e "Load Average: Current$RED "$loadnow$RESET
    fi
 
#15 min load check
loadcheck15test=$(echo - | awk -v l15=$load15 '{print ( l15 < 0.80 )}')
    if [ "$loadcheck15test" -ge 1 ]; then
    echo -e "Load Average: 15min Average$GREEN "$loadnow$RESET
    elif [ "$loadcheck15test" -le 0 ]; then
        echo -e "Load Average: 15min Average$RED "$loadnow$RESET
    fi
 
#####RAM#####
echo ""
echo ""
echo "###RAM usage###"
 
echo -e "Free Ram: "$free"MB"
echo "Used RAM: "$used"MB"
ramtest=$( printf "%.0f\n" $(echo - | awk -v u=$used -v f=$free '{ print 100 - ( u / ( f + u ) ) * 100 }' ))
 
 
echo ""
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 "" 
 
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#####
echo ""
echo ""
echo "###CPU usage###"
echo -e "Top Process: " $BLUE$topprocess$RESET
 
cpuchecktest=$( echo - | awk -v topcpu=$topcpu '{print ( topcpu < 50 )}' )
    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"
 
#add all of the processes up and then print the total:
totalcpu=$( ps aux | grep $topprocess | grep -v grep | awk '{total += $3}END{print total}' )
 
 
totalcpuchecktest=$( echo - | awk -v totalcpu=$totalcpu '{print ( totalcpu < 70 )}')
    if [ "$totalcpuchecktest" -ge 1 ]; then
        echo -e "Total CPU % for $BLUE$topprocess$RESET = " "$GREEN$totalcpu$RESET"
    elif [ "$totalcpuchecktest" -le 0 ]; then
        echo -e "Total CPU % for $BLUE$topprocess$RESET = " "$RED$totalcpu$RESET"
    fi
 
 
echo ""
echo $neat
#######################################

server_stats.1431453993.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