|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package metrics |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/prometheus/client_golang/prometheus" |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + collectorsNamespace = "smb" |
| 11 | +) |
| 12 | + |
| 13 | +func (sme *smbMetricsExporter) register() error { |
| 14 | + cols := []prometheus.Collector{ |
| 15 | + sme.newSmbSharesCollector(), |
| 16 | + sme.newSmbLocksCollector(), |
| 17 | + } |
| 18 | + for _, c := range cols { |
| 19 | + if err := sme.reg.Register(c); err != nil { |
| 20 | + sme.log.Error(err, "failed to register collector") |
| 21 | + return err |
| 22 | + } |
| 23 | + } |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
| 27 | +type smbCollector struct { |
| 28 | + // nolint:structcheck |
| 29 | + sme *smbMetricsExporter |
| 30 | + dsc []*prometheus.Desc |
| 31 | +} |
| 32 | + |
| 33 | +func (col *smbCollector) Describe(ch chan<- *prometheus.Desc) { |
| 34 | + for _, d := range col.dsc { |
| 35 | + ch <- d |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +type smbSharesCollector struct { |
| 40 | + smbCollector |
| 41 | +} |
| 42 | + |
| 43 | +func (col *smbSharesCollector) Collect(ch chan<- prometheus.Metric) { |
| 44 | + sharesTotal := 0 |
| 45 | + sharesMap, _ := SmbStatusSharesByMachine() |
| 46 | + for machine, share := range sharesMap { |
| 47 | + sharesCount := len(share) |
| 48 | + ch <- prometheus.MustNewConstMetric(col.dsc[0], |
| 49 | + prometheus.GaugeValue, float64(sharesCount), machine) |
| 50 | + sharesTotal += sharesCount |
| 51 | + } |
| 52 | + |
| 53 | + ch <- prometheus.MustNewConstMetric(col.dsc[1], |
| 54 | + prometheus.GaugeValue, float64(sharesTotal)) |
| 55 | +} |
| 56 | + |
| 57 | +func (sme *smbMetricsExporter) newSmbSharesCollector() prometheus.Collector { |
| 58 | + col := &smbSharesCollector{} |
| 59 | + col.sme = sme |
| 60 | + col.dsc = []*prometheus.Desc{ |
| 61 | + prometheus.NewDesc( |
| 62 | + collectorName("shares", "machine"), |
| 63 | + "Number of shares by host-machine ip", |
| 64 | + []string{"machine"}, nil), |
| 65 | + |
| 66 | + prometheus.NewDesc( |
| 67 | + collectorName("shares", "total"), |
| 68 | + "Total number of active shares", |
| 69 | + []string{}, nil), |
| 70 | + } |
| 71 | + return col |
| 72 | +} |
| 73 | + |
| 74 | +type smbLocksCollector struct { |
| 75 | + smbCollector |
| 76 | +} |
| 77 | + |
| 78 | +func (col *smbLocksCollector) Collect(ch chan<- prometheus.Metric) { |
| 79 | + locks, _ := RunSmbStatusLocks() |
| 80 | + ch <- prometheus.MustNewConstMetric(col.dsc[0], |
| 81 | + prometheus.GaugeValue, float64(len(locks))) |
| 82 | +} |
| 83 | + |
| 84 | +func (sme *smbMetricsExporter) newSmbLocksCollector() prometheus.Collector { |
| 85 | + col := &smbLocksCollector{} |
| 86 | + col.sme = sme |
| 87 | + col.dsc = []*prometheus.Desc{ |
| 88 | + prometheus.NewDesc( |
| 89 | + collectorName("locks", "total"), |
| 90 | + "Total number of active locks", |
| 91 | + []string{}, nil), |
| 92 | + } |
| 93 | + return col |
| 94 | +} |
| 95 | + |
| 96 | +func collectorName(subsystem, name string) string { |
| 97 | + return prometheus.BuildFQName(collectorsNamespace, subsystem, name) |
| 98 | +} |
0 commit comments