src: Add DMA localagent
[barometer.git] / src / dma / cmd / threshold / transmit.go
1 /*
2  * Copyright 2018 NEC Corporation
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16
17 package main
18
19 import (
20         "bytes"
21         "fmt"
22         "github.com/distributed-monitoring/agent/pkg/common"
23         "github.com/go-redis/redis"
24         "strconv"
25         "strings"
26         "time"
27 )
28
29 type collectdNotifier struct {
30         pluginName string
31         typeName   string
32 }
33
34 func send(cn collectdNotifier, message string, severity string, metaData [][2]string) error {
35         unixNow := float64(time.Now().UnixNano()) / 1000000000
36
37         var metaDataStr bytes.Buffer
38         for _, data := range metaData {
39                 metaDataStr.WriteString(" s:")
40                 metaDataStr.WriteString(data[0])
41                 metaDataStr.WriteString("=\"")
42                 metaDataStr.WriteString(strings.Replace(data[1], "\"", "\\\"", -1))
43                 metaDataStr.WriteString("\"")
44         }
45
46         fmt.Printf("PUTNOTIF message=\"%s\" severity=%s time=%f "+
47                 "host=localhost plugin=%s type=%s %s\n",
48                 message, severity, unixNow, cn.pluginName, cn.typeName, metaDataStr.String())
49
50         return nil
51 }
52
53 func transmit(config *Config, edlist []evalData) {
54         annoConfig := config.Common
55
56         client := redis.NewClient(&redis.Options{
57                 Addr:     annoConfig.RedisHost + ":" + annoConfig.RedisPort,
58                 Password: annoConfig.RedisPassword,
59                 DB:       annoConfig.RedisDB,
60         })
61         pool := common.RedisPool{Client: client}
62
63         notifier := collectdNotifier{
64                 pluginName: config.Threshold.CollectdPlugin,
65                 typeName:   config.Threshold.CollectdType}
66
67         for _, ed := range edlist {
68                 if ed.label == 1 {
69
70                         fmt.Println("kick action")
71
72                         item := strings.Split(ed.key, "/")
73                         ifItem := strings.SplitN(item[3], "-", 2)
74                         virtName := item[1]
75                         virtIF := ifItem[1]
76
77                         var message bytes.Buffer
78                         message.WriteString("Value exceeded threshold ")
79                         message.WriteString(strconv.Itoa(config.Threshold.Min))
80                         message.WriteString(".")
81
82                         nameVal, _ := pool.Get(fmt.Sprintf("%s/%s/vminfo", "vm", virtName))
83                         ifVal, _ := pool.Get(fmt.Sprintf("%s/%s/neutron_network", "if", virtIF))
84
85                         nameInfo := fmt.Sprintf("{\"%s\": %s}", virtName, nameVal)
86                         ifInfo := fmt.Sprintf("{\"%s\": %s}", virtIF, ifVal)
87
88                         fmt.Println(nameInfo)
89                         fmt.Println(ifInfo)
90
91                         send(notifier, message.String(),
92                                 "warning",
93                                 [][2]string{{"vminfo", nameInfo}, {"neutron_network", ifInfo}})
94
95                 }
96         }
97 }