mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-12 21:17:25 +01:00
Read file line by line
This commit is contained in:
parent
0cf32d5988
commit
ae44b7f826
@ -1,6 +1,7 @@
|
|||||||
package ccTopology
|
package ccTopology
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -20,6 +21,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// fileToInt reads an integer value from a file
|
// fileToInt reads an integer value from a file
|
||||||
|
// In case of an error -1 is returned
|
||||||
// Used internally for sysfs file reads
|
// Used internally for sysfs file reads
|
||||||
func fileToInt(path string) int {
|
func fileToInt(path string) int {
|
||||||
buffer, err := os.ReadFile(path)
|
buffer, err := os.ReadFile(path)
|
||||||
@ -39,23 +41,27 @@ func fileToInt(path string) int {
|
|||||||
|
|
||||||
// Get list of CPU socket IDs
|
// Get list of CPU socket IDs
|
||||||
func SocketList() []int {
|
func SocketList() []int {
|
||||||
buffer, err := os.ReadFile(string(PROCFS_CPUINFO))
|
|
||||||
|
packs := make([]int, 0)
|
||||||
|
|
||||||
|
file, err := os.Open(string(PROCFS_CPUINFO))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ll := strings.Split(string(buffer), "\n")
|
defer file.Close()
|
||||||
packs := make([]int, 0)
|
|
||||||
for _, line := range ll {
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
if strings.HasPrefix(line, "physical id") {
|
if strings.HasPrefix(line, "physical id") {
|
||||||
lv := strings.Fields(line)
|
lv := strings.Fields(line)
|
||||||
id, err := strconv.ParseInt(lv[3], 10, 32)
|
id, err := strconv.ParseInt(lv[3], 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return packs
|
return nil
|
||||||
}
|
}
|
||||||
found := slices.Contains(packs, int(id))
|
if found := slices.Contains(packs, int(id)); !found {
|
||||||
if !found {
|
|
||||||
packs = append(packs, int(id))
|
packs = append(packs, int(id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user