diff --git a/src/bean/entity/alert_overview.go b/src/bean/entity/alert_overview.go index b29b092a7b1a10532b0feaddb3372ea53729e366..44320cd789bb216da27865d124b8792b47095fb6 100644 --- a/src/bean/entity/alert_overview.go +++ b/src/bean/entity/alert_overview.go @@ -22,26 +22,31 @@ type AlertArray struct { } type RiskLevelDistribution struct { - Name string `json:"name"` // 名称 - RiskLevel int `json:"risk_level"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 - Percentage string `json:"percentage"` // 百分比 + Name string `json:"name"` // 名称 + //RiskLevel int `json:"risk_level"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 + //Percentage string `json:"percentage"` // 百分比 + Value int `json:"value"` } type AlertStatusDistribution struct { - Name string `json:"name"` // 名称 - Status int `json:"status"` // 状态,1:已恢复 2:未恢复 3:已关闭 - Percentage string `json:"percentage"` // 百分比 + Name string `json:"name"` // 名称 + //Status int `json:"status"` // 状态,1:已恢复 2:未恢复 3:已关闭 + //Percentage string `json:"percentage"` // 百分比 + Value int `json:"value"` } type AlertClassDistribution struct { - Name string `json:"name"` // 名称 - ClassId int `json:"class_id"` // 预警分类id - Percentage string `json:"percentage"` // 百分比 + Name string `json:"name"` // 名称 + //ClassId int `json:"class_id"` // 预警分类id + //Percentage string `json:"percentage"` // 百分比 + Value int `json:"value"` } type AlertFrequencyDistribution struct { - Name string `json:"name"` // 名称 - Count int `json:"count"` // 数量 + //Name string `json:"name"` // 名称 + //Value int `json:"count"` // 数量 + XAxis []string `json:"xAxis"` + Data []int `json:"data"` } func (a *AlertOverview) TableName() string { diff --git a/src/bean/vo/request/task_manage.go b/src/bean/vo/request/task_manage.go index 29bfaf25439ac3e442608905356451172b779883..65b91ce076b8b9ce164d7aca64398bf240f89c4b 100644 --- a/src/bean/vo/request/task_manage.go +++ b/src/bean/vo/request/task_manage.go @@ -33,10 +33,10 @@ type ListTaskManageReq 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 - Type int `json:"type" binding:"oneof=1 2"` //脚本额外变量类型1yaml 2json - Value string `json:"value"` //脚本额外变量值 - Script string `json:"script"` //执行脚本 - YmlFileName string `json:"yml_file_name"` //执行脚本url + Type int `form:"type" binding:"oneof=1 2"` //脚本额外变量类型1yaml 2json + Value string `form:"value"` //脚本额外变量值 + Script string `form:"script"` //执行脚本 + YmlFileName string `form:"yml_file_name"` //执行脚本url } diff --git a/src/bean/vo/response/alert_overview.go b/src/bean/vo/response/alert_overview.go index 3885be2cd60085b3a1251c4926364d2cfcc04bce..ec2615e3106d669e23e388ff514e976b4ed57db4 100644 --- a/src/bean/vo/response/alert_overview.go +++ b/src/bean/vo/response/alert_overview.go @@ -5,11 +5,11 @@ import ( ) type AlertOverviewItem struct { - AlertOverview []entity.AlertOverview `json:"alert_overview"` - RiskLevelDistribution []entity.RiskLevelDistribution `json:"risk_level_distribution"` - AlertStatusDistribution []entity.AlertStatusDistribution `json:"alert_status_distribution"` - AlertClassDistribution []entity.AlertClassDistribution `json:"alert_class_distribution"` - AlertFrequencyDistribution []entity.AlertFrequencyDistribution `json:"alert_frequency_distribution"` + AlertOverview []entity.AlertOverview `json:"alert_overview"` + RiskLevelDistribution []entity.RiskLevelDistribution `json:"risk_level_distribution"` + AlertStatusDistribution []entity.AlertStatusDistribution `json:"alert_status_distribution"` + AlertClassDistribution []entity.AlertClassDistribution `json:"alert_class_distribution"` + AlertFrequencyDistribution entity.AlertFrequencyDistribution `json:"alert_frequency_distribution"` } type AlertOverviewList struct { diff --git a/src/controller/task_manage.go b/src/controller/task_manage.go index 7cb10019a6301dd3e79434bdc424e3132ac96355..a1d9433aa13174897d7d94602dd87f45811ff866 100644 --- a/src/controller/task_manage.go +++ b/src/controller/task_manage.go @@ -1,7 +1,10 @@ package controller import ( + "encoding/json" "errors" + "fmt" + "github.com/ghodss/yaml" "github.com/gin-gonic/gin" "github.com/minio/minio-go/v7" "github.com/spf13/cast" @@ -123,7 +126,7 @@ func ListTaskManage(c *gin.Context) { func ExecScript(c *gin.Context) { 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) return } @@ -153,11 +156,50 @@ func ExecScript(c *gin.Context) { 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{} - err := taskManageSvc.ExecScript(req) + id, err := taskManageSvc.ExecScript(req) if err != nil { SendJsonResponse(c, err, nil) return } - SendJsonResponse(c, nil, nil) + SendJsonResponse(c, nil, id) } diff --git a/src/service/alert_overview.go b/src/service/alert_overview.go index 1d8cc722581daf6a22759222df0d38aac396afdc..4a70ec18c7c581df1508c48a920e8b90603fc6ac 100644 --- a/src/service/alert_overview.go +++ b/src/service/alert_overview.go @@ -187,93 +187,53 @@ func (a *AlertOverviewSvc) Overview(req request.DetailAlertOverview) (resp respo }, RiskLevelDistribution: []entity.RiskLevelDistribution{ { - Name: "重大风险", - RiskLevel: 4, - Percentage: "10%", + Name: "重大风险", + Value: 1, }, { - Name: "较大风险", - RiskLevel: 3, - Percentage: "20%", + Name: "较大风险", + Value: 2, }, { - Name: "一般风险", - RiskLevel: 2, - Percentage: "30%", + Name: "一般风险", + Value: 3, }, { - Name: "低风险", - RiskLevel: 1, - Percentage: "40%", + Name: "低风险", + Value: 4, }, }, AlertStatusDistribution: []entity.AlertStatusDistribution{ { - Name: "未恢复", - Status: 1, - Percentage: "30%", + Name: "未恢复", + Value: 4, }, { - Name: "已恢复", - Status: 2, - Percentage: "70%", + Name: "已恢复", + Value: 6, }, }, AlertClassDistribution: []entity.AlertClassDistribution{ { - Name: "容器集群", - ClassId: 1, - Percentage: "30%", + Name: "容器集群", + Value: 1, }, { - Name: "容器节点", - ClassId: 2, - Percentage: "20%", + Name: "容器节点", + Value: 2, }, { - Name: "容器组", - ClassId: 3, - Percentage: "30%", + Name: "容器组", + Value: 3, }, { - Name: "网关", - ClassId: 4, - Percentage: "20%", + Name: "网关", + Value: 4, }, }, - AlertFrequencyDistribution: []entity.AlertFrequencyDistribution{ - { - Name: "0-3时", - 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, - }, + AlertFrequencyDistribution: entity.AlertFrequencyDistribution{ + XAxis: []string{"0-3时", "3-6时", "6-9时", "9-12时", "12-15时", "15-18时", "18-21时", "21-24时"}, + Data: []int{12, 20, 11, 50, 60, 30, 16, 6}, }, } diff --git a/src/service/task_history.go b/src/service/task_history.go index 9d7f6e22851224854cc5fe0671a97b6f44357d0a..1aebfdab4bee8424687ab329b3d79d5208aecbbc 100644 --- a/src/service/task_history.go +++ b/src/service/task_history.go @@ -154,7 +154,7 @@ func AddExecHistory(req request.AddExecHistory) (id int, err error) { err = resp.DbConnectError.WithError(err) 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 { err = resp.DbSelectError.WithError(err) return diff --git a/src/service/task_manage.go b/src/service/task_manage.go index 67fa7addba57af79bb6d7206444bf9b974934b50..28e4dcb71c3653e70871e92a398a56d0172d7cf5 100644 --- a/src/service/task_manage.go +++ b/src/service/task_manage.go @@ -182,7 +182,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int 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 script map[string]interface{} //j2, err := yaml.YAMLToJSON([]byte(req.Script)) @@ -207,32 +207,20 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { //写入执行脚本 f2, err := os.Create("/etc/ansible/ansible.yml") if err != nil { - return resp.FileExecError.WithError(err) + err = resp.FileExecError.WithError(err) + return } defer f2.Close() _, err = f2.Write([]byte(req.Script)) if err != nil { - return resp.FileExecError.WithError(err) - } - //写入额外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") + err = resp.FileExecError.WithError(err) + return } //新增任务历史 - id, err := AddExecHistory(request.AddExecHistory{ + id, err = AddExecHistory(request.AddExecHistory{ TaskId: req.TaskId, - CreateUser: t.User.SystemAccount, + CreateUser: "", }) if err != nil { return @@ -248,22 +236,25 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { //捕获正常日志 stdout, err := cmd.StdoutPipe() if err != nil { - return resp.CmdExecError.WithError(err) + err = resp.CmdExecError.WithError(err) + return } //捕获异常日志 stderr, err := cmd.StderrPipe() if err != nil { - return resp.CmdExecError.WithError(err) + err = resp.CmdExecError.WithError(err) + return } //执行cmd命令 if err = cmd.Start(); err != nil { - return resp.CmdExecError.WithError(err) + err = resp.CmdExecError.WithError(err) + return } //获取 正常/异常 输出流 outputBuf := bufio.NewReader(stdout) readerr := bufio.NewReader(stderr) - var out, outErr, errFlag int + var out, outErr int var execLog string for { @@ -286,7 +277,6 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { outErr = 1 } else if outErr == 0 { //存储异常执行日志 - errFlag = 1 execLog = execLog + lineErr + " \n " UpdateExecHistory(request.UpdateExecHistory{ TaskHistoryId: id, @@ -295,25 +285,25 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) { } 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 } } 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 }