Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

升级内容, 支持api查询eventlist;支持服务重启event 不丢失 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cfg.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"userSmsQueue": "/queue/user/sms",
"userMailQueue": "/queue/user/mail"
},
"savefile":"/tmp/alarm.cache.file",
"api": {
"portal": "http://falcon.example.com",
"uic": "http://uic.example.com",
Expand Down
1 change: 1 addition & 0 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type GlobalConfig struct {
Queue *QueueConfig `json:"queue"`
Redis *RedisConfig `json:"redis"`
Api *ApiConfig `json:"api"`
SaveFile string `json:"savefile"`
}

var (
Expand Down
8 changes: 8 additions & 0 deletions g/eventdto.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type EventDto struct {
ExpressionId int `json:"expressionId"`
StrategyId int `json:"strategyId"`
TemplateId int `json:"templateId"`
ActionId int `json:"actionId"`

Link string `json:"link"`
}
Expand All @@ -53,6 +54,12 @@ func (this OrderedEvents) Less(i, j int) bool {

var Events = &SafeEvents{M: make(map[string]*EventDto)}

func (this *SafeEvents) Init(m map[string]*EventDto) {
this.Lock()
defer this.Unlock()
this.M = m
}

func (this *SafeEvents) Delete(id string) {
this.Lock()
defer this.Unlock()
Expand Down Expand Up @@ -102,6 +109,7 @@ func (this *SafeEvents) Put(event *model.Event) {
dto.ExpressionId = event.ExpressionId()
dto.StrategyId = event.StrategyId()
dto.TemplateId = event.TplId()
dto.ActionId = event.ActionId()

dto.Link = Link(event)

Expand Down
51 changes: 51 additions & 0 deletions g/savetofile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g

import(
"os"
"io/ioutil"
"encoding/json"
"log"
)


func SaveCacheToFile() {
event := Events.Clone()
body, err := json.Marshal(event)
if err != nil {
log.Println("SaveCacheToFile Fail:",err)
return
}

if config.SaveFile == "" {
log.Println("config savefile is empty")
return
}
err2 := ioutil.WriteFile(config.SaveFile, body, 0666)
if err2 != nil {
log.Println("SaveCacheToFile save event list to file Fail!",err2)
}
}

func ReadCacheFromFile() {
fi,err := os.Open( config.SaveFile )
if err != nil {
log.Println("ReadCacheFromFile , cache file open fail:",err)
return
}

defer fi.Close()

var events map[string]*EventDto
fd,err := ioutil.ReadAll(fi)
jerr := json.Unmarshal(fd,&events)
if err != nil || jerr != nil {
log.Println("ReadCacheFromFile, json decode cache file content error: ", err,jerr)
return
}

if config.Debug {
log.Println("read event from cache file is:",string(fd))
}

Events.Init(events)
}
5 changes: 5 additions & 0 deletions http/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func (this *MainController) Health() {
this.Ctx.WriteString("ok")
}

func (this *MainController) EventList() {
events := g.Events.Clone()
this.Ctx.Output.JSON(events,false,false)
}

func (this *MainController) Workdir() {
this.Ctx.WriteString(fmt.Sprintf("%s", file.SelfDir()))
}
Expand Down
6 changes: 3 additions & 3 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package http

import (
"fmt"
"log"
_ "net/http/pprof"

"github.com/astaxie/beego"
"github.com/open-falcon/alarm/g"
"log"
_ "net/http/pprof"
)

func configRoutes() {
Expand All @@ -15,6 +14,7 @@ func configRoutes() {
beego.Router("/health", &MainController{}, "get:Health")
beego.Router("/workdir", &MainController{}, "get:Workdir")
beego.Router("/config/reload", &MainController{}, "get:ConfigReload")
beego.Router("/eventlist", &MainController{}, "get:EventList")
beego.Router("/event/solve", &MainController{}, "post:Solve")
}

Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func main() {

g.ParseConfig(*cfg)
g.InitRedisConnPool()
//初始化,从cache文件中获取event list
g.ReadCacheFromFile()

go http.Start()

Expand All @@ -38,10 +40,12 @@ func main() {
go cron.CombineMail()

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
go func() {
<-sigs
fmt.Println()
//将当前的未恢复报警落地到文件
g.SaveCacheToFile()
g.RedisConnPool.Close()
os.Exit(0)
}()
Expand Down