Fix staticcheck warnings (#66)

This commit is contained in:
Thomas Gruber 2022-03-15 16:38:20 +01:00 committed by GitHub
parent 57629a2e0a
commit c182d295f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 24 deletions

View File

@ -57,7 +57,7 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error {
const cpuInfoFile = "/proc/cpuinfo" const cpuInfoFile = "/proc/cpuinfo"
file, err := os.Open(cpuInfoFile) file, err := os.Open(cpuInfoFile)
if err != nil { if err != nil {
return fmt.Errorf("Failed to open file '%s': %v", cpuInfoFile, err) return fmt.Errorf("failed to open file '%s': %v", cpuInfoFile, err)
} }
defer file.Close() defer file.Close()
@ -106,14 +106,14 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error {
topology.coreID = coreID topology.coreID = coreID
topology.coreID_int, err = strconv.ParseInt(coreID, 10, 64) topology.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)
} }
// Physical package ID // Physical package ID
topology.physicalPackageID = physicalPackageID topology.physicalPackageID = physicalPackageID
topology.physicalPackageID_int, err = strconv.ParseInt(physicalPackageID, 10, 64) topology.physicalPackageID_int, err = strconv.ParseInt(physicalPackageID, 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("Unable to convert physicalPackageID '%s' to int64: %v", physicalPackageID, err) return fmt.Errorf("unable to convert physicalPackageID '%s' to int64: %v", physicalPackageID, err)
} }
// increase maximun socket / package ID, when required // increase maximun socket / package ID, when required

View File

@ -70,10 +70,10 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error {
globPattern := filepath.Join(baseDir, "cpu[0-9]*") globPattern := filepath.Join(baseDir, "cpu[0-9]*")
cpuDirs, err := filepath.Glob(globPattern) cpuDirs, err := filepath.Glob(globPattern)
if err != nil { if err != nil {
return fmt.Errorf("Unable to glob files with pattern '%s': %v", globPattern, err) return fmt.Errorf("unable to glob files with pattern '%s': %v", globPattern, err)
} }
if cpuDirs == nil { if cpuDirs == nil {
return fmt.Errorf("Unable to find any files with pattern '%s'", globPattern) return fmt.Errorf("unable to find any files with pattern '%s'", globPattern)
} }
// Initialize CPU topology // Initialize CPU topology
@ -82,38 +82,38 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error {
processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu") processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu")
processor_int, err := strconv.ParseInt(processor, 10, 64) processor_int, err := strconv.ParseInt(processor, 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("Unable to convert cpuID '%s' to int64: %v", processor, err) return fmt.Errorf("unable to convert cpuID '%s' to int64: %v", processor, err)
} }
// Read package ID // Read package ID
physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id") physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id")
line, err := ioutil.ReadFile(physicalPackageIDFile) line, err := ioutil.ReadFile(physicalPackageIDFile)
if err != nil { if err != nil {
return fmt.Errorf("Unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err) return fmt.Errorf("unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err)
} }
physicalPackageID := strings.TrimSpace(string(line)) 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)
} }
// Read core ID // Read core ID
coreIDFile := filepath.Join(cpuDir, "topology", "core_id") coreIDFile := filepath.Join(cpuDir, "topology", "core_id")
line, err = ioutil.ReadFile(coreIDFile) line, err = ioutil.ReadFile(coreIDFile)
if err != nil { if err != nil {
return fmt.Errorf("Unable to read core ID from file '%s': %v", coreIDFile, err) return fmt.Errorf("unable to read core ID from file '%s': %v", coreIDFile, err)
} }
coreID := strings.TrimSpace(string(line)) 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)
} }
// Check access to current frequency file // Check access to current frequency file
scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq") scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq")
err = unix.Access(scalingCurFreqFile, unix.R_OK) err = unix.Access(scalingCurFreqFile, unix.R_OK)
if err != nil { if err != nil {
return fmt.Errorf("Unable to access file '%s': %v", scalingCurFreqFile, err) return fmt.Errorf("unable to access file '%s': %v", scalingCurFreqFile, err)
} }
t := &m.topology[processor_int] t := &m.topology[processor_int]

View File

@ -25,7 +25,6 @@ type CpustatCollector struct {
matches map[string]int matches map[string]int
cputags map[string]map[string]string cputags map[string]map[string]string
nodetags map[string]string nodetags map[string]string
num_cpus_metric lp.CCMetric
} }
func (m *CpustatCollector) Init(config json.RawMessage) error { func (m *CpustatCollector) Init(config json.RawMessage) error {

View File

@ -61,7 +61,7 @@ func (m *CustomCmdCollector) Init(config json.RawMessage) error {
} }
} }
if len(m.files) == 0 && len(m.commands) == 0 { if len(m.files) == 0 && len(m.commands) == 0 {
return errors.New("No metrics to collect") return errors.New("no metrics to collect")
} }
m.handler = influx.NewMetricHandler() m.handler = influx.NewMetricHandler()
m.parser = influx.NewParser(m.handler) m.parser = influx.NewParser(m.handler)

View File

@ -54,7 +54,7 @@ func (m *IpmiCollector) Init(config json.RawMessage) error {
m.ipmisensors = p m.ipmisensors = p
} }
if len(m.ipmitool) == 0 && len(m.ipmisensors) == 0 { if len(m.ipmitool) == 0 && len(m.ipmisensors) == 0 {
return errors.New("No IPMI reader found") return errors.New("no IPMI reader found")
} }
m.init = true m.init = true
return nil return nil

View File

@ -125,5 +125,5 @@ func RemoveFromStringList(s []string, r string) ([]string, error) {
return append(s[:i], s[i+1:]...), nil return append(s[:i], s[i+1:]...), nil
} }
} }
return s, fmt.Errorf("No such string in list") return s, fmt.Errorf("no such string in list")
} }

View File

@ -70,10 +70,10 @@ func (m *TempCollector) Init(config json.RawMessage) error {
globPattern := filepath.Join("/sys/class/hwmon", "*", "temp*_input") globPattern := filepath.Join("/sys/class/hwmon", "*", "temp*_input")
inputFiles, err := filepath.Glob(globPattern) inputFiles, err := filepath.Glob(globPattern)
if err != nil { if err != nil {
return fmt.Errorf("Unable to glob files with pattern '%s': %v", globPattern, err) return fmt.Errorf("unable to glob files with pattern '%s': %v", globPattern, err)
} }
if inputFiles == nil { if inputFiles == nil {
return fmt.Errorf("Unable to find any files with pattern '%s'", globPattern) return fmt.Errorf("unable to find any files with pattern '%s'", globPattern)
} }
// Get sensor name for each temperature sensor file // Get sensor name for each temperature sensor file
@ -158,7 +158,7 @@ func (m *TempCollector) Init(config json.RawMessage) error {
// Empty sensors map // Empty sensors map
if len(m.sensors) == 0 { if len(m.sensors) == 0 {
return fmt.Errorf("No temperature sensors found") return fmt.Errorf("no temperature sensors found")
} }
// Finished initialization // Finished initialization

View File

@ -39,14 +39,14 @@ func (m *TopProcsCollector) Init(config json.RawMessage) error {
m.config.Num_procs = int(DEFAULT_NUM_PROCS) m.config.Num_procs = int(DEFAULT_NUM_PROCS)
} }
if m.config.Num_procs <= 0 || m.config.Num_procs > MAX_NUM_PROCS { if m.config.Num_procs <= 0 || m.config.Num_procs > MAX_NUM_PROCS {
return errors.New(fmt.Sprintf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS)) return fmt.Errorf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS)
} }
m.setup() m.setup()
command := exec.Command("ps", "-Ao", "comm", "--sort=-pcpu") command := exec.Command("ps", "-Ao", "comm", "--sort=-pcpu")
command.Wait() command.Wait()
_, err = command.Output() _, err = command.Output()
if err != nil { if err != nil {
return errors.New("Failed to execute command") return errors.New("failed to execute command")
} }
m.init = true m.init = true
return nil return nil