diff --git a/README.md b/README.md index d2668ad..cdaba9e 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,17 @@ V1.x版本是一个简单的定时任务管理系统,进入V1.0 :https://git - 运行 go build - 运行 ./run.sh start|stop +mac +- 运行 ./package.sh -a amd64 -p darwin -v v2.x.0 + +linux +- 运行 ./package.sh -a 386 -p linux -v v2.x.0 +- 运行 ./package.sh -a amd64 -p linux -v v2.x.0 + +windows +- 运行 ./package.sh -a amd64 -p windows -v v2.x.0 + + 方法二、直接使用 linux @@ -79,9 +90,8 @@ mac windows - 进入 https://github.com/george518/PPGo_Job/releases -- 下载 ppgo_job-linux-2.x.0.zip 并解压 +- 下载 ppgo_job-windows-2.x.0.zip 并解压 - 进入文件夹,设置好数据库(创建数据库,导入ppgo_job2.sql)和配置文件(conf/app.conf) -- 运行 go build - 运行 run.bat ---- diff --git a/controllers/notify_tpl.go b/controllers/notify_tpl.go index fbf1e05..95aaecb 100644 --- a/controllers/notify_tpl.go +++ b/controllers/notify_tpl.go @@ -59,7 +59,7 @@ func (self *NotifyTplController) AjaxSave() { notifyTpl.Type = models.NotifyTplTypeDefault notifyTpl.Status, _ = self.GetInt("status") - if notifyTpl.TplType == 1 || notifyTpl.TplType == 3 { + if notifyTpl.TplType == 1 || notifyTpl.TplType == 2 || notifyTpl.TplType == 3 { m := make(map[string]string) err := json.Unmarshal([]byte(notifyTpl.Content), &m) if err != nil { @@ -85,7 +85,7 @@ func (self *NotifyTplController) AjaxSave() { notifyTpl.Content = strings.TrimSpace(self.GetString("content")) notifyTpl.Status, _ = self.GetInt("status") - if notifyTpl.TplType == 1 || notifyTpl.TplType == 3 { + if notifyTpl.TplType == 1 || notifyTpl.TplType == 2 || notifyTpl.TplType == 3 { m := make(map[string]string) err := json.Unmarshal([]byte(notifyTpl.Content), &m) if err != nil { diff --git a/controllers/server.go b/controllers/server.go index f5109b7..71ef930 100644 --- a/controllers/server.go +++ b/controllers/server.go @@ -26,6 +26,7 @@ type ServerController struct { func (self *ServerController) List() { self.Data["pageTitle"] = "资源管理" + self.Data["serverGroup"] = serverGroupLists(self.serverGroups, self.userId) self.display() } @@ -368,6 +369,9 @@ func (self *ServerController) Table() { if err != nil { limit = 30 } + + serverGroupId, err := self.GetInt("serverGroupId") + serverName := strings.TrimSpace(self.GetString("serverName")) StatusText := []string{ "正常", @@ -390,16 +394,28 @@ func (self *ServerController) Table() { //查询条件 filters := make([]interface{}, 0) filters = append(filters, "status", 0) + + groupsIds := make([]int, 0) if self.userId != 1 { groups := strings.Split(self.serverGroups, ",") - groupsIds := make([]int, 0) for _, v := range groups { id, _ := strconv.Atoi(v) - groupsIds = append(groupsIds, id) + if serverGroupId > 0 { + if id == serverGroupId { + groupsIds = append(groupsIds, id) + break + } + } else { + groupsIds = append(groupsIds, id) + } } filters = append(filters, "group_id__in", groupsIds) + } else if serverGroupId > 0 { + groupsIds = append(groupsIds, serverGroupId) + filters = append(filters, "group_id__in", groupsIds) } + if serverName != "" { filters = append(filters, "server_name__icontains", serverName) } diff --git a/jobs/job.go b/jobs/job.go index 9539c43..7ab9c99 100644 --- a/jobs/job.go +++ b/jobs/job.go @@ -240,28 +240,24 @@ func RemoteCommandJobByTelnetPassword(id int, name string, command string, serve defer conn.Close() buf := make([]byte, 4096) - _, err = conn.Read(buf) - if err != nil { + + if _, err = conn.Read(buf); err != nil { return "", "", err, false } - _, err = conn.Write([]byte(servers.ServerAccount + "\r\n")) - if err != nil { + if _, err = conn.Write([]byte(servers.ServerAccount + "\r\n")); err != nil { return "", "", err, false } - _, err = conn.Read(buf) - if err != nil { + if _, err = conn.Read(buf); err != nil { return "", "", err, false } - _, err = conn.Write([]byte(servers.Password + "\r\n")) - if err != nil { + if _, err = conn.Write([]byte(servers.Password + "\r\n")); err != nil { return "", "", err, false } - _, err = conn.Read(buf) - if err != nil { + if _, err = conn.Read(buf); err != nil { return "", "", err, false } @@ -392,7 +388,7 @@ func (j *Job) Run() { status := log.Status + 2 - title, content := "", "" + title, content, taskOutput, errOutput := "", "", "", "" notifyTpl, err := models.NotifyTplGetById(j.task.NotifyTplId) if err != nil { @@ -406,22 +402,31 @@ func (j *Job) Run() { content = notifyTpl.Content } + taskOutput = strings.Replace(log.Output, "\n", " ", -1) + taskOutput = strings.Replace(taskOutput, "\"", "\\\"", -1) + errOutput = strings.Replace(log.Error, "\n", " ", -1) + errOutput = strings.Replace(errOutput, "\"", "\\\"", -1) + if title != "" { title = strings.Replace(title, "{{TaskId}}", strconv.Itoa(j.task.Id), -1) title = strings.Replace(title, "{{TaskName}}", j.task.TaskName, -1) + title = strings.Replace(title, "{{ExecuteCommand}}", j.task.Command, -1) title = strings.Replace(title, "{{ExecuteTime}}", beego.Date(time.Unix(log.CreateTime, 0), "Y-m-d H:i:s"), -1) title = strings.Replace(title, "{{ProcessTime}}", strconv.FormatFloat(float64(log.ProcessTime)/1000, 'f', 6, 64), -1) title = strings.Replace(title, "{{ExecuteStatus}}", TextStatus[status], -1) - title = strings.Replace(title, "{{TaskOutput}}", log.Error, -1) + title = strings.Replace(title, "{{TaskOutput}}", taskOutput, -1) + title = strings.Replace(title, "{{ErrorOutput}}", errOutput, -1) } if content != "" { content = strings.Replace(content, "{{TaskId}}", strconv.Itoa(j.task.Id), -1) content = strings.Replace(content, "{{TaskName}}", j.task.TaskName, -1) + content = strings.Replace(content, "{{ExecuteCommand}}", j.task.Command, -1) content = strings.Replace(content, "{{ExecuteTime}}", beego.Date(time.Unix(log.CreateTime, 0), "Y-m-d H:i:s"), -1) content = strings.Replace(content, "{{ProcessTime}}", strconv.FormatFloat(float64(log.ProcessTime)/1000, 'f', 6, 64), -1) content = strings.Replace(content, "{{ExecuteStatus}}", TextStatus[status], -1) - content = strings.Replace(content, "{{TaskOutput}}", log.Error, -1) + content = strings.Replace(content, "{{TaskOutput}}", taskOutput, -1) + content = strings.Replace(content, "{{ErrorOutput}}", errOutput, -1) } if j.task.NotifyType == 0 && toEmail != "" { @@ -446,12 +451,20 @@ func (j *Job) Run() { } } else if j.task.NotifyType == 2 && len(dingtalk) > 0 { //钉钉 - ok := notify.SendDingtalkToChan(dingtalk, content) + param := make(map[string]interface{}) + + err := json.Unmarshal([]byte(content), ¶m) + if err != nil { + fmt.Println("发送钉钉错误", err) + return + } + + ok := notify.SendDingtalkToChan(dingtalk, param) if !ok { fmt.Println("发送钉钉错误", dingtalk) } } else if j.task.NotifyType == 3 && len(wechat) > 0 { - //信息 + //微信 param := make(map[string]string) err := json.Unmarshal([]byte(content), ¶m) if err != nil { diff --git a/notify/dingtalk.go b/notify/dingtalk.go index 4675860..7f3e05f 100644 --- a/notify/dingtalk.go +++ b/notify/dingtalk.go @@ -17,18 +17,9 @@ import ( "bytes" ) -type Msg struct { - MsgType string `json:"msgtype"` - Text *Text `json:"text"` -} - -type Text struct { - Content string `json:"content"` -} - type Dingtalk struct { Dingtalks map[string]string - Content string + Content map[string]interface{} } var DingtalkChan chan *Dingtalk @@ -57,7 +48,7 @@ func init() { } -func SendDingtalkToChan(dingtalks map[string]string, content string) bool { +func SendDingtalkToChan(dingtalks map[string]string, content map[string]interface{}) bool { dingTalk := &Dingtalk{ Dingtalks: dingtalks, Content: content, @@ -74,13 +65,7 @@ func SendDingtalkToChan(dingtalks map[string]string, content string) bool { func (s *Dingtalk) SendDingtalk() error { for _, v := range s.Dingtalks { - - msg := Msg{MsgType: "text"} - text := new(Text) - text.Content = s.Content - msg.Text = text - - body, err := json.Marshal(msg) + body, err := json.Marshal(s.Content) if err != nil { log.Println(err) return err diff --git a/package.sh b/package.sh index 71b1147..f5d7dff 100755 --- a/package.sh +++ b/package.sh @@ -43,6 +43,13 @@ SUPPORT_ARCH=(386 amd64) LDFLAGS='' # 需要打包的文件 INCLUDE_FILE=() +# linux需要打包的文件 +INCLUDE_LINUX_FILE=() +# darwin需要打包的文件 +INCLUDE_DARWIN_FILE=() +# windows需要打包的文件 +INCLUDE_WINDOWS_FILE=() + # 打包文件生成目录 PACKAGE_DIR='' # 编译文件生成目录 @@ -164,6 +171,22 @@ package_file() { for item in "${INCLUDE_FILE[@]}"; do cp -r ../${item} $1 done + + for OS in "${INPUT_OS[@]}";do + if [[ "${OS}" = "linux" ]];then + for item in "${INCLUDE_LINUX_FILE[@]}"; do + cp -r ../${item} $1 + done + elif [[ "${OS}" = "darwin" ]];then + for item in "${INCLUDE_DARWIN_FILE[@]}"; do + cp -r ../${item} $1 + done + elif [[ "${OS}" = "windows" ]];then + for item in "${INCLUDE_WINDOWS_FILE[@]}"; do + cp -r ../${item} $1 + done + fi + done } # 清理 @@ -184,7 +207,10 @@ run() { package_ppgo_job() { BINARY_NAME='PPGo_Job' MAIN_FILE="./main.go" - INCLUDE_FILE=("conf" "static" "views" "ppgo_job2.sql" "run.sh" "run.bat") + INCLUDE_FILE=("conf" "static" "views" "ppgo_job2.sql") + INCLUDE_LINUX_FILE=("run.sh") + INCLUDE_DARWIN_FILE=("run.sh") + INCLUDE_WINDOWS_FILE=("run.bat") run } diff --git a/ppgo_job2.sql b/ppgo_job2.sql index 35447b3..0104b78 100644 --- a/ppgo_job2.sql +++ b/ppgo_job2.sql @@ -312,10 +312,10 @@ CREATE TABLE `pp_notify_tpl` ( -- 转存表中的数据 `pp_notify_tpl` -- BEGIN; -INSERT INTO `pp_notify_tpl` VALUES(1, 'system', '默认邮箱通知模板', 0, '定时任务异常:{{TaskName}}', 'Hello,定时任务出问题了:\r\n

任务执行详情:

\r\n

\r\n任务 ID:{{TaskId}}
\r\n任务名称:{{TaskName}}
\r\n执行时间:{{ExecuteTime}}
\r\n执行耗时:{{ProcessTime}}秒
\r\n执行状态:{{ExecuteStatus}}\r\n

\r\n

任务执行输出

\r\n

\r\n{{TaskOutput}}\r\n

', 1, 1550255030, 1, 1550338305, 1); -INSERT INTO `pp_notify_tpl` VALUES(2, 'system', '默认短信通知模板', 1, '', '{\r\n \"task_id\": \"{{TaskId}}\",\r\n \"task_name\": \"{{TaskName}}\",\r\n \"execute_status\": \"{{ExecuteStatus}}\"\r\n}', 1, 1550255030, 1, 1550338215, 1); -INSERT INTO `pp_notify_tpl` VALUES(3, 'system', '默认钉钉通知模板', 2, '', '任务执行异常详情:\r\n任务 ID:{{TaskId}}\r\n任务名称:{{TaskName}}\r\n执行时间:{{ExecuteTime}}\r\n执行耗时:{{ProcessTime}}秒\r\n执行状态:{{ExecuteStatus}}\r\n任务执行输出:\r\n{{TaskOutput}}', 1, 1550255030, 1, 1550338880, 1); -INSERT INTO `pp_notify_tpl` VALUES(4, 'system', '默认微信通知模板', 3, '', '{\r\n \"task_id\": \"{{TaskId}}\",\r\n \"task_name\": \"{{TaskName}}\",\r\n \"execute_status\": \"{{ExecuteStatus}}\"\r\n}', 1, 1550347183, 1, 1550347201, 1); +INSERT INTO `pp_notify_tpl` VALUES(1, 'system', '默认邮箱通知模板', 0, '定时任务异常:{{TaskName}}', 'Hello,定时任务出问题了:\r\n

任务执行详情:

\r\n

\r\n任务 ID:{{TaskId}}
\r\n任务名称:{{TaskName}}
\r\n执行命令:{{ExecuteCommand}}
\r\n执行时间:{{ExecuteTime}}
\r\n执行耗时:{{ProcessTime}}秒
\r\n执行状态:{{ExecuteStatus}}\r\n

\r\n

任务执行输出

\r\n

\r\n{{TaskOutput}}\r\n

\r\n

错误输出

\r\n

\r\n{{ErrorOutput}}\r\n

', 1, 1550255030, 1, 1553282382, 1); +INSERT INTO `pp_notify_tpl` VALUES(2, 'system', '默认短信通知模板', 1, '', '{\r\n \"task_id\": \"{{TaskId}}\",\r\n \"task_name\": \"{{TaskName}}\",\r\n \"execute_command\": \"{{ExecuteCommand}}\",\r\n \"execute_status\": \"{{ExecuteStatus}}\"\r\n}', 1, 1550255030, 1, 1550338215, 1); +INSERT INTO `pp_notify_tpl` VALUES(3, 'system', '默认钉钉通知模板', 2, '', '{\r\n \"msgtype\": \"text\",\r\n \"text\": {\r\n \"content\": \"任务执行异常详情:\\n任务 ID:{{TaskId}}\\n任务名称:{{TaskName}}\\n执行命令:{{ExecuteCommand}}\\n执行时间:{{ExecuteTime}}\\n执行耗时:{{ProcessTime}}秒\\n执行状态:{{ExecuteStatus}}\\n任务执行输出:\\n{{TaskOutput}}\\n错误输出:\\n{{ErrorOutput}}\"\r\n }\r\n}', 1, 1550255030, 1, 1553282245, 1); +INSERT INTO `pp_notify_tpl` VALUES(4, 'system', '默认微信通知模板', 3, '', '{\r\n \"task_id\": \"{{TaskId}}\",\r\n \"task_name\": \"{{TaskName}}\",\r\n \"execute_command\": \"{{ExecuteCommand}}\",\r\n \"execute_status\": \"{{ExecuteStatus}}\"\r\n}', 1, 1550347183, 1, 1550347201, 1); COMMIT; BEGIN; diff --git a/views/admin/add.html b/views/admin/add.html index 5af1064..c28d40e 100644 --- a/views/admin/add.html +++ b/views/admin/add.html @@ -40,14 +40,14 @@
- +
- +
diff --git a/views/admin/edit.html b/views/admin/edit.html index 7277425..0de4d1f 100644 --- a/views/admin/edit.html +++ b/views/admin/edit.html @@ -35,14 +35,14 @@
- +
- +
diff --git a/views/notifytpl/add.html b/views/notifytpl/add.html index 409f60b..a259f6c 100644 --- a/views/notifytpl/add.html +++ b/views/notifytpl/add.html @@ -41,10 +41,12 @@ {{"任务 ID {{TaskId}}"}} {{"任务名称 {{TaskName}}"}} + {{"执行命令 {{ExecuteCommand}}"}} {{"执行时间 {{ExecuteTime}}"}} {{"执行耗时 {{ProcessTime}}"}} {{"执行状态 {{ExecuteStatus}}"}} {{"任务输出 {{TaskOutput}}"}} + {{"错误输出 {{ErrorOutput}}"}} diff --git a/views/notifytpl/edit.html b/views/notifytpl/edit.html index f44d0b3..5997ea8 100644 --- a/views/notifytpl/edit.html +++ b/views/notifytpl/edit.html @@ -42,10 +42,12 @@ {{"任务 ID {{TaskId}}"}} {{"任务名称 {{TaskName}}"}} + {{"执行命令 {{ExecuteCommand}}"}} {{"执行时间 {{ExecuteTime}}"}} {{"执行耗时 {{ProcessTime}}"}} {{"执行状态 {{ExecuteStatus}}"}} {{"任务输出 {{TaskOutput}}"}} + {{"错误输出 {{ErrorOutput}}"}} diff --git a/views/server/list.html b/views/server/list.html index ee7ea6a..045dd03 100644 --- a/views/server/list.html +++ b/views/server/list.html @@ -9,6 +9,14 @@
+
+ +
@@ -64,6 +72,7 @@ table.reload('listReload', { where: { serverName: $('#serverName').val(), + serverGroupId:$('#serverGroupId').val(), } }); } diff --git a/views/user/edit.html b/views/user/edit.html index bf5ec6c..29d29af 100644 --- a/views/user/edit.html +++ b/views/user/edit.html @@ -36,14 +36,14 @@
- +
- +