示例#1
0
文件: reader.go 项目: donh/alarm
func popEvent(queues []string) (*model.Event, error) {
	log := logger.Logger()
	count := len(queues)

	params := make([]interface{}, count+1)
	for i := 0; i < count; i++ {
		params[i] = queues[i]
	}
	// set timeout 0
	params[count] = 0

	rc := g.RedisConnPool.Get()
	defer rc.Close()

	reply, err := redis.Strings(rc.Do("BRPOP", params...))
	if err != nil {
		log.Error(fmt.Sprintf("get alarm event from redis fail: %v", err))
		return nil, err
	}

	var event model.Event
	err = json.Unmarshal([]byte(reply[1]), &event)
	if err != nil {
		log.Error(fmt.Sprintf("parse alarm event fail: %v", err))
		return nil, err
	}

	log.Debug(event.String())
	//insert event into database
	eventmodel.InsertEvent(&event)
	// save in memory. display in dashboard
	g.Events.Put(&event)

	return &event, nil
}
示例#2
0
文件: consumer.go 项目: myhung/alarm
func ParseUserSms(event *model.Event, action *api.Action) {
	userMap := api.GetUsers(action.Uic)

	content := GenerateSmsContent(event)
	metric := event.Metric()
	status := event.Status
	priority := event.Priority()

	queue := g.Config().Redis.UserSmsQueue

	rc := g.RedisConnPool.Get()
	defer rc.Close()

	for _, user := range userMap {
		dto := SmsDto{
			Priority: priority,
			Metric:   metric,
			Content:  content,
			Phone:    user.Phone,
			Status:   status,
		}
		bs, err := json.Marshal(dto)
		if err != nil {
			log.Println("json marshal SmsDto fail:", err)
			continue
		}

		_, err = rc.Do("LPUSH", queue, string(bs))
		if err != nil {
			log.Println("LPUSH redis", queue, "fail:", err, "dto:", string(bs))
		}
	}
}
示例#3
0
文件: consumer.go 项目: myhung/alarm
// 低优先级的做报警合并
func consumeLowEvents(event *model.Event, action *api.Action) {
	if action.Uic == "" {
		return
	}

	if event.Priority() < 3 {
		ParseUserSms(event, action)
	}

	ParseUserMail(event, action)
	ParseUserQQ(event, action)
	ParseUserServerchan(event, action)
}
示例#4
0
文件: eventdto.go 项目: myhung/alarm
func Link(event *model.Event) string {
	tplId := event.TplId()
	if tplId != 0 {
		return fmt.Sprintf("%s/template/view/%d", Config().Api.Portal, tplId)
	}

	eid := event.ExpressionId()
	if eid != 0 {
		return fmt.Sprintf("%s/expression/view/%d", Config().Api.Portal, eid)
	}

	return ""
}
示例#5
0
文件: consumer.go 项目: myhung/alarm
// 高优先级的不做报警合并
func consumeHighEvents(event *model.Event, action *api.Action) {
	if action.Uic == "" {
		return
	}

	phones, mails := api.ParseTeams(action.Uic)

	smsContent := GenerateSmsContent(event)
	mailContent := GenerateMailContent(event)
	QQContent := GenerateQQContent(event)

	if event.Priority() < 3 {
		redis.WriteSms(phones, smsContent)
	}

	redis.WriteMail(mails, smsContent, mailContent)
	redis.WriteQQ(mails, smsContent, QQContent)
	ParseUserServerchan(event, action)
}
示例#6
0
文件: consumer.go 项目: myhung/alarm
func consume(event *model.Event, isHigh bool) {
	actionId := event.ActionId()
	if actionId <= 0 {
		return
	}

	action := api.GetAction(actionId)
	if action == nil {
		return
	}

	if action.Callback == 1 {
		HandleCallback(event, action)
		return
	}

	if isHigh {
		consumeHighEvents(event, action)
	} else {
		consumeLowEvents(event, action)
	}
}
示例#7
0
func insertEvent(q orm.Ormer, eve *coommonModel.Event) (res interface{}, err error) {
	var status int
	if status = 0; eve.Status == "OK" {
		status = 1
	}
	sqltemplete := `INSERT INTO events (
		event_caseId,
		step,
		cond,
		status,
		timestamp
	) VALUES(?,?,?,?,?)`
	res, err = q.Raw(
		sqltemplete,
		eve.Id,
		eve.CurrentStep,
		fmt.Sprintf("%v %v %v", eve.LeftValue, eve.Operator(), eve.RightValue()),
		status,
		time.Unix(eve.EventTime, 0),
	).Exec()
	return
}
示例#8
0
文件: reader.go 项目: myhung/alarm
func popEvent(queues []string) (*model.Event, error) {

	count := len(queues)

	params := make([]interface{}, count+1)
	for i := 0; i < count; i++ {
		params[i] = queues[i]
	}
	// set timeout 0
	params[count] = 0

	rc := g.RedisConnPool.Get()
	defer rc.Close()

	reply, err := redis.Strings(rc.Do("BRPOP", params...))
	if err != nil {
		log.Printf("get alarm event from redis fail: %v", err)
		return nil, err
	}

	var event model.Event
	err = json.Unmarshal([]byte(reply[1]), &event)
	if err != nil {
		log.Printf("parse alarm event fail: %v", err)
		return nil, err
	}

	if g.Config().Debug {
		log.Println("======>>>>")
		log.Println(event.String())
	}

	// save in memory. display in dashboard
	g.Events.Put(&event)

	return &event, nil
}
示例#9
0
文件: callback.go 项目: myhung/alarm
func Callback(event *model.Event, action *api.Action) string {
	if action.Url == "" {
		return "callback url is blank"
	}

	L := make([]string, 0)
	if len(event.PushedTags) > 0 {
		for k, v := range event.PushedTags {
			L = append(L, fmt.Sprintf("%s:%s", k, v))
		}
	}

	tags := ""
	if len(L) > 0 {
		tags = strings.Join(L, ",")
	}

	req := httplib.Get(action.Url).SetTimeout(3*time.Second, 20*time.Second)

	req.Param("endpoint", event.Endpoint)
	req.Param("metric", event.Metric())
	req.Param("func", event.Func())
	req.Param("leftValue", fmt.Sprintf("%.3f", event.LeftValue))
	req.Param("operator", event.Operator())
	req.Param("rightValue", fmt.Sprintf("%.3f", event.RightValue()))
	req.Param("note", event.Note())
	req.Param("status", event.Status)
	req.Param("step", fmt.Sprintf("%d", event.CurrentStep))
	req.Param("priority", fmt.Sprintf("%d", event.Priority()))
	req.Param("time", event.FormattedTime())
	req.Param("tpl_id", fmt.Sprintf("%d", event.TplId()))
	req.Param("exp_id", fmt.Sprintf("%d", event.ExpressionId()))
	req.Param("stra_id", fmt.Sprintf("%d", event.StrategyId()))
	req.Param("tags", tags)

	resp, e := req.String()

	success := "success"
	if e != nil {
		success = fmt.Sprintf("fail:%s", e.Error())
	}
	message := fmt.Sprintf("curl %s %s. resp: %s", action.Url, success, resp)

	return message
}
示例#10
0
文件: eventdto.go 项目: myhung/alarm
func (this *SafeEvents) Put(event *model.Event) {
	if event.Status == "OK" {
		this.Delete(event.Id)
		return
	}

	dto := &EventDto{}
	dto.Id = event.Id
	dto.Endpoint = event.Endpoint
	dto.Metric = event.Metric()
	dto.Counter = event.Counter()
	dto.Func = event.Func()
	dto.LeftValue = utils.ReadableFloat(event.LeftValue)
	dto.Operator = event.Operator()
	dto.RightValue = utils.ReadableFloat(event.RightValue())
	dto.Note = event.Note()

	dto.MaxStep = event.MaxStep()
	dto.CurrentStep = event.CurrentStep
	dto.Priority = event.Priority()

	dto.Status = event.Status
	dto.Timestamp = event.EventTime

	dto.ExpressionId = event.ExpressionId()
	dto.StrategyId = event.StrategyId()
	dto.TemplateId = event.TplId()

	dto.Link = Link(event)

	this.Lock()
	defer this.Unlock()
	this.M[dto.Id] = dto
}
示例#11
0
文件: builder.go 项目: myhung/alarm
func BuildCommonQQContent(event *model.Event) string {
	link := g.Link(event)
	return fmt.Sprintf(
		"%s\r\nP%d\r\nEndpoint:%s\r\nMetric:%s\r\nTags:%s\r\n%s: %s%s%s\r\nNote:%s\r\nMax:%d, Current:%d\r\nTimestamp:%s\r\n%s\r\n",
		event.Status,
		event.Priority(),
		event.Endpoint,
		event.Metric(),
		utils.SortedTags(event.PushedTags),
		event.Func(),
		utils.ReadableFloat(event.LeftValue),
		event.Operator(),
		utils.ReadableFloat(event.RightValue()),
		event.Note(),
		event.MaxStep(),
		event.CurrentStep,
		event.FormattedTime(),
		link,
	)
}
示例#12
0
文件: builder.go 项目: myhung/alarm
func BuildCommonSMSContent(event *model.Event) string {
	return fmt.Sprintf(
		"[P%d][%s][%s][][%s %s %s %s %s%s%s][O%d %s]",
		event.Priority(),
		event.Status,
		event.Endpoint,
		event.Note(),
		event.Func(),
		event.Metric(),
		utils.SortedTags(event.PushedTags),
		utils.ReadableFloat(event.LeftValue),
		event.Operator(),
		utils.ReadableFloat(event.RightValue()),
		event.CurrentStep,
		event.FormattedTime(),
	)
}
示例#13
0
文件: builder.go 项目: myhung/alarm
func BuildCommonMailContent(event *model.Event) string {
	link := g.Link(event)
	tdtl := `style="border: 1px solid #ccc; background: #FFF4F4;"`
	tdtr := `style="border: 1px solid #ccc; border-left: none;"`
	tdl := `style="border: 1px solid #ccc; border-top:  none; background: #FFF4F4;"`
	tdr := `style="border: 1px solid #ccc; border-top:  none; border-left: none;"`
	return fmt.Sprintf(
		`<html><head><meta charset="utf-8"></head>
		<body>
			<table border="0" cellpadding="5" cellspacing="0">
                                <tr>
                                        <td %s >%s&nbsp;</td>
                                        <td %s >%d&nbsp;</td></tr>
                                <tr>
                                        <td %s>Endpoint:</td>
                                        <td %s>%s&nbsp;</td>
                                </tr>
                                <tr>
                                        <td %s>Metric:</td>
                                        <td %s>%s&nbsp;</td>
                                </tr>
                                <tr>
                                        <td %s>Tags:</td>
                                        <td %s>%s&nbsp;</td>
                                </tr>
                                <tr>
                                        <td %s>%s</td>
                                        <td %s>%s%s%s&nbsp;</td>
                                </tr>
                                <tr>
                                        <td %s>Note:</td>
                                        <td %s>%s&nbsp;</td>
                                </tr>
                                <tr>
                                        <td %s>Max:</td>
                                        <td %s>%d&nbsp;</td>
                                </tr>
                                <tr>
                                        <td %s>Current:</td>
                                        <td %s>%d&nbsp;</td>
                                </tr>
                                <tr>
                                        <td %s>Timesramp:</td>
                                        <td %s>%s&nbsp;</td>
                                </tr>
                        </table>
			<br>
			<a href="%s">%s</a>
		</body></html>`,

		tdtl, event.Status, tdtr, event.Priority(),
		tdl, tdr, event.Endpoint,
		tdl, tdr, event.Metric(),
		tdl, tdr, utils.SortedTags(event.PushedTags),
		tdl, event.Func(), tdr, utils.ReadableFloat(event.LeftValue), event.Operator(), utils.ReadableFloat(event.RightValue()),
		tdl, tdr, event.Note(),
		tdl, tdr, event.MaxStep(),
		tdl, tdr, event.CurrentStep,
		tdl, tdr, event.FormattedTime(),
		link,
		link,
	)
}
示例#14
0
func InsertEvent(eve *coommonModel.Event) {
	log := logger.Logger()
	q := orm.NewOrm()
	q.Using("falcon_portal")
	var event []EventCases
	q.Raw("select * from event_cases where id = ?", eve.Id).QueryRows(&event)
	if len(event) == 0 {
		//create cases
		sqltemplete := `INSERT INTO event_cases (
					id,
					endpoint,
					metric,
					func,
					cond,
					note,
					max_step,
					current_step,
					priority,
					status,
					timestamp,
					update_at,
					tpl_creator,
					expression_id,
					strategy_id,
					template_id
					) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
		res1, err := q.Raw(
			sqltemplete,
			eve.Id,
			eve.Endpoint,
			counterGen(eve.Metric(), utils.SortedTags(eve.PushedTags)),
			eve.Func(),
			//cond
			fmt.Sprintf("%v %v %v", eve.LeftValue, eve.Operator(), eve.RightValue()),
			eve.Strategy.Note,
			eve.MaxStep(),
			eve.CurrentStep,
			eve.Priority(),
			eve.Status,
			//start_at
			time.Unix(eve.EventTime, 0),
			//update_at
			time.Unix(eve.EventTime, 0),
			eve.Strategy.Tpl.Creator,
			eve.ExpressionId(),
			eve.StrategyId(),
			//template_id
			eve.TplId()).Exec()
		log.Debug(fmt.Sprintf("%v, %v", res1, err))

		//insert case
		res2, err := insertEvent(q, eve)
		log.Debug(fmt.Sprintf("%v, %v", res2, err))

	} else {
		sqltemplete := `UPDATE event_cases SET
				update_at = ?,
				max_step = ?,
				current_step = ?,
				note = ?,
				cond = ?,
				status = ?`
		//reopen case
		if event[0].ProcessStatus == "resolved" || event[0].ProcessStatus == "ignored" {
			sqltemplete = fmt.Sprintf("%v ,process_status = '%s', process_note = %d", sqltemplete, "unresolved", 0)
		}
		if eve.CurrentStep == 1 {

			sqltemplete = fmt.Sprintf("%v ,timestamp = ? WHERE id = ?", sqltemplete)
			//update start time of cases
			res1, err := q.Raw(
				sqltemplete,
				time.Unix(eve.EventTime, 0),
				eve.MaxStep(),
				eve.CurrentStep,
				eve.Strategy.Note,
				fmt.Sprintf("%v %v %v", eve.LeftValue, eve.Operator(), eve.RightValue()),
				eve.Status,
				time.Unix(eve.EventTime, 0),
				eve.Id).Exec()
			log.Debug(fmt.Sprintf("%v, %v", res1, err))
			//insert case
			res2, err := insertEvent(q, eve)
			log.Debug(fmt.Sprintf("%v, %v", res2, err))
		} else {
			sqltemplete = fmt.Sprintf("%v WHERE id = ?", sqltemplete)
			res1, err := q.Raw(
				sqltemplete,
				time.Unix(eve.EventTime, 0),
				eve.MaxStep(),
				eve.CurrentStep,
				eve.Strategy.Note,
				fmt.Sprintf("%v %v %v", eve.LeftValue, eve.Operator(), eve.RightValue()),
				eve.Status,
				eve.Id).Exec()
			log.Debug(fmt.Sprintf("%v, %v", res1, err))
			//insert case
			res2, err := insertEvent(q, eve)
			log.Debug(fmt.Sprintf("%v, %v", res2, err))
		}
	}
}