...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31 type WatchdogStats struct {
32 Name string
33 Bootstatus *int64
34 Options *string
35 FwVersion *int64
36 Identity *string
37 Nowayout *int64
38 State *string
39 Status *string
40 Timeleft *int64
41 Timeout *int64
42 Pretimeout *int64
43 PretimeoutGovernor *string
44 AccessCs0 *int64
45 }
46
47
48
49
50 type WatchdogClass map[string]WatchdogStats
51
52
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 }
124
View as plain text