1
0

stub_status.go 5.3 KB

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