forked from feiyu563/PrometheusAlert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (98 loc) · 3.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"PrometheusAlert/models"
_ "PrometheusAlert/routers"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/robfig/cron/v3"
"os"
"path"
"runtime"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
// Infos are set at build time use ldflags.
var (
Version string
Revision string
BuildUser string
BuildDate string
GoVersion = runtime.Version()
)
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
func init() {
db_driver := beego.AppConfig.String("db_driver")
switch db_driver {
case "sqlite3":
// 检查数据库文件
Db_name := "./db/PrometheusAlertDB.db"
if !IsExist(Db_name) {
os.MkdirAll(path.Dir(Db_name), os.ModePerm)
os.Create(Db_name)
}
// 注册驱动(“sqlite3” 属于默认注册,此处代码可省略)
orm.RegisterDriver("db_driver", orm.DRSqlite)
// 注册默认数据库
orm.RegisterDataBase("default", "sqlite3", Db_name, 10)
case "mysql":
orm.RegisterDriver("mysql", orm.DRMySQL)
dataSource := beego.AppConfig.String("db_user") + ":" + beego.AppConfig.String("db_password") + "@tcp(" + beego.AppConfig.String("db_host") + ":" + beego.AppConfig.String("db_port") + ")/" + beego.AppConfig.String("db_name") + "?charset=utf8mb4"
orm.RegisterDataBase("default", "mysql", dataSource)
orm.SetMaxIdleConns("default", 10)
orm.SetMaxOpenConns("default", 10)
case "postgres":
orm.RegisterDriver("postgres", orm.DRPostgres)
dataSource := "user=" + beego.AppConfig.String("db_user") + " password=" + beego.AppConfig.String("db_password") + " dbname=" + beego.AppConfig.String("db_name") + " host=" + beego.AppConfig.String("db_host") + " port=" + beego.AppConfig.String("db_port") + " sslmode=disable"
orm.RegisterDataBase("default", "postgres", dataSource)
orm.SetMaxIdleConns("default", 10)
orm.SetMaxOpenConns("default", 10)
default:
// 检查数据库文件
Db_name := "./db/PrometheusAlertDB.db"
if !IsExist(Db_name) {
os.MkdirAll(path.Dir(Db_name), os.ModePerm)
os.Create(Db_name)
}
// 注册驱动(“sqlite3” 属于默认注册,此处代码可省略)
orm.RegisterDriver("db_driver", orm.DRSqlite)
// 注册默认数据库
orm.RegisterDataBase("default", "sqlite3", Db_name, 10)
}
// 注册模型
orm.RegisterModel(new(models.PrometheusAlertDB), new(models.AlertRecord), new(models.AlertRouter))
orm.RunSyncdb("default", false, true)
}
func main() {
orm.Debug = true
logtype := beego.AppConfig.String("logtype")
if logtype == "console" {
logs.SetLogger(logtype)
} else if logtype == "file" {
logpath := beego.AppConfig.String("logpath")
logs.SetLogger(logtype, `{"filename":"`+logpath+`"}`)
}
// 输出应用信息
logs.Info("[main] 构建的Go版本: %s", GoVersion)
logs.Info("[main] 应用当前版本: %s", Version)
logs.Info("[main] 应用当前提交: %s", Revision)
logs.Info("[main] 应用构建时间: %s", BuildDate)
logs.Info("[main] 应用构建用户: %s", BuildUser)
// 定时删除日志
RecordLive, _ := beego.AppConfig.Int("RecordLive")
if RecordLive == 1 {
RecordLiveDay, _ := beego.AppConfig.Int("RecordLiveDay")
logs.Info("[main] 告警记录生存周期: %dd", RecordLiveDay)
c := cron.New(cron.WithSeconds())
c.AddFunc("0 0 1 * * *", func() { models.RecordCleanByTime(RecordLiveDay) })
c.Start()
}
models.MetricsInit()
beego.Handler("/metrics", promhttp.Handler())
beego.Run()
}