goploy/internal/model/monitor.go

317 lines
8.2 KiB
Go
Raw Normal View History

2022-04-09 21:53:37 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
2020-08-04 14:28:25 +08:00
package model
import (
2021-11-20 15:51:19 +08:00
"bytes"
"encoding/json"
2023-03-06 12:28:22 +08:00
"io"
2021-11-20 15:51:19 +08:00
"net/http"
2020-08-04 20:00:21 +08:00
2020-08-04 14:28:25 +08:00
sq "github.com/Masterminds/squirrel"
)
const monitorTable = "`monitor`"
type Monitor struct {
ID int64 `json:"id"`
NamespaceID int64 `json:"namespaceId"`
Name string `json:"name"`
Type int `json:"type"`
Target string `json:"target"`
Second int `json:"second"`
Times uint16 `json:"times"`
SilentCycle int `json:"silentCycle"`
NotifyType uint8 `json:"notifyType"`
NotifyTarget string `json:"notifyTarget"`
SuccessServerID int64 `json:"successServerId"`
SuccessScript string `json:"successScript"`
FailServerID int64 `json:"failServerId"`
FailScript string `json:"failScript"`
Description string `json:"description"`
ErrorContent string `json:"errorContent"`
State uint8 `json:"state"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
2020-08-04 14:28:25 +08:00
}
type Monitors []Monitor
2022-04-09 16:32:17 +08:00
func (m Monitor) GetList() (Monitors, error) {
2020-08-04 14:28:25 +08:00
rows, err := sq.
Select("id, name, type, target, second, times, silent_cycle, notify_type, notify_target, description, error_content, state, success_script, fail_script, success_server_id, fail_server_id, insert_time, update_time").
2020-08-04 14:28:25 +08:00
From(monitorTable).
Where(sq.Eq{
"namespace_id": m.NamespaceID,
}).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
monitors := Monitors{}
for rows.Next() {
var monitor Monitor
if err := rows.Scan(
&monitor.ID,
&monitor.Name,
2022-04-29 14:59:18 +08:00
&monitor.Type,
&monitor.Target,
2020-08-04 14:28:25 +08:00
&monitor.Second,
&monitor.Times,
2022-04-29 14:59:18 +08:00
&monitor.SilentCycle,
2020-08-04 14:28:25 +08:00
&monitor.NotifyType,
&monitor.NotifyTarget,
&monitor.Description,
2020-09-11 15:09:38 +08:00
&monitor.ErrorContent,
2020-08-04 14:28:25 +08:00
&monitor.State,
&monitor.SuccessScript,
&monitor.FailScript,
&monitor.SuccessServerID,
&monitor.FailServerID,
2020-08-04 14:28:25 +08:00
&monitor.InsertTime,
&monitor.UpdateTime); err != nil {
return nil, err
}
monitors = append(monitors, monitor)
}
return monitors, nil
}
func (m Monitor) GetData() (Monitor, error) {
var monitor Monitor
err := sq.
Select("id, name, type, target, second, times, silent_cycle, notify_type, notify_target, state", "success_script", "fail_script", "success_server_id", "fail_server_id").
2020-08-04 14:28:25 +08:00
From(monitorTable).
Where(sq.Eq{"id": m.ID}).
OrderBy("id DESC").
RunWith(DB).
QueryRow().
Scan(&monitor.ID, &monitor.Name, &monitor.Type, &monitor.Target, &monitor.Second, &monitor.Times, &monitor.SilentCycle, &monitor.NotifyType, &monitor.NotifyTarget, &monitor.State, &monitor.SuccessScript, &monitor.FailScript, &monitor.SuccessServerID, &monitor.FailServerID)
2020-08-04 14:28:25 +08:00
if err != nil {
2022-04-29 14:59:18 +08:00
return monitor, err
2020-08-04 14:28:25 +08:00
}
return monitor, nil
}
func (m Monitor) GetAllByState() (Monitors, error) {
rows, err := sq.
Select("id, name, type, target, second, times, silent_cycle, notify_type, notify_target, success_script, fail_script, success_server_id, fail_server_id, description, update_time").
2020-08-04 14:28:25 +08:00
From(monitorTable).
Where(sq.Eq{
"state": m.State,
}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
monitors := Monitors{}
for rows.Next() {
var monitor Monitor
if err := rows.Scan(
&monitor.ID,
&monitor.Name,
2022-04-29 14:59:18 +08:00
&monitor.Type,
&monitor.Target,
2020-08-04 14:28:25 +08:00
&monitor.Second,
&monitor.Times,
2022-04-29 14:59:18 +08:00
&monitor.SilentCycle,
2020-08-04 14:28:25 +08:00
&monitor.NotifyType,
&monitor.NotifyTarget,
&monitor.SuccessScript,
&monitor.FailScript,
&monitor.SuccessServerID,
&monitor.FailServerID,
2023-04-29 11:21:45 +08:00
&monitor.Description,
&monitor.UpdateTime,
); err != nil {
2020-08-04 14:28:25 +08:00
return nil, err
}
monitors = append(monitors, monitor)
}
return monitors, nil
}
func (m Monitor) AddRow() (int64, error) {
result, err := sq.
Insert(monitorTable).
Columns("namespace_id", "name", "type", "target", "second", "times", "silent_cycle", "notify_type", "notify_target", "description", "error_content", "success_script", "fail_script", "success_server_id", "fail_server_id").
Values(m.NamespaceID, m.Name, m.Type, m.Target, m.Second, m.Times, m.SilentCycle, m.NotifyType, m.NotifyTarget, m.Description, "", m.SuccessScript, m.FailScript, m.SuccessServerID, m.FailServerID).
2020-08-04 14:28:25 +08:00
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
func (m Monitor) EditRow() error {
_, err := sq.
Update(monitorTable).
SetMap(sq.Eq{
"name": m.Name,
"type": m.Type,
"target": m.Target,
"second": m.Second,
"times": m.Times,
"silent_cycle": m.SilentCycle,
"notify_type": m.NotifyType,
"notify_target": m.NotifyTarget,
"description": m.Description,
"success_script": m.SuccessScript,
"fail_script": m.FailScript,
"success_server_id": m.SuccessServerID,
"fail_server_id": m.FailServerID,
2020-08-04 14:28:25 +08:00
}).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
}
func (m Monitor) ToggleState() error {
_, err := sq.
Update(monitorTable).
SetMap(sq.Eq{
2022-09-15 11:50:05 +08:00
"state": m.State,
2020-08-04 14:28:25 +08:00
}).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
}
func (m Monitor) DeleteRow() error {
_, err := sq.
Delete(monitorTable).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
}
2020-09-11 15:09:38 +08:00
func (m Monitor) TurnOff(errorContent string) error {
_, err := sq.
Update(monitorTable).
SetMap(sq.Eq{
2021-10-15 15:58:24 +08:00
"state": Disable,
2020-09-11 15:09:38 +08:00
"error_content": errorContent,
}).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
2021-10-15 15:58:24 +08:00
}
2021-11-20 15:51:19 +08:00
2021-12-09 16:25:30 +08:00
func (m Monitor) Notify(errMsg string) (string, error) {
var err error
var resp *http.Response
2021-11-20 15:51:19 +08:00
if m.NotifyType == NotifyWeiXin {
type markdown struct {
Content string `json:"content"`
}
type message struct {
Msgtype string `json:"msgtype"`
Markdown markdown `json:"markdown"`
}
content := "Monitor: <font color=\"warning\">" + m.Name + "</font>\n "
content += "> <font color=\"warning\">can not access</font> \n "
2021-12-09 16:25:30 +08:00
content += "> <font color=\"comment\">" + errMsg + "</font> \n "
2021-11-20 15:51:19 +08:00
msg := message{
Msgtype: "markdown",
Markdown: markdown{
Content: content,
},
}
b, _ := json.Marshal(msg)
2021-12-09 16:25:30 +08:00
resp, err = http.Post(m.NotifyTarget, "application/json", bytes.NewBuffer(b))
2021-11-20 15:51:19 +08:00
} else if m.NotifyType == NotifyDingTalk {
type markdown struct {
Title string `json:"title"`
Text string `json:"text"`
}
type message struct {
Msgtype string `json:"msgtype"`
Markdown markdown `json:"markdown"`
}
2021-12-09 16:25:30 +08:00
text := "#### Monitor: " + m.Name + " can not access \n >" + errMsg
2021-11-20 15:51:19 +08:00
msg := message{
Msgtype: "markdown",
Markdown: markdown{
Title: m.Name,
Text: text,
},
}
b, _ := json.Marshal(msg)
2021-12-09 16:25:30 +08:00
resp, err = http.Post(m.NotifyTarget, "application/json", bytes.NewBuffer(b))
2021-11-20 15:51:19 +08:00
} else if m.NotifyType == NotifyFeiShu {
2021-12-14 17:53:30 +08:00
type content struct {
Text string `json:"text"`
}
2021-11-20 15:51:19 +08:00
type message struct {
2021-12-14 17:53:30 +08:00
MsgType string `json:"msg_type"`
Content content `json:"content"`
2021-11-20 15:51:19 +08:00
}
2022-07-21 15:02:22 +08:00
text := m.Name + " can not access\n "
2021-12-09 16:25:30 +08:00
text += "detail: " + errMsg
2021-11-20 15:51:19 +08:00
msg := message{
2021-12-14 17:53:30 +08:00
MsgType: "text",
Content: content{
Text: text,
},
2021-11-20 15:51:19 +08:00
}
b, _ := json.Marshal(msg)
2021-12-09 16:25:30 +08:00
resp, err = http.Post(m.NotifyTarget, "application/json", bytes.NewBuffer(b))
2021-11-20 15:51:19 +08:00
} else if m.NotifyType == NotifyCustom {
type message struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
MonitorName string `json:"monitorName"`
2022-04-29 14:59:18 +08:00
Type int `json:"type"`
Target string `json:"target"`
2021-11-20 15:51:19 +08:00
Second int `json:"second"`
Times uint16 `json:"times"`
2022-07-21 15:02:22 +08:00
Error string `json:"error"`
2021-11-20 15:51:19 +08:00
} `json:"data"`
}
code := 0
msg := message{
Code: code,
2022-07-21 15:02:22 +08:00
Message: m.Name + " can not access",
2021-11-20 15:51:19 +08:00
}
msg.Data.MonitorName = m.Name
2022-04-29 14:59:18 +08:00
msg.Data.Type = m.Type
msg.Data.Target = m.Target
2021-11-20 15:51:19 +08:00
msg.Data.Second = m.Second
msg.Data.Times = m.Times
2022-07-21 15:02:22 +08:00
msg.Data.Error = errMsg
2021-11-20 15:51:19 +08:00
b, _ := json.Marshal(msg)
2021-12-09 16:25:30 +08:00
resp, err = http.Post(m.NotifyTarget, "application/json", bytes.NewBuffer(b))
}
2022-04-29 14:59:18 +08:00
if err != nil {
return "", err
}
2021-12-09 16:25:30 +08:00
defer resp.Body.Close()
2023-03-06 12:28:22 +08:00
responseData, err := io.ReadAll(resp.Body)
2021-12-09 16:25:30 +08:00
if err != nil {
return "", err
} else {
return string(responseData), err
2021-11-20 15:51:19 +08:00
}
}