-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexporter.go
More file actions
134 lines (117 loc) · 3.41 KB
/
exporter.go
File metadata and controls
134 lines (117 loc) · 3.41 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
package main
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type Exporter struct {
Hostname string
AccessKey string
}
func NewExporter(hostname string, accessKey string) *Exporter {
return &Exporter{
Hostname: hostname,
AccessKey: accessKey,
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- up
ch <- scrapeDuration
ch <- inputStatus
ch <- cloudServiceStatus
ch <- mqttServerStatus
ch <- freeMemory
ch <- freeDiskSpace
ch <- systemStatus
ch <- loggedErrors
ch <- redundancyStatus
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
status := e.Scrape(ch)
duration := time.Since(start)
ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, status)
ch <- prometheus.MustNewConstMetric(scrapeDuration, prometheus.GaugeValue, duration.Seconds())
}
func (e *Exporter) Scrape(ch chan<- prometheus.Metric) float64 {
errors := 0
// Get alarm inputs
inputResponse, err := QueryInputs(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
for _, input := range *inputResponse {
for _, state := range []string{"OK", "ERROR", "NOT_USED"} {
ch <- prometheus.MustNewConstMetric(
inputStatus, prometheus.GaugeValue, CheckState(input.State, state), input.Name, input.Identifier, state,
)
}
}
for _, input := range *inputResponse {
if v, err := input.GetValue(); err == nil {
ch <- prometheus.MustNewConstMetric(
inputValue, prometheus.GaugeValue, v, input.Name, input.Identifier,
)
}
}
}
// Get cloud services
serviceResponse, err := QueryCloudServices(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
for _, service := range *serviceResponse {
for _, state := range []string{"OK", "ERROR"} {
ch <- prometheus.MustNewConstMetric(
cloudServiceStatus, prometheus.GaugeValue, CheckState(service.State, state), service.Name, state,
)
}
}
}
// System status
statusResponse, err := QueryStatus(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
ch <- prometheus.MustNewConstMetric(loggedErrors, prometheus.GaugeValue, statusResponse.NbrOfLoggedErrors)
for _, state := range []string{"OK", "WARN", "ERROR"} {
ch <- prometheus.MustNewConstMetric(systemStatus, prometheus.GaugeValue, CheckState(statusResponse.State, state), state)
}
for _, state := range []string{"OK", "WARN"} {
ch <- prometheus.MustNewConstMetric(redundancyStatus, prometheus.GaugeValue, CheckState(statusResponse.RedundancyState.State, state), state)
}
}
// MQTT status
mqttResponse, err := QueryMQTTServer(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
for _, server := range *mqttResponse {
for _, state := range []string{"OK", "ERROR", "NOT_USED"} {
ch <- prometheus.MustNewConstMetric(
mqttServerStatus, prometheus.GaugeValue, CheckState(server.State, state), server.Name, state,
)
}
}
}
// Storage/Memory status
systemReponse, err := QuerySystem(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
ch <- prometheus.MustNewConstMetric(freeMemory, prometheus.GaugeValue, systemReponse.FreeMemory)
for _, disk := range systemReponse.Disks {
ch <- prometheus.MustNewConstMetric(freeDiskSpace, prometheus.GaugeValue, disk.FreeSpace, disk.DriveLetter)
}
}
if errors == 0 {
return 1
} else {
return 0
}
}