-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupernice.sh
215 lines (171 loc) · 4.13 KB
/
supernice.sh
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
set -e
# minimum uid to be considered
MIN_UID=1000
# number of samples kept.
MAX_SAMPLES=15
# minimum number of samples required to be
# eligible for penalties
MIN_SAMPLES=10
# number of samples that need to exceed
# the SAMPLE_LIMIT to trigger penalties
SAMPLES_OVER_LIMIT=8
# the time distance between samples
# in seconds
SAMPLE_DISTANCE=2
# the amount of cpu time that is
# "too much" per sample (in seconds)
SAMPLE_LIMIT=$(( ${SAMPLE_DISTANCE} / 2))
PID_FILE="/var/run/supernice.pid"
USERS=()
STATS=()
PENALIZED=()
ps_call() {
ps -e --format=uid,user,time h | \
awk "{ if (\$1 >= ${MIN_UID} ) print; }"
}
display() {
echo -e "UID:\t TIMES"
for uid in "${USERS[@]}";
do
echo -e "${uid}:\t ${STATS[$uid]}"
done
}
update() {
users_=()
tmp=()
IFS=$'\n'
for line in `ps_call`;
do
IFS=" " read uid user time_ <<< "$line"
IFS=":" read hours mins secs <<< "$time_"
set +e
t=$(( 10#$hours * 360))
((t += 10#$mins * 60 ))
((t += 10#$secs ))
set -e
users_[$user]=$user
tmp[$user]=$(( ${tmp[${user}]:-0} + $t ))
done
for user in "${users_[@]}";
do
if (( !${USERS[$user]:-0} ));
then
USERS[$user]=$user
fi
done
for user in "${USERS[@]}";
do
stats=( ${STATS[$user]=""} )
n=$( echo "${STATS[$user]}" | wc -w )
if (( $n == $MAX_SAMPLES ));
then
STATS[$user]="${tmp[$user]:-0} ${stats[@]:1:${MAX_SAMPLES}}"
else
STATS[$user]="${tmp[$user]:-0} ${stats[@]}"
fi
done
}
penalize() {
user=$1
PENALIZED[$user]=$user
renice -n 20 -u $user > /dev/null
ps -u $user --format=pid h | xargs ionice -c idle -p
}
check() {
PENALIZED=()
for user in "${USERS[@]}";
do
IFS=" " read -ra values <<< "${STATS[$user]}"
n=$( echo "${values[@]}" | wc -w )
if (( $n >= $MIN_SAMPLES ));
then
oldv=${values[0]}
m=0
for v in "${values[@]:1:$MAX_SAMPLES}";
do
diff=$(($oldv - $v))
if (( $diff >= $SAMPLE_LIMIT ));
then
((m += 1))
fi
oldv=$v
done
if (( $m >= $SAMPLES_OVER_LIMIT ));
then
penalize $user
fi
fi
done
}
loop() {
while true;
do
update
check
#display
sleep $SAMPLE_DISTANCE
done
}
hup() {
logger -p user.debug -t "supernice" "currently penalized: ${PENALIZED[@]}"
}
daemonize() {
if [ -f "$PID_FILE" ];
then
old_pid=$(cat "$PID_FILE")
ps -p $old_pid h > /dev/null && echo "supernice already running with pid $old_pid" > /dev/stderr && exit
fi
(
trap hup SIGHUP
trap "rm \"$PID_FILE\"; exit" SIGINT SIGTERM
loop
) &
jobs -p > "$PID_FILE"
}
stop() {
set +e
if [ -f "$PID_FILE" ];
then
old_pid=$(cat "$PID_FILE")
ps -p $old_pid h | grep supernice > /dev/null
if [ "$?" == "0" ];
then
echo -n "killing supernice daemon process with pid $old_pid" > /dev/stderr
kill $old_pid
for i in `seq 5`;
do
echo -n "." > /dev/stderr
ps -p $old_pid h | grep supernice > /dev/null
test "$?" != "0" && echo " success" > /dev/stderr && exit
sleep 1
done
echo " timeout" > /dev/stderr
exit 1
else
echo "could not find supernice daemon with pid $old_pid" > /dev/stderr
fi
else
echo "no supernice daemon pid file found" > /dev/stderr
fi
set -e
}
start() {
echo "starting supernice daemon" > /dev/stderr
daemonize
}
status() {
set +e
if [ -f "$PID_FILE" ];
then
old_pid=$(cat "$PID_FILE")
echo "sending HUP to supernice daemon with pid $old_pid - check /var/log/syslog for stats" > /dev/stderr
kill -HUP $old_pid
fi
set -e
}
restart() {
stop
start
}
${1:-start}