-
Notifications
You must be signed in to change notification settings - Fork 3
/
gtbox_sys_net_optimize.go
159 lines (134 loc) · 3.69 KB
/
gtbox_sys_net_optimize.go
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
/*
Package gtbox_sys_net_optimize 主要提供网络并发优化功能
*/
package gtbox_sys_net_optimize
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)
type Config struct {
Current map[string]string `json:"current"`
New map[string]string `json:"new"`
}
func getSystemInfo() (int, uint64) {
cpuCount := runtime.NumCPU()
vmStat, err := exec.Command("vmstat", "-s").Output()
if err != nil {
log.Fatal(err)
}
var memorySize uint64
lines := strings.Split(string(vmStat), "\n")
for _, line := range lines {
if strings.Contains(line, "total memory") {
fields := strings.Fields(line)
if len(fields) == 4 {
memorySize, _ = strconv.ParseUint(fields[0], 10, 64)
break
}
}
}
return cpuCount, memorySize
}
func readConfig(file string, delimiter string) (map[string]string, error) {
content, err := os.ReadFile(file)
if err != nil {
return nil, err
}
lines := strings.Split(string(content), "\n")
config := make(map[string]string)
for _, line := range lines {
fields := strings.Split(line, delimiter)
if len(fields) == 2 {
key, value := strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1])
config[key] = value
}
}
return config, nil
}
func applyConfig(file string, delimiter string, newConfig map[string]string) error {
config, err := readConfig(file, delimiter)
if err != nil {
return err
}
for key, value := range newConfig {
config[key] = value
}
content := ""
for key, value := range config {
content += fmt.Sprintf("%s %s %s\n", key, delimiter, value)
}
err = os.WriteFile(file, []byte(content), 0644)
if err != nil {
return err
}
return nil
}
func calculateConfig(cpuCount int, memorySize uint64) map[string]string {
concurrency := cpuCount * 12800
newConfig := map[string]string{
"fs.file-max": strconv.Itoa(concurrency),
"net.core.somaxconn": strconv.Itoa(concurrency),
"net.ipv4.tcp_max_syn_backlog": strconv.Itoa(concurrency),
"net.ipv4.ip_local_port_range": "1024 " + strconv.Itoa(concurrency),
"net.ipv4.tcp_tw_reuse": "1",
"net.ipv4.tcp_sack": "1",
"net.ipv4.tcp_timestamps": "1",
"net.ipv4.tcp_keepalive_time": "300",
"net.ipv4.tcp_keepalive_probes": "5",
"net.ipv4.tcp_keepalive_intvl": "15",
"* soft nofile": strconv.Itoa(concurrency),
"* hard nofile": strconv.Itoa(concurrency),
"* soft nproc": strconv.Itoa(concurrency),
"* hard nproc": strconv.Itoa(concurrency),
}
return newConfig
}
func autoExecute() {
cpuCount, memorySize := getSystemInfo()
limitsConfig, err := readConfig("/etc/security/limits.conf", " ")
if err != nil {
log.Fatal(err)
}
sysctlConfig, err := readConfig("/etc/sysctl.conf", "=")
if err != nil {
log.Fatal(err)
}
newConfig := calculateConfig(cpuCount, memorySize)
// 打印当前配置
config := Config{
Current: make(map[string]string),
New: make(map[string]string),
}
for key := range newConfig {
if value, ok := limitsConfig[key]; ok {
config.Current[key] = value
} else if value, ok := sysctlConfig[key]; ok {
config.Current[key] = value
}
config.New[key] = newConfig[key]
}
jsonConfig, _ := json.MarshalIndent(config, "", " ")
fmt.Println("Current and new configurations:")
fmt.Println(string(jsonConfig))
// 应用新配置
err = applyConfig("/etc/security/limits.conf", " ", newConfig)
if err != nil {
log.Fatal(err)
}
err = applyConfig("/etc/sysctl.conf", "=", newConfig)
if err != nil {
log.Fatal(err)
}
// 重新加载 sysctl 配置
_, err = exec.Command("sysctl", "-p").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println("Configuration updated successfully.")
}