Commit 61958889 authored by jingbo.wang's avatar jingbo.wang

生成总表,并且发送到指定邮箱

parent 061c81b8
...@@ -11,4 +11,6 @@ require ( ...@@ -11,4 +11,6 @@ require (
github.com/valyala/fasthttp v1.6.0 github.com/valyala/fasthttp v1.6.0
github.com/vrg0/go-common v0.0.0-20191111075058-891b371eb18a github.com/vrg0/go-common v0.0.0-20191111075058-891b371eb18a
go.uber.org/zap v1.13.0 go.uber.org/zap v1.13.0
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
) )
...@@ -6,12 +6,14 @@ import ( ...@@ -6,12 +6,14 @@ import (
"git.quantgroup.cn/DevOps/enoch/pkg/global" "git.quantgroup.cn/DevOps/enoch/pkg/global"
"git.quantgroup.cn/DevOps/enoch/pkg/glog" "git.quantgroup.cn/DevOps/enoch/pkg/glog"
"git.quantgroup.cn/DevOps/enoch/pkg/points" "git.quantgroup.cn/DevOps/enoch/pkg/points"
report_form "git.quantgroup.cn/DevOps/enoch/pkg/report-form"
"github.com/Shopify/sarama" "github.com/Shopify/sarama"
_ "github.com/mkevac/debugcharts" _ "github.com/mkevac/debugcharts"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
) )
...@@ -74,6 +76,20 @@ func handlerKafkaMsg() { ...@@ -74,6 +76,20 @@ func handlerKafkaMsg() {
} }
} }
func isMaster() bool {
//开发环境,但InfluxDb的Ip是生产环境ip的时候,不执行初始化操作
if global.IsDev() && strings.Contains(global.InfluxDbAddress, "172.16") {
return false
}
//生产环境,但LocalIp不是"172.30.12.22",不执行初始化操作
if !global.IsDev() && global.LocalIp != "172.30.12.22" {
return false
}
return true
}
func main() { func main() {
//性能监控 //性能监控
go func() { go func() {
...@@ -87,35 +103,21 @@ func main() { ...@@ -87,35 +103,21 @@ func main() {
} }
}() }()
//初始化数据库(创建DB,创建连续查询) if isMaster() {
dao.DbInit() //初始化数据库(创建DB,创建连续查询)
dao.DbInit()
//健康状态报表
report_form.RegularReport(global.ReportFormDir)
//告警策略 //TODO 告警策略
}
//对外api //对外api ??
//处理消息(阻塞) //处理消息(阻塞)
handlerKafkaMsg() handlerKafkaMsg()
/* /*
if quartz {
log.Println("启动定时任务")
job.AutoEmailPerformInfo()
}
//开启服务状态监控,当服务状态异常时,调用web-hook函数
if denv == "dev" { //开发环境
go node_check.NodeCheck()
job.AutoAlarm()
} else if job.CheckIp("172.30.12.22") { //生产环境,只有172.30.12.22执行
go node_check.NodeCheck()
job.AutoAlarm()
continuous_queries.Load() //连续查询设置只在生产环境上执行
}
go func() {
_ = http.ListenAndServe("0.0.0.0:"+strconv.Itoa(intPort+1), nil)
}()
http.HandleFunc("/duration", service.DurationInterface) http.HandleFunc("/duration", service.DurationInterface)
http.HandleFunc("/tech/health/check", func(writer http.ResponseWriter, request *http.Request) { http.HandleFunc("/tech/health/check", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK) writer.WriteHeader(http.StatusOK)
......
package email
import (
"crypto/tls"
"gopkg.in/gomail.v2"
)
type HostInfo struct {
address string
port int
}
func SendEmail(title string, content string, receiver ...string) {
hostInfo := HostInfo{"mail.quantgroup.cn", 587}
m := gomail.NewMessage()
m.SetHeader("From", "program@quantgroup.cn")
//m.SetHeader("To", []string{receiver})
m.SetHeader("To", receiver...)
m.SetHeader("Subject", title)
m.SetBody("text/html", "<pre>"+content+"</pre>")
userName := "program@quantgroup.cn"
pwd := "Fuck147999!!!"
d := gomail.NewDialer(hostInfo.address, hostInfo.port, userName, pwd)
// 解决 x509: certificate signed by unknown authority
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
...@@ -46,6 +46,7 @@ var ( ...@@ -46,6 +46,7 @@ var (
DaoFileCacheDir = "" DaoFileCacheDir = ""
ConsulDc = "" ConsulDc = ""
ConsulAddress = "" ConsulAddress = ""
ReportFormDir = ""
) )
type EosResult struct { type EosResult struct {
...@@ -105,6 +106,7 @@ func init() { ...@@ -105,6 +106,7 @@ func init() {
InfluxDbAddress = Config.GetOrDefault(NamespaceApplication, "influxdb.address", "") InfluxDbAddress = Config.GetOrDefault(NamespaceApplication, "influxdb.address", "")
//InfluxDbAddress = "http://172.20.6.33:8086" //InfluxDbAddress = "http://172.20.6.33:8086"
DaoFileCacheDir = Config.GetOrDefault(NamespaceApplication, "dao.file.cache.dir", "/var") DaoFileCacheDir = Config.GetOrDefault(NamespaceApplication, "dao.file.cache.dir", "/var")
ReportFormDir = Config.GetOrDefault(NamespaceApplication, "report.form.dir", "/var")
//初始化registry //初始化registry
if consulDc, ok := Config.Get(NamespaceApplication, "consul.datacenter"); !ok { if consulDc, ok := Config.Get(NamespaceApplication, "consul.datacenter"); !ok {
......
package report_form
import (
"encoding/json"
"git.quantgroup.cn/DevOps/enoch/pkg/email"
"git.quantgroup.cn/DevOps/enoch/pkg/global"
"git.quantgroup.cn/DevOps/enoch/pkg/glog"
"github.com/vrg0/go-common/util"
"io/ioutil"
"runtime/debug"
"time"
)
var (
receiverList = make([]string, 0)
)
func init() {
global.Config.Watch(global.NamespaceApplication, "report.form.receiver", func(oldCfg string, newCfg string) {
rList := make([]string, 0)
if err := json.Unmarshal([]byte(newCfg), &rList); err != nil {
glog.Error("can not unmarshal json:", err, " ", newCfg)
return
}
receiverList = rList
})
}
//保存服务报表的目录
func RegularReport(dir string) {
if !util.IsDir(dir) {
return
}
go func() {
defer func() {
if err := recover(); err != nil {
glog.Error(err, "\n", string(debug.Stack()))
}
}()
timer := time.NewTimer(0)
for {
<-timer.C
now := time.Now()
fileNamePrefix := now.Format("2006-01-02")
//周表
// if now.Weekday() == time.Monday {
if now.Weekday() == time.Wednesday {
fileName := dir + "/" + fileNamePrefix + "_week.txt"
if !util.Exists(fileName) {
body := GeneralTableRun(7)
if err := ioutil.WriteFile(fileName, []byte(body), 0644); err != nil {
glog.Error("报表写入文件失败:", fileName, err)
glog.Info(body)
}
email.SendEmail("服务监控状态总表-"+fileNamePrefix, body, receiverList...)
}
glog.Info("每周总表报表完成:", fileName)
}
//日表
//TODO
//fmt.Println("如果文件不存在,则报表:", fileNamePrefix)
next := now.Add(time.Hour * 24)
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
timer.Reset(next.Sub(now))
}
}()
}
package report_form
import "testing"
func TestRegularReport(t *testing.T) {
RegularReport("/var/enoch_health_table")
select {}
}
...@@ -118,7 +118,7 @@ func (t *Table) AddRecord(values ...interface{}) error { ...@@ -118,7 +118,7 @@ func (t *Table) AddRecord(values ...interface{}) error {
return nil return nil
} }
func Run(n int) string { func GeneralTableRun(n int) string {
const ( const (
cpuMin = 1 cpuMin = 1
memMin = 1 memMin = 1
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
) )
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
fmt.Println(Run(1)) fmt.Println(GeneralTableRun(10))
} }
func TestUtf8StringLen(t *testing.T) { func TestUtf8StringLen(t *testing.T) {
......
...@@ -72,7 +72,7 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service { ...@@ -72,7 +72,7 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service {
//初始化node //初始化node
nodeList := rtn.getNodeList() nodeList := rtn.getNodeList()
for _, node := range nodeList { for _, node := range nodeList {
glog.Info("init node:", rtn.name, node) glog.Info("init node: ", rtn.name, " ", node)
rtn.nodeMap[node] = NewNode(rtn.name, node, rtn.startTime, rtn.endTime) rtn.nodeMap[node] = NewNode(rtn.name, node, rtn.startTime, rtn.endTime)
} }
......
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