-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-stats.sh
More file actions
49 lines (41 loc) · 1.37 KB
/
server-stats.sh
File metadata and controls
49 lines (41 loc) · 1.37 KB
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
#!/bin/bash
#Show server stats, using bash
#
#script: github.com/tahatsahin/server-stats
while :; do
# ============ CPU STATS ===========
# Get the first line of /proc/stats
cpu_now=($(head -n1 /proc/stat))
# Get all columns, skip first
cpu_sum="${cpu_now[@]:1}"
# Replace the spaces with + => x=${cpu_sum// /+} sum=$((x))
cpu_sum=$((${cpu_sum// /+}))
# Get te delta
cpu_delta=$((cpu_sum - cpu_last_sum))
# Get idle time delta
cpu_idle=$((cpu_now[4] - cpu_last[4]))
# Time spent working
cpu_used=$((cpu_delta - cpu_idle))
# percentage
cpu_usage=$((100 * cpu_used / cpu_delta))
cpu_last=("${cpu_now[@]}")
cpu_last_sum=$cpu_sum
# ============ MEMORY STATS ===========
# get current memory usages via ps ax, filter for memory column
# use awk to sum all usages
mem_sum=$(ps ax -o %mem= | awk '{s+=$1} END {print s}')
# ============ DISK USAGE ===========
# get total disk usage using df
total_info=($(df -hl --total | tail -n 1))
clear
echo "Memory usage at $mem_sum%"
echo "CPU usage at $cpu_usage%"
echo "Total disk space: ${total_info[1]}, Used Disk: ${total_info[2]}, Available Space: ${total_info[3]}"
echo "=============================================="
echo "Top 5 CPU Using Processes"
ps aux --sort -pcpu | head -n 6
echo "=============================================="
echo "Top 5 Memory Using Processes"
ps aux --sort -pmem | head -n 6
sleep 1
done