1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 22:18:07 +08:00

fix: 解决没有curl导致的nginx负载状态获取不到的BUG

This commit is contained in:
zhengkunwang223 2022-12-09 10:02:36 +08:00 committed by zhengkunwang223
parent 62a298f6dd
commit 0d73852760

View File

@ -1,14 +1,13 @@
package service package service
import ( import (
"errors" "io/ioutil"
"fmt" "net/http"
"path" "path"
"strings" "strings"
"github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
) )
@ -16,14 +15,11 @@ type NginxService struct {
} }
func (n NginxService) GetNginxConfig() (dto.FileInfo, error) { func (n NginxService) GetNginxConfig() (dto.FileInfo, error) {
nginxInstall, err := getAppInstallByKey("nginx") nginxInstall, err := getAppInstallByKey("nginx")
if err != nil { if err != nil {
return dto.FileInfo{}, err return dto.FileInfo{}, err
} }
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "nginx.conf") configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "nginx.conf")
info, err := files.NewFileInfo(files.FileOption{ info, err := files.NewFileInfo(files.FileOption{
Path: configPath, Path: configPath,
Expand: true, Expand: true,
@ -35,12 +31,10 @@ func (n NginxService) GetNginxConfig() (dto.FileInfo, error) {
} }
func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam, error) { func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam, error) {
keys, ok := dto.ScopeKeyMap[req.Scope] keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 { if !ok || len(keys) == 0 {
return nil, nil return nil, nil
} }
return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil) return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil)
} }
@ -53,16 +47,16 @@ func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error {
} }
func (n NginxService) GetStatus() (dto.NginxStatus, error) { func (n NginxService) GetStatus() (dto.NginxStatus, error) {
nginxInstall, err := getAppInstallByKey("nginx") res, err := http.Get("http://127.0.0.1/nginx_status")
if err != nil { if err != nil {
return dto.NginxStatus{}, err return dto.NginxStatus{}, err
} }
res, err := cmd.Exec(fmt.Sprintf("docker exec -i %s curl http://127.0.0.1/nginx_status", nginxInstall.ContainerName)) content, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return dto.NginxStatus{}, errors.New(res) return dto.NginxStatus{}, err
} }
var status dto.NginxStatus var status dto.NginxStatus
resArray := strings.Split(res, " ") resArray := strings.Split(string(content), " ")
status.Active = resArray[2] status.Active = resArray[2]
status.Accepts = resArray[7] status.Accepts = resArray[7]
status.Handled = resArray[8] status.Handled = resArray[8]
@ -70,6 +64,5 @@ func (n NginxService) GetStatus() (dto.NginxStatus, error) {
status.Reading = resArray[11] status.Reading = resArray[11]
status.Writing = resArray[13] status.Writing = resArray[13]
status.Waiting = resArray[15] status.Waiting = resArray[15]
return status, nil return status, nil
} }