|
| 1 | +package icinga2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/influxdata/telegraf" |
| 12 | + "github.com/influxdata/telegraf/internal" |
| 13 | + "github.com/influxdata/telegraf/internal/tls" |
| 14 | + "github.com/influxdata/telegraf/plugins/inputs" |
| 15 | +) |
| 16 | + |
| 17 | +type Icinga2 struct { |
| 18 | + Server string |
| 19 | + ObjectType string |
| 20 | + Username string |
| 21 | + Password string |
| 22 | + ResponseTimeout internal.Duration |
| 23 | + tls.ClientConfig |
| 24 | + |
| 25 | + client *http.Client |
| 26 | +} |
| 27 | + |
| 28 | +type Result struct { |
| 29 | + Results []Object `json:"results"` |
| 30 | +} |
| 31 | + |
| 32 | +type Object struct { |
| 33 | + Attrs Attribute `json:"attrs"` |
| 34 | + Name string `json:"name"` |
| 35 | + Joins struct{} `json:"joins"` |
| 36 | + Meta struct{} `json:"meta"` |
| 37 | + Type ObjectType `json:"type"` |
| 38 | +} |
| 39 | + |
| 40 | +type Attribute struct { |
| 41 | + CheckCommand string `json:"check_command"` |
| 42 | + DisplayName string `json:"display_name"` |
| 43 | + Name string `json:"name"` |
| 44 | + State int `json:"state"` |
| 45 | +} |
| 46 | + |
| 47 | +var levels = []string{"ok", "warning", "critical", "unknown"} |
| 48 | + |
| 49 | +type ObjectType string |
| 50 | + |
| 51 | +var sampleConfig = ` |
| 52 | + ## Required Icinga2 server address (default: "https://localhost:5665") |
| 53 | + # server = "https://localhost:5665" |
| 54 | + |
| 55 | + ## Required Icinga2 object type ("services" or "hosts, default "services") |
| 56 | + # object_type = "services" |
| 57 | +
|
| 58 | + ## Credentials for basic HTTP authentication |
| 59 | + # username = "admin" |
| 60 | + # password = "admin" |
| 61 | +
|
| 62 | + ## Maximum time to receive response. |
| 63 | + # response_timeout = "5s" |
| 64 | +
|
| 65 | + ## Optional TLS Config |
| 66 | + # tls_ca = "/etc/telegraf/ca.pem" |
| 67 | + # tls_cert = "/etc/telegraf/cert.pem" |
| 68 | + # tls_key = "/etc/telegraf/key.pem" |
| 69 | + ## Use TLS but skip chain & host verification |
| 70 | + # insecure_skip_verify = true |
| 71 | + ` |
| 72 | + |
| 73 | +func (i *Icinga2) Description() string { |
| 74 | + return "Gather Icinga2 status" |
| 75 | +} |
| 76 | + |
| 77 | +func (i *Icinga2) SampleConfig() string { |
| 78 | + return sampleConfig |
| 79 | +} |
| 80 | + |
| 81 | +func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) { |
| 82 | + for _, check := range checks { |
| 83 | + fields := make(map[string]interface{}) |
| 84 | + tags := make(map[string]string) |
| 85 | + |
| 86 | + url, err := url.Parse(i.Server) |
| 87 | + if err != nil { |
| 88 | + log.Fatal(err) |
| 89 | + } |
| 90 | + |
| 91 | + fields["name"] = check.Attrs.Name |
| 92 | + fields["state_code"] = check.Attrs.State |
| 93 | + |
| 94 | + tags["display_name"] = check.Attrs.DisplayName |
| 95 | + tags["check_command"] = check.Attrs.CheckCommand |
| 96 | + tags["state"] = levels[check.Attrs.State] |
| 97 | + tags["source"] = url.Hostname() |
| 98 | + tags["scheme"] = url.Scheme |
| 99 | + tags["port"] = url.Port() |
| 100 | + |
| 101 | + acc.AddFields(fmt.Sprintf("icinga2_%s", i.ObjectType), fields, tags) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (i *Icinga2) createHttpClient() (*http.Client, error) { |
| 106 | + tlsCfg, err := i.ClientConfig.TLSConfig() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + client := &http.Client{ |
| 112 | + Transport: &http.Transport{ |
| 113 | + TLSClientConfig: tlsCfg, |
| 114 | + }, |
| 115 | + Timeout: i.ResponseTimeout.Duration, |
| 116 | + } |
| 117 | + |
| 118 | + return client, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (i *Icinga2) Gather(acc telegraf.Accumulator) error { |
| 122 | + if i.ResponseTimeout.Duration < time.Second { |
| 123 | + i.ResponseTimeout.Duration = time.Second * 5 |
| 124 | + } |
| 125 | + |
| 126 | + if i.client == nil { |
| 127 | + client, err := i.createHttpClient() |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + i.client = client |
| 132 | + } |
| 133 | + |
| 134 | + url := fmt.Sprintf("%s/v1/objects/%s?attrs=name&attrs=display_name&attrs=state&attrs=check_command", i.Server, i.ObjectType) |
| 135 | + |
| 136 | + req, err := http.NewRequest("GET", url, nil) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + if i.Username != "" { |
| 142 | + req.SetBasicAuth(i.Username, i.Password) |
| 143 | + } |
| 144 | + |
| 145 | + resp, err := i.client.Do(req) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + defer resp.Body.Close() |
| 151 | + |
| 152 | + result := Result{} |
| 153 | + json.NewDecoder(resp.Body).Decode(&result) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + i.GatherStatus(acc, result.Results) |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func init() { |
| 164 | + inputs.Add("icinga2", func() telegraf.Input { |
| 165 | + return &Icinga2{ |
| 166 | + Server: "https://localhost:5665", |
| 167 | + ObjectType: "services", |
| 168 | + } |
| 169 | + }) |
| 170 | +} |
0 commit comments