2021-03-25 15:55:06 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-01-26 20:18:47 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2022-01-26 20:18:47 +01:00
|
|
|
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-11-25 14:04:03 +01:00
|
|
|
"path/filepath"
|
2021-03-25 15:55:06 +01:00
|
|
|
"strconv"
|
2021-03-25 17:47:08 +01:00
|
|
|
"strings"
|
2021-03-25 15:55:06 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-01-26 20:18:47 +01:00
|
|
|
const IB_BASEPATH = `/sys/class/infiniband/`
|
2021-11-25 14:04:03 +01:00
|
|
|
|
2021-03-25 15:55:06 +01:00
|
|
|
type InfinibandCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-01-26 20:18:47 +01:00
|
|
|
tags map[string]string
|
|
|
|
lids map[string]map[string]string
|
|
|
|
config struct {
|
|
|
|
ExcludeDevices []string `json:"exclude_devices,omitempty"`
|
|
|
|
}
|
2021-03-25 15:55:06 +01:00
|
|
|
}
|
|
|
|
|
2021-11-25 16:25:20 +01:00
|
|
|
func (m *InfinibandCollector) Help() {
|
2022-01-26 20:18:47 +01:00
|
|
|
fmt.Println("This collector includes all devices that can be found below ", IB_BASEPATH)
|
|
|
|
fmt.Println("and where any of the ports provides a 'lid' file (glob ", IB_BASEPATH, "/<dev>/ports/<port>/lid).")
|
2021-11-25 16:25:20 +01:00
|
|
|
fmt.Println("The devices can be filtered with the 'exclude_devices' option in the configuration.")
|
|
|
|
fmt.Println("For each found LIDs the collector calls the 'perfquery' command")
|
2022-01-25 10:32:08 +01:00
|
|
|
fmt.Println("")
|
2021-11-25 16:25:20 +01:00
|
|
|
fmt.Println("Full configuration object:")
|
|
|
|
fmt.Println("\"ibstat\" : {")
|
|
|
|
fmt.Println(" \"exclude_devices\" : [\"dev1\"]")
|
2022-01-25 10:32:08 +01:00
|
|
|
fmt.Println("}")
|
|
|
|
fmt.Println("")
|
2021-11-25 16:25:20 +01:00
|
|
|
fmt.Println("Metrics:")
|
|
|
|
fmt.Println("- ib_recv")
|
|
|
|
fmt.Println("- ib_xmit")
|
2022-01-19 10:15:41 +01:00
|
|
|
fmt.Println("- ib_recv_pkts")
|
|
|
|
fmt.Println("- ib_xmit_pkts")
|
2021-11-25 16:25:20 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *InfinibandCollector) Init(config json.RawMessage) error {
|
2021-11-25 15:11:39 +01:00
|
|
|
var err error
|
2021-03-25 17:47:08 +01:00
|
|
|
m.name = "InfinibandCollector"
|
2021-03-25 15:55:06 +01:00
|
|
|
m.setup()
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "Network"}
|
2021-10-04 15:23:43 +02:00
|
|
|
m.tags = map[string]string{"type": "node"}
|
2021-11-25 14:04:03 +01:00
|
|
|
if len(config) > 0 {
|
2021-11-25 15:11:39 +01:00
|
|
|
err = json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
m.lids = make(map[string]map[string]string)
|
2022-01-26 20:18:47 +01:00
|
|
|
p := fmt.Sprintf("%s/*/ports/*/lid", string(IB_BASEPATH))
|
2021-11-25 15:11:39 +01:00
|
|
|
files, err := filepath.Glob(p)
|
|
|
|
for _, f := range files {
|
|
|
|
lid, err := ioutil.ReadFile(f)
|
|
|
|
if err == nil {
|
2022-01-26 20:18:47 +01:00
|
|
|
plist := strings.Split(strings.Replace(f, string(IB_BASEPATH), "", -1), "/")
|
2021-11-25 15:11:39 +01:00
|
|
|
skip := false
|
|
|
|
for _, d := range m.config.ExcludeDevices {
|
|
|
|
if d == plist[0] {
|
|
|
|
skip = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !skip {
|
|
|
|
m.lids[plist[0]] = make(map[string]string)
|
|
|
|
m.lids[plist[0]][plist[2]] = string(lid)
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
|
2022-01-26 20:18:47 +01:00
|
|
|
if len(m.lids) == 0 {
|
|
|
|
return errors.New("No usable IB devices")
|
2021-03-25 15:55:06 +01:00
|
|
|
}
|
2021-03-25 17:47:08 +01:00
|
|
|
|
2022-01-26 20:18:47 +01:00
|
|
|
m.init = true
|
|
|
|
return nil
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-03-25 17:47:08 +01:00
|
|
|
|
2022-01-26 20:18:47 +01:00
|
|
|
func (m *InfinibandCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-03-25 15:55:06 +01:00
|
|
|
|
2022-01-26 20:18:47 +01:00
|
|
|
if m.init {
|
|
|
|
for dev, ports := range m.lids {
|
|
|
|
for port, lid := range ports {
|
|
|
|
tags := map[string]string{
|
|
|
|
"type": "node",
|
|
|
|
"device": dev,
|
|
|
|
"port": port,
|
|
|
|
"lid": lid}
|
|
|
|
path := fmt.Sprintf("%s/%s/ports/%s/counters/", string(IB_BASEPATH), dev, port)
|
|
|
|
buffer, err := ioutil.ReadFile(fmt.Sprintf("%s/port_rcv_data", path))
|
2022-01-25 15:37:43 +01:00
|
|
|
if err == nil {
|
2022-01-26 20:18:47 +01:00
|
|
|
data := strings.Replace(string(buffer), "\n", "", -1)
|
|
|
|
v, err := strconv.ParseFloat(data, 64)
|
|
|
|
if err == nil {
|
|
|
|
y, err := lp.New("ib_recv", tags, m.meta, map[string]interface{}{"value": float64(v)}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
output <- y
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
2022-01-26 20:18:47 +01:00
|
|
|
buffer, err = ioutil.ReadFile(fmt.Sprintf("%s/port_xmit_data", path))
|
2022-01-25 15:37:43 +01:00
|
|
|
if err == nil {
|
2022-01-26 20:18:47 +01:00
|
|
|
data := strings.Replace(string(buffer), "\n", "", -1)
|
|
|
|
v, err := strconv.ParseFloat(data, 64)
|
|
|
|
if err == nil {
|
|
|
|
y, err := lp.New("ib_xmit", tags, m.meta, map[string]interface{}{"value": float64(v)}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
output <- y
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
2022-01-26 20:18:47 +01:00
|
|
|
buffer, err = ioutil.ReadFile(fmt.Sprintf("%s/port_rcv_packets", path))
|
2021-10-04 15:23:43 +02:00
|
|
|
if err == nil {
|
2022-01-26 20:18:47 +01:00
|
|
|
data := strings.Replace(string(buffer), "\n", "", -1)
|
|
|
|
v, err := strconv.ParseFloat(data, 64)
|
|
|
|
if err == nil {
|
|
|
|
y, err := lp.New("ib_recv_pkts", tags, m.meta, map[string]interface{}{"value": float64(v)}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
output <- y
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2022-01-26 20:18:47 +01:00
|
|
|
buffer, err = ioutil.ReadFile(fmt.Sprintf("%s/port_xmit_packets", path))
|
2022-01-19 10:15:41 +01:00
|
|
|
if err == nil {
|
2022-01-26 20:18:47 +01:00
|
|
|
data := strings.Replace(string(buffer), "\n", "", -1)
|
|
|
|
v, err := strconv.ParseFloat(data, 64)
|
|
|
|
if err == nil {
|
|
|
|
y, err := lp.New("ib_xmit_pkts", tags, m.meta, map[string]interface{}{"value": float64(v)}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
output <- y
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 15:11:39 +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 15:55:06 +01:00
|
|
|
}
|