-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpucsekk
executable file
·148 lines (121 loc) · 4.63 KB
/
cpucsekk
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
#!/bin/bash
#Credits go to Paul Colby (http://colby.id.au), who says no rights reserved ;)
#http://colby.id.au/node/39
# Had problems with narrow screens. So, save just two columns: %CPU and COMMAND from within top to ~/bin/.toprc
delay=1 # Seconds to wait before next measurement
swapthr=80 # Also hangs if swap is more full than this percent
chkrsync=1 # Turn this to 0 if you want to skip check for a running rsync. Especially with hard drives, it's not nice to load the system during rsyncing.
# Randomize delay: it's no good to finish all instances at the same time if a script launches multiple cpucsekks.
rdelay=`echo "$delay*($RANDOM/32767+0.5)"|bc -l`
if [ ! -d $HOME/.cpucsekk/ ]
then
mkdir $HOME/.cpucsekk/
fi
# Edit this file for different CPU load levels.
if [ ! -f $HOME/.cpucsekk/cpucsekk.txt ]
then
echo "15" > $HOME/.cpucsekk/cpucsekk.txt
fi
LOAD=100
CPU=(`grep '^cpu ' /proc/stat`)
unset CPU[0] # Discard the "cpu" prefix.
IDLEA=${CPU[4]} # Get the idle CPU time.
# Calculate the total CPU time.
TOTALA=0
for VALUE in "${CPU[@]}"; do
let "TOTALA=$TOTALA+$VALUE"
done
echo "cpucsekk: press c [enter] to skip."
while true; do # While load > $cpupc%
cpupc=`cat $HOME/.cpucsekk/cpucsekk.txt` # How low do we allow to step to the next task
re='^[0-9]+$'
if ! [[ $cpupc =~ $re ]] ; then
echo "$HOME/.cpucsekk/cpucsekk.txt should contain a single percentage number." >&2; exit 1
fi
if [[ -f /sys/block/sda/stat ]]
then
#osda=`cat /sys/block/sda/stat | awk '{ print($NF) }'` # also check sda is not busy. (currently only sda is checked...)
osda=`cat /sys/block/sda/stat | awk '{ print($11) }'` # also check sda is not busy.
ki="n"
# There have been read error problems: "read error: 0: Resource temporarily unavailable"
# https://stackoverflow.com/questions/19895185/bash-shell-read-error-0-resource-temporarily-unavailable
# cpucsekk seems a good place to try cure this.
# python3 -c $'import os\nos.set_blocking(0, True)'
#cat /proc/self/fdinfo/0
read -t $rdelay -p "" ki
if [ "$ki" == "c" ]; then
break
fi
#nsda=`cat /sys/block/sda/stat | awk '{ print($NF) }'`
nsda=`cat /sys/block/sda/stat | awk '{ print($11) }'`
elif [[ -f /sys/block/nvme0n1/stat ]]
then
osda=`cat /sys/block/nvme0n1/stat | awk '{ print($11) }'` # also check ssd is not busy.
ki="n"
# There have been read error problems: "read error: 0: Resource temporarily unavailable"
# https://stackoverflow.com/questions/19895185/bash-shell-read-error-0-resource-temporarily-unavailable
# cpucsekk seems a good place to try cure this.
# python3 -c $'import os\nos.set_blocking(0, True)'
#cat /proc/self/fdinfo/0
read -t $rdelay -p "" ki
if [ "$ki" == "c" ]; then
break
fi
nsda=`cat /sys/block/nvme0n1/stat | awk '{ print($11) }'`
else
osda=0
nsda=0
fi
CPU=(`grep '^cpu ' /proc/stat`)
unset CPU[0] # Discard the "cpu" prefix.
IDLEB=${CPU[4]} # Get the idle CPU time.
# Calculate the total CPU time.
TOTALB=0
for VALUE in "${CPU[@]}"; do
let "TOTALB=$TOTALB+$VALUE"
done
let "TOTAL=$TOTALB-$TOTALA" # Increments since last round
let "IDLE=$IDLEB-$IDLEA"
let "LOAD=100*($TOTAL-$IDLE)/$TOTAL" # Load percent since last round
# Now let's look at the swap
swaptot=$(awk '$1~/^SwapTotal/ {print($2)}' /proc/meminfo)
swapfr=$(awk '$1~/^SwapFree/ {print($2)}' /proc/meminfo)
if [[ "$swaptot" =~ ^[0-9]+$ ]] && [[ "$swaptot" != "0" ]]
then
swappc=$((100-100*$swapfr/$swaptot))
else
swappc=0
fi
ido=`date +%T`
topbol=`$HOME/bin/topoftop`
let "ioincr=$nsda-$osda"
siotop=`$HOME/bin/showiotop`
echo "$ido io: $ioincr t $LOAD% sw $swappc% $topbol $siotop"
TOTALA=$TOTALB # Turn this round into last round
IDLEA=$IDLEB
# Check for running rsync
rsyncruns="0"
if [[ $chkrsync == "1" ]]
then
ps cax | grep rsync > /dev/null
if [ $? -eq 0 ]; then
echo "Found runnning rsync, waiting..."
rsyncruns="1"
fi
fi
# Check for other cpucsekks, to avoid simultaneous ending
# ps -C seems to output list in increasing order so we just check the first item. We can let things go if we are the first.
allcpids=`ps -C cpucsekk -C silent_cpucsekk -o pid=|sed 's/ //g'`
#echo "My PID: $$; all PIDS: $allcpids"
cqueuepos=`echo "$allcpids"|awk -v line=$$ '$0 == line {print NR}'`
if [[ $cqueuepos > 1 ]]
then
echo "We are at position $cqueuepos in the cpucsekk line."
fi
#if [[ $LOAD -lt $cpupc ]] ; then
if [[ $LOAD -lt $cpupc ]] && [[ $osda == $nsda ]] && [[ $swappc -lt $swapthr ]] && [[ $rsyncruns == "0" ]] && [[ $cqueuepos == 1 ]] ; then
# python3 -c $'import os\nos.set_blocking(0, True)'
#cat /proc/self/fdinfo/0
break
fi
done