...

Source file src/github.com/prometheus/procfs/sysfs/class_power_supply.go

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2018 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  	"errors"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/prometheus/procfs/internal/util"
    26  )
    27  
    28  // PowerSupply contains info from files in /sys/class/power_supply for a
    29  // single power supply.
    30  type PowerSupply struct {
    31  	Name                     string // Power Supply Name
    32  	Authentic                *int64 // /sys/class/power_supply/<Name>/authentic
    33  	Calibrate                *int64 // /sys/class/power_supply/<Name>/calibrate
    34  	Capacity                 *int64 // /sys/class/power_supply/<Name>/capacity
    35  	CapacityAlertMax         *int64 // /sys/class/power_supply/<Name>/capacity_alert_max
    36  	CapacityAlertMin         *int64 // /sys/class/power_supply/<Name>/capacity_alert_min
    37  	CapacityLevel            string // /sys/class/power_supply/<Name>/capacity_level
    38  	ChargeAvg                *int64 // /sys/class/power_supply/<Name>/charge_avg
    39  	ChargeControlLimit       *int64 // /sys/class/power_supply/<Name>/charge_control_limit
    40  	ChargeControlLimitMax    *int64 // /sys/class/power_supply/<Name>/charge_control_limit_max
    41  	ChargeCounter            *int64 // /sys/class/power_supply/<Name>/charge_counter
    42  	ChargeEmpty              *int64 // /sys/class/power_supply/<Name>/charge_empty
    43  	ChargeEmptyDesign        *int64 // /sys/class/power_supply/<Name>/charge_empty_design
    44  	ChargeFull               *int64 // /sys/class/power_supply/<Name>/charge_full
    45  	ChargeFullDesign         *int64 // /sys/class/power_supply/<Name>/charge_full_design
    46  	ChargeNow                *int64 // /sys/class/power_supply/<Name>/charge_now
    47  	ChargeTermCurrent        *int64 // /sys/class/power_supply/<Name>/charge_term_current
    48  	ChargeType               string // /sys/class/power_supply/<Name>/charge_type
    49  	ConstantChargeCurrent    *int64 // /sys/class/power_supply/<Name>/constant_charge_current
    50  	ConstantChargeCurrentMax *int64 // /sys/class/power_supply/<Name>/constant_charge_current_max
    51  	ConstantChargeVoltage    *int64 // /sys/class/power_supply/<Name>/constant_charge_voltage
    52  	ConstantChargeVoltageMax *int64 // /sys/class/power_supply/<Name>/constant_charge_voltage_max
    53  	CurrentAvg               *int64 // /sys/class/power_supply/<Name>/current_avg
    54  	CurrentBoot              *int64 // /sys/class/power_supply/<Name>/current_boot
    55  	CurrentMax               *int64 // /sys/class/power_supply/<Name>/current_max
    56  	CurrentNow               *int64 // /sys/class/power_supply/<Name>/current_now
    57  	CycleCount               *int64 // /sys/class/power_supply/<Name>/cycle_count
    58  	EnergyAvg                *int64 // /sys/class/power_supply/<Name>/energy_avg
    59  	EnergyEmpty              *int64 // /sys/class/power_supply/<Name>/energy_empty
    60  	EnergyEmptyDesign        *int64 // /sys/class/power_supply/<Name>/energy_empty_design
    61  	EnergyFull               *int64 // /sys/class/power_supply/<Name>/energy_full
    62  	EnergyFullDesign         *int64 // /sys/class/power_supply/<Name>/energy_full_design
    63  	EnergyNow                *int64 // /sys/class/power_supply/<Name>/energy_now
    64  	Health                   string // /sys/class/power_supply/<Name>/health
    65  	InputCurrentLimit        *int64 // /sys/class/power_supply/<Name>/input_current_limit
    66  	Manufacturer             string // /sys/class/power_supply/<Name>/manufacturer
    67  	ModelName                string // /sys/class/power_supply/<Name>/model_name
    68  	Online                   *int64 // /sys/class/power_supply/<Name>/online
    69  	PowerAvg                 *int64 // /sys/class/power_supply/<Name>/power_avg
    70  	PowerNow                 *int64 // /sys/class/power_supply/<Name>/power_now
    71  	PrechargeCurrent         *int64 // /sys/class/power_supply/<Name>/precharge_current
    72  	Present                  *int64 // /sys/class/power_supply/<Name>/present
    73  	Scope                    string // /sys/class/power_supply/<Name>/scope
    74  	SerialNumber             string // /sys/class/power_supply/<Name>/serial_number
    75  	Status                   string // /sys/class/power_supply/<Name>/status
    76  	Technology               string // /sys/class/power_supply/<Name>/technology
    77  	Temp                     *int64 // /sys/class/power_supply/<Name>/temp
    78  	TempAlertMax             *int64 // /sys/class/power_supply/<Name>/temp_alert_max
    79  	TempAlertMin             *int64 // /sys/class/power_supply/<Name>/temp_alert_min
    80  	TempAmbient              *int64 // /sys/class/power_supply/<Name>/temp_ambient
    81  	TempAmbientMax           *int64 // /sys/class/power_supply/<Name>/temp_ambient_max
    82  	TempAmbientMin           *int64 // /sys/class/power_supply/<Name>/temp_ambient_min
    83  	TempMax                  *int64 // /sys/class/power_supply/<Name>/temp_max
    84  	TempMin                  *int64 // /sys/class/power_supply/<Name>/temp_min
    85  	TimeToEmptyAvg           *int64 // /sys/class/power_supply/<Name>/time_to_empty_avg
    86  	TimeToEmptyNow           *int64 // /sys/class/power_supply/<Name>/time_to_empty_now
    87  	TimeToFullAvg            *int64 // /sys/class/power_supply/<Name>/time_to_full_avg
    88  	TimeToFullNow            *int64 // /sys/class/power_supply/<Name>/time_to_full_now
    89  	Type                     string // /sys/class/power_supply/<Name>/type
    90  	UsbType                  string // /sys/class/power_supply/<Name>/usb_type
    91  	VoltageAvg               *int64 // /sys/class/power_supply/<Name>/voltage_avg
    92  	VoltageBoot              *int64 // /sys/class/power_supply/<Name>/voltage_boot
    93  	VoltageMax               *int64 // /sys/class/power_supply/<Name>/voltage_max
    94  	VoltageMaxDesign         *int64 // /sys/class/power_supply/<Name>/voltage_max_design
    95  	VoltageMin               *int64 // /sys/class/power_supply/<Name>/voltage_min
    96  	VoltageMinDesign         *int64 // /sys/class/power_supply/<Name>/voltage_min_design
    97  	VoltageNow               *int64 // /sys/class/power_supply/<Name>/voltage_now
    98  	VoltageOCV               *int64 // /sys/class/power_supply/<Name>/voltage_ocv
    99  }
   100  
   101  // PowerSupplyClass is a collection of every power supply in
   102  // /sys/class/power_supply.
   103  //
   104  // The map keys are the names of the power supplies.
   105  type PowerSupplyClass map[string]PowerSupply
   106  
   107  // PowerSupplyClass returns info for all power supplies read from
   108  // /sys/class/power_supply.
   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