This repository has been archived by the owner on Jul 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
185 lines (167 loc) · 3.94 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/influxdata/influxdb/client/v2"
"github.com/spf13/viper"
"github.com/terorie/netdata-influx/netdata"
)
var influx client.Client
const (
ConfLogTimestamps = "log_timestamps"
ConfInfluxAddr = "influx_addr"
ConfInfluxDB = "influx_db"
ConfRefresh = "refresh"
ConfNetdata = "netdata_api"
ConfHostTag = "host_tag"
ConfCharts = "charts"
ConfPoints = "points"
)
func main() {
viper.SetDefault(ConfRefresh, 10 * time.Second)
viper.SetDefault(ConfLogTimestamps, true)
viper.SetDefault(ConfPoints, 0)
viper.SetDefault(ConfCharts, []string{
"system.cpu",
"system.net",
"system.pgpgio",
})
viper.SetEnvPrefix("ni")
viper.AutomaticEnv()
viper.SetDefault(ConfHostTag, viper.GetString(ConfNetdata))
if !viper.GetBool(ConfLogTimestamps) {
log.SetFlags(0)
}
if viper.GetString(ConfInfluxAddr) == "" {
log.Fatal("$NI_INFLUX_ADDR not set")
}
if viper.GetString(ConfInfluxDB) == "" {
log.Fatal("$NI_INFLUX_DB not set")
}
if viper.GetString(ConfNetdata) == "" {
log.Fatal("$NI_NETDATA_API not set")
}
var err error
influx, err = client.NewHTTPClient(client.HTTPConfig{
Addr: viper.GetString(ConfInfluxAddr),
})
if err != nil {
log.Fatal(err)
}
defer closeInflux()
for range time.Tick(viper.GetDuration(ConfRefresh)) {
charts := viper.GetStringSlice(ConfCharts)
for _, chart := range charts {
res, err := getChart(chart)
if err != nil {
log.Printf("Failed to get chart %s: %s", chart, err)
continue
}
if err := pushChart(res); err != nil {
log.Printf("Failed to push chart %s: %s", chart, err)
}
}
}
}
func getChart(chart string) (*netdata.Response, error) {
builder := netdata.RequestBuilder{
BaseURL: viper.GetString(ConfNetdata),
Chart: chart,
Points: viper.GetInt(ConfPoints),
After: -viper.GetInt(ConfPoints),
}
req, err := builder.Build()
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http %s", res.Status)
}
dec := json.NewDecoder(res.Body)
ndRes := new(netdata.Response)
err = dec.Decode(&ndRes)
if err != nil {
return nil, err
}
if ndRes.API != 1 {
return nil, fmt.Errorf("unexpected data at %s", req.URL.String())
}
return ndRes, err
}
func pushChart(res *netdata.Response) error {
// Get number of columns
nCols := len(res.Result.Labels)
if nCols == 0 {
return fmt.Errorf("no columns in result")
}
// Find index of time field
timeIndex := -1
for i, label := range res.Result.Labels {
if label == "time" {
timeIndex = i
}
}
if timeIndex < 0 {
return fmt.Errorf("no time column in result")
}
// Create batch
batch, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: viper.GetString(ConfInfluxDB),
Precision: "s",
})
if err != nil {
return err
}
host := viper.GetString(ConfHostTag)
// Fill batch with data
for rowNr, row := range res.Result.Data {
if len(row) != nCols {
return fmt.Errorf("malformed row")
}
// Read timestamp from "time" column
unix, err := row[timeIndex].Int64()
if err != nil { return err }
timestamp := time.Unix(unix, 0)
// Create a point for each dimension
for i, col := range row {
if i == timeIndex {
continue
}
var val float64
if col == "" {
val = 0
} else {
val, err = col.Float64()
if err != nil {
log.Printf("Chart %s: invalid data value at row %d: %s", res.ID, rowNr, err)
continue
}
}
tags := map[string]string{
"host": host,
"dimension": res.Result.Labels[i],
}
fields := map[string]interface{}{
"value": val,
}
point, err := client.NewPoint(res.ID, tags, fields, timestamp)
if err != nil { return err }
batch.AddPoint(point)
}
}
// Write batch
return influx.Write(batch)
}
func closeInflux() {
if err := influx.Close(); err != nil {
log.Println("Error closing influx client", err)
}
}