forked from Griesbacher/nagflux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NagfluxPrintable.go
43 lines (39 loc) · 1.24 KB
/
NagfluxPrintable.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
package nagflux
import (
"fmt"
"github.com/griesbacher/nagflux/helper"
)
//Printable converts from nagfluxfile format to X
type Printable struct {
Table string
Timestamp string
tags map[string]string
fields map[string]string
}
//PrintForInfluxDB prints the data in influxdb lineformat
func (p Printable) PrintForInfluxDB(version string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
line := p.Table
if len(p.tags) > 0 {
line += fmt.Sprintf(`,%s`, helper.PrintMapAsString(p.tags, ",", "="))
}
line += " "
if len(p.fields) > 0 {
line += fmt.Sprintf(`%s`, helper.PrintMapAsString(p.fields, ",", "="))
}
return fmt.Sprintf("%s %s", line, p.Timestamp)
}
return ""
}
//PrintForElasticsearch prints in the elasticsearch json format
func (p Printable) PrintForElasticsearch(version, index string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("2.0") {
head := fmt.Sprintf(`{"index":{"_index":"%s","_type":"%s"}}`, helper.GenIndex(index, p.Timestamp), p.Table) + "\n"
data := fmt.Sprintf(`{"timestamp":%s`, p.Timestamp)
data += helper.CreateJSONFromStringMap(p.tags)
data += helper.CreateJSONFromStringMap(p.fields)
data += "}\n"
return head + data
}
return ""
}