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