-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtop
executable file
·176 lines (130 loc) · 5.95 KB
/
mtop
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
export LC_ALL=C.UTF-8
usage()
{
clear
printf '\n\033[3mNAME\033[m \n\t'
printf 'mtop - minimalist process monitor for Unix systems \n'
printf '\n\033[3mSYNOPSIS\033[m\n\t'
printf 'mtop [\033[4mOPTION\033[m] \n'
printf '\n\033[3mDESCRIPTION\033[m\n'
printf 'mtop provides a minimalistic dynamic real-time view of a running system. It can display several modes, all of them intended to have a clean view of the system processes. \n\n'
printf '\n\033[3mCOMMAND-LINE Options\033[m\n'
printf 'The command-line syntax for mtop consists of:\n'
printf '\t-i, --interval \n\t\tseconds between command outputs \n'
printf '\t-p, --pattern \n\t\tpattern to filter the processes in monitor mode \n'
printf '\t-l, --lines \n\t\tlines to display in monitor mode \n'
printf '\t-m, --mode \n\t\tselect in which mode to run \n'
printf '\t--help \n\t\tdisplay this help and exit \n'
printf '\n\033[3mMODES\033[m\n'
printf 'different running modes are supported when using the --m (--mode) command line option:\n\t'
printf 'monitor - shows CPU, Memory and processes running in the system \n\t'
printf 'min \t - minimalistic mode shows only CPU and Memory \n\t'
printf 'agg \t - aggregate CPU and Memory by user in the system \n'
printf '\n\033[3mOPERATION\033[m\n'
printf 'while mtop is running in monitor or aggregated mode, there are several keys that can be used to modify the program behaviour: \n\t'
printf '\033[4mkey \t description\033[m\n\t'
printf 'c \t order processes by CPU usage \n\t'
printf 'm \t order processes by Memory consumption \n\t'
printf 'q \t quit'
printf '\n\n\n'
}
trap "printf '\033[u' ; clear ; stty echo ; exit " INT
##### MAIN
GREPFILTER='all'
NUMBERLINES=10
REFRESHTIME=3
MODE='monitor'
ORDERVAL=2
printf '\033[s'
while [ '$1' != '' ]
do
case $1 in
-i | --interval ) shift
REFRESHTIME=$1
;;
-p | --pattern ) shift
GREPFILTER=$1
;;
-l | --lines ) shift
NUMBERLINES=$1
;;
-m | --mode ) shift
MODE=$1
REFRESHTIME=2
;;
--help ) usage
exit
;;
* ) break
;;
esac
done
clear
while true
do
stty -echo
if [ "$MODE" = 'monitor' ]; then
printf '%s \t\t\t %s \n\n' "$(date)" "$(id -nu)"
if [ "$ORDERVAL" = 2 ]; then
top_out=$(top -bn 1 -o %CPU)
fi
if [ "$ORDERVAL" = 3 ]; then
top_out=$(top -bn 1 -o %MEM)
fi
free_out=$(free -h)
echo "$top_out" | awk 'NR>7 { sum += $9; } END {printf("%% CPU usage = \t %7.2f \033[K \n", sum); }'
echo "$free_out" | grep -w -F 'Mem: ' | awk '{ printf("%% MEM free = \t %7.2f \033[K \n", (100*$4/$2) ); }'
printf '\n'
if [ "$GREPFILTER" = 'all' ]
then
echo "$top_out" | grep '^[0-9 ]' | awk '{ printf("\033[4m%10s %8s %2s %7s %7s %15s \033[m \n", $1, $2, $8, $9, $10, $12); }' | head -n 1
echo "$top_out" | grep '^[0-9 ]' | awk '{ printf("%10s %8s %2s %7.2f %7.2f %15s \033[K \n", $1, $2, $8, $9, $10, $12); }' | head -n $((++NUMBERLINES)) | tail -n $NUMBERLINES
fi
if [ "$GREPFILTER" != 'all' ]
then
echo "$top_out" | grep '^[0-9 ]' | awk '{ printf("\033[4m%10s %8s %2s %7s %7s %15s \033[m \n", $1, $2, $8, $9, $10, $12); }' | head -n 1
echo "$top_out" | grep $GREPFILTER | awk '{ printf("%10s %8s %2s %7.2f %7.2f %15s \033[K \n", $1, $2, $8, $9, $10, $12); }' | head -n $((++NUMBERLINES)) | tail -n $NUMBERLINES
fi
printf '\n'
echo "" | awk '{ printf("%10s %8s %8s %8s \033[K \n", "", "TOTAL", "USED", "FREE"); }'
echo "$free_out" | grep -w -F 'Mem: ' | awk '{ printf("%10s %8s %8s %8s \033[K \n", $1, $2, $3, $4); }'
echo "$free_out" | grep -w -F 'Swap: ' | awk '{ printf("%10s %8s %8s %8s \033[K \n", $1, $2, $3, $4); }'
fi
if [ "$MODE" = 'min' ]; then
printf '%s \t\t\t %s \n\n' "$(date)" "$(id -nu)"
top_out=$(top -bn 1)
free_out=$(free -h)
echo "$top_out" | awk 'NR>7 { sum += $9; } END {printf("%% CPU usage = \t %7.2f \033[K \n", sum); }'
echo "$free_out" | grep -w -F 'Mem: ' | awk '{ printf("%% MEM free = \t %7.2f \033[K \n", (100*$4/$2) ); }'
printf '\n'
fi
if [ "$MODE" = 'agg' ]; then
printf '%s \t\t\t %s \n\n' "$(date)" "$(id -nu)"
top_out=$(top -bn 1 -o %CPU)
free_out=$(free -h)
echo "$top_out" | awk 'NR>7 { sum += $9; } END {printf("%% CPU usage = \t %8.2f \033[K \n", sum); }'
echo "$free_out" | grep -w -F 'Mem: ' | awk '{ printf("%% MEM free = \t %8.2f \033[K \n", (100*$4/$2) ); }'
printf '\n\n\033[4m%8s\t %7s\t %7s\033[m\n' 'USER' '%%CPU' '%%MEM'
own=$(id -nu)
cpus=$(lscpu | grep "^CPU(s):" | awk '{print $2}')
user_list=$(echo "$top_out" | grep '^[0-9]' | awk '{printf("%8s\n", $2);}' | sort -u )
user_output=''
for user in $user_list
do
# print other user's CPU usage in parallel but skip own one because
# spawning many processes will increase our CPU usage significantly
user_output+=$(echo "$top_out" | grep $user | awk -v user=$user '{ sum_cpu += $9; sum_mem += $10 } END { printf("%8s %7.2f %7.2f ,", user, sum_cpu, sum_mem); }')
done
if [ "$user_in" = "c" ]; then ORDERVAL=2; fi
if [ "$user_in" = "m" ]; then ORDERVAL=3; fi
printf "$user_output" | awk '{ for (i = 1; i <= 100; i++){ if ($i != "") { if (i%4 == 1){ printf("%8s ",$i);} if (i%4 == 2){ printf("%7.2f ",$i); } if (i%4 == 3){ printf("%7.2f \n",$i); } }}}' | sort -rgk$ORDERVAL | awk '{ for (i = 1; i <= 100; i++){ if ($i != "") { if (i%4 == 1){ printf("%8s \t ",$i);} if (i%4 == 2){ printf("%7.2f \t ",$i); } if (i%4 == 3){ printf("%7.2f \n",$i); } }}}'
printf '\033[K'
fi
stty echo
read -sn1 -t $REFRESHTIME user_in
if [ "$user_in" = "q" ]; then printf '\033[u' ; clear; stty echo ; exit; fi
if [ "$user_in" = "c" ]; then ORDERVAL=2; fi
if [ "$user_in" = "m" ]; then ORDERVAL=3; fi # ORDERVAL 2 -> cpu 3 -> mem
printf '\033[H'
done