|
| 1 | +package collectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/prometheus/client_golang/prometheus" |
| 5 | + "time" |
| 6 | + "github.com/DRuggeri/dhcpdleasesreader" |
| 7 | +) |
| 8 | + |
| 9 | +type StatCollector struct { |
| 10 | + namespace string |
| 11 | + validMetric prometheus.Gauge |
| 12 | + expiredMetric prometheus.Gauge |
| 13 | + countMetric prometheus.Gauge |
| 14 | + modTimeMetric prometheus.Gauge |
| 15 | + info *dhcpdleasesreader.DhcpdInfo |
| 16 | + |
| 17 | + scrapesTotalMetric prometheus.Counter |
| 18 | + scrapeErrorsTotalMetric prometheus.Counter |
| 19 | + lastScrapeErrorMetric prometheus.Gauge |
| 20 | + lastScrapeTimestampMetric prometheus.Gauge |
| 21 | + lastScrapeDurationSecondsMetric prometheus.Gauge |
| 22 | +} |
| 23 | + |
| 24 | +func NewStatsCollector(namespace string, info *dhcpdleasesreader.DhcpdInfo) *StatCollector { |
| 25 | + validMetric := prometheus.NewGauge( |
| 26 | + prometheus.GaugeOpts{ |
| 27 | + Namespace: namespace, |
| 28 | + Subsystem: "stats", |
| 29 | + Name: "valid_leases", |
| 30 | + Help: "The number of leases in dhcpd.leases that have not yet expired.", |
| 31 | + }, |
| 32 | + ) |
| 33 | + |
| 34 | + expiredMetric := prometheus.NewGauge( |
| 35 | + prometheus.GaugeOpts{ |
| 36 | + Namespace: namespace, |
| 37 | + Subsystem: "stats", |
| 38 | + Name: "expired_leases", |
| 39 | + Help: "The number of leases in dhcpd.leases that have expired.", |
| 40 | + }, |
| 41 | + ) |
| 42 | + |
| 43 | + countMetric := prometheus.NewGauge( |
| 44 | + prometheus.GaugeOpts{ |
| 45 | + Namespace: namespace, |
| 46 | + Subsystem: "stats", |
| 47 | + Name: "count", |
| 48 | + Help: "The number of leases in dhcpd.leases", |
| 49 | + }, |
| 50 | + ) |
| 51 | + |
| 52 | + modTimeMetric := prometheus.NewGauge( |
| 53 | + prometheus.GaugeOpts{ |
| 54 | + Namespace: namespace, |
| 55 | + Subsystem: "stats", |
| 56 | + Name: "filetime", |
| 57 | + Help: "The file timestamp in seconds since epoch of the dhcpd.leases file", |
| 58 | + }, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | + scrapesTotalMetric := prometheus.NewCounter( |
| 63 | + prometheus.CounterOpts{ |
| 64 | + Namespace: namespace, |
| 65 | + Subsystem: "stats", |
| 66 | + Name: "scrapes_total", |
| 67 | + Help: "Total number of scrapes for stats.", |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + scrapeErrorsTotalMetric := prometheus.NewCounter( |
| 72 | + prometheus.CounterOpts{ |
| 73 | + Namespace: namespace, |
| 74 | + Subsystem: "stats", |
| 75 | + Name: "scrape_errors_total", |
| 76 | + Help: "Total number of scrapes errors for stats.", |
| 77 | + }, |
| 78 | + ) |
| 79 | + |
| 80 | + lastScrapeErrorMetric := prometheus.NewGauge( |
| 81 | + prometheus.GaugeOpts{ |
| 82 | + Namespace: namespace, |
| 83 | + Subsystem: "stats", |
| 84 | + Name: "last_scrape_error", |
| 85 | + Help: "Whether the last scrape of stats resulted in an error (1 for error, 0 for success).", |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + lastScrapeTimestampMetric := prometheus.NewGauge( |
| 90 | + prometheus.GaugeOpts{ |
| 91 | + Namespace: namespace, |
| 92 | + Subsystem: "stats", |
| 93 | + Name: "last_scrape_timestamp", |
| 94 | + Help: "Number of seconds since 1970 since last scrape of stat metrics.", |
| 95 | + }, |
| 96 | + ) |
| 97 | + |
| 98 | + lastScrapeDurationSecondsMetric := prometheus.NewGauge( |
| 99 | + prometheus.GaugeOpts{ |
| 100 | + Namespace: namespace, |
| 101 | + Subsystem: "stats", |
| 102 | + Name: "last_scrape_duration_seconds", |
| 103 | + Help: "Duration of the last scrape of stats.", |
| 104 | + }, |
| 105 | + ) |
| 106 | + |
| 107 | + return &StatCollector{ |
| 108 | + validMetric: validMetric, |
| 109 | + expiredMetric: expiredMetric, |
| 110 | + countMetric: countMetric, |
| 111 | + modTimeMetric: modTimeMetric, |
| 112 | + info: info, |
| 113 | + |
| 114 | + namespace: namespace, |
| 115 | + scrapesTotalMetric: scrapesTotalMetric, |
| 116 | + scrapeErrorsTotalMetric: scrapeErrorsTotalMetric, |
| 117 | + lastScrapeErrorMetric: lastScrapeErrorMetric, |
| 118 | + lastScrapeTimestampMetric: lastScrapeTimestampMetric, |
| 119 | + lastScrapeDurationSecondsMetric: lastScrapeDurationSecondsMetric, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func (c *StatCollector) Collect(ch chan<- prometheus.Metric) { |
| 124 | + var begun = time.Now() |
| 125 | + |
| 126 | + c.validMetric.Set(float64(c.info.Valid)) |
| 127 | + c.validMetric.Collect(ch) |
| 128 | + |
| 129 | + c.expiredMetric.Set(float64(c.info.Expired)) |
| 130 | + c.expiredMetric.Collect(ch) |
| 131 | + |
| 132 | + c.countMetric.Set(float64(len(c.info.Leases))) |
| 133 | + c.countMetric.Collect(ch) |
| 134 | + |
| 135 | + c.modTimeMetric.Set(float64(c.info.ModTime.Unix())) |
| 136 | + c.modTimeMetric.Collect(ch) |
| 137 | + |
| 138 | + c.scrapeErrorsTotalMetric.Collect(ch) |
| 139 | + |
| 140 | + c.scrapesTotalMetric.Inc() |
| 141 | + c.scrapesTotalMetric.Collect(ch) |
| 142 | + |
| 143 | + c.lastScrapeErrorMetric.Set(0) |
| 144 | + c.lastScrapeErrorMetric.Collect(ch) |
| 145 | + |
| 146 | + c.lastScrapeTimestampMetric.Set(float64(time.Now().Unix())) |
| 147 | + c.lastScrapeTimestampMetric.Collect(ch) |
| 148 | + |
| 149 | + c.lastScrapeDurationSecondsMetric.Set(time.Since(begun).Seconds()) |
| 150 | + c.lastScrapeDurationSecondsMetric.Collect(ch) |
| 151 | +} |
| 152 | + |
| 153 | +func (c *StatCollector) Describe(ch chan<- *prometheus.Desc) { |
| 154 | + c.validMetric.Describe(ch) |
| 155 | + c.expiredMetric.Describe(ch) |
| 156 | + c.countMetric.Describe(ch) |
| 157 | + c.scrapesTotalMetric.Describe(ch) |
| 158 | + c.scrapeErrorsTotalMetric.Describe(ch) |
| 159 | + c.lastScrapeErrorMetric.Describe(ch) |
| 160 | + c.lastScrapeTimestampMetric.Describe(ch) |
| 161 | + c.lastScrapeDurationSecondsMetric.Describe(ch) |
| 162 | +} |
0 commit comments