forked from aws/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icinga2.go
176 lines (140 loc) · 3.63 KB
/
icinga2.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
package icinga2
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
type Icinga2 struct {
Server string
ObjectType string
Username string
Password string
ResponseTimeout internal.Duration
tls.ClientConfig
Log telegraf.Logger
client *http.Client
}
type Result struct {
Results []Object `json:"results"`
}
type Object struct {
Attrs Attribute `json:"attrs"`
Name string `json:"name"`
Joins struct{} `json:"joins"`
Meta struct{} `json:"meta"`
Type ObjectType `json:"type"`
}
type Attribute struct {
CheckCommand string `json:"check_command"`
DisplayName string `json:"display_name"`
Name string `json:"name"`
State float64 `json:"state"`
}
var levels = []string{"ok", "warning", "critical", "unknown"}
type ObjectType string
var sampleConfig = `
## Required Icinga2 server address
# server = "https://localhost:5665"
## Required Icinga2 object type ("services" or "hosts")
# object_type = "services"
## Credentials for basic HTTP authentication
# username = "admin"
# password = "admin"
## Maximum time to receive response.
# response_timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = true
`
func (i *Icinga2) Description() string {
return "Gather Icinga2 status"
}
func (i *Icinga2) SampleConfig() string {
return sampleConfig
}
func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
for _, check := range checks {
url, err := url.Parse(i.Server)
if err != nil {
i.Log.Error(err.Error())
continue
}
state := int64(check.Attrs.State)
fields := map[string]interface{}{
"name": check.Attrs.Name,
"state_code": state,
}
tags := map[string]string{
"display_name": check.Attrs.DisplayName,
"check_command": check.Attrs.CheckCommand,
"state": levels[state],
"source": url.Hostname(),
"scheme": url.Scheme,
"port": url.Port(),
}
acc.AddFields(fmt.Sprintf("icinga2_%s", i.ObjectType), fields, tags)
}
}
func (i *Icinga2) createHttpClient() (*http.Client, error) {
tlsCfg, err := i.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
},
Timeout: i.ResponseTimeout.Duration,
}
return client, nil
}
func (i *Icinga2) Gather(acc telegraf.Accumulator) error {
if i.ResponseTimeout.Duration < time.Second {
i.ResponseTimeout.Duration = time.Second * 5
}
if i.client == nil {
client, err := i.createHttpClient()
if err != nil {
return err
}
i.client = client
}
url := fmt.Sprintf("%s/v1/objects/%s?attrs=name&attrs=display_name&attrs=state&attrs=check_command", i.Server, i.ObjectType)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
if i.Username != "" {
req.SetBasicAuth(i.Username, i.Password)
}
resp, err := i.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
result := Result{}
json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return err
}
i.GatherStatus(acc, result.Results)
return nil
}
func init() {
inputs.Add("icinga2", func() telegraf.Input {
return &Icinga2{
Server: "https://localhost:5665",
ObjectType: "services",
ResponseTimeout: internal.Duration{Duration: time.Second * 5},
}
})
}