stub_status.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package nginx
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/pkg/errors"
  12. )
  13. // StubStatusInfo Store the stub_status module status
  14. type StubStatusInfo struct {
  15. Enabled bool `json:"stub_status_enabled"` // stub_status module is enabled
  16. URL string `json:"stub_status_url"` // stub_status access address
  17. }
  18. type StubStatusData struct {
  19. Active int `json:"active"`
  20. Accepts int `json:"accepts"`
  21. Handled int `json:"handled"`
  22. Requests int `json:"requests"`
  23. Reading int `json:"reading"`
  24. Writing int `json:"writing"`
  25. Waiting int `json:"waiting"`
  26. }
  27. const (
  28. StubStatusPort = 51828
  29. StubStatusPath = "/stub_status"
  30. StubStatusHost = "localhost"
  31. StubStatusProtocol = "http"
  32. StubStatusAllow = "127.0.0.1"
  33. StubStatusDeny = "all"
  34. StubStatusConfigName = "stub_status_nginx-ui.conf"
  35. )
  36. // GetStubStatusData Get the stub_status module data
  37. func GetStubStatusData() (bool, *StubStatusData, error) {
  38. result := &StubStatusData{
  39. Active: 0,
  40. Accepts: 0,
  41. Handled: 0,
  42. Requests: 0,
  43. Reading: 0,
  44. Writing: 0,
  45. Waiting: 0,
  46. }
  47. // Get the stub_status status information
  48. enabled, statusURL := IsStubStatusEnabled()
  49. if !enabled {
  50. return false, result, fmt.Errorf("stub_status is not enabled")
  51. }
  52. // Create an HTTP client
  53. client := &http.Client{
  54. Timeout: 5 * time.Second,
  55. }
  56. // Send a request to get the stub_status data
  57. resp, err := client.Get(statusURL)
  58. if err != nil {
  59. return enabled, result, fmt.Errorf("failed to get stub status: %v", err)
  60. }
  61. defer resp.Body.Close()
  62. // Read the response content
  63. body, err := io.ReadAll(resp.Body)
  64. if err != nil {
  65. return enabled, result, fmt.Errorf("failed to read response body: %v", err)
  66. }
  67. // Parse the response content
  68. statusContent := string(body)
  69. // Match the active connection number
  70. activeRe := regexp.MustCompile(`Active connections:\s+(\d+)`)
  71. if matches := activeRe.FindStringSubmatch(statusContent); len(matches) > 1 {
  72. result.Active, _ = strconv.Atoi(matches[1])
  73. }
  74. // Match the request statistics information
  75. serverRe := regexp.MustCompile(`(\d+)\s+(\d+)\s+(\d+)`)
  76. if matches := serverRe.FindStringSubmatch(statusContent); len(matches) > 3 {
  77. result.Accepts, _ = strconv.Atoi(matches[1])
  78. result.Handled, _ = strconv.Atoi(matches[2])
  79. result.Requests, _ = strconv.Atoi(matches[3])
  80. }
  81. // Match the read and write waiting numbers
  82. connRe := regexp.MustCompile(`Reading:\s+(\d+)\s+Writing:\s+(\d+)\s+Waiting:\s+(\d+)`)
  83. if matches := connRe.FindStringSubmatch(statusContent); len(matches) > 3 {
  84. result.Reading, _ = strconv.Atoi(matches[1])
  85. result.Writing, _ = strconv.Atoi(matches[2])
  86. result.Waiting, _ = strconv.Atoi(matches[3])
  87. }
  88. return enabled, result, nil
  89. }
  90. // GetStubStatus Get the stub_status module status
  91. func GetStubStatus() *StubStatusInfo {
  92. enabled, statusURL := IsStubStatusEnabled()
  93. return &StubStatusInfo{
  94. Enabled: enabled,
  95. URL: statusURL,
  96. }
  97. }
  98. // IsStubStatusEnabled Check if the stub_status module is enabled and return the access address
  99. // Only check the stub_status_nginx-ui.conf configuration file
  100. func IsStubStatusEnabled() (bool, string) {
  101. stubStatusConfPath := GetConfPath("conf.d", StubStatusConfigName)
  102. if _, err := os.Stat(stubStatusConfPath); os.IsNotExist(err) {
  103. return false, ""
  104. }
  105. ngxConfig, err := ParseNgxConfig(stubStatusConfPath)
  106. if err != nil {
  107. return false, ""
  108. }
  109. // Find the stub_status configuration
  110. for _, server := range ngxConfig.Servers {
  111. protocol := StubStatusProtocol
  112. host := StubStatusHost
  113. port := strconv.Itoa(StubStatusPort)
  114. for _, location := range server.Locations {
  115. // Check if the location content contains stub_status
  116. if strings.Contains(location.Content, "stub_status") {
  117. stubStatusURL := fmt.Sprintf("%s://%s:%s%s", protocol, host, port, StubStatusPath)
  118. return true, stubStatusURL
  119. }
  120. }
  121. }
  122. return false, ""
  123. }
  124. // EnableStubStatus Enable stub_status module
  125. func EnableStubStatus() error {
  126. enabled, _ := IsStubStatusEnabled()
  127. if enabled {
  128. return nil
  129. }
  130. return CreateStubStatusConfig()
  131. }
  132. // DisableStubStatus Disable stub_status module
  133. func DisableStubStatus() error {
  134. stubStatusConfPath := GetConfPath("conf.d", StubStatusConfigName)
  135. if _, err := os.Stat(stubStatusConfPath); os.IsNotExist(err) {
  136. return nil
  137. }
  138. return os.Remove(stubStatusConfPath)
  139. }
  140. // CreateStubStatusConfig Create a new stub_status configuration file
  141. func CreateStubStatusConfig() error {
  142. httpConfPath := GetConfPath("conf.d", StubStatusConfigName)
  143. stubStatusConfig := `
  144. # DO NOT EDIT THIS FILE, IT IS AUTO GENERATED BY NGINX-UI
  145. # Nginx stub_status configuration for Nginx-UI
  146. # Modified at ` + time.Now().Format("2006-01-02 15:04:05") + `
  147. server {
  148. listen 51828; # Use non-standard port to avoid conflicts
  149. server_name localhost;
  150. # Status monitoring interface
  151. location /stub_status {
  152. stub_status;
  153. allow 127.0.0.1; # Only allow local access
  154. deny all;
  155. }
  156. }
  157. `
  158. ngxConfig, err := ParseNgxConfigByContent(stubStatusConfig)
  159. if err != nil {
  160. return errors.Wrap(err, "failed to parse new nginx config")
  161. }
  162. ngxConfig.FileName = httpConfPath
  163. configText, err := ngxConfig.BuildConfig()
  164. if err != nil {
  165. return errors.Wrap(err, "failed to build nginx config")
  166. }
  167. return os.WriteFile(httpConfPath, []byte(configText), 0644)
  168. }