-
Notifications
You must be signed in to change notification settings - Fork 34
/
templating.go
213 lines (178 loc) · 4.9 KB
/
templating.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package rest
import (
"fmt"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/pkg/util"
"regexp"
"strconv"
"strings"
"time"
)
func (r *Rest) LoadTemplate() (string, error) {
template, path, err := r.ImportSubTemplate("", TemplateFn(r.Params, r.Object), r.Client.Cluster().Version)
if err != nil {
return "", err
}
r.Params.Union(template)
return path, nil
}
func (r *Rest) InitCache() error {
var (
counters *node.Node
)
if x := r.Params.GetChildContentS("object"); x != "" {
r.Prop.Object = x
} else {
r.Prop.Object = strings.ToLower(r.Object)
}
if e := r.Params.GetChildS("export_options"); e != nil {
r.Matrix[r.Object].SetExportOptions(e)
}
if r.Prop.Query = r.Params.GetChildContentS("query"); r.Prop.Query == "" {
return errs.New(errs.ErrMissingParam, "query")
}
// create metric cache
if counters = r.Params.GetChildS("counters"); counters == nil {
return errs.New(errs.ErrMissingParam, "counters")
}
// default value for ONTAP is 15 sec
if returnTimeout := r.Params.GetChildContentS("return_timeout"); returnTimeout != "" {
iReturnTimeout, err := strconv.Atoi(returnTimeout)
if err != nil {
r.Logger.Warn().Str("returnTimeout", returnTimeout).Msg("Invalid value of returnTimeout")
} else {
r.Prop.ReturnTimeOut = &iReturnTimeout
}
}
// private end point do not support * as fields. We need to pass fields in endpoint
query := r.Params.GetChildS("query")
r.Prop.IsPublic = true
if query != nil {
r.Prop.IsPublic = util.IsPublicAPI(query.GetContentS())
}
r.ParseRestCounters(counters, r.Prop)
r.Logger.Debug().
Strs("extracted Instance Keys", r.Prop.InstanceKeys).
Int("numMetrics", len(r.Prop.Metrics)).
Int("numLabels", len(r.Prop.InstanceLabels)).
Msg("Initialized metric cache")
return nil
}
func HandleDuration(value string) float64 {
// Example: duration: PT8H35M42S
timeDurationRegex := `^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:.\d+)?)S)?$`
regexTimeDuration := regexp.MustCompile(timeDurationRegex)
if match := regexTimeDuration.MatchString(value); match {
// example: PT8H35M42S ==> 30942
matches := regexTimeDuration.FindStringSubmatch(value)
if matches == nil {
return 0
}
seconds := 0.0
// years
// months
// days
if matches[3] != "" {
f, err := strconv.ParseFloat(matches[3], 64)
if err != nil {
fmt.Printf("%v", err)
return 0
}
seconds += f * 24 * 60 * 60
}
// hours
if matches[4] != "" {
f, err := strconv.ParseFloat(matches[4], 64)
if err != nil {
fmt.Printf("%v", err)
return 0
}
seconds += f * 60 * 60
}
// minutes
if matches[5] != "" {
f, err := strconv.ParseFloat(matches[5], 64)
if err != nil {
fmt.Printf("%v", err)
return 0
}
seconds += f * 60
}
// seconds & milliseconds
if matches[6] != "" {
f, err := strconv.ParseFloat(matches[6], 64)
if err != nil {
fmt.Printf("%v", err)
return 0
}
seconds += f
}
return seconds
}
return 0
}
// Example: timestamp: 2020-12-02T18:36:19-08:00
var regexTimeStamp = regexp.MustCompile(
`[+-]?\d{4}(-[01]\d(-[0-3]\d(T[0-2]\d:[0-5]\d:?([0-5]\d(\.\d+)?)?[+-][0-2]\d:[0-5]\d?)?)?)?`)
func HandleTimestamp(value string) float64 {
var timestamp time.Time
var err error
if match := regexTimeStamp.MatchString(value); match {
// example: 2020-12-02T18:36:19-08:00 ==> 1606962979
if timestamp, err = time.Parse(time.RFC3339, value); err != nil {
fmt.Printf("%v", err)
return 0
}
return float64(timestamp.Unix())
}
return 0
}
func (r *Rest) ParseRestCounters(counter *node.Node, prop *prop) {
var (
display, name, kind, metricType string
)
instanceKeys := make(map[string]string)
for _, c := range counter.GetAllChildContentS() {
if c != "" {
name, display, kind, metricType = util.ParseMetric(c)
prop.Counters[name] = display
switch kind {
case "key":
prop.InstanceLabels[name] = display
instanceKeys[display] = name
case "label":
prop.InstanceLabels[name] = display
case "float":
m := &Metric{Label: display, Name: name, MetricType: metricType, Exportable: true}
prop.Metrics[name] = m
}
}
}
// populate prop.instanceKeys
// sort keys by display name. This is needed to match counter and endpoints keys
keys := util.GetSortedKeys(instanceKeys)
// Append instance keys to prop
for _, k := range keys {
prop.InstanceKeys = append(prop.InstanceKeys, instanceKeys[k])
}
counterKey := make([]string, len(prop.Counters))
i := 0
for k := range prop.Counters {
counterKey[i] = k
i++
}
prop.Fields = counterKey
if counter != nil {
if x := counter.GetChildS("filter"); x != nil {
prop.Filter = append(prop.Filter, x.GetAllChildContentS()...)
}
}
if prop.IsPublic {
if counter != nil {
if x := counter.GetChildS("hidden_fields"); x != nil {
prop.HiddenFields = append(prop.HiddenFields, x.GetAllChildContentS()...)
}
}
}
}