|
| 1 | +// Copyright 2021 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package collector |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "io/ioutil" |
| 20 | + "net/http" |
| 21 | + "net/url" |
| 22 | + "path" |
| 23 | + |
| 24 | + "github.com/go-kit/log" |
| 25 | + "github.com/go-kit/log/level" |
| 26 | + "github.com/prometheus/client_golang/prometheus" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + ilm_statuses = []string{"STOPPED", "RUNNING", "STOPPING"} |
| 31 | +) |
| 32 | + |
| 33 | +type ilmStatusMetric struct { |
| 34 | + Type prometheus.ValueType |
| 35 | + Desc *prometheus.Desc |
| 36 | + Value func(ilm *IlmStatusResponse, status string) float64 |
| 37 | + Labels func(status string) []string |
| 38 | +} |
| 39 | + |
| 40 | +// IlmStatusCollector information struct |
| 41 | +type IlmStatusCollector struct { |
| 42 | + logger log.Logger |
| 43 | + client *http.Client |
| 44 | + url *url.URL |
| 45 | + |
| 46 | + up prometheus.Gauge |
| 47 | + totalScrapes, jsonParseFailures prometheus.Counter |
| 48 | + |
| 49 | + metric ilmStatusMetric |
| 50 | +} |
| 51 | + |
| 52 | +// NewIlmStatus defines Indices IndexIlms Prometheus metrics |
| 53 | +func NewIlmStatus(logger log.Logger, client *http.Client, url *url.URL) *IlmStatusCollector { |
| 54 | + subsystem := "ilm" |
| 55 | + |
| 56 | + return &IlmStatusCollector{ |
| 57 | + logger: logger, |
| 58 | + client: client, |
| 59 | + url: url, |
| 60 | + |
| 61 | + up: prometheus.NewGauge(prometheus.GaugeOpts{ |
| 62 | + Name: prometheus.BuildFQName(namespace, subsystem, "up"), |
| 63 | + Help: "Was the last scrape of the ElasticSearch Indices Ilms endpoint successful.", |
| 64 | + }), |
| 65 | + totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{ |
| 66 | + Name: prometheus.BuildFQName(namespace, subsystem, "scrapes_total"), |
| 67 | + Help: "Current total ElasticSearch Indices Ilms scrapes.", |
| 68 | + }), |
| 69 | + jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{ |
| 70 | + Name: prometheus.BuildFQName(namespace, subsystem, "json_parse_failures_total"), |
| 71 | + Help: "Number of errors while parsing JSON.", |
| 72 | + }), |
| 73 | + metric: ilmStatusMetric{ |
| 74 | + Type: prometheus.GaugeValue, |
| 75 | + Desc: prometheus.NewDesc( |
| 76 | + prometheus.BuildFQName(namespace, subsystem, "status"), |
| 77 | + "Current status of ilm. Status can be STOPPED, RUNNING, STOPPING.", |
| 78 | + ilm_statuses, nil, |
| 79 | + ), |
| 80 | + Value: func(ilm *IlmStatusResponse, status string) float64 { |
| 81 | + if ilm.OperationMode == status { |
| 82 | + return 1 |
| 83 | + } |
| 84 | + return 0 |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Describe add Snapshots metrics descriptions |
| 91 | +func (im *IlmStatusCollector) Describe(ch chan<- *prometheus.Desc) { |
| 92 | + ch <- im.metric.Desc |
| 93 | + ch <- im.up.Desc() |
| 94 | + ch <- im.totalScrapes.Desc() |
| 95 | + ch <- im.jsonParseFailures.Desc() |
| 96 | +} |
| 97 | + |
| 98 | +func (im *IlmStatusCollector) getAndParseURL(u *url.URL) (*IlmStatusResponse, error) { |
| 99 | + res, err := im.client.Get(u.String()) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to get from %s://%s:%s%s: %s", |
| 102 | + u.Scheme, u.Hostname(), u.Port(), u.Path, err) |
| 103 | + } |
| 104 | + |
| 105 | + if res.StatusCode != http.StatusOK { |
| 106 | + return nil, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode) |
| 107 | + } |
| 108 | + |
| 109 | + body, err := ioutil.ReadAll(res.Body) |
| 110 | + if err != nil { |
| 111 | + _ = level.Warn(im.logger).Log("msg", "failed to read response body", "err", err) |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + err = res.Body.Close() |
| 116 | + if err != nil { |
| 117 | + _ = level.Warn(im.logger).Log("msg", "failed to close response body", "err", err) |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + var imr IlmStatusResponse |
| 122 | + if err := json.Unmarshal(body, &imr); err != nil { |
| 123 | + im.jsonParseFailures.Inc() |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + return &imr, nil |
| 128 | +} |
| 129 | + |
| 130 | +func (im *IlmStatusCollector) fetchAndDecodeIlm() (*IlmStatusResponse, error) { |
| 131 | + u := *im.url |
| 132 | + u.Path = path.Join(u.Path, "/_ilm/status") |
| 133 | + return im.getAndParseURL(&u) |
| 134 | +} |
| 135 | + |
| 136 | +// Collect gets all indices Ilms metric values |
| 137 | +func (im *IlmStatusCollector) Collect(ch chan<- prometheus.Metric) { |
| 138 | + |
| 139 | + im.totalScrapes.Inc() |
| 140 | + defer func() { |
| 141 | + ch <- im.up |
| 142 | + ch <- im.totalScrapes |
| 143 | + ch <- im.jsonParseFailures |
| 144 | + }() |
| 145 | + |
| 146 | + indicesIlmsResponse, err := im.fetchAndDecodeIlm() |
| 147 | + if err != nil { |
| 148 | + im.up.Set(0) |
| 149 | + _ = level.Warn(im.logger).Log( |
| 150 | + "msg", "failed to fetch and decode cluster ilm status", |
| 151 | + "err", err, |
| 152 | + ) |
| 153 | + return |
| 154 | + } |
| 155 | + im.up.Set(1) |
| 156 | + |
| 157 | + for _, status := range ilm_statuses { |
| 158 | + ch <- prometheus.MustNewConstMetric( |
| 159 | + im.metric.Desc, |
| 160 | + im.metric.Type, |
| 161 | + im.metric.Value(indicesIlmsResponse, status), |
| 162 | + status, |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments