引入lumberjack和fluentbit自动分发

This commit is contained in:
robin
2026-02-13 22:36:17 +08:00
parent c6da67db79
commit e9093baffb
47 changed files with 4589 additions and 317 deletions

View File

@@ -3,13 +3,18 @@
package db
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/actions"
"strings"
)
const clickhouseConfigCode = "clickhouseConfig"
@@ -29,29 +34,37 @@ func (this *ClickHouseAction) RunGet(params struct{}) {
this.ErrorPage(err)
return
}
cfg := &systemconfigs.ClickHouseSetting{Port: 8123, Database: "default", Scheme: "http"}
cfg := &systemconfigs.ClickHouseSetting{Port: 8443, Database: "default", Scheme: "https"}
if len(resp.ValueJSON) > 0 {
_ = json.Unmarshal(resp.ValueJSON, cfg)
}
if cfg.Port <= 0 {
cfg.Port = 8123
cfg.Port = 8443
}
if cfg.Database == "" {
cfg.Database = "default"
}
if strings.TrimSpace(cfg.Scheme) == "" {
cfg.Scheme = "http"
cfg.Scheme = "https"
}
this.Data["config"] = map[string]interface{}{
"host": cfg.Host,
"port": cfg.Port,
"user": cfg.User,
"password": cfg.Password,
"database": cfg.Database,
"scheme": cfg.Scheme,
"tlsSkipVerify": cfg.TLSSkipVerify,
"tlsServerName": cfg.TLSServerName,
"host": cfg.Host,
"port": cfg.Port,
"user": cfg.User,
"password": cfg.Password,
"database": cfg.Database,
"scheme": cfg.Scheme,
}
// 自动检测连接状态
connStatus := "unconfigured" // unconfigured / connected / disconnected
connError := ""
if strings.TrimSpace(cfg.Host) != "" {
connStatus, connError = this.probeClickHouse(cfg)
}
this.Data["connStatus"] = connStatus
this.Data["connError"] = connError
this.Show()
}
@@ -62,20 +75,18 @@ func (this *ClickHouseAction) RunPost(params struct {
Password string
Database string
Scheme string
TLSSkipVerify bool
TLSServerName string
Must *actions.Must
}) {
defer this.CreateLogInfo(codes.DBNode_LogUpdateDBNode, 0)
if params.Port <= 0 {
params.Port = 8123
}
if params.Database == "" {
params.Database = "default"
}
if params.Scheme != "https" {
params.Scheme = "http"
if params.Scheme != "http" {
params.Scheme = "https"
}
if params.Port <= 0 {
params.Port = 8443
}
password := params.Password
if password == "" {
@@ -94,8 +105,8 @@ func (this *ClickHouseAction) RunPost(params struct {
Password: password,
Database: params.Database,
Scheme: params.Scheme,
TLSSkipVerify: params.TLSSkipVerify,
TLSServerName: strings.TrimSpace(params.TLSServerName),
TLSSkipVerify: true,
TLSServerName: "",
}
valueJSON, err := json.Marshal(cfg)
if err != nil {
@@ -112,3 +123,51 @@ func (this *ClickHouseAction) RunPost(params struct {
}
this.Success()
}
// probeClickHouse 快速检测 ClickHouse 连接状态SELECT 1
func (this *ClickHouseAction) probeClickHouse(cfg *systemconfigs.ClickHouseSetting) (status string, errMsg string) {
scheme := strings.ToLower(strings.TrimSpace(cfg.Scheme))
if scheme == "" {
scheme = "https"
}
port := cfg.Port
if port <= 0 {
port = 8443
}
db := cfg.Database
if db == "" {
db = "default"
}
testURL := fmt.Sprintf("%s://%s:%d/?query=SELECT+1&database=%s", scheme, cfg.Host, port, db)
transport := &http.Transport{}
if scheme == "https" {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
client := &http.Client{
Timeout: 3 * time.Second,
Transport: transport,
}
req, err := http.NewRequest(http.MethodGet, testURL, nil)
if err != nil {
return "disconnected", err.Error()
}
if cfg.User != "" || cfg.Password != "" {
req.SetBasicAuth(cfg.User, cfg.Password)
}
resp, err := client.Do(req)
if err != nil {
return "disconnected", err.Error()
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "disconnected", fmt.Sprintf("HTTP %d", resp.StatusCode)
}
return "connected", ""
}

View File

@@ -17,7 +17,7 @@ func (this *ClickHouseAction) Init() {
func (this *ClickHouseAction) RunGet(params struct{}) {
this.Data["mainTab"] = "clickhouse"
this.Data["config"] = map[string]interface{}{
"host": "", "port": 8123, "user": "", "password": "", "database": "default",
"host": "", "port": 8443, "user": "", "password": "", "database": "default", "scheme": "https",
}
this.Show()
}

View File

@@ -25,6 +25,7 @@ func init() {
Get("/logs", new(LogsAction)).
Post("/status", new(StatusAction)).
GetPost("/clickhouse", new(ClickHouseAction)).
Post("/testClickhouse", new(TestClickHouseAction)).
EndAll()
})
}

View File

@@ -0,0 +1,92 @@
//go:build plus
package db
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
type TestClickHouseAction struct {
actionutils.ParentAction
}
func (this *TestClickHouseAction) Init() {
this.Nav("db", "db", "clickhouse")
}
func (this *TestClickHouseAction) RunPost(params struct {
Host string
Port int
User string
Password string
Database string
Scheme string
Must *actions.Must
}) {
params.Must.
Field("host", params.Host).
Require("请输入 ClickHouse 连接地址")
if params.Database == "" {
params.Database = "default"
}
scheme := "https"
if strings.EqualFold(params.Scheme, "http") {
scheme = "http"
}
if params.Port <= 0 {
params.Port = 8443
}
// 构造测试请求
testURL := fmt.Sprintf("%s://%s:%d/?query=SELECT+1&database=%s",
scheme, params.Host, params.Port, params.Database)
transport := &http.Transport{}
if scheme == "https" {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
client := &http.Client{
Timeout: 5 * time.Second,
Transport: transport,
}
req, err := http.NewRequest(http.MethodGet, testURL, nil)
if err != nil {
this.Fail("请求构造失败: " + err.Error())
return
}
if params.User != "" || params.Password != "" {
req.SetBasicAuth(params.User, params.Password)
}
resp, err := client.Do(req)
if err != nil {
this.Fail("连接失败: " + err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
msg := strings.TrimSpace(string(body))
if len(msg) > 200 {
msg = msg[:200] + "..."
}
this.Fail(fmt.Sprintf("ClickHouse 返回 HTTP %d: %s", resp.StatusCode, msg))
return
}
this.Success()
}

View File

@@ -0,0 +1,23 @@
//go:build !plus
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
type TestClickHouseAction struct {
actionutils.ParentAction
}
func (this *TestClickHouseAction) Init() {
this.Nav("db", "db", "clickhouse")
}
func (this *TestClickHouseAction) RunPost(params struct {
Host string
Must *actions.Must
}) {
this.Fail("请使用商业版以测试 ClickHouse 连接")
}