forked from Cepave/open-falcon-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarshal.go
More file actions
151 lines (133 loc) · 4.23 KB
/
Copy pathmarshal.go
File metadata and controls
151 lines (133 loc) · 4.23 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/Cepave/open-falcon-backend/common/model"
)
type ParamToAgent struct {
Metric string `json:"metric"`
Endpoint string `json:"endpoint"`
Value interface{} `json:"value"`
CounterType string `json:"counterType"`
Tags string `json:"tags"`
Timestamp int64 `json:"timestamp"`
Step int64 `json:"step"`
}
func (p ParamToAgent) String() string {
return fmt.Sprintf(
" {metric: %v, endpoint: %v, value: %v, counterType:%v, tags:%v, timestamp:%d, step:%d}",
p.Metric,
p.Endpoint,
p.Value,
p.CounterType,
p.Tags,
p.Timestamp,
p.Step,
)
}
type nqmNodeData struct {
Id string
IspId string
ProvinceId string
CityId string
NameTagId string
GroupTagIds string
}
func marshalJSONParamsToCassandra(nqmDataGram string, metric string) ParamToAgent {
data := ParamToAgent{}
data.Tags = nqmDataGram
data.Metric = metric
data.Timestamp = time.Now().Unix()
data.Endpoint = Meta().Hostname
data.Value = "0"
data.CounterType = "GAUGE"
data.Step = int64(60) // a useless field in Cassandra
return data
}
func convToKeyValueString(arg map[string]string) string {
Str := ""
for key, value := range arg {
Str = Str + "," + key + "=" + value
}
return Str
}
func assembleTags(target nqmNodeData, agent nqmNodeData, dataMap map[string]string) string {
return "agent-id=" + agent.Id +
",agent-isp-id=" + agent.IspId +
",agent-province-id=" + agent.ProvinceId +
",agent-city-id=" + agent.CityId +
",agent-name-tag-id=" + agent.NameTagId +
",agent-group-tag-ids=" + agent.GroupTagIds +
",target-id=" + target.Id +
",target-isp-id=" + target.IspId +
",target-province-id=" + target.ProvinceId +
",target-city-id=" + target.CityId +
",target-name-tag-id=" + target.NameTagId +
",target-group-tag-ids=" + target.GroupTagIds +
convToKeyValueString(dataMap)
}
func convIntSlcToStr(args []int32) string {
strSlc := []string{}
for _, arg := range args {
strSlc = append(strSlc, strconv.Itoa(int(arg)))
}
return strings.Join(strSlc, "-")
}
func convToNqmAgent(s model.NqmAgent) nqmNodeData {
return nqmNodeData{
Id: strconv.Itoa(s.Id),
IspId: strconv.Itoa(int(s.IspId)),
ProvinceId: strconv.Itoa(int(s.ProvinceId)),
CityId: strconv.Itoa(int(s.CityId)),
NameTagId: strconv.Itoa(int(s.NameTagId)),
GroupTagIds: convIntSlcToStr(s.GroupTagIds),
}
}
func convToNqmTarget(s model.NqmTarget) nqmNodeData {
return nqmNodeData{
Id: strconv.Itoa(s.Id),
IspId: strconv.Itoa(int(s.IspId)),
ProvinceId: strconv.Itoa(int(s.ProvinceId)),
CityId: strconv.Itoa(int(s.CityId)),
NameTagId: strconv.Itoa(int(s.NameTagId)),
GroupTagIds: convIntSlcToStr(s.GroupTagIds),
}
}
/**
* value could be:
* Packet Loss - int
* Transmission Time - float64
*/
func marshalJSONToGraph(target model.NqmTarget, agent model.NqmAgent, metric string, value interface{}, step int64) ParamToAgent {
endpoint := Meta().Hostname
counterType := "GAUGE"
tags := "nqm-agent-isp=" + agent.IspName +
",nqm-agent-province=" + agent.ProvinceName +
",nqm-agent-city=" + agent.CityName +
",target-ip=" + target.Host +
",target-isp=" + target.IspName +
",target-province=" + target.ProvinceName +
",target-city=" + target.CityName +
",target-name-tag=" + target.NameTag
timestamp := time.Now().Unix()
return ParamToAgent{metric, endpoint, value, counterType, tags, timestamp, step}
}
func marshalStatsRow(row map[string]string, target model.NqmTarget, agent model.NqmAgent, step int64, u Utility) []ParamToAgent {
var params []ParamToAgent
params = append(params, u.MarshalJSONParamsToGraph(target, agent, row, step)...)
t := convToNqmTarget(target)
a := convToNqmAgent(agent)
cassandraDataGram := assembleTags(t, a, row)
params = append(params, marshalJSONParamsToCassandra(cassandraDataGram, "nqm-"+u.UtilName()))
return params
}
func Marshal(statsData []map[string]string, u Utility, targets []model.NqmTarget, agent model.NqmAgent, step int64) []ParamToAgent {
var params []ParamToAgent
for rowIdx, statsRow := range statsData {
target := targets[rowIdx]
params = append(params, marshalStatsRow(statsRow, target, agent, step, u)...)
}
return params
}