-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (105 loc) · 3.36 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/cxnam/prometheus-pusher/pkg/logger"
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
"github.com/ghodss/yaml"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/cxnam/prometheus-pusher/pkg/config"
)
type queryConfig map[string]string
var (
queryConfigFile = flag.String("c", "queries.yaml", "Query config file")
metricInterval = flag.Duration("i", 10*time.Second, "Metric push interval")
// logger = log.With(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)), "caller", log.DefaultCaller)
log = logger.GetLogger("push-status-page")
httpClient = &http.Client{}
)
func main() {
flag.Parse()
qConfig := queryConfig{}
qcd, err := ioutil.ReadFile(*queryConfigFile)
if err != nil {
log.Infof("msg", "Couldn't read config file", "error", err.Error())
}
if err := yaml.Unmarshal(qcd, &qConfig); err != nil {
log.Infof("msg", "Couldn't parse config file", "error", err.Error())
}
// prometheusURL := fmt.Sprintf(config.Config.Systemmetric.Prometheusurl)
// client, err := api.NewClient(api.Config{Address: *prometheusURL})
client, err := api.NewClient(api.Config{Address: config.Config.Systemmetric.Prometheusurl})
if err != nil {
log.Infof("msg", "Couldn't create Prometheus client", "error", err.Error())
}
api := v1.NewAPI(client)
for {
for metricID, query := range qConfig {
ts := time.Now()
resp, warnings, err := api.Query(context.Background(), query, ts)
if err != nil {
log.Infof("msg", "Couldn't query Prometheus", "error", err.Error())
continue
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
vec := resp.(model.Vector)
if l := vec.Len(); l != 1 {
log.Infof("msg", "Expected query to return single value", "samples", l)
continue
}
value := vec[0].Value
if "NaN" == value.String() {
log.Infof("msg", "Expected query to return", value)
value = 0
// continue
}
log.Infof(metricID, value)
if err := sendStatusPage(ts, metricID, float64(value)); err != nil {
log.Infof("msg", "Couldn't send metric to Statuspage", "error", err.Error())
continue
}
}
time.Sleep(*metricInterval)
}
}
func sendStatusPage(ts time.Time, metricID string, value float64) error {
values := url.Values{
"data[timestamp]": []string{strconv.FormatInt(ts.Unix(), 10)},
"data[value]": []string{strconv.FormatFloat(value, 'f', -1, 64)},
}
url := config.Config.Systemmetric.Statuspageurl + path.Join("/v1", "pages", config.Config.Systemmetric.Statuspageid, "metrics", metricID, "data.json")
req, err := http.NewRequest("POST", url, strings.NewReader(values.Encode()))
if err != nil {
return err
}
req.Header.Set("Authorization", "OAuth "+config.Config.Systemmetric.Statuspagetoken)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req = req.WithContext(ctx)
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
respStr, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Empty API Error")
}
return errors.New("API Error: " + string(respStr))
}
return nil
}