config_info.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package nginx
  2. import (
  3. "os"
  4. "regexp"
  5. "runtime"
  6. "strconv"
  7. "github.com/pkg/errors"
  8. )
  9. type NginxConfigInfo struct {
  10. WorkerProcesses int `json:"worker_processes"`
  11. WorkerConnections int `json:"worker_connections"`
  12. ProcessMode string `json:"process_mode"`
  13. KeepaliveTimeout int `json:"keepalive_timeout"`
  14. Gzip string `json:"gzip"`
  15. GzipMinLength int `json:"gzip_min_length"`
  16. GzipCompLevel int `json:"gzip_comp_level"`
  17. ClientMaxBodySize string `json:"client_max_body_size"` // with unit
  18. ServerNamesHashBucketSize int `json:"server_names_hash_bucket_size"`
  19. ClientHeaderBufferSize string `json:"client_header_buffer_size"` // with unit
  20. ClientBodyBufferSize string `json:"client_body_buffer_size"` // with unit
  21. }
  22. // GetNginxWorkerConfigInfo Get Nginx config info of worker_processes and worker_connections
  23. func GetNginxWorkerConfigInfo() (*NginxConfigInfo, error) {
  24. result := &NginxConfigInfo{
  25. WorkerProcesses: 1,
  26. WorkerConnections: 1024,
  27. ProcessMode: "manual",
  28. KeepaliveTimeout: 65,
  29. Gzip: "off",
  30. GzipMinLength: 1,
  31. GzipCompLevel: 1,
  32. ClientMaxBodySize: "1m",
  33. ServerNamesHashBucketSize: 32,
  34. ClientHeaderBufferSize: "1k",
  35. ClientBodyBufferSize: "8k",
  36. }
  37. confPath := GetConfPath("nginx.conf")
  38. if confPath == "" {
  39. return nil, errors.New("failed to get nginx.conf path")
  40. }
  41. // Read the current configuration
  42. content, err := os.ReadFile(confPath)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "failed to read nginx.conf")
  45. }
  46. outputStr := string(content)
  47. // Parse worker_processes
  48. wpRe := regexp.MustCompile(`worker_processes\s+(\d+|auto);`)
  49. if matches := wpRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  50. if matches[1] == "auto" {
  51. result.WorkerProcesses = runtime.NumCPU()
  52. result.ProcessMode = "auto"
  53. } else {
  54. result.WorkerProcesses, _ = strconv.Atoi(matches[1])
  55. result.ProcessMode = "manual"
  56. }
  57. }
  58. // Parse worker_connections
  59. wcRe := regexp.MustCompile(`worker_connections\s+(\d+);`)
  60. if matches := wcRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  61. result.WorkerConnections, _ = strconv.Atoi(matches[1])
  62. }
  63. // Parse keepalive_timeout
  64. ktRe := regexp.MustCompile(`keepalive_timeout\s+(\d+);`)
  65. if matches := ktRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  66. result.KeepaliveTimeout, _ = strconv.Atoi(matches[1])
  67. }
  68. // Parse gzip
  69. gzipRe := regexp.MustCompile(`gzip\s+(on|off);`)
  70. if matches := gzipRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  71. result.Gzip = matches[1]
  72. }
  73. // Parse gzip_min_length
  74. gzipMinRe := regexp.MustCompile(`gzip_min_length\s+(\d+);`)
  75. if matches := gzipMinRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  76. result.GzipMinLength, _ = strconv.Atoi(matches[1])
  77. }
  78. // Parse gzip_comp_level
  79. gzipCompRe := regexp.MustCompile(`gzip_comp_level\s+(\d+);`)
  80. if matches := gzipCompRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  81. result.GzipCompLevel, _ = strconv.Atoi(matches[1])
  82. }
  83. // Parse client_max_body_size with any unit (k, m, g)
  84. cmaxRe := regexp.MustCompile(`client_max_body_size\s+(\d+[kmg]?);`)
  85. if matches := cmaxRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  86. result.ClientMaxBodySize = matches[1]
  87. }
  88. // Parse server_names_hash_bucket_size
  89. hashRe := regexp.MustCompile(`server_names_hash_bucket_size\s+(\d+);`)
  90. if matches := hashRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  91. result.ServerNamesHashBucketSize, _ = strconv.Atoi(matches[1])
  92. }
  93. // Parse client_header_buffer_size with any unit (k, m, g)
  94. headerRe := regexp.MustCompile(`client_header_buffer_size\s+(\d+[kmg]?);`)
  95. if matches := headerRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  96. result.ClientHeaderBufferSize = matches[1]
  97. }
  98. // Parse client_body_buffer_size with any unit (k, m, g)
  99. bodyRe := regexp.MustCompile(`client_body_buffer_size\s+(\d+[kmg]?);`)
  100. if matches := bodyRe.FindStringSubmatch(outputStr); len(matches) > 1 {
  101. result.ClientBodyBufferSize = matches[1]
  102. }
  103. return result, nil
  104. }