forked from frappe/frappe_docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhost_optimize.sh
130 lines (110 loc) · 3.66 KB
/
host_optimize.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
#!/bin/bash
# Function to show usage
show_usage() {
echo "Usage: $0 [enable|disable|status]"
echo " enable - Enable host optimizations"
echo " disable - Disable host optimizations"
echo " status - Show current optimization status"
exit 1
}
# Function to check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root or with sudo"
exit 1
fi
}
# Function to get current status
get_status() {
echo "=== Current System Settings ==="
echo "Memory Management:"
sysctl vm.overcommit_memory
sysctl vm.swappiness
echo -e "\nFile Descriptors:"
ulimit -n
echo -e "\nNetwork Settings:"
sysctl net.core.somaxconn
sysctl net.ipv4.tcp_max_syn_backlog
sysctl net.ipv4.ip_local_port_range
echo -e "\nI/O Settings:"
cat /sys/block/$(mount | grep " / " | cut -d' ' -f1 | sed 's/[0-9]*$//g' | sed 's/\/dev\///g')/queue/scheduler
}
# Function to enable optimizations
enable_optimizations() {
check_root
echo "Enabling host optimizations for virtual server..."
# Memory Management
echo "Configuring memory management..."
echo 'vm.overcommit_memory = 1' > /etc/sysctl.d/90-memory.conf
echo 'vm.swappiness = 10' >> /etc/sysctl.d/90-memory.conf
# File descriptor limits
echo "Setting file descriptor limits..."
if ! grep -q "* soft nofile" /etc/security/limits.conf; then
echo "* soft nofile 32768" >> /etc/security/limits.conf
echo "* hard nofile 32768" >> /etc/security/limits.conf
fi
# Network optimizations (conservative settings for virtual server)
echo "Configuring network settings..."
cat > /etc/sysctl.d/91-network.conf << EOF
# Max connection backlog
net.core.somaxconn = 2048
# TCP backlog
net.ipv4.tcp_max_syn_backlog = 2048
# Local port range
net.ipv4.ip_local_port_range = 10240 65535
# TCP keepalive
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 3
# TCP memory usage (auto-tuned)
net.ipv4.tcp_moderate_rcvbuf = 1
EOF
# I/O Scheduler optimization
echo "Configuring I/O scheduler..."
root_disk=$(mount | grep " / " | cut -d' ' -f1 | sed 's/[0-9]*$//g' | sed 's/\/dev\///g')
if [ -f "/sys/block/$root_disk/queue/scheduler" ]; then
echo "none" > "/sys/block/$root_disk/queue/scheduler"
fi
# Apply sysctl changes
echo "Applying changes..."
sysctl --system
echo "Host optimizations have been enabled"
echo "Note: Some changes may require a system reboot to take effect"
get_status
}
# Function to disable optimizations
disable_optimizations() {
check_root
echo "Disabling host optimizations..."
# Remove custom sysctl configurations
rm -f /etc/sysctl.d/90-memory.conf
rm -f /etc/sysctl.d/91-network.conf
# Remove custom limits
sed -i '/\* soft nofile/d' /etc/security/limits.conf
sed -i '/\* hard nofile/d' /etc/security/limits.conf
# Reset I/O scheduler to default (cfq or mq-deadline depending on the system)
root_disk=$(mount | grep " / " | cut -d' ' -f1 | sed 's/[0-9]*$//g' | sed 's/\/dev\///g')
if [ -f "/sys/block/$root_disk/queue/scheduler" ]; then
echo "mq-deadline" > "/sys/block/$root_disk/queue/scheduler"
fi
# Apply default sysctl settings
sysctl --system
echo "Host optimizations have been disabled"
echo "Note: Some changes may require a system reboot to take effect"
get_status
}
# Main script
case "$1" in
enable)
enable_optimizations
;;
disable)
disable_optimizations
;;
status)
get_status
;;
*)
show_usage
;;
esac