cc-metric-collector/collectors/infinibandMetric.go

84 lines
1.8 KiB
Go
Raw Normal View History

2021-03-25 15:55:06 +01:00
package collectors
import (
"fmt"
2021-10-04 15:23:43 +02:00
lp "github.com/influxdata/line-protocol"
2021-03-25 15:55:06 +01:00
"io/ioutil"
"log"
"os/exec"
"strconv"
2021-03-25 17:47:08 +01:00
"strings"
2021-03-25 15:55:06 +01:00
"time"
)
const LIDFILE = `/sys/class/infiniband/mlx4_0/ports/1/lid`
const PERFQUERY = `/usr/sbin/perfquery`
2021-03-25 15:55:06 +01:00
type InfinibandCollector struct {
MetricCollector
2021-10-04 15:23:43 +02:00
tags map[string]string
2021-03-25 15:55:06 +01:00
}
func (m *InfinibandCollector) Init() error {
2021-03-25 17:47:08 +01:00
m.name = "InfinibandCollector"
2021-03-25 15:55:06 +01:00
m.setup()
2021-10-04 15:23:43 +02:00
m.tags = map[string]string{"type": "node"}
_, err := ioutil.ReadFile(string(LIDFILE))
2021-10-04 15:23:43 +02:00
if err == nil {
_, err = ioutil.ReadFile(string(PERFQUERY))
if err == nil {
m.init = true
}
}
return err
2021-03-25 15:55:06 +01:00
}
2021-10-04 15:23:43 +02:00
func (m *InfinibandCollector) Read(interval time.Duration, out *[]lp.MutableMetric) {
2021-03-25 15:55:06 +01:00
buffer, err := ioutil.ReadFile(string(LIDFILE))
if err != nil {
log.Print(err)
return
}
2021-03-25 17:47:08 +01:00
2021-03-25 15:55:06 +01:00
args := fmt.Sprintf("-r %s 1 0xf000", string(buffer))
2021-03-25 17:47:08 +01:00
command := exec.Command(PERFQUERY, args)
2021-03-25 15:55:06 +01:00
command.Wait()
stdout, err := command.Output()
if err != nil {
2021-03-25 17:47:08 +01:00
log.Print(err)
2021-03-25 15:55:06 +01:00
return
}
ll := strings.Split(string(stdout), "\n")
for _, line := range ll {
2021-03-25 17:47:08 +01:00
if strings.HasPrefix(line, "PortRcvData") || strings.HasPrefix(line, "RcvData") {
lv := strings.Fields(line)
v, err := strconv.ParseFloat(lv[1], 64)
if err == nil {
2021-10-04 15:23:43 +02:00
y, err := lp.New("ib_recv", m.tags, map[string]interface{}{"value": float64(v)}, time.Now())
if err == nil {
*out = append(*out, y)
}
2021-03-25 17:47:08 +01:00
}
}
2021-03-25 15:55:06 +01:00
if strings.HasPrefix(line, "PortXmitData") || strings.HasPrefix(line, "XmtData") {
2021-03-25 17:47:08 +01:00
lv := strings.Fields(line)
v, err := strconv.ParseFloat(lv[1], 64)
if err == nil {
2021-10-04 15:23:43 +02:00
y, err := lp.New("ib_xmit", m.tags, map[string]interface{}{"value": float64(v)}, time.Now())
if err == nil {
*out = append(*out, y)
}
2021-03-25 17:47:08 +01:00
}
}
2021-03-25 15:55:06 +01:00
}
}
func (m *InfinibandCollector) Close() {
2021-10-04 15:47:03 +02:00
m.init = false
2021-03-25 17:47:08 +01:00
return
2021-03-25 15:55:06 +01:00
}