|
| 1 | +// Copyright 2023 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 | +//go:build !nowatchdog |
| 15 | +// +build !nowatchdog |
| 16 | + |
| 17 | +package collector |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/go-kit/log" |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | + "github.com/prometheus/procfs/sysfs" |
| 25 | +) |
| 26 | + |
| 27 | +type watchdogCollector struct { |
| 28 | + fs sysfs.FS |
| 29 | + logger log.Logger |
| 30 | +} |
| 31 | + |
| 32 | +func init() { |
| 33 | + registerCollector("watchdog", defaultDisabled, NewWatchdogCollector) |
| 34 | +} |
| 35 | + |
| 36 | +// NewWatchdogCollector returns a new Collector exposing watchdog stats. |
| 37 | +func NewWatchdogCollector(logger log.Logger) (Collector, error) { |
| 38 | + fs, err := sysfs.NewFS(*sysPath) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("failed to open procfs: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + return &watchdogCollector{ |
| 44 | + fs: fs, |
| 45 | + logger: logger, |
| 46 | + }, nil |
| 47 | +} |
| 48 | + |
| 49 | +var ( |
| 50 | + watchdogBootstatusDesc = prometheus.NewDesc( |
| 51 | + prometheus.BuildFQName(namespace, "watchdog", "bootstatus"), |
| 52 | + "Value of /sys/class/watchdog/<watchdog>/bootstatus", |
| 53 | + []string{"name"}, nil, |
| 54 | + ) |
| 55 | + watchdogFwVersionDesc = prometheus.NewDesc( |
| 56 | + prometheus.BuildFQName(namespace, "watchdog", "fw_version"), |
| 57 | + "Value of /sys/class/watchdog/<watchdog>/fw_version", |
| 58 | + []string{"name"}, nil, |
| 59 | + ) |
| 60 | + watchdogNowayoutDesc = prometheus.NewDesc( |
| 61 | + prometheus.BuildFQName(namespace, "watchdog", "nowayout"), |
| 62 | + "Value of /sys/class/watchdog/<watchdog>/nowayout", |
| 63 | + []string{"name"}, nil, |
| 64 | + ) |
| 65 | + watchdogTimeleftDesc = prometheus.NewDesc( |
| 66 | + prometheus.BuildFQName(namespace, "watchdog", "timeleft_seconds"), |
| 67 | + "Value of /sys/class/watchdog/<watchdog>/timeleft", |
| 68 | + []string{"name"}, nil, |
| 69 | + ) |
| 70 | + watchdogTimeoutDesc = prometheus.NewDesc( |
| 71 | + prometheus.BuildFQName(namespace, "watchdog", "timeout_seconds"), |
| 72 | + "Value of /sys/class/watchdog/<watchdog>/timeout", |
| 73 | + []string{"name"}, nil, |
| 74 | + ) |
| 75 | + watchdogPretimeoutDesc = prometheus.NewDesc( |
| 76 | + prometheus.BuildFQName(namespace, "watchdog", "pretimeout_seconds"), |
| 77 | + "Value of /sys/class/watchdog/<watchdog>/pretimeout", |
| 78 | + []string{"name"}, nil, |
| 79 | + ) |
| 80 | + watchdogAccessCs0Desc = prometheus.NewDesc( |
| 81 | + prometheus.BuildFQName(namespace, "watchdog", "access_cs0"), |
| 82 | + "Value of /sys/class/watchdog/<watchdog>/access_cs0", |
| 83 | + []string{"name"}, nil, |
| 84 | + ) |
| 85 | + watchdogInfoDesc = prometheus.NewDesc( |
| 86 | + prometheus.BuildFQName(namespace, "watchdog", "info"), |
| 87 | + "Info of /sys/class/watchdog/<watchdog>", |
| 88 | + []string{"name", "options", "identity", "state", "status", "pretimeout_governor"}, nil, |
| 89 | + ) |
| 90 | +) |
| 91 | + |
| 92 | +func toLabelValue(ptr *string) string { |
| 93 | + if ptr == nil { |
| 94 | + return "" |
| 95 | + } |
| 96 | + return *ptr |
| 97 | +} |
| 98 | + |
| 99 | +func (c *watchdogCollector) Update(ch chan<- prometheus.Metric) error { |
| 100 | + watchdogClass, err := c.fs.WatchdogClass() |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + for _, wd := range watchdogClass { |
| 106 | + if wd.Bootstatus != nil { |
| 107 | + ch <- prometheus.MustNewConstMetric(watchdogBootstatusDesc, prometheus.GaugeValue, float64(*wd.Bootstatus), wd.Name) |
| 108 | + } |
| 109 | + if wd.FwVersion != nil { |
| 110 | + ch <- prometheus.MustNewConstMetric(watchdogFwVersionDesc, prometheus.GaugeValue, float64(*wd.FwVersion), wd.Name) |
| 111 | + } |
| 112 | + if wd.Nowayout != nil { |
| 113 | + ch <- prometheus.MustNewConstMetric(watchdogNowayoutDesc, prometheus.GaugeValue, float64(*wd.Nowayout), wd.Name) |
| 114 | + } |
| 115 | + if wd.Timeleft != nil { |
| 116 | + ch <- prometheus.MustNewConstMetric(watchdogTimeleftDesc, prometheus.GaugeValue, float64(*wd.Timeleft), wd.Name) |
| 117 | + } |
| 118 | + if wd.Timeout != nil { |
| 119 | + ch <- prometheus.MustNewConstMetric(watchdogTimeoutDesc, prometheus.GaugeValue, float64(*wd.Timeout), wd.Name) |
| 120 | + } |
| 121 | + if wd.Pretimeout != nil { |
| 122 | + ch <- prometheus.MustNewConstMetric(watchdogPretimeoutDesc, prometheus.GaugeValue, float64(*wd.Pretimeout), wd.Name) |
| 123 | + } |
| 124 | + if wd.AccessCs0 != nil { |
| 125 | + ch <- prometheus.MustNewConstMetric(watchdogAccessCs0Desc, prometheus.GaugeValue, float64(*wd.AccessCs0), wd.Name) |
| 126 | + } |
| 127 | + |
| 128 | + ch <- prometheus.MustNewConstMetric(watchdogInfoDesc, prometheus.GaugeValue, 1.0, |
| 129 | + wd.Name, toLabelValue(wd.Options), toLabelValue(wd.Identity), toLabelValue(wd.State), toLabelValue(wd.Status), toLabelValue(wd.PretimeoutGovernor)) |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments