package main
import (
"bytes"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/MySQL"
"github.com/go-xorm/xorm"
"io/ioutil"
"net/http"
"time"
)
var engine *xorm.Engine
const webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
func dingtoinfo(s string) bool {
content, data := make(map[string]string), make(map[string]interface{})
content["content"] = s
data["msgtype"] = "text"
data["text"] = content
b, _ := json.Marshal(data)
resp, err := http.Post(webhook_url,
"application/json",
bytes.NewBuffer(b))
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
return true
}
func recoverName() {
if r := recover(); r != nil {
res := fmt.Sprintf("%s %s", "监控程序 报错:", r)
dingtoinfo(res)
}
}
func main() {
defer recoverName()
var err error
engine, err = xorm.NewEngine("mysql", "root:xxxxxxxxxxxxxxx@tcp(127.0.0.1:3306)/new?charset=utf8")
if err != nil {
panic(err.Error())
}
gsql := " select up_time from token where type = 0 ;"
gres, gerr := engine.Query(gsql)
if gerr != nil {
panic(gerr.Error())
}
var data string
for _, v := range gres {
data = string(v["up_time"])
}
loc, _ := time.LoadLocation("Asia/Shanghai")
nowTime := time.Now().In(loc)
tm, _ := time.Parse("2006-01-02 03:04:05", data)
ntm := tm.In(loc)
subM := nowTime.Sub(ntm)
fmt.Println("token更新时间 与 系统时间 相差",subM)
subtract := int(subM.Minutes())
if subtract > 20 {
dingtoinfo("异常, 请及时登录查看 !!!")
}
}
分享题目:年轻人的第一个go程序:监控数据库字段报警
文章路径:https://www.cdcxhl.com/article32/jdiesc.html
成都网站建设公司_创新互联,为您提供定制开发、标签优化、小程序开发、定制网站、营销型网站建设、做网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联