|
@@ -0,0 +1,98 @@
|
|
|
+package utils
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ log "github.com/sirupsen/logrus"
|
|
|
+
|
|
|
+ qu "qfw/util"
|
|
|
+
|
|
|
+ "github.com/robfig/cron"
|
|
|
+ . "gopkg.in/mgo.v2/bson"
|
|
|
+)
|
|
|
+
|
|
|
+//定时任务,邮件服务
|
|
|
+var (
|
|
|
+ api string
|
|
|
+ tomail string
|
|
|
+)
|
|
|
+
|
|
|
+func task() {
|
|
|
+ jkmail, _ := Sysconfig["jkmail"].(map[string]interface{})
|
|
|
+ if jkmail != nil {
|
|
|
+ log.Debug("enter task...")
|
|
|
+ api, _ = jkmail["api"].(string)
|
|
|
+ tomail, _ = jkmail["to"].(string)
|
|
|
+ c := cron.New()
|
|
|
+ //c.AddFunc(jkmail["cron-report"].(string), report)
|
|
|
+ c.AddFunc(jkmail["cron-check"].(string), check)
|
|
|
+ c.Start()
|
|
|
+ defer c.Stop()
|
|
|
+ select {}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//出报告,每周一次,信息量情况和调用情况,暂时不用
|
|
|
+func report() {
|
|
|
+ log.Debug("每周出报告")
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//监控报警,量小于30天、15天、7天告警,取最近7天的求出最大值
|
|
|
+var LM = map[int64]bool{1: true, 3: true, 7: true, 15: true, 30: true, 45: true}
|
|
|
+
|
|
|
+func check() {
|
|
|
+ log.Debug("每天信息量监控")
|
|
|
+ sess := Mgo.GetMgoConn()
|
|
|
+ defer Mgo.DestoryMongoConn(sess)
|
|
|
+ var res []M
|
|
|
+ now := time.Now()
|
|
|
+
|
|
|
+ nowZero := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
|
|
+ threeBefor := time.Date(now.Year(), now.Month(), now.Day()-4, 0, 0, 0, 0, time.Local)
|
|
|
+ sess.DB(Mgo.DbName).C("usermail").Pipe([]M{
|
|
|
+ M{"$match": M{"createtime": M{"$gte": threeBefor.Unix(), "$lt": nowZero.Unix()}}},
|
|
|
+ M{"$group": M{"_id": M{"appid": "$appid", "day": M{"$trunc": M{"$divide": []interface{}{M{"$subtract": []interface{}{nowZero.Unix(), "$createtime"}}, 86400}}}}, "sum": M{"$sum": 1}}},
|
|
|
+ }).All(&res)
|
|
|
+ if res != nil && len(res) > 0 {
|
|
|
+ MaxMap := map[string]int64{}
|
|
|
+ for _, v := range res {
|
|
|
+ vm, _ := v["_id"].(M) //用M否则会报错
|
|
|
+ appid := qu.ObjToString(vm["appid"])
|
|
|
+ v1 := MaxMap[appid]
|
|
|
+ v2 := qu.Int64All(v["sum"])
|
|
|
+ if v2 > v1 {
|
|
|
+ MaxMap[appid] = v2
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //查询用户信息
|
|
|
+ us, b := Mgo.Find("user", `{}`, nil, `{"appid":1,"username":1,"plan":1}`, false, -1, -1)
|
|
|
+ alert := ""
|
|
|
+ if b && us != nil {
|
|
|
+ for _, v := range *us {
|
|
|
+ id, _ := v["appid"].(string)
|
|
|
+ name, _ := v["username"].(string)
|
|
|
+ plan, _ := v["plan"].(map[string]interface{})
|
|
|
+ current := qu.Int64All(plan["current"])
|
|
|
+ endtime := qu.Int64All(plan["endtime"])
|
|
|
+ last := (endtime - now.Unix()) / 86400
|
|
|
+ yc := last * MaxMap[id]
|
|
|
+ if LM[last] || yc > current { //到期发告警或者余额不足告警
|
|
|
+ alert += fmt.Sprintf("%s:%s__剩余天数:%d__剩余条数:%d__预测条数:%d<br/>", id, name, last, current, yc)
|
|
|
+ }
|
|
|
+ log.Debug(id, name, "剩余天数:", last, "剩余条数:", current, "预测:", yc)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if alert != "" {
|
|
|
+ SendMail("企业级服务每日监控", alert)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func SendMail(zt, str string) {
|
|
|
+ res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, zt, str))
|
|
|
+ log.Debug("sendemail", res, err)
|
|
|
+}
|