Skip to content

Commit 4092831

Browse files
author
曾庆国
committed
First commit
0 parents  commit 4092831

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+11879
-0
lines changed

Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM alpine:3.4
2+
MAINTAINER Barnett <[email protected]>
3+
4+
COPY ./conf /alert/conf
5+
COPY ./static /alert/static
6+
COPY ./views /alert/views
7+
COPY ./alertCenter /alert/alertCenter
8+
9+
CMD chmod 655 /alert/alertCenter
10+
11+
EXPOSE 8888
12+
13+
ENTRYPOINT ["/alert/alertCenter"]

a.out

10.8 MB
Binary file not shown.

alertCenter

14.8 MB
Binary file not shown.

alert_receiver/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

conf/app.conf

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
appname = alertCenter
2+
httpport = 8888
3+
runmode = dev
4+
copyrequestbody = true
5+
url=http://10.12.1.129:8080
6+
cloudURI=http://103.235.250.11:8002
7+
8+
mongoURI = 10.10.12.19:27017
9+
mongoDB = admin
10+
mongoUser = root
11+
mongoPass = root
12+
13+
14+
weURI = http://10.12.1.129:18081
15+
weToken = f6974BCU7Btd1eZ83yX17TLOmZBHI9DZ8Km06JM1v0X7stj7
16+
weAgentId = 3
17+
weCount = 20
18+
weReCount = 3
19+
20+
mailServer = smtp.exmail.qq.com
21+
mailPort = 465
22+
23+
mailPassword = GOyoo#12345
24+
25+
mailCount = 20
26+
mailReCount = 3
27+
28+
LADPServer=127.0.0.1
29+
LDAPPort=8389
30+
LDAPDN = cn=admin,dc=yunpro,dc=cn
31+
LDAPPass=admin123
32+
33+

controllers/APIController.go

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
"strconv"
6+
"time"
7+
8+
"github.com/astaxie/beego"
9+
"github.com/barnettZQG/alertCenter/core"
10+
"github.com/barnettZQG/alertCenter/models"
11+
"github.com/barnettZQG/alertCenter/util"
12+
)
13+
14+
type APIController struct {
15+
beego.Controller
16+
session *core.MongoSession
17+
alertService *core.AlertService
18+
teamServcie *core.TeamService
19+
}
20+
21+
func (e *APIController) ReceiveAlert() {
22+
23+
data := e.Ctx.Input.RequestBody
24+
if data != nil && len(data) > 0 {
25+
var AlertMessage *models.AlertReceive = &models.AlertReceive{}
26+
err := json.Unmarshal(data, AlertMessage)
27+
if err == nil {
28+
util.Info("get a alert message,receiver:" + AlertMessage.Receiver)
29+
go core.HandleMessage(AlertMessage)
30+
e.Data["json"] = util.GetSuccessJson("receive alert success")
31+
} else {
32+
util.Error("Parse the received message to AlertMessage faild." + err.Error())
33+
}
34+
} else {
35+
util.Error("receive a unknow data")
36+
//util.Info(string(data))
37+
}
38+
e.ServeJSON()
39+
}
40+
func (e *APIController) Receive() {
41+
42+
data := e.Ctx.Input.RequestBody
43+
if data != nil && len(data) > 0 {
44+
var Alerts []*models.Alert = make([]*models.Alert, 0)
45+
err := json.Unmarshal(data, &Alerts)
46+
if err == nil {
47+
core.HandleAlerts(Alerts)
48+
e.Data["json"] = util.GetSuccessJson("receive alert success")
49+
} else {
50+
util.Error("Parse the received message to Alerts faild." + err.Error())
51+
}
52+
} else {
53+
util.Error("receive a unknow data")
54+
//util.Info(string(data))
55+
}
56+
e.ServeJSON()
57+
}
58+
func (e *APIController) AddTag() {
59+
we := &core.WeAlertSend{}
60+
if ok := we.GetAllTags(); ok {
61+
e.Data["json"] = util.GetSuccessJson("get weiTag success")
62+
} else {
63+
e.Data["json"] = util.GetFailJson("get weiTag faild")
64+
}
65+
e.ServeJSON()
66+
}
67+
68+
func (e *APIController) HandleAlert() {
69+
ID := e.GetString(":ID")
70+
Type := e.GetString(":type")
71+
message := e.GetString("message")
72+
if len(ID) == 0 || len(Type) == 0 {
73+
e.Data["json"] = util.GetErrorJson("参数格式错误")
74+
} else {
75+
session := core.GetMongoSession()
76+
defer session.Close()
77+
alertService := core.GetAlertService(session)
78+
alert := alertService.FindByID(ID)
79+
if alert == nil {
80+
e.Data["json"] = util.GetFailJson("报警信息不存在,id信息错误")
81+
} else {
82+
if Type == "handle" {
83+
alert.IsHandle = 1
84+
} else if Type == "miss" {
85+
alert.IsHandle = -1
86+
}
87+
alert.HandleDate = time.Now()
88+
alert.HandleMessage = message
89+
if ok := alertService.Update(alert); ok {
90+
e.Data["json"] = util.GetSuccessJson("登记成功")
91+
} else {
92+
e.Data["json"] = util.GetFailJson("登记失败")
93+
}
94+
}
95+
}
96+
e.ServeJSON()
97+
}
98+
99+
func (e *APIController) GetAlerts() {
100+
receiver := e.GetString(":receiver")
101+
e.session = core.GetMongoSession()
102+
if e.session == nil {
103+
e.Data["json"] = util.GetFailJson("get database session faild.")
104+
} else {
105+
e.alertService = core.GetAlertService(e.session)
106+
defer e.session.Close()
107+
if len(receiver) != 0 && receiver != "all" {
108+
alerts := e.alertService.FindByUser(receiver)
109+
util.Info("Get" + strconv.Itoa(len(alerts)) + " alerts")
110+
if alerts == nil {
111+
e.Data["json"] = util.GetFailJson("get database collection faild or receiver is error ")
112+
} else {
113+
e.Data["json"] = util.GetSuccessReJson(alerts)
114+
}
115+
} else if receiver == "all" {
116+
alerts := e.alertService.FindAll()
117+
if alerts == nil {
118+
e.Data["json"] = util.GetFailJson("get database collection faild")
119+
} else {
120+
e.Data["json"] = util.GetSuccessReJson(alerts)
121+
}
122+
} else {
123+
e.Data["json"] = util.GetErrorJson("api use error,please provide receiver")
124+
}
125+
}
126+
e.ServeJSON()
127+
}
128+
func (e *APIController) GetTeams() {
129+
130+
e.session = core.GetMongoSession()
131+
if e.session == nil {
132+
e.Data["json"] = util.GetFailJson("get database session faild.")
133+
} else {
134+
relation := &core.Relation{}
135+
teams := relation.GetAllTeam()
136+
if teams == nil {
137+
e.Data["json"] = util.GetFailJson("There is no info of team")
138+
} else {
139+
e.Data["json"] = util.GetSuccessReJson(teams)
140+
}
141+
}
142+
e.ServeJSON()
143+
}
144+
145+
func (e *APIController) AddTeam() {
146+
data := e.Ctx.Input.RequestBody
147+
if data != nil && len(data) > 0 {
148+
var team *models.Team = &models.Team{}
149+
err := json.Unmarshal(data, team)
150+
if err == nil {
151+
relation := &core.Relation{}
152+
relation.SetTeam(team)
153+
e.Data["json"] = util.GetSuccessJson("receive team info success")
154+
} else {
155+
util.Error("Parse the received message to teams faild." + err.Error())
156+
e.Data["json"] = util.GetFailJson("Parse the received message to teams faild.")
157+
}
158+
} else {
159+
util.Error("receive a unknow data")
160+
e.Data["jaon"] = util.GetErrorJson("receive a unknow data")
161+
}
162+
e.ServeJSON()
163+
}

controllers/AlertController.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package controllers
2+
3+
import "github.com/astaxie/beego"
4+
5+
type AlertController struct {
6+
beego.Controller
7+
}
8+
9+
func (e *AlertController) AlertList() {
10+
receiver := e.GetString("receiver")
11+
if len(receiver) != 0 {
12+
e.TplName = "alertList.html"
13+
} else {
14+
e.TplName = "alertListAll.html"
15+
}
16+
}

controllers/TeamController.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package controllers
2+
3+
import "github.com/astaxie/beego"
4+
5+
type TeamController struct {
6+
beego.Controller
7+
}
8+
9+
func (e *TeamController) GetTeams() {
10+
e.TplName = "teams.html"
11+
}

controllers/default.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package controllers
2+
3+
import "github.com/astaxie/beego"
4+
5+
type MainController struct {
6+
beego.Controller
7+
}
8+
9+
func (c *MainController) Get() {
10+
c.Data["Website"] = "beego.me"
11+
c.Data["Email"] = "[email protected]"
12+
c.TplName = "index.tpl"
13+
}

core/AlertService.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package core
2+
3+
import (
4+
"github.com/barnettZQG/alertCenter/models"
5+
"github.com/barnettZQG/alertCenter/util"
6+
"gopkg.in/mgo.v2/bson"
7+
)
8+
9+
type AlertService struct {
10+
Session *MongoSession
11+
}
12+
13+
func GetAlertService(session *MongoSession) *AlertService {
14+
return &AlertService{
15+
Session: session,
16+
}
17+
}
18+
19+
//GetAlertByLabels 获取报警根据labels
20+
func (e *AlertService) GetAlertByLabels(alert *models.Alert) (result *models.Alert) {
21+
mark := alert.Fingerprint().String()
22+
coll := e.Session.GetCollection("Alert")
23+
if coll == nil {
24+
return nil
25+
}
26+
err := coll.Find(bson.M{"mark": mark, "ishandle": 0}).Select(nil).One(&result)
27+
if err != nil {
28+
util.Debug("Get alert by Mark " + mark + " error." + err.Error())
29+
return nil
30+
}
31+
return
32+
}
33+
34+
//Update 更新可变信息
35+
func (e *AlertService) Update(alert *models.Alert) bool {
36+
coll := e.Session.GetCollection("Alert")
37+
if coll == nil {
38+
return false
39+
}
40+
err := coll.Update(bson.M{"mark": alert.Mark, "ishandle": 0}, bson.M{
41+
"$set": bson.M{
42+
"alertcount": alert.AlertCount,
43+
"ishandle": alert.IsHandle,
44+
"handledate": alert.HandleDate,
45+
"handlemessage": alert.HandleMessage,
46+
"endsat": alert.EndsAt,
47+
"startsat": alert.StartsAt,
48+
"updatedat": alert.UpdatedAt,
49+
},
50+
})
51+
if err != nil {
52+
util.Debug("Update the alert Error By Mark " + alert.Mark + "," + err.Error())
53+
return false
54+
}
55+
return true
56+
}
57+
58+
// Save 存储报警
59+
func (e *AlertService) Save(alert *models.Alert) bool {
60+
return e.Session.Insert("Alert", alert)
61+
}
62+
63+
//FindByUser 根据receiver的name或者id获取报警信息
64+
func (e *AlertService) FindByUser(user string) (alerts []*models.Alert) {
65+
coll := e.Session.GetCollection("Alert")
66+
if coll == nil {
67+
return nil
68+
}
69+
coll.Find(bson.M{"receiver.name": user, "ishandle": 0}).Select(nil).All(&alerts)
70+
if alerts == nil || len(alerts) == 0 {
71+
coll.Find(bson.M{"receiver.id": user, "ishandle": 0}).Select(nil).All(&alerts)
72+
}
73+
return
74+
}
75+
76+
//FindByID 根据ID获取报警
77+
func (e *AlertService) FindByID(ID string) (alert *models.Alert) {
78+
coll := e.Session.GetCollection("Alert")
79+
if coll == nil {
80+
return nil
81+
}
82+
err := coll.Find(bson.M{"id": ID}).One(&alert)
83+
if err != nil {
84+
util.Debug("find alert by id faild." + err.Error())
85+
}
86+
return
87+
}
88+
89+
//FindAll 获取全部报警
90+
func (e *AlertService) FindAll() (alerts []*models.Alert) {
91+
coll := e.Session.GetCollection("Alert")
92+
if coll == nil {
93+
return nil
94+
}
95+
coll.Find(nil).Select(nil).All(&alerts)
96+
return
97+
}

0 commit comments

Comments
 (0)