2 * Copyright 2018 NEC Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 "github.com/distributed-monitoring/agent/pkg/common"
23 "github.com/go-redis/redis"
29 type collectdNotifier struct {
34 func send(cn collectdNotifier, message string, severity string, metaData [][2]string) error {
35 unixNow := float64(time.Now().UnixNano()) / 1000000000
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("\"")
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())
53 func transmit(config *Config, edlist []evalData) {
54 annoConfig := config.Common
56 client := redis.NewClient(&redis.Options{
57 Addr: annoConfig.RedisHost + ":" + annoConfig.RedisPort,
58 Password: annoConfig.RedisPassword,
59 DB: annoConfig.RedisDB,
61 pool := common.RedisPool{Client: client}
63 notifier := collectdNotifier{
64 pluginName: config.Threshold.CollectdPlugin,
65 typeName: config.Threshold.CollectdType}
67 for _, ed := range edlist {
70 fmt.Println("kick action")
72 item := strings.Split(ed.key, "/")
73 ifItem := strings.SplitN(item[3], "-", 2)
77 var message bytes.Buffer
78 message.WriteString("Value exceeded threshold ")
79 message.WriteString(strconv.Itoa(config.Threshold.Min))
80 message.WriteString(".")
82 nameVal, _ := pool.Get(fmt.Sprintf("%s/%s/vminfo", "vm", virtName))
83 ifVal, _ := pool.Get(fmt.Sprintf("%s/%s/neutron_network", "if", virtIF))
85 nameInfo := fmt.Sprintf("{\"%s\": %s}", virtName, nameVal)
86 ifInfo := fmt.Sprintf("{\"%s\": %s}", virtIF, ifVal)
91 send(notifier, message.String(),
93 [][2]string{{"vminfo", nameInfo}, {"neutron_network", ifInfo}})