Commit 4d9c2fca authored by 黄智's avatar 黄智

Merge remote-tracking branch 'origin/dev' into dev

parents 6880b83c 44444026
...@@ -22,26 +22,31 @@ type AlertArray struct { ...@@ -22,26 +22,31 @@ type AlertArray struct {
} }
type RiskLevelDistribution struct { type RiskLevelDistribution struct {
Name string `json:"name"` // 名称 Name string `json:"name"` // 名称
RiskLevel int `json:"risk_level"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 //RiskLevel int `json:"risk_level"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险
Percentage string `json:"percentage"` // 百分比 //Percentage string `json:"percentage"` // 百分比
Value int `json:"value"`
} }
type AlertStatusDistribution struct { type AlertStatusDistribution struct {
Name string `json:"name"` // 名称 Name string `json:"name"` // 名称
Status int `json:"status"` // 状态,1:已恢复 2:未恢复 3:已关闭 //Status int `json:"status"` // 状态,1:已恢复 2:未恢复 3:已关闭
Percentage string `json:"percentage"` // 百分比 //Percentage string `json:"percentage"` // 百分比
Value int `json:"value"`
} }
type AlertClassDistribution struct { type AlertClassDistribution struct {
Name string `json:"name"` // 名称 Name string `json:"name"` // 名称
ClassId int `json:"class_id"` // 预警分类id //ClassId int `json:"class_id"` // 预警分类id
Percentage string `json:"percentage"` // 百分比 //Percentage string `json:"percentage"` // 百分比
Value int `json:"value"`
} }
type AlertFrequencyDistribution struct { type AlertFrequencyDistribution struct {
Name string `json:"name"` // 名称 //Name string `json:"name"` // 名称
Count int `json:"count"` // 数量 //Value int `json:"count"` // 数量
XAxis []string `json:"xAxis"`
Data []int `json:"data"`
} }
func (a *AlertOverview) TableName() string { func (a *AlertOverview) TableName() string {
......
...@@ -33,10 +33,10 @@ type ListTaskManageReq struct { ...@@ -33,10 +33,10 @@ type ListTaskManageReq struct {
} }
type ExecScriptReq struct { type ExecScriptReq struct {
TaskId int `json:"task_id" binding:"required"` //主机分组id TaskId int `form:"task_id" binding:"required"` //任务id
HostGroupId int `form:"host_group_id" binding:"required"` //主机分组id HostGroupId int `form:"host_group_id" binding:"required"` //主机分组id
Type int `json:"type" binding:"oneof=1 2"` //脚本额外变量类型1yaml 2json Type int `form:"type" binding:"oneof=1 2"` //脚本额外变量类型1yaml 2json
Value string `json:"value"` //脚本额外变量值 Value string `form:"value"` //脚本额外变量值
Script string `json:"script"` //执行脚本 Script string `form:"script"` //执行脚本
YmlFileName string `json:"yml_file_name"` //执行脚本url YmlFileName string `form:"yml_file_name"` //执行脚本url
} }
...@@ -5,11 +5,11 @@ import ( ...@@ -5,11 +5,11 @@ import (
) )
type AlertOverviewItem struct { type AlertOverviewItem struct {
AlertOverview []entity.AlertOverview `json:"alert_overview"` AlertOverview []entity.AlertOverview `json:"alert_overview"`
RiskLevelDistribution []entity.RiskLevelDistribution `json:"risk_level_distribution"` RiskLevelDistribution []entity.RiskLevelDistribution `json:"risk_level_distribution"`
AlertStatusDistribution []entity.AlertStatusDistribution `json:"alert_status_distribution"` AlertStatusDistribution []entity.AlertStatusDistribution `json:"alert_status_distribution"`
AlertClassDistribution []entity.AlertClassDistribution `json:"alert_class_distribution"` AlertClassDistribution []entity.AlertClassDistribution `json:"alert_class_distribution"`
AlertFrequencyDistribution []entity.AlertFrequencyDistribution `json:"alert_frequency_distribution"` AlertFrequencyDistribution entity.AlertFrequencyDistribution `json:"alert_frequency_distribution"`
} }
type AlertOverviewList struct { type AlertOverviewList struct {
......
package controller package controller
import ( import (
"encoding/json"
"errors" "errors"
"fmt"
"github.com/ghodss/yaml"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/spf13/cast" "github.com/spf13/cast"
...@@ -123,7 +126,7 @@ func ListTaskManage(c *gin.Context) { ...@@ -123,7 +126,7 @@ func ListTaskManage(c *gin.Context) {
func ExecScript(c *gin.Context) { func ExecScript(c *gin.Context) {
var req request.ExecScriptReq var req request.ExecScriptReq
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return return
} }
...@@ -153,11 +156,50 @@ func ExecScript(c *gin.Context) { ...@@ -153,11 +156,50 @@ func ExecScript(c *gin.Context) {
req.Script = string(obj) req.Script = string(obj)
} }
//写入额外yml参数
if req.Type == 1 {
//var scripts []map[string]interface{}
var value map[string]interface{}
j2, err := yaml.YAMLToJSON([]byte(req.Script))
if err != nil {
err = resp.YamlAnalysisError.WithError(err)
return
}
err = json.Unmarshal(j2, &value)
if err != nil {
err = resp.InvalidParam.WithError(errors.New("yaml格式错误"))
return
}
value["hosts"] = fmt.Sprintf("%s%d", service.AnsibleGroup, req.HostGroupId)
j, err := json.Marshal(value)
if err != nil {
err = resp.MarshalError.WithError(err)
return
}
req.Value = fmt.Sprintf("%s", j)
//写入执行脚本
//f3, err := os.Create("/etc/ansible/ansible_extra.yml")
//if err != nil {
// err = resp.FileExecError.WithError(err)
// return
//}
//defer f3.Close()
//_, err = f3.Write([]byte(req.Value))
//if err != nil {
// err = resp.FileExecError.WithError(err)
// return
//}
//req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
}
taskManageSvc := service.TaskManageSvc{} taskManageSvc := service.TaskManageSvc{}
err := taskManageSvc.ExecScript(req) id, err := taskManageSvc.ExecScript(req)
if err != nil { if err != nil {
SendJsonResponse(c, err, nil) SendJsonResponse(c, err, nil)
return return
} }
SendJsonResponse(c, nil, nil) SendJsonResponse(c, nil, id)
} }
...@@ -187,93 +187,53 @@ func (a *AlertOverviewSvc) Overview(req request.DetailAlertOverview) (resp respo ...@@ -187,93 +187,53 @@ func (a *AlertOverviewSvc) Overview(req request.DetailAlertOverview) (resp respo
}, },
RiskLevelDistribution: []entity.RiskLevelDistribution{ RiskLevelDistribution: []entity.RiskLevelDistribution{
{ {
Name: "重大风险", Name: "重大风险",
RiskLevel: 4, Value: 1,
Percentage: "10%",
}, },
{ {
Name: "较大风险", Name: "较大风险",
RiskLevel: 3, Value: 2,
Percentage: "20%",
}, },
{ {
Name: "一般风险", Name: "一般风险",
RiskLevel: 2, Value: 3,
Percentage: "30%",
}, },
{ {
Name: "低风险", Name: "低风险",
RiskLevel: 1, Value: 4,
Percentage: "40%",
}, },
}, },
AlertStatusDistribution: []entity.AlertStatusDistribution{ AlertStatusDistribution: []entity.AlertStatusDistribution{
{ {
Name: "未恢复", Name: "未恢复",
Status: 1, Value: 4,
Percentage: "30%",
}, },
{ {
Name: "已恢复", Name: "已恢复",
Status: 2, Value: 6,
Percentage: "70%",
}, },
}, },
AlertClassDistribution: []entity.AlertClassDistribution{ AlertClassDistribution: []entity.AlertClassDistribution{
{ {
Name: "容器集群", Name: "容器集群",
ClassId: 1, Value: 1,
Percentage: "30%",
}, },
{ {
Name: "容器节点", Name: "容器节点",
ClassId: 2, Value: 2,
Percentage: "20%",
}, },
{ {
Name: "容器组", Name: "容器组",
ClassId: 3, Value: 3,
Percentage: "30%",
}, },
{ {
Name: "网关", Name: "网关",
ClassId: 4, Value: 4,
Percentage: "20%",
}, },
}, },
AlertFrequencyDistribution: []entity.AlertFrequencyDistribution{ AlertFrequencyDistribution: entity.AlertFrequencyDistribution{
{ XAxis: []string{"0-3时", "3-6时", "6-9时", "9-12时", "12-15时", "15-18时", "18-21时", "21-24时"},
Name: "0-3时", Data: []int{12, 20, 11, 50, 60, 30, 16, 6},
Count: 12,
},
{
Name: "3-6时",
Count: 20,
},
{
Name: "6-9时",
Count: 11,
},
{
Name: "9-12时",
Count: 50,
},
{
Name: "12-15时",
Count: 60,
},
{
Name: "15-18时",
Count: 30,
},
{
Name: "18-21时",
Count: 16,
},
{
Name: "21-24时",
Count: 6,
},
}, },
} }
......
...@@ -154,7 +154,7 @@ func AddExecHistory(req request.AddExecHistory) (id int, err error) { ...@@ -154,7 +154,7 @@ func AddExecHistory(req request.AddExecHistory) (id int, err error) {
err = resp.DbConnectError.WithError(err) err = resp.DbConnectError.WithError(err)
return return
} }
count, err := db.Table("task_history").Where("id = ?", req.TaskId).Count(&entity.TaskHistory{}) count, err := db.Table("task_history").Where("task_id = ?", req.TaskId).Count(&entity.TaskHistory{})
if err != nil { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
......
...@@ -182,7 +182,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int ...@@ -182,7 +182,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
return return
} }
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (id int, err error) {
//var scripts []map[string]interface{} //var scripts []map[string]interface{}
//var script map[string]interface{} //var script map[string]interface{}
//j2, err := yaml.YAMLToJSON([]byte(req.Script)) //j2, err := yaml.YAMLToJSON([]byte(req.Script))
...@@ -207,32 +207,20 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { ...@@ -207,32 +207,20 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) {
//写入执行脚本 //写入执行脚本
f2, err := os.Create("/etc/ansible/ansible.yml") f2, err := os.Create("/etc/ansible/ansible.yml")
if err != nil { if err != nil {
return resp.FileExecError.WithError(err) err = resp.FileExecError.WithError(err)
return
} }
defer f2.Close() defer f2.Close()
_, err = f2.Write([]byte(req.Script)) _, err = f2.Write([]byte(req.Script))
if err != nil { if err != nil {
return resp.FileExecError.WithError(err) err = resp.FileExecError.WithError(err)
} return
//写入额外yml参数
if req.Type == 1 {
//写入执行脚本
f3, err := os.Create("/etc/ansible/ansible_extra.yml")
if err != nil {
return resp.FileExecError.WithError(err)
}
defer f3.Close()
_, err = f3.Write([]byte(req.Value))
if err != nil {
return resp.FileExecError.WithError(err)
}
req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
} }
//新增任务历史 //新增任务历史
id, err := AddExecHistory(request.AddExecHistory{ id, err = AddExecHistory(request.AddExecHistory{
TaskId: req.TaskId, TaskId: req.TaskId,
CreateUser: t.User.SystemAccount, CreateUser: "",
}) })
if err != nil { if err != nil {
return return
...@@ -248,22 +236,25 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { ...@@ -248,22 +236,25 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) {
//捕获正常日志 //捕获正常日志
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return resp.CmdExecError.WithError(err) err = resp.CmdExecError.WithError(err)
return
} }
//捕获异常日志 //捕获异常日志
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {
return resp.CmdExecError.WithError(err) err = resp.CmdExecError.WithError(err)
return
} }
//执行cmd命令 //执行cmd命令
if err = cmd.Start(); err != nil { if err = cmd.Start(); err != nil {
return resp.CmdExecError.WithError(err) err = resp.CmdExecError.WithError(err)
return
} }
//获取 正常/异常 输出流 //获取 正常/异常 输出流
outputBuf := bufio.NewReader(stdout) outputBuf := bufio.NewReader(stdout)
readerr := bufio.NewReader(stderr) readerr := bufio.NewReader(stderr)
var out, outErr, errFlag int var out, outErr int
var execLog string var execLog string
for { for {
...@@ -286,7 +277,6 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { ...@@ -286,7 +277,6 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) {
outErr = 1 outErr = 1
} else if outErr == 0 { } else if outErr == 0 {
//存储异常执行日志 //存储异常执行日志
errFlag = 1
execLog = execLog + lineErr + " \n " execLog = execLog + lineErr + " \n "
UpdateExecHistory(request.UpdateExecHistory{ UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id, TaskHistoryId: id,
...@@ -295,25 +285,25 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { ...@@ -295,25 +285,25 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) {
} }
if out == 1 && outErr == 1 { if out == 1 && outErr == 1 {
if errFlag == 1 {
//任务执行失败
UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id,
ExecLog: execLog,
State: 2,
})
} else {
//任务执行成功
UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id,
ExecLog: execLog,
State: 1,
})
}
break break
} }
} }
cmd.Wait() cmd.Wait()
if cmd.ProcessState.Success() {
//任务执行成功
UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id,
ExecLog: execLog,
State: 1,
})
} else {
//任务执行失败
UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id,
ExecLog: execLog,
State: 2,
})
}
return return
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment