|
| 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 linux |
| 15 | +// +build linux |
| 16 | + |
| 17 | +package sysfs |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "github.com/prometheus/procfs/internal/util" |
| 25 | +) |
| 26 | + |
| 27 | +const watchdogClassPath = "class/watchdog" |
| 28 | + |
| 29 | +// WatchdogStats contains info from files in /sys/class/watchdog for a single watchdog device. |
| 30 | +// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-watchdog |
| 31 | +type WatchdogStats struct { |
| 32 | + Name string |
| 33 | + Bootstatus *int64 // /sys/class/watchdog/<Name>/bootstatus |
| 34 | + Options *string // /sys/class/watchdog/<Name>/options |
| 35 | + FwVersion *int64 // /sys/class/watchdog/<Name>/fw_version |
| 36 | + Identity *string // /sys/class/watchdog/<Name>/identity |
| 37 | + Nowayout *int64 // /sys/class/watchdog/<Name>/nowayout |
| 38 | + State *string // /sys/class/watchdog/<Name>/state |
| 39 | + Status *string // /sys/class/watchdog/<Name>/status |
| 40 | + Timeleft *int64 // /sys/class/watchdog/<Name>/timeleft |
| 41 | + Timeout *int64 // /sys/class/watchdog/<Name>/timeout |
| 42 | + Pretimeout *int64 // /sys/class/watchdog/<Name>/pretimeout |
| 43 | + PretimeoutGovernor *string // /sys/class/watchdog/<Name>/pretimeout_governor |
| 44 | + AccessCs0 *int64 // /sys/class/watchdog/<Name>/access_cs0 |
| 45 | +} |
| 46 | + |
| 47 | +// WatchdogClass is a collection of statistics for every watchdog device in /sys/class/watchdog. |
| 48 | +// |
| 49 | +// The map keys are the names of the watchdog devices. |
| 50 | +type WatchdogClass map[string]WatchdogStats |
| 51 | + |
| 52 | +// WatchdogClass returns info for all watchdog devices read from /sys/class/watchdog. |
| 53 | +func (fs FS) WatchdogClass() (WatchdogClass, error) { |
| 54 | + path := fs.sys.Path(watchdogClassPath) |
| 55 | + |
| 56 | + dirs, err := os.ReadDir(path) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("failed to list watchdog devices at %q: %w", path, err) |
| 59 | + } |
| 60 | + |
| 61 | + wds := make(WatchdogClass, len(dirs)) |
| 62 | + for _, d := range dirs { |
| 63 | + stats, err := fs.parseWatchdog(d.Name()) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + wds[stats.Name] = *stats |
| 69 | + } |
| 70 | + |
| 71 | + return wds, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (fs FS) parseWatchdog(wdName string) (*WatchdogStats, error) { |
| 75 | + path := fs.sys.Path(watchdogClassPath, wdName) |
| 76 | + wd := WatchdogStats{Name: wdName} |
| 77 | + |
| 78 | + for _, f := range [...]string{"bootstatus", "options", "fw_version", "identity", "nowayout", "state", "status", "timeleft", "timeout", "pretimeout", "pretimeout_governor", "access_cs0"} { |
| 79 | + name := filepath.Join(path, f) |
| 80 | + value, err := util.SysReadFile(name) |
| 81 | + if err != nil { |
| 82 | + if os.IsNotExist(err) { |
| 83 | + continue |
| 84 | + } |
| 85 | + return nil, fmt.Errorf("failed to read file %q: %w", name, err) |
| 86 | + } |
| 87 | + |
| 88 | + vp := util.NewValueParser(value) |
| 89 | + |
| 90 | + switch f { |
| 91 | + case "bootstatus": |
| 92 | + wd.Bootstatus = vp.PInt64() |
| 93 | + case "options": |
| 94 | + wd.Options = &value |
| 95 | + case "fw_version": |
| 96 | + wd.FwVersion = vp.PInt64() |
| 97 | + case "identity": |
| 98 | + wd.Identity = &value |
| 99 | + case "nowayout": |
| 100 | + wd.Nowayout = vp.PInt64() |
| 101 | + case "state": |
| 102 | + wd.State = &value |
| 103 | + case "status": |
| 104 | + wd.Status = &value |
| 105 | + case "timeleft": |
| 106 | + wd.Timeleft = vp.PInt64() |
| 107 | + case "timeout": |
| 108 | + wd.Timeout = vp.PInt64() |
| 109 | + case "pretimeout": |
| 110 | + wd.Pretimeout = vp.PInt64() |
| 111 | + case "pretimeout_governor": |
| 112 | + wd.PretimeoutGovernor = &value |
| 113 | + case "access_cs0": |
| 114 | + wd.AccessCs0 = vp.PInt64() |
| 115 | + } |
| 116 | + |
| 117 | + if err := vp.Err(); err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return &wd, nil |
| 123 | +} |
0 commit comments