Esempio n. 1
0
func ParseUserMail(event *model.Event, action *api.Action) {
	userMap := api.GetUsers(action.Uic)

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

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

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

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

		_, err = rc.Do("LPUSH", queue, string(bs))
		if err != nil {
			log.Println("LPUSH redis", queue, "fail:", err, "dto:", string(bs))
		}
	}
}
Esempio n. 2
0
func LinkToSMS(content string) (string, error) {
	links := g.Config().Api.Links
	uri := fmt.Sprintf("%s/store", links)
	req := httplib.Post(uri).SetTimeout(3*time.Second, 10*time.Second)
	req.Body([]byte(content))
	return req.String()
}
Esempio n. 3
0
func popAllQQDto() []*QQDto {
	ret := []*QQDto{}
	queue := g.Config().Redis.UserQQQueue

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

	for {
		reply, err := redis.String(rc.Do("RPOP", queue))
		if err != nil {
			if err != redis.ErrNil {
				log.Println("get QQDto fail", err)
			}
			break
		}

		if reply == "" || reply == "nil" {
			continue
		}

		var qqDto QQDto
		err = json.Unmarshal([]byte(reply), &qqDto)
		if err != nil {
			log.Printf("json unmarshal QQDto: %s fail: %v", reply, err)
			continue
		}

		ret = append(ret, &qqDto)
	}

	return ret
}
Esempio n. 4
0
func (this *MainController) ConfigReload() {
	remoteAddr := this.Ctx.Input.Request.RemoteAddr
	if strings.HasPrefix(remoteAddr, "127.0.0.1") {
		g.ParseConfig(g.ConfigFile)
		this.Data["json"] = g.Config()
		this.ServeJson()
	} else {
		this.Ctx.WriteString("no privilege")
	}
}
Esempio n. 5
0
func Start() {
	if !g.Config().Http.Enabled {
		return
	}

	addr := g.Config().Http.Listen
	if addr == "" {
		return
	}

	if g.Config().Debug {
		beego.RunMode = "dev"
	} else {
		beego.RunMode = "prod"
	}

	beego.Run(addr)

	log.Println("http listening", addr)
}
Esempio n. 6
0
func combineSms() {
	dtos := popAllSmsDto()
	count := len(dtos)
	if count == 0 {
		return
	}

	dtoMap := make(map[string][]*SmsDto)
	for i := 0; i < count; i++ {
		key := fmt.Sprintf("%d%s%s%s", dtos[i].Priority, dtos[i].Status, dtos[i].Phone, dtos[i].Metric)
		if _, ok := dtoMap[key]; ok {
			dtoMap[key] = append(dtoMap[key], dtos[i])
		} else {
			dtoMap[key] = []*SmsDto{dtos[i]}
		}
	}

	for _, arr := range dtoMap {
		size := len(arr)
		if size == 1 {
			redi.WriteSms([]string{arr[0].Phone}, arr[0].Content)
			continue
		}

		// 把多个sms内容写入数据库,只给用户提供一个链接
		contentArr := make([]string, size)
		for i := 0; i < size; i++ {
			contentArr[i] = arr[i].Content
		}
		content := strings.Join(contentArr, ",,")

		first := arr[0].Content
		t := strings.Split(first, "][")
		eg := ""
		if len(t) >= 3 {
			eg = t[2]
		}

		path, err := api.LinkToSMS(content)
		sms := ""
		if err != nil || path == "" {
			sms = fmt.Sprintf("[P%d][%s] %d %s.  e.g. %s detail in email", arr[0].Priority, arr[0].Status, size, arr[0].Metric, eg)
			log.Println("get link fail", err)
		} else {
			links := g.Config().Api.Links
			sms = fmt.Sprintf("[P%d][%s] %d %s e.g. %s %s/%s ", arr[0].Priority, arr[0].Status, size, arr[0].Metric, eg, links, path)
		}

		redi.WriteSms([]string{arr[0].Phone}, sms)
	}

}
Esempio n. 7
0
func WriteQQModel(qq *model.QQ) {
	if qq == nil {
		return
	}

	bs, err := json.Marshal(qq)
	if err != nil {
		log.Println(err)
		return
	}

	LPUSH(g.Config().Queue.QQ, string(bs))
}
Esempio n. 8
0
func WriteMailModel(mail *model.Mail) {
	if mail == nil {
		return
	}

	bs, err := json.Marshal(mail)
	if err != nil {
		log.Println(err)
		return
	}

	LPUSH(g.Config().Queue.Mail, string(bs))
}
Esempio n. 9
0
func WriteSmsModel(sms *model.Sms) {
	if sms == nil {
		return
	}

	bs, err := json.Marshal(sms)
	if err != nil {
		log.Println(err)
		return
	}

	LPUSH(g.Config().Queue.Sms, string(bs))
}
Esempio n. 10
0
func CurlUic(team string) []*User {
	if team == "" {
		return []*User{}
	}

	uri := fmt.Sprintf("%s/team/users", g.Config().Api.Uic)
	req := httplib.Get(uri).SetTimeout(2*time.Second, 10*time.Second)
	req.Param("name", team)
	req.Param("token", g.Config().UicToken)

	var usersWrap UsersWrap
	err := req.ToJson(&usersWrap)
	if err != nil {
		log.Printf("curl %s fail: %v", uri, err)
		return nil
	}

	if usersWrap.Msg != "" {
		log.Printf("curl %s return msg: %v", uri, usersWrap.Msg)
		return nil
	}

	return usersWrap.Users
}
Esempio n. 11
0
func ReadLowEvent() {
	queues := g.Config().Redis.LowQueues
	if len(queues) == 0 {
		return
	}

	for {
		event, err := popEvent(queues)
		if err != nil {
			time.Sleep(time.Second)
			continue
		}
		consume(event, false)
	}
}
Esempio n. 12
0
func CurlAction(id int) *Action {
	if id <= 0 {
		return nil
	}

	uri := fmt.Sprintf("%s/api/action/%d", g.Config().Api.Portal, id)
	req := httplib.Get(uri).SetTimeout(5*time.Second, 30*time.Second)

	var actionWrap ActionWrap
	err := req.ToJson(&actionWrap)
	if err != nil {
		log.Printf("curl %s fail: %v", uri, err)
		return nil
	}

	if actionWrap.Msg != "" {
		log.Printf("curl %s return msg: %v", uri, actionWrap.Msg)
		return nil
	}

	return actionWrap.Data
}
Esempio n. 13
0
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
}