cc-metric-collector/collectors/metricCollector.go

154 lines
3.2 KiB
Go
Raw Normal View History

2021-03-25 14:46:21 +01:00
package collectors
import (
2021-11-25 15:11:39 +01:00
"errors"
2021-10-04 15:23:43 +02:00
lp "github.com/influxdata/line-protocol"
2021-03-25 14:46:21 +01:00
"io/ioutil"
"log"
"strconv"
2021-03-25 17:47:08 +01:00
"strings"
"time"
2021-03-25 14:46:21 +01:00
)
type MetricGetter interface {
2021-03-25 17:47:08 +01:00
Name() string
Init(config []byte) error
2021-10-04 15:23:43 +02:00
Read(time.Duration, *[]lp.MutableMetric)
2021-03-25 14:46:21 +01:00
Close()
2021-10-04 15:47:03 +02:00
// GetNodeMetric() map[string]interface{}
// GetSocketMetrics() map[int]map[string]interface{}
// GetCpuMetrics() map[int]map[string]interface{}
2021-03-25 14:46:21 +01:00
}
type MetricCollector struct {
2021-10-04 15:47:03 +02:00
name string
init bool
// node map[string]interface{}
// sockets map[int]map[string]interface{}
// cpus map[int]map[string]interface{}
2021-03-25 14:46:21 +01:00
}
func (c *MetricCollector) Name() string {
return c.name
}
2021-10-04 15:23:43 +02:00
//func (c *MetricCollector) GetNodeMetric() map[string]interface{} {
// return c.node
//}
2021-03-25 14:46:21 +01:00
2021-10-04 15:23:43 +02:00
//func (c *MetricCollector) GetSocketMetrics() map[int]map[string]interface{} {
// return c.sockets
//}
2021-03-25 14:46:21 +01:00
2021-10-04 15:23:43 +02:00
//func (c *MetricCollector) GetCpuMetrics() map[int]map[string]interface{} {
// return c.cpus
//}
2021-03-25 14:46:21 +01:00
func (c *MetricCollector) setup() error {
2021-10-04 15:47:03 +02:00
// slist := SocketList()
// clist := CpuList()
// c.node = make(map[string]interface{})
// c.sockets = make(map[int]map[string]interface{}, len(slist))
// for _, s := range slist {
// c.sockets[s] = make(map[string]interface{})
// }
// c.cpus = make(map[int]map[string]interface{}, len(clist))
// for _, s := range clist {
// c.cpus[s] = make(map[string]interface{})
// }
2021-03-25 17:47:08 +01:00
return nil
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
}
func stringArrayContains(array []string, str string) (int, bool) {
2021-11-25 15:11:39 +01:00
for i, a := range array {
if a == str {
return i, true
}
}
return -1, false
}
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
}
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
}
func Tags2Map(metric lp.Metric) map[string]string {
tags := make(map[string]string)
for _, t := range metric.TagList() {
tags[t.Key] = t.Value
}
return tags
}
func Fields2Map(metric lp.Metric) map[string]interface{} {
fields := make(map[string]interface{})
for _, f := range metric.FieldList() {
fields[f.Key] = f.Value
}
return fields
}
func RemoveFromStringList(s []string, r string) ([]string, error) {
2021-11-25 15:11:39 +01:00
for i, item := range s {
if r == item {
return append(s[:i], s[i+1:]...), nil
}
}
return s, errors.New("No such string in list")
}