...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package sysfs
18
19 import (
20 "errors"
21 "fmt"
22 "os"
23 "path/filepath"
24
25 "github.com/prometheus/procfs/internal/util"
26 )
27
28
29
30 type PowerSupply struct {
31 Name string
32 Authentic *int64
33 Calibrate *int64
34 Capacity *int64
35 CapacityAlertMax *int64
36 CapacityAlertMin *int64
37 CapacityLevel string
38 ChargeAvg *int64
39 ChargeControlLimit *int64
40 ChargeControlLimitMax *int64
41 ChargeCounter *int64
42 ChargeEmpty *int64
43 ChargeEmptyDesign *int64
44 ChargeFull *int64
45 ChargeFullDesign *int64
46 ChargeNow *int64
47 ChargeTermCurrent *int64
48 ChargeType string
49 ConstantChargeCurrent *int64
50 ConstantChargeCurrentMax *int64
51 ConstantChargeVoltage *int64
52 ConstantChargeVoltageMax *int64
53 CurrentAvg *int64
54 CurrentBoot *int64
55 CurrentMax *int64
56 CurrentNow *int64
57 CycleCount *int64
58 EnergyAvg *int64
59 EnergyEmpty *int64
60 EnergyEmptyDesign *int64
61 EnergyFull *int64
62 EnergyFullDesign *int64
63 EnergyNow *int64
64 Health string
65 InputCurrentLimit *int64
66 Manufacturer string
67 ModelName string
68 Online *int64
69 PowerAvg *int64
70 PowerNow *int64
71 PrechargeCurrent *int64
72 Present *int64
73 Scope string
74 SerialNumber string
75 Status string
76 Technology string
77 Temp *int64
78 TempAlertMax *int64
79 TempAlertMin *int64
80 TempAmbient *int64
81 TempAmbientMax *int64
82 TempAmbientMin *int64
83 TempMax *int64
84 TempMin *int64
85 TimeToEmptyAvg *int64
86 TimeToEmptyNow *int64
87 TimeToFullAvg *int64
88 TimeToFullNow *int64
89 Type string
90 UsbType string
91 VoltageAvg *int64
92 VoltageBoot *int64
93 VoltageMax *int64
94 VoltageMaxDesign *int64
95 VoltageMin *int64
96 VoltageMinDesign *int64
97 VoltageNow *int64
98 VoltageOCV *int64
99 }
100
101
102
103
104
105 type PowerSupplyClass map[string]PowerSupply
106
107
108
109 func (fs FS) PowerSupplyClass() (PowerSupplyClass, error) {
110 path := fs.sys.Path("class/power_supply")
111
112 dirs, err := os.ReadDir(path)
113 if err != nil {
114 return nil, err
115 }
116
117 psc := make(PowerSupplyClass, len(dirs))
118 for _, d := range dirs {
119 ps, err := parsePowerSupply(filepath.Join(path, d.Name()))
120 if err != nil {
121 return nil, err
122 }
123
124 ps.Name = d.Name()
125 psc[d.Name()] = *ps
126 }
127
128 return psc, nil
129 }
130
131 func parsePowerSupply(path string) (*PowerSupply, error) {
132 files, err := os.ReadDir(path)
133 if err != nil {
134 return nil, err
135 }
136
137 var ps PowerSupply
138 for _, f := range files {
139 if !f.Type().IsRegular() {
140 continue
141 }
142
143 name := filepath.Join(path, f.Name())
144 value, err := util.SysReadFile(name)
145 if err != nil {
146 if os.IsNotExist(err) || err.Error() == "operation not supported" || errors.Is(err, os.ErrInvalid) {
147 continue
148 }
149 return nil, fmt.Errorf("failed to read file %q: %w", name, err)
150 }
151
152 vp := util.NewValueParser(value)
153
154 switch f.Name() {
155 case "authentic":
156 ps.Authentic = vp.PInt64()
157 case "calibrate":
158 ps.Calibrate = vp.PInt64()
159 case "capacity":
160 ps.Capacity = vp.PInt64()
161 case "capacity_alert_max":
162 ps.CapacityAlertMax = vp.PInt64()
163 case "capacity_alert_min":
164 ps.CapacityAlertMin = vp.PInt64()
165 case "capacity_level":
166 ps.CapacityLevel = value
167 case "charge_avg":
168 ps.ChargeAvg = vp.PInt64()
169 case "charge_control_limit":
170 ps.ChargeControlLimit = vp.PInt64()
171 case "charge_control_limit_max":
172 ps.ChargeControlLimitMax = vp.PInt64()
173 case "charge_counter":
174 ps.ChargeCounter = vp.PInt64()
175 case "charge_empty":
176 ps.ChargeEmpty = vp.PInt64()
177 case "charge_empty_design":
178 ps.ChargeEmptyDesign = vp.PInt64()
179 case "charge_full":
180 ps.ChargeFull = vp.PInt64()
181 case "charge_full_design":
182 ps.ChargeFullDesign = vp.PInt64()
183 case "charge_now":
184 ps.ChargeNow = vp.PInt64()
185 case "charge_term_current":
186 ps.ChargeTermCurrent = vp.PInt64()
187 case "charge_type":
188 ps.ChargeType = value
189 case "constant_charge_current":
190 ps.ConstantChargeCurrent = vp.PInt64()
191 case "constant_charge_current_max":
192 ps.ConstantChargeCurrentMax = vp.PInt64()
193 case "constant_charge_voltage":
194 ps.ConstantChargeVoltage = vp.PInt64()
195 case "constant_charge_voltage_max":
196 ps.ConstantChargeVoltageMax = vp.PInt64()
197 case "current_avg":
198 ps.CurrentAvg = vp.PInt64()
199 case "current_boot":
200 ps.CurrentBoot = vp.PInt64()
201 case "current_max":
202 ps.CurrentMax = vp.PInt64()
203 case "current_now":
204 ps.CurrentNow = vp.PInt64()
205 case "cycle_count":
206 ps.CycleCount = vp.PInt64()
207 case "energy_avg":
208 ps.EnergyAvg = vp.PInt64()
209 case "energy_empty":
210 ps.EnergyEmpty = vp.PInt64()
211 case "energy_empty_design":
212 ps.EnergyEmptyDesign = vp.PInt64()
213 case "energy_full":
214 ps.EnergyFull = vp.PInt64()
215 case "energy_full_design":
216 ps.EnergyFullDesign = vp.PInt64()
217 case "energy_now":
218 ps.EnergyNow = vp.PInt64()
219 case "health":
220 ps.Health = value
221 case "input_current_limit":
222 ps.InputCurrentLimit = vp.PInt64()
223 case "manufacturer":
224 ps.Manufacturer = value
225 case "model_name":
226 ps.ModelName = value
227 case "online":
228 ps.Online = vp.PInt64()
229 case "power_avg":
230 ps.PowerAvg = vp.PInt64()
231 case "power_now":
232 ps.PowerNow = vp.PInt64()
233 case "precharge_current":
234 ps.PrechargeCurrent = vp.PInt64()
235 case "present":
236 ps.Present = vp.PInt64()
237 case "scope":
238 ps.Scope = value
239 case "serial_number":
240 ps.SerialNumber = value
241 case "status":
242 ps.Status = value
243 case "technology":
244 ps.Technology = value
245 case "temp":
246 ps.Temp = vp.PInt64()
247 case "temp_alert_max":
248 ps.TempAlertMax = vp.PInt64()
249 case "temp_alert_min":
250 ps.TempAlertMin = vp.PInt64()
251 case "temp_ambient":
252 ps.TempAmbient = vp.PInt64()
253 case "temp_ambient_max":
254 ps.TempAmbientMax = vp.PInt64()
255 case "temp_ambient_min":
256 ps.TempAmbientMin = vp.PInt64()
257 case "temp_max":
258 ps.TempMax = vp.PInt64()
259 case "temp_min":
260 ps.TempMin = vp.PInt64()
261 case "time_to_empty_avg":
262 ps.TimeToEmptyAvg = vp.PInt64()
263 case "time_to_empty_now":
264 ps.TimeToEmptyNow = vp.PInt64()
265 case "time_to_full_avg":
266 ps.TimeToFullAvg = vp.PInt64()
267 case "time_to_full_now":
268 ps.TimeToFullNow = vp.PInt64()
269 case "type":
270 ps.Type = value
271 case "usb_type":
272 ps.UsbType = value
273 case "voltage_avg":
274 ps.VoltageAvg = vp.PInt64()
275 case "voltage_boot":
276 ps.VoltageBoot = vp.PInt64()
277 case "voltage_max":
278 ps.VoltageMax = vp.PInt64()
279 case "voltage_max_design":
280 ps.VoltageMaxDesign = vp.PInt64()
281 case "voltage_min":
282 ps.VoltageMin = vp.PInt64()
283 case "voltage_min_design":
284 ps.VoltageMinDesign = vp.PInt64()
285 case "voltage_now":
286 ps.VoltageNow = vp.PInt64()
287 case "voltage_ocv":
288 ps.VoltageOCV = vp.PInt64()
289 }
290
291 if err := vp.Err(); err != nil {
292 return nil, err
293 }
294 }
295
296 return &ps, nil
297 }
298
View as plain text