diff options
author | Paul Gier <pgier@redhat.com> | 2019-02-05 09:37:27 -0600 |
---|---|---|
committer | Ben Kochie <superq@gmail.com> | 2019-02-05 16:37:27 +0100 |
commit | 2b81bff51843525dc210749c33d96ac382921b1f (patch) | |
tree | bb2b62702671e72b61c141004be4b35c7f7646d5 | |
parent | dda51ad06a024ea3a75b3399de46c0ce3ebab418 (diff) | |
download | prometheus_node_collector-2b81bff51843525dc210749c33d96ac382921b1f.tar.bz2 prometheus_node_collector-2b81bff51843525dc210749c33d96ac382921b1f.tar.xz prometheus_node_collector-2b81bff51843525dc210749c33d96ac382921b1f.zip |
collector: use path/filepath for handling file paths (#1245)
Similar to #1228. Update the remaining collectors to use
'path/filepath' intead of 'path' for manipulating file paths.
Signed-off-by: Paul Gier <pgier@redhat.com>
-rw-r--r-- | collector/bonding_linux.go | 10 | ||||
-rw-r--r-- | collector/edac_linux.go | 13 | ||||
-rw-r--r-- | collector/hwmon_linux.go | 23 | ||||
-rw-r--r-- | collector/ksmd_linux.go | 4 | ||||
-rw-r--r-- | collector/meminfo_numa_linux.go | 5 |
5 files changed, 26 insertions, 29 deletions
diff --git a/collector/bonding_linux.go b/collector/bonding_linux.go index 8f441e8..829ab6d 100644 --- a/collector/bonding_linux.go +++ b/collector/bonding_linux.go | |||
@@ -19,7 +19,7 @@ import ( | |||
19 | "fmt" | 19 | "fmt" |
20 | "io/ioutil" | 20 | "io/ioutil" |
21 | "os" | 21 | "os" |
22 | "path" | 22 | "path/filepath" |
23 | "strings" | 23 | "strings" |
24 | 24 | ||
25 | "github.com/prometheus/client_golang/prometheus" | 25 | "github.com/prometheus/client_golang/prometheus" |
@@ -71,21 +71,21 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error { | |||
71 | 71 | ||
72 | func readBondingStats(root string) (status map[string][2]int, err error) { | 72 | func readBondingStats(root string) (status map[string][2]int, err error) { |
73 | status = map[string][2]int{} | 73 | status = map[string][2]int{} |
74 | masters, err := ioutil.ReadFile(path.Join(root, "bonding_masters")) | 74 | masters, err := ioutil.ReadFile(filepath.Join(root, "bonding_masters")) |
75 | if err != nil { | 75 | if err != nil { |
76 | return nil, err | 76 | return nil, err |
77 | } | 77 | } |
78 | for _, master := range strings.Fields(string(masters)) { | 78 | for _, master := range strings.Fields(string(masters)) { |
79 | slaves, err := ioutil.ReadFile(path.Join(root, master, "bonding", "slaves")) | 79 | slaves, err := ioutil.ReadFile(filepath.Join(root, master, "bonding", "slaves")) |
80 | if err != nil { | 80 | if err != nil { |
81 | return nil, err | 81 | return nil, err |
82 | } | 82 | } |
83 | sstat := [2]int{0, 0} | 83 | sstat := [2]int{0, 0} |
84 | for _, slave := range strings.Fields(string(slaves)) { | 84 | for _, slave := range strings.Fields(string(slaves)) { |
85 | state, err := ioutil.ReadFile(path.Join(root, master, fmt.Sprintf("lower_%s", slave), "operstate")) | 85 | state, err := ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("lower_%s", slave), "operstate")) |
86 | if os.IsNotExist(err) { | 86 | if os.IsNotExist(err) { |
87 | // some older? kernels use slave_ prefix | 87 | // some older? kernels use slave_ prefix |
88 | state, err = ioutil.ReadFile(path.Join(root, master, fmt.Sprintf("slave_%s", slave), "operstate")) | 88 | state, err = ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("slave_%s", slave), "operstate")) |
89 | } | 89 | } |
90 | if err != nil { | 90 | if err != nil { |
91 | return nil, err | 91 | return nil, err |
diff --git a/collector/edac_linux.go b/collector/edac_linux.go index f4fef51..de33873 100644 --- a/collector/edac_linux.go +++ b/collector/edac_linux.go | |||
@@ -17,7 +17,6 @@ package collector | |||
17 | 17 | ||
18 | import ( | 18 | import ( |
19 | "fmt" | 19 | "fmt" |
20 | "path" | ||
21 | "path/filepath" | 20 | "path/filepath" |
22 | "regexp" | 21 | "regexp" |
23 | 22 | ||
@@ -82,28 +81,28 @@ func (c *edacCollector) Update(ch chan<- prometheus.Metric) error { | |||
82 | } | 81 | } |
83 | controllerNumber := controllerMatch[1] | 82 | controllerNumber := controllerMatch[1] |
84 | 83 | ||
85 | value, err := readUintFromFile(path.Join(controller, "ce_count")) | 84 | value, err := readUintFromFile(filepath.Join(controller, "ce_count")) |
86 | if err != nil { | 85 | if err != nil { |
87 | return fmt.Errorf("couldn't get ce_count for controller %s: %s", controllerNumber, err) | 86 | return fmt.Errorf("couldn't get ce_count for controller %s: %s", controllerNumber, err) |
88 | } | 87 | } |
89 | ch <- prometheus.MustNewConstMetric( | 88 | ch <- prometheus.MustNewConstMetric( |
90 | c.ceCount, prometheus.CounterValue, float64(value), controllerNumber) | 89 | c.ceCount, prometheus.CounterValue, float64(value), controllerNumber) |
91 | 90 | ||
92 | value, err = readUintFromFile(path.Join(controller, "ce_noinfo_count")) | 91 | value, err = readUintFromFile(filepath.Join(controller, "ce_noinfo_count")) |
93 | if err != nil { | 92 | if err != nil { |
94 | return fmt.Errorf("couldn't get ce_noinfo_count for controller %s: %s", controllerNumber, err) | 93 | return fmt.Errorf("couldn't get ce_noinfo_count for controller %s: %s", controllerNumber, err) |
95 | } | 94 | } |
96 | ch <- prometheus.MustNewConstMetric( | 95 | ch <- prometheus.MustNewConstMetric( |
97 | c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, "unknown") | 96 | c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, "unknown") |
98 | 97 | ||
99 | value, err = readUintFromFile(path.Join(controller, "ue_count")) | 98 | value, err = readUintFromFile(filepath.Join(controller, "ue_count")) |
100 | if err != nil { | 99 | if err != nil { |
101 | return fmt.Errorf("couldn't get ue_count for controller %s: %s", controllerNumber, err) | 100 | return fmt.Errorf("couldn't get ue_count for controller %s: %s", controllerNumber, err) |
102 | } | 101 | } |
103 | ch <- prometheus.MustNewConstMetric( | 102 | ch <- prometheus.MustNewConstMetric( |
104 | c.ueCount, prometheus.CounterValue, float64(value), controllerNumber) | 103 | c.ueCount, prometheus.CounterValue, float64(value), controllerNumber) |
105 | 104 | ||
106 | value, err = readUintFromFile(path.Join(controller, "ue_noinfo_count")) | 105 | value, err = readUintFromFile(filepath.Join(controller, "ue_noinfo_count")) |
107 | if err != nil { | 106 | if err != nil { |
108 | return fmt.Errorf("couldn't get ue_noinfo_count for controller %s: %s", controllerNumber, err) | 107 | return fmt.Errorf("couldn't get ue_noinfo_count for controller %s: %s", controllerNumber, err) |
109 | } | 108 | } |
@@ -122,14 +121,14 @@ func (c *edacCollector) Update(ch chan<- prometheus.Metric) error { | |||
122 | } | 121 | } |
123 | csrowNumber := csrowMatch[1] | 122 | csrowNumber := csrowMatch[1] |
124 | 123 | ||
125 | value, err = readUintFromFile(path.Join(csrow, "ce_count")) | 124 | value, err = readUintFromFile(filepath.Join(csrow, "ce_count")) |
126 | if err != nil { | 125 | if err != nil { |
127 | return fmt.Errorf("couldn't get ce_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err) | 126 | return fmt.Errorf("couldn't get ce_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err) |
128 | } | 127 | } |
129 | ch <- prometheus.MustNewConstMetric( | 128 | ch <- prometheus.MustNewConstMetric( |
130 | c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, csrowNumber) | 129 | c.csRowCECount, prometheus.CounterValue, float64(value), controllerNumber, csrowNumber) |
131 | 130 | ||
132 | value, err = readUintFromFile(path.Join(csrow, "ue_count")) | 131 | value, err = readUintFromFile(filepath.Join(csrow, "ue_count")) |
133 | if err != nil { | 132 | if err != nil { |
134 | return fmt.Errorf("couldn't get ue_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err) | 133 | return fmt.Errorf("couldn't get ue_count for controller/csrow %s/%s: %s", controllerNumber, csrowNumber, err) |
135 | } | 134 | } |
diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index d5c1de6..400a151 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go | |||
@@ -19,7 +19,6 @@ import ( | |||
19 | "errors" | 19 | "errors" |
20 | "io/ioutil" | 20 | "io/ioutil" |
21 | "os" | 21 | "os" |
22 | "path" | ||
23 | "path/filepath" | 22 | "path/filepath" |
24 | "regexp" | 23 | "regexp" |
25 | "strconv" | 24 | "strconv" |
@@ -138,7 +137,7 @@ func collectSensorData(dir string, data map[string]map[string]string) error { | |||
138 | 137 | ||
139 | for _, t := range hwmonSensorTypes { | 138 | for _, t := range hwmonSensorTypes { |
140 | if t == sensorType { | 139 | if t == sensorType { |
141 | addValueFile(data, sensorType+strconv.Itoa(sensorNum), sensorProperty, path.Join(dir, file.Name())) | 140 | addValueFile(data, sensorType+strconv.Itoa(sensorNum), sensorProperty, filepath.Join(dir, file.Name())) |
142 | break | 141 | break |
143 | } | 142 | } |
144 | } | 143 | } |
@@ -157,8 +156,8 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er | |||
157 | if err != nil { | 156 | if err != nil { |
158 | return err | 157 | return err |
159 | } | 158 | } |
160 | if _, err := os.Stat(path.Join(dir, "device")); err == nil { | 159 | if _, err := os.Stat(filepath.Join(dir, "device")); err == nil { |
161 | err := collectSensorData(path.Join(dir, "device"), data) | 160 | err := collectSensorData(filepath.Join(dir, "device"), data) |
162 | if err != nil { | 161 | if err != nil { |
163 | return err | 162 | return err |
164 | } | 163 | } |
@@ -353,10 +352,10 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) { | |||
353 | 352 | ||
354 | // preference 1: construct a name based on device name, always unique | 353 | // preference 1: construct a name based on device name, always unique |
355 | 354 | ||
356 | devicePath, devErr := filepath.EvalSymlinks(path.Join(dir, "device")) | 355 | devicePath, devErr := filepath.EvalSymlinks(filepath.Join(dir, "device")) |
357 | if devErr == nil { | 356 | if devErr == nil { |
358 | devPathPrefix, devName := path.Split(devicePath) | 357 | devPathPrefix, devName := filepath.Split(devicePath) |
359 | _, devType := path.Split(strings.TrimRight(devPathPrefix, "/")) | 358 | _, devType := filepath.Split(strings.TrimRight(devPathPrefix, "/")) |
360 | 359 | ||
361 | cleanDevName := cleanMetricName(devName) | 360 | cleanDevName := cleanMetricName(devName) |
362 | cleanDevType := cleanMetricName(devType) | 361 | cleanDevType := cleanMetricName(devType) |
@@ -371,7 +370,7 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) { | |||
371 | } | 370 | } |
372 | 371 | ||
373 | // preference 2: is there a name file | 372 | // preference 2: is there a name file |
374 | sysnameRaw, nameErr := ioutil.ReadFile(path.Join(dir, "name")) | 373 | sysnameRaw, nameErr := ioutil.ReadFile(filepath.Join(dir, "name")) |
375 | if nameErr == nil && string(sysnameRaw) != "" { | 374 | if nameErr == nil && string(sysnameRaw) != "" { |
376 | cleanName := cleanMetricName(string(sysnameRaw)) | 375 | cleanName := cleanMetricName(string(sysnameRaw)) |
377 | if cleanName != "" { | 376 | if cleanName != "" { |
@@ -388,7 +387,7 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) { | |||
388 | } | 387 | } |
389 | 388 | ||
390 | // take the last path element, this will be hwmonX | 389 | // take the last path element, this will be hwmonX |
391 | _, name := path.Split(realDir) | 390 | _, name := filepath.Split(realDir) |
392 | cleanName := cleanMetricName(name) | 391 | cleanName := cleanMetricName(name) |
393 | if cleanName != "" { | 392 | if cleanName != "" { |
394 | return cleanName, nil | 393 | return cleanName, nil |
@@ -399,7 +398,7 @@ func (c *hwMonCollector) hwmonName(dir string) (string, error) { | |||
399 | // hwmonHumanReadableChipName is similar to the methods in hwmonName, but with | 398 | // hwmonHumanReadableChipName is similar to the methods in hwmonName, but with |
400 | // different precedences -- we can allow duplicates here. | 399 | // different precedences -- we can allow duplicates here. |
401 | func (c *hwMonCollector) hwmonHumanReadableChipName(dir string) (string, error) { | 400 | func (c *hwMonCollector) hwmonHumanReadableChipName(dir string) (string, error) { |
402 | sysnameRaw, nameErr := ioutil.ReadFile(path.Join(dir, "name")) | 401 | sysnameRaw, nameErr := ioutil.ReadFile(filepath.Join(dir, "name")) |
403 | if nameErr != nil { | 402 | if nameErr != nil { |
404 | return "", nameErr | 403 | return "", nameErr |
405 | } | 404 | } |
@@ -418,7 +417,7 @@ func (c *hwMonCollector) Update(ch chan<- prometheus.Metric) error { | |||
418 | // Step 1: scan /sys/class/hwmon, resolve all symlinks and call | 417 | // Step 1: scan /sys/class/hwmon, resolve all symlinks and call |
419 | // updatesHwmon for each folder | 418 | // updatesHwmon for each folder |
420 | 419 | ||
421 | hwmonPathName := path.Join(sysFilePath("class"), "hwmon") | 420 | hwmonPathName := filepath.Join(sysFilePath("class"), "hwmon") |
422 | 421 | ||
423 | hwmonFiles, err := ioutil.ReadDir(hwmonPathName) | 422 | hwmonFiles, err := ioutil.ReadDir(hwmonPathName) |
424 | if err != nil { | 423 | if err != nil { |
@@ -431,7 +430,7 @@ func (c *hwMonCollector) Update(ch chan<- prometheus.Metric) error { | |||
431 | } | 430 | } |
432 | 431 | ||
433 | for _, hwDir := range hwmonFiles { | 432 | for _, hwDir := range hwmonFiles { |
434 | hwmonXPathName := path.Join(hwmonPathName, hwDir.Name()) | 433 | hwmonXPathName := filepath.Join(hwmonPathName, hwDir.Name()) |
435 | 434 | ||
436 | if hwDir.Mode()&os.ModeSymlink > 0 { | 435 | if hwDir.Mode()&os.ModeSymlink > 0 { |
437 | hwDir, err = os.Stat(hwmonXPathName) | 436 | hwDir, err = os.Stat(hwmonXPathName) |
diff --git a/collector/ksmd_linux.go b/collector/ksmd_linux.go index c220e08..857664f 100644 --- a/collector/ksmd_linux.go +++ b/collector/ksmd_linux.go | |||
@@ -17,7 +17,7 @@ package collector | |||
17 | 17 | ||
18 | import ( | 18 | import ( |
19 | "fmt" | 19 | "fmt" |
20 | "path" | 20 | "path/filepath" |
21 | 21 | ||
22 | "github.com/prometheus/client_golang/prometheus" | 22 | "github.com/prometheus/client_golang/prometheus" |
23 | ) | 23 | ) |
@@ -62,7 +62,7 @@ func NewKsmdCollector() (Collector, error) { | |||
62 | // Update implements Collector and exposes kernel and system statistics. | 62 | // Update implements Collector and exposes kernel and system statistics. |
63 | func (c *ksmdCollector) Update(ch chan<- prometheus.Metric) error { | 63 | func (c *ksmdCollector) Update(ch chan<- prometheus.Metric) error { |
64 | for _, n := range ksmdFiles { | 64 | for _, n := range ksmdFiles { |
65 | val, err := readUintFromFile(sysFilePath(path.Join("kernel/mm/ksm", n))) | 65 | val, err := readUintFromFile(sysFilePath(filepath.Join("kernel/mm/ksm", n))) |
66 | if err != nil { | 66 | if err != nil { |
67 | return err | 67 | return err |
68 | } | 68 | } |
diff --git a/collector/meminfo_numa_linux.go b/collector/meminfo_numa_linux.go index b096164..3d121bd 100644 --- a/collector/meminfo_numa_linux.go +++ b/collector/meminfo_numa_linux.go | |||
@@ -20,7 +20,6 @@ import ( | |||
20 | "fmt" | 20 | "fmt" |
21 | "io" | 21 | "io" |
22 | "os" | 22 | "os" |
23 | "path" | ||
24 | "path/filepath" | 23 | "path/filepath" |
25 | "regexp" | 24 | "regexp" |
26 | "strconv" | 25 | "strconv" |
@@ -86,7 +85,7 @@ func getMemInfoNuma() ([]meminfoMetric, error) { | |||
86 | return nil, err | 85 | return nil, err |
87 | } | 86 | } |
88 | for _, node := range nodes { | 87 | for _, node := range nodes { |
89 | meminfoFile, err := os.Open(path.Join(node, "meminfo")) | 88 | meminfoFile, err := os.Open(filepath.Join(node, "meminfo")) |
90 | if err != nil { | 89 | if err != nil { |
91 | return nil, err | 90 | return nil, err |
92 | } | 91 | } |
@@ -98,7 +97,7 @@ func getMemInfoNuma() ([]meminfoMetric, error) { | |||
98 | } | 97 | } |
99 | metrics = append(metrics, numaInfo...) | 98 | metrics = append(metrics, numaInfo...) |
100 | 99 | ||
101 | numastatFile, err := os.Open(path.Join(node, "numastat")) | 100 | numastatFile, err := os.Open(filepath.Join(node, "numastat")) |
102 | if err != nil { | 101 | if err != nil { |
103 | return nil, err | 102 | return nil, err |
104 | } | 103 | } |