-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-renice.sh
More file actions
106 lines (93 loc) · 2.09 KB
/
Copy pathserver-renice.sh
File metadata and controls
106 lines (93 loc) · 2.09 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
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
#!/bin/bash
#####
# Скрипт меняет приоритеты и ограничения сервера
# в зависимости от актывных подключений.
#####
IFS=$'\n'
PORTS=$(seq 2302 12 2362)
NICE_NORMAL=0
NICE_LOW=19
IONICE_CLASS_NORMAL=0
IONICE_CLASS_LOW=3
prefix_nft="user_counter"
prefix_difference="prefix_difference"
sleep=20
bytes_idle=$((5 * 1000 * ${sleep}))
fnc_port_getReNice () {
if [[ $(ssh mikrotik ip/firewall/connection/print | egrep -c "d udp.*:${port}") -gt 0 ]]
then
renice=${NICE_NORMAL}
else
bytes=$(nft -a list chain inet filter output | grep ${prefix_nft} | grep ${port} | awk '{ print $11 }' | head -n 1)
if [[ -z ${bytes} ]]
then
renice=${NICE_LOW}
ionice_class=${IONICE_CLASS_LOW}
else
str="${prefix_difference}_${port}"
bytes_old=${!str}
if [[ -z ${bytes_old} ]]
then
bytes_old=${bytes}
fi
difference=$((${bytes} - ${bytes_old}))
eval ${prefix_difference}_${port}=${bytes}
if [[ ${difference} -gt ${bytes_idle} ]]
then
renice=${NICE_NORMAL}
ionice_class=${IONICE_CLASS_NORMAL}
else
renice=${NICE_LOW}
ionice_class=${IONICE_CLASS_LOW}
fi
fi
fi
}
fnc_renice () {
if [[ ${renice} -ge 5 ]]
then
chrt --pid --idle 0 ${pid}
else
chrt --pid --other 0 ${pid}
fi
pgid=$(awk '{print $5}' /proc/${pid}/stat)
renice -n ${renice} --pgrp ${pgid}
ionice --class ${ionice_class} --pgid ${pgid}
children=$(cat /proc/${pid}/task/*/children)
for i in ${children[@]}
do
renice -n ${renice} --pid ${i}
ionice --class ${ionice_class} --pid ${i}
done
}
fnc_headlessClient () {
services_children=$(find /proc/${pid}/cwd/ -type f -name "*.service" -printf "%P ")
for i in ${services_children}
do
if [[ ${renice} -ge 5 ]]
then
systemctl stop ${i}
else
if [[ -n $(pgrep -i steam.exe) ]]
then
systemctl start ${i}
fi
fi
done
}
while sleep ${sleep}
do
for port in ${PORTS}
do
pids=$(lsof -t -n -i :${port} -a -c ^wineserver)
if [[ -n ${pids} ]]
then
fnc_port_getReNice
for pid in ${pids[@]}
do
fnc_renice
fnc_headlessClient
done
fi
done
done