Refactor: Replace readOneLine() by ioutil.ReadFile()

This commit is contained in:
Holger Obermaier 2022-02-10 09:28:06 +01:00
parent acf5db543e
commit 82138df48e
2 changed files with 24 additions and 34 deletions

View File

@ -1,10 +1,9 @@
package collectors package collectors
import ( import (
"bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "io/ioutil"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -15,23 +14,6 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
//
// readOneLine reads one line from a file.
// It returns ok when file was successfully read.
// In this case text contains the first line of the files contents.
//
func readOneLine(filename string) (text string, ok bool) {
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
ok = scanner.Scan()
text = scanner.Text()
return
}
type CPUFreqCollectorTopology struct { type CPUFreqCollectorTopology struct {
processor string // logical processor number (continuous, starting at 0) processor string // logical processor number (continuous, starting at 0)
coreID string // socket local core ID coreID string // socket local core ID
@ -105,10 +87,11 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error {
// Read package ID // Read package ID
physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id") physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id")
physicalPackageID, ok := readOneLine(physicalPackageIDFile) line, err := ioutil.ReadFile(physicalPackageIDFile)
if !ok { if err != nil {
return fmt.Errorf("Unable to read physical package ID from file '%s'", physicalPackageIDFile) return fmt.Errorf("Unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err)
} }
physicalPackageID := strings.TrimSpace(string(line))
physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64) physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("Unable to convert packageID '%s' to int64: %v", physicalPackageID, err) return fmt.Errorf("Unable to convert packageID '%s' to int64: %v", physicalPackageID, err)
@ -116,10 +99,11 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error {
// Read core ID // Read core ID
coreIDFile := filepath.Join(cpuDir, "topology", "core_id") coreIDFile := filepath.Join(cpuDir, "topology", "core_id")
coreID, ok := readOneLine(coreIDFile) line, err = ioutil.ReadFile(coreIDFile)
if !ok { if err != nil {
return fmt.Errorf("Unable to read core ID from file '%s'", coreIDFile) return fmt.Errorf("Unable to read core ID from file '%s': %v", coreIDFile, err)
} }
coreID := strings.TrimSpace(string(line))
coreID_int, err := strconv.ParseInt(coreID, 10, 64) coreID_int, err := strconv.ParseInt(coreID, 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("Unable to convert coreID '%s' to int64: %v", coreID, err) return fmt.Errorf("Unable to convert coreID '%s' to int64: %v", coreID, err)
@ -205,14 +189,14 @@ func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric)
} }
// Read current frequency // Read current frequency
line, ok := readOneLine(t.scalingCurFreqFile) line, err := ioutil.ReadFile(t.scalingCurFreqFile)
if !ok { if err != nil {
cclog.ComponentError( cclog.ComponentError(
m.name, m.name,
fmt.Sprintf("Read(): Failed to read one line from file '%s'", t.scalingCurFreqFile)) fmt.Sprintf("Read(): Failed to read file '%s': %v", t.scalingCurFreqFile, err))
continue continue
} }
cpuFreq, err := strconv.ParseInt(line, 10, 64) cpuFreq, err := strconv.ParseInt(strings.TrimSpace(string(line)), 10, 64)
if err != nil { if err != nil {
cclog.ComponentError( cclog.ComponentError(
m.name, m.name,

View File

@ -2,6 +2,7 @@ package collectors
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
@ -68,8 +69,12 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error {
for _, path := range ibDirs { for _, path := range ibDirs {
// Skip, when no LID is assigned // Skip, when no LID is assigned
LID, ok := readOneLine(path + "/lid") line, err := ioutil.ReadFile(filepath.Join(path, "lid"))
if !ok || LID == "0x0" { if err != nil {
continue
}
LID := strings.TrimSpace(string(line))
if LID == "0x0" {
continue continue
} }
@ -142,13 +147,14 @@ func (m *InfinibandCollector) Read(interval time.Duration, output chan lp.CCMetr
// device info // device info
info := &m.info[i] info := &m.info[i]
for counterName, counterFile := range info.portCounterFiles { for counterName, counterFile := range info.portCounterFiles {
data, ok := readOneLine(counterFile) line, err := ioutil.ReadFile(counterFile)
if !ok { if err != nil {
cclog.ComponentError( cclog.ComponentError(
m.name, m.name,
fmt.Sprintf("Read(): Failed to read one line from file '%s'", counterFile)) fmt.Sprintf("Read(): Failed to read from file '%s': %v", counterFile, err))
continue continue
} }
data := strings.TrimSpace(string(line))
v, err := strconv.ParseInt(data, 10, 64) v, err := strconv.ParseInt(data, 10, 64)
if err != nil { if err != nil {
cclog.ComponentError( cclog.ComponentError(