2021-03-25 14:46:21 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2022-01-25 15:37:43 +01:00
|
|
|
"encoding/json"
|
2022-01-25 17:20:20 +01:00
|
|
|
"fmt"
|
2021-03-25 14:46:21 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
2021-03-25 17:47:08 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-25 17:20:20 +01:00
|
|
|
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-03-25 14:46:21 +01:00
|
|
|
)
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
type MetricCollector interface {
|
2022-02-28 12:16:48 +01:00
|
|
|
Name() string // Name of the metric collector
|
|
|
|
Init(config json.RawMessage) error // Initialize metric collector
|
|
|
|
Initialized() bool // Is metric collector initialized?
|
|
|
|
Read(duration time.Duration, output chan lp.CCMetric) // Read metrics from metric collector
|
|
|
|
Close() // Close / finish metric collector
|
2021-03-25 14:46:21 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
type metricCollector struct {
|
2022-02-28 12:16:48 +01:00
|
|
|
name string // name of the metric
|
|
|
|
init bool // is metric collector initialized?
|
|
|
|
meta map[string]string // static meta data tags
|
2021-03-25 14:46:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 12:16:48 +01:00
|
|
|
// Name returns the name of the metric collector
|
2022-01-25 15:37:43 +01:00
|
|
|
func (c *metricCollector) Name() string {
|
2021-03-25 14:46:21 +01:00
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:16:48 +01:00
|
|
|
// Setup is for future use
|
2022-01-25 15:37:43 +01:00
|
|
|
func (c *metricCollector) setup() error {
|
2021-03-25 17:47:08 +01:00
|
|
|
return nil
|
2021-03-25 14:46:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 12:16:48 +01:00
|
|
|
// Initialized indicates whether the metric collector has been initialized
|
2022-01-25 15:37:43 +01:00
|
|
|
func (c *metricCollector) Initialized() bool {
|
2022-01-25 17:20:20 +01:00
|
|
|
return c.init
|
2021-11-26 19:01:31 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 17:20:20 +01:00
|
|
|
// intArrayContains scans an array of ints if the value str is present in the array
|
|
|
|
// If the specified value is found, the corresponding array index is returned.
|
|
|
|
// The bool value is used to signal success or failure
|
2021-03-25 14:46:21 +01:00
|
|
|
func intArrayContains(array []int, str int) (int, bool) {
|
2021-03-25 17:47:08 +01:00
|
|
|
for i, a := range array {
|
|
|
|
if a == str {
|
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, false
|
2021-03-25 14:46:21 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 17:20:20 +01:00
|
|
|
// stringArrayContains scans an array of strings if the value str is present in the array
|
|
|
|
// If the specified value is found, the corresponding array index is returned.
|
|
|
|
// The bool value is used to signal success or failure
|
2021-11-25 14:04:03 +01:00
|
|
|
func stringArrayContains(array []string, str string) (int, bool) {
|
2021-11-25 15:11:39 +01:00
|
|
|
for i, a := range array {
|
2021-11-25 14:04:03 +01:00
|
|
|
if a == str {
|
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, false
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:16:48 +01:00
|
|
|
// SocketList returns the list of physical sockets as read from /proc/cpuinfo
|
2021-03-25 14:46:21 +01:00
|
|
|
func SocketList() []int {
|
2021-03-25 17:47:08 +01:00
|
|
|
buffer, err := ioutil.ReadFile("/proc/cpuinfo")
|
|
|
|
if err != nil {
|
2021-03-25 14:46:21 +01:00
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ll := strings.Split(string(buffer), "\n")
|
|
|
|
var packs []int
|
|
|
|
for _, line := range ll {
|
2021-03-25 17:47:08 +01:00
|
|
|
if strings.HasPrefix(line, "physical id") {
|
|
|
|
lv := strings.Fields(line)
|
|
|
|
id, err := strconv.ParseInt(lv[3], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return packs
|
|
|
|
}
|
|
|
|
_, found := intArrayContains(packs, int(id))
|
|
|
|
if !found {
|
|
|
|
packs = append(packs, int(id))
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 14:46:21 +01:00
|
|
|
}
|
|
|
|
return packs
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:16:48 +01:00
|
|
|
// CpuList returns the list of physical CPUs (in contrast to logical CPUs) as read from /proc/cpuinfo
|
2021-03-25 14:46:21 +01:00
|
|
|
func CpuList() []int {
|
2021-03-25 17:47:08 +01:00
|
|
|
buffer, err := ioutil.ReadFile("/proc/cpuinfo")
|
|
|
|
if err != nil {
|
2021-03-25 14:46:21 +01:00
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ll := strings.Split(string(buffer), "\n")
|
|
|
|
var cpulist []int
|
|
|
|
for _, line := range ll {
|
2021-03-25 17:47:08 +01:00
|
|
|
if strings.HasPrefix(line, "processor") {
|
|
|
|
lv := strings.Fields(line)
|
|
|
|
id, err := strconv.ParseInt(lv[2], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return cpulist
|
|
|
|
}
|
|
|
|
_, found := intArrayContains(cpulist, int(id))
|
|
|
|
if !found {
|
|
|
|
cpulist = append(cpulist, int(id))
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 14:46:21 +01:00
|
|
|
}
|
|
|
|
return cpulist
|
|
|
|
}
|
2021-10-08 13:28:02 +02:00
|
|
|
|
2022-01-25 17:20:20 +01:00
|
|
|
// RemoveFromStringList removes the string r from the array of strings s
|
|
|
|
// If r is not contained in the array an error is returned
|
2021-11-25 14:04:03 +01:00
|
|
|
func RemoveFromStringList(s []string, r string) ([]string, error) {
|
2022-02-28 12:16:48 +01:00
|
|
|
for i := range s {
|
|
|
|
if r == s[i] {
|
2021-11-25 15:11:39 +01:00
|
|
|
return append(s[:i], s[i+1:]...), nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 16:38:20 +01:00
|
|
|
return s, fmt.Errorf("no such string in list")
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|