aboutsummaryrefslogtreecommitdiffstats
path: root/src/dma/cmd/threshold/transmit.go
blob: 8c60cc1abb3e25a03d054801aa0b5faa7794791a (plain)
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
/*
 * Copyright 2018 NEC Corporation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package main

import (
	"bytes"
	"fmt"
	"github.com/go-redis/redis"
	"github.com/opnfv/barometer/src/dma/pkg/common"
	"strconv"
	"strings"
	"time"
)

type collectdNotifier struct {
	pluginName string
	typeName   string
}

func send(cn collectdNotifier, message string, severity string, metaData [][2]string) error {
	unixNow := float64(time.Now().UnixNano()) / 1000000000

	var metaDataStr bytes.Buffer
	for _, data := range metaData {
		metaDataStr.WriteString(" s:")
		metaDataStr.WriteString(data[0])
		metaDataStr.WriteString("=\"")
		metaDataStr.WriteString(strings.Replace(data[1], "\"", "\\\"", -1))
		metaDataStr.WriteString("\"")
	}

	fmt.Printf("PUTNOTIF message=\"%s\" severity=%s time=%f "+
		"host=localhost plugin=%s type=%s %s\n",
		message, severity, unixNow, cn.pluginName, cn.typeName, metaDataStr.String())

	return nil
}

func transmit(config *Config, edlist []evalData) {
	annoConfig := config.Common

	client := redis.NewClient(&redis.Options{
		Addr:     annoConfig.RedisHost + ":" + annoConfig.RedisPort,
		Password: annoConfig.RedisPassword,
		DB:       annoConfig.RedisDB,
	})
	pool := common.RedisPool{Client: client}

	notifier := collectdNotifier{
		pluginName: config.Threshold.CollectdPlugin,
		typeName:   config.Threshold.CollectdType}

	for _, ed := range edlist {
		if ed.label == 1 {

			fmt.Println("kick action")

			item := strings.Split(ed.key, "/")
			ifItem := strings.SplitN(item[3], "-", 2)
			virtName := item[1]
			virtIF := ifItem[1]

			var message bytes.Buffer
			message.WriteString("Value exceeded threshold ")
			message.WriteString(strconv.Itoa(config.Threshold.Min))
			message.WriteString(".")

			nameVal, _ := pool.Get(fmt.Sprintf("%s/%s/vminfo", "vm", virtName))
			ifVal, _ := pool.Get(fmt.Sprintf("%s/%s/neutron_network", "if", virtIF))

			nameInfo := fmt.Sprintf("{\"%s\": %s}", virtName, nameVal)
			ifInfo := fmt.Sprintf("{\"%s\": %s}", virtIF, ifVal)

			fmt.Println(nameInfo)
			fmt.Println(ifInfo)

			send(notifier, message.String(),
				"warning",
				[][2]string{{"vminfo", nameInfo}, {"neutron_network", ifInfo}})

		}
	}
}