This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
forked from spagno/iscdhcpd_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iscdhcpd-exporter.go
320 lines (286 loc) · 9.97 KB
/
iscdhcpd-exporter.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2018 Andrea Spagnolo
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9367").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
dhcpdPidFile = kingpin.Flag("dhcpd.pid-file", "Path where dhcpd PID file is located.").Default("/var/run/dhcpd.pid").String()
dhcpdConfigFile = kingpin.Flag("dhcpd.config-file", "Path where dhcpd main config file (dhcpd.conf) is located.").Default("/etc/dhcp/dhcpd.conf").String()
dhcpdLeaseFile = kingpin.Flag("dhcpd.lease-file", "Path where dhcpd lease file is located.").Default("/var/lib/dhcp/dhcpd.leases").String()
)
const (
namespace = "iscdhcpd"
)
type Exporter struct {
summaryFree *prometheus.Desc
summaryTouched *prometheus.Desc
summaryUsed *prometheus.Desc
summaryDefined *prometheus.Desc
subnetFree *prometheus.Desc
subnetTouched *prometheus.Desc
subnetUsed *prometheus.Desc
subnetDefined *prometheus.Desc
sharedNetworkFree *prometheus.Desc
sharedNetworkTouched *prometheus.Desc
sharedNetworkUsed *prometheus.Desc
sharedNetworkDefined *prometheus.Desc
dhcpdUp *prometheus.Desc
scrapeFailures prometheus.Counter
}
type Subnet struct {
Location string `json:"location"`
Range string `json:"range"`
Defined float64 `json:"defined"`
Used float64 `json:"used"`
Touched float64 `json:"touched"`
Free float64 `json:"free"`
}
type SharedNetwork struct {
Location string `json:"location"`
Defined float64 `json:"defined"`
Used float64 `json:"used"`
Touched float64 `json:"touched"`
Free float64 `json:"free"`
}
type PoolsStats struct {
Subnets []Subnet `json:"subnets"`
SharedNetworks []SharedNetwork `json:"shared-networks"`
Summary Subnet `json:"summary"`
}
func NewExporter() *Exporter {
return &Exporter{
summaryFree: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "summary", "free"),
"Overall IPs Free",
nil,
nil),
summaryTouched: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "summary", "touched"),
"Overall IPs Touched",
nil,
nil),
summaryUsed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "summary", "used"),
"Overall IPs Used",
nil,
nil),
summaryDefined: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "summary", "defined"),
"Overall IPs Defined",
nil,
nil),
subnetFree: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "subnet", "free"),
"Subnet IPs Defined", []string{"location", "range"}, nil,
),
subnetTouched: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "subnet", "touched"),
"Subnet IPs Touched", []string{"location", "range"}, nil,
),
subnetUsed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "subnet", "used"),
"Subnet IPs Used", []string{"location", "range"}, nil,
),
subnetDefined: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "subnet", "defined"),
"Subnet IPs Defined", []string{"location", "range"}, nil,
),
sharedNetworkFree: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "shared_network", "free"),
"Shared Network IPs Defined", []string{"location"}, nil,
),
sharedNetworkTouched: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "shared_network", "touched"),
"Shared Network IPs Touched", []string{"location"}, nil,
),
sharedNetworkUsed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "shared_network", "used"),
"Shared Network IPs Used", []string{"location"}, nil,
),
sharedNetworkDefined: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "shared_network", "defined"),
"Shared Network IPs Defined", []string{"location"}, nil,
),
dhcpdUp: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "process", "up"),
"Whether dhcpd daemon is running at PID defined at its pid-file.",
[]string{}, nil,
),
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrape_failures",
Help: "Number of errors while scraping dhcpd-pools.",
}),
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.summaryFree
ch <- e.summaryTouched
ch <- e.summaryUsed
ch <- e.summaryDefined
ch <- e.subnetFree
ch <- e.subnetTouched
ch <- e.subnetUsed
ch <- e.subnetDefined
ch <- e.sharedNetworkFree
ch <- e.sharedNetworkTouched
ch <- e.sharedNetworkUsed
ch <- e.sharedNetworkDefined
ch <- e.dhcpdUp
e.scrapeFailures.Describe(ch)
}
func getoutputPool() PoolsStats {
outputPools, err := exec.Command("/usr/bin/dhcpd-pools", "-c", *dhcpdConfigFile, "--leases=" + *dhcpdLeaseFile, "-f", "j").Output()
if err != nil {
log.Errorf("Error: %s", err)
}
var poolsStats PoolsStats
err = json.Unmarshal(outputPools, &poolsStats)
if err != nil {
log.Errorf("Error: %s", err)
}
return poolsStats
}
func getDhcpdStatus() (status int) {
status = 0
strPid, err := ioutil.ReadFile(*dhcpdPidFile)
if err != nil {
log.Debugf("Unable to read DHCPD PID file: %s", err)
return
}
pid, err := strconv.Atoi(strings.Trim(string(strPid), "\n"))
if err != nil {
log.Debugf("Unable to convert DHCP PID to integer: %s", err)
return
}
log.Debugf("Read PID from dhcpd pid file: %d", pid)
proc, err := os.FindProcess(pid)
if err != nil {
log.Debugf("Unknown error when retrieving process %d info: %s", pid, err)
return
}
if err = proc.Signal(syscall.Signal(0)); err != nil {
log.Debugf("DHCP process at PID %d is not running: %s", pid, err)
return
}
status = 1
log.Debugf("DHCP process with PID %d is running", pid)
return
}
func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
ch <- prometheus.MustNewConstMetric(
e.dhcpdUp, prometheus.GaugeValue, float64(getDhcpdStatus()),
)
outputPool := getoutputPool()
ch <- prometheus.MustNewConstMetric(
e.summaryFree, prometheus.GaugeValue, outputPool.Summary.Free,
)
ch <- prometheus.MustNewConstMetric(
e.summaryTouched, prometheus.GaugeValue, outputPool.Summary.Touched,
)
ch <- prometheus.MustNewConstMetric(
e.summaryUsed, prometheus.GaugeValue, outputPool.Summary.Used,
)
ch <- prometheus.MustNewConstMetric(
e.summaryDefined, prometheus.GaugeValue, outputPool.Summary.Defined,
)
for subnet := 0; subnet < len(outputPool.Subnets); subnet++ {
ch <- prometheus.MustNewConstMetric(
e.subnetFree, prometheus.GaugeValue, outputPool.Subnets[subnet].Free,
outputPool.Subnets[subnet].Location, outputPool.Subnets[subnet].Range,
)
ch <- prometheus.MustNewConstMetric(
e.subnetTouched, prometheus.GaugeValue, outputPool.Subnets[subnet].Touched,
outputPool.Subnets[subnet].Location, outputPool.Subnets[subnet].Range,
)
ch <- prometheus.MustNewConstMetric(
e.subnetUsed, prometheus.GaugeValue, outputPool.Subnets[subnet].Used,
outputPool.Subnets[subnet].Location, outputPool.Subnets[subnet].Range,
)
ch <- prometheus.MustNewConstMetric(
e.subnetDefined, prometheus.GaugeValue, outputPool.Subnets[subnet].Defined,
outputPool.Subnets[subnet].Location, outputPool.Subnets[subnet].Range,
)
}
for sharedNetwork := 0; sharedNetwork < len(outputPool.SharedNetworks); sharedNetwork++ {
ch <- prometheus.MustNewConstMetric(
e.sharedNetworkFree, prometheus.GaugeValue, outputPool.SharedNetworks[sharedNetwork].Free,
outputPool.SharedNetworks[sharedNetwork].Location,
)
ch <- prometheus.MustNewConstMetric(
e.sharedNetworkTouched, prometheus.GaugeValue, outputPool.SharedNetworks[sharedNetwork].Touched,
outputPool.SharedNetworks[sharedNetwork].Location,
)
ch <- prometheus.MustNewConstMetric(
e.sharedNetworkUsed, prometheus.GaugeValue, outputPool.SharedNetworks[sharedNetwork].Used,
outputPool.SharedNetworks[sharedNetwork].Location,
)
ch <- prometheus.MustNewConstMetric(
e.sharedNetworkDefined, prometheus.GaugeValue, outputPool.SharedNetworks[sharedNetwork].Defined,
outputPool.SharedNetworks[sharedNetwork].Location,
)
}
return nil
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
if err := e.collect(ch); err != nil {
log.Errorf("Error scraping dhcpd-pools: %s", err)
e.scrapeFailures.Inc()
e.scrapeFailures.Collect(ch)
}
return
}
func init() {
prometheus.MustRegister(version.NewCollector("iscdhcpd_exporter"))
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
var landingPage = []byte(`<html>
<head><title>ISC-DHCPD exporter</title></head>
<body>
<h1>ISC-DHCPD exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`)
exporter := NewExporter()
prometheus.MustRegister(exporter)
log.Infoln("Starting iscdhcpd_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(landingPage)
})
log.Infoln("Listening on", *listenAddress)
err := http.ListenAndServe(*listenAddress, nil)
if err != nil {
log.Fatal(err)
}
}