Skip to content

Commit

Permalink
collector: use path/filepath for handling file paths (prometheus#1245)
Browse files Browse the repository at this point in the history
Similar to prometheus#1228.  Update the remaining collectors to use
'path/filepath' intead of 'path' for manipulating file paths.

Signed-off-by: Paul Gier <[email protected]>
  • Loading branch information
pgier authored and oblitorum committed Apr 9, 2024
1 parent 77ca3a1 commit 93fb307
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
10 changes: 5 additions & 5 deletions collector/bonding_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -71,21 +71,21 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error {

func readBondingStats(root string) (status map[string][2]int, err error) {
status = map[string][2]int{}
masters, err := ioutil.ReadFile(path.Join(root, "bonding_masters"))
masters, err := ioutil.ReadFile(filepath.Join(root, "bonding_masters"))
if err != nil {
return nil, err
}
for _, master := range strings.Fields(string(masters)) {
slaves, err := ioutil.ReadFile(path.Join(root, master, "bonding", "slaves"))
slaves, err := ioutil.ReadFile(filepath.Join(root, master, "bonding", "slaves"))
if err != nil {
return nil, err
}
sstat := [2]int{0, 0}
for _, slave := range strings.Fields(string(slaves)) {
state, err := ioutil.ReadFile(path.Join(root, master, fmt.Sprintf("lower_%s", slave), "operstate"))
state, err := ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("lower_%s", slave), "operstate"))
if os.IsNotExist(err) {
// some older? kernels use slave_ prefix
state, err = ioutil.ReadFile(path.Join(root, master, fmt.Sprintf("slave_%s", slave), "operstate"))
state, err = ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("slave_%s", slave), "operstate"))
}
if err != nil {
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions collector/edac_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package collector

import (
"fmt"
"path"
"path/filepath"
"regexp"

Expand Down Expand Up @@ -82,28 +81,28 @@ func (c *edacCollector) Update(ch chan<- prometheus.Metric) error {
}
controllerNumber := controllerMatch[1]

value, err := readUintFromFile(path.Join(controller, "ce_count"))
value, err := readUintFromFile(filepath.Join(controller, "ce_count"))
if err != nil {
return fmt.Errorf("couldn't get ce_count for controller %s: %s", controllerNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.ceCount, prometheus.CounterValue, float64(value), controllerNumber)

value, err = readUintFromFile(path.Join(controller, "ce_noinfo_count"))
value, err = readUintFromFile(filepath.Join(controller, "ce_noinfo_count"))
if err != nil {
return fmt.Errorf("couldn't get ce_noinfo_count for controller %s: %s", controllerNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, "unknown")

value, err = readUintFromFile(path.Join(controller, "ue_count"))
value, err = readUintFromFile(filepath.Join(controller, "ue_count"))
if err != nil {
return fmt.Errorf("couldn't get ue_count for controller %s: %s", controllerNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.ueCount, prometheus.CounterValue, float64(value), controllerNumber)

value, err = readUintFromFile(path.Join(controller, "ue_noinfo_count"))
value, err = readUintFromFile(filepath.Join(controller, "ue_noinfo_count"))
if err != nil {
return fmt.Errorf("couldn't get ue_noinfo_count for controller %s: %s", controllerNumber, err)
}
Expand All @@ -122,14 +121,14 @@ func (c *edacCollector) Update(ch chan<- prometheus.Metric) error {
}
csrowNumber := csrowMatch[1]

value, err = readUintFromFile(path.Join(csrow, "ce_count"))
value, err = readUintFromFile(filepath.Join(csrow, "ce_count"))
if err != nil {
return fmt.Errorf("couldn't get ce_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err)
}
ch <- prometheus.MustNewConstMetric(
c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, csrowNumber)

value, err = readUintFromFile(path.Join(csrow, "ue_count"))
value, err = readUintFromFile(filepath.Join(csrow, "ue_count"))
if err != nil {
return fmt.Errorf("couldn't get ue_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err)
}
Expand Down
23 changes: 11 additions & 12 deletions collector/hwmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -138,7 +137,7 @@ func collectSensorData(dir string, data map[string]map[string]string) error {

for _, t := range hwmonSensorTypes {
if t == sensorType {
addValueFile(data, sensorType+strconv.Itoa(sensorNum), sensorProperty, path.Join(dir, file.Name()))
addValueFile(data, sensorType+strconv.Itoa(sensorNum), sensorProperty, filepath.Join(dir, file.Name()))
break
}
}
Expand All @@ -157,8 +156,8 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er
if err != nil {
return err
}
if _, err := os.Stat(path.Join(dir, "device")); err == nil {
err := collectSensorData(path.Join(dir, "device"), data)
if _, err := os.Stat(filepath.Join(dir, "device")); err == nil {
err := collectSensorData(filepath.Join(dir, "device"), data)
if err != nil {
return err
}
Expand Down Expand Up @@ -353,10 +352,10 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) {

// preference 1: construct a name based on device name, always unique

devicePath, devErr := filepath.EvalSymlinks(path.Join(dir, "device"))
devicePath, devErr := filepath.EvalSymlinks(filepath.Join(dir, "device"))
if devErr == nil {
devPathPrefix, devName := path.Split(devicePath)
_, devType := path.Split(strings.TrimRight(devPathPrefix, "/"))
devPathPrefix, devName := filepath.Split(devicePath)
_, devType := filepath.Split(strings.TrimRight(devPathPrefix, "/"))

cleanDevName := cleanMetricName(devName)
cleanDevType := cleanMetricName(devType)
Expand All @@ -371,7 +370,7 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) {
}

// preference 2: is there a name file
sysnameRaw, nameErr := ioutil.ReadFile(path.Join(dir, "name"))
sysnameRaw, nameErr := ioutil.ReadFile(filepath.Join(dir, "name"))
if nameErr == nil && string(sysnameRaw) != "" {
cleanName := cleanMetricName(string(sysnameRaw))
if cleanName != "" {
Expand All @@ -388,7 +387,7 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) {
}

// take the last path element, this will be hwmonX
_, name := path.Split(realDir)
_, name := filepath.Split(realDir)
cleanName := cleanMetricName(name)
if cleanName != "" {
return cleanName, nil
Expand All @@ -399,7 +398,7 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) {
// hwmonHumanReadableChipName is similar to the methods in hwmonName, but with
// different precedences -- we can allow duplicates here.
func (c *hwMonCollector) hwmonHumanReadableChipName(dir string) (string, error) {
sysnameRaw, nameErr := ioutil.ReadFile(path.Join(dir, "name"))
sysnameRaw, nameErr := ioutil.ReadFile(filepath.Join(dir, "name"))
if nameErr != nil {
return "", nameErr
}
Expand All @@ -418,7 +417,7 @@ func (c *hwMonCollector) Update(ch chan<- prometheus.Metric) error {
// Step 1: scan /sys/class/hwmon, resolve all symlinks and call
// updatesHwmon for each folder

hwmonPathName := path.Join(sysFilePath("class"), "hwmon")
hwmonPathName := filepath.Join(sysFilePath("class"), "hwmon")

hwmonFiles, err := ioutil.ReadDir(hwmonPathName)
if err != nil {
Expand All @@ -431,7 +430,7 @@ func (c *hwMonCollector) Update(ch chan<- prometheus.Metric) error {
}

for _, hwDir := range hwmonFiles {
hwmonXPathName := path.Join(hwmonPathName, hwDir.Name())
hwmonXPathName := filepath.Join(hwmonPathName, hwDir.Name())

if hwDir.Mode()&os.ModeSymlink > 0 {
hwDir, err = os.Stat(hwmonXPathName)
Expand Down
4 changes: 2 additions & 2 deletions collector/ksmd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package collector

import (
"fmt"
"path"
"path/filepath"

"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -62,7 +62,7 @@ func NewKsmdCollector() (Collector, error) {
// Update implements Collector and exposes kernel and system statistics.
func (c *ksmdCollector) Update(ch chan<- prometheus.Metric) error {
for _, n := range ksmdFiles {
val, err := readUintFromFile(sysFilePath(path.Join("kernel/mm/ksm", n)))
val, err := readUintFromFile(sysFilePath(filepath.Join("kernel/mm/ksm", n)))
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions collector/meminfo_numa_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -86,7 +85,7 @@ func getMemInfoNuma() ([]meminfoMetric, error) {
return nil, err
}
for _, node := range nodes {
meminfoFile, err := os.Open(path.Join(node, "meminfo"))
meminfoFile, err := os.Open(filepath.Join(node, "meminfo"))
if err != nil {
return nil, err
}
Expand All @@ -98,7 +97,7 @@ func getMemInfoNuma() ([]meminfoMetric, error) {
}
metrics = append(metrics, numaInfo...)

numastatFile, err := os.Open(path.Join(node, "numastat"))
numastatFile, err := os.Open(filepath.Join(node, "numastat"))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 93fb307

Please sign in to comment.