mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2026-02-13 14:41:45 +01:00
* Add golangci-lin as make target * Fix: could omit type ... from declaration; it will be inferred from the right-hand side (staticcheck) * Fix func intArrayContains is unused (unused) * Fix: could use strings.ReplaceAll instead (staticcheck) * Fix: could expand call to math.Pow (staticcheck) * Fix: could use tagged switch on `...` (staticcheck) * Fix: Error return value of `...` is not checked (errcheck) * Fix: ineffectual assignment to err (ineffassign) * Fix: There is no need to wait for command completion * Add cpustat, diskstat and schedstat config * Use slices to exclude metrics * Replaced stringArrayContains by slices.Contains * Replace m[k]=v loop with maps.Copy * Use module slices from the standard library. Remove use of golang.org/x/exp/slices * Use SplitSeq and max to modernize code
207 lines
4.4 KiB
Go
207 lines
4.4 KiB
Go
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
|
// All rights reserved. This file is part of cc-lib.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
// additional authors:
|
|
// Holger Obermaier (NHR@KIT)
|
|
|
|
package collectors
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"slices"
|
|
|
|
// "os"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
|
|
lp "github.com/ClusterCockpit/cc-lib/v2/ccMessage"
|
|
)
|
|
|
|
// First part contains the code for the general NfsCollector.
|
|
// Later, the general NfsCollector is more limited to Nfs3- and Nfs4Collector.
|
|
|
|
const NFSSTAT_EXEC = `nfsstat`
|
|
|
|
type NfsCollectorData struct {
|
|
current int64
|
|
last int64
|
|
}
|
|
|
|
type nfsCollector struct {
|
|
metricCollector
|
|
tags map[string]string
|
|
version string
|
|
config struct {
|
|
Nfsstats string `json:"nfsstat"`
|
|
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
|
}
|
|
data map[string]NfsCollectorData
|
|
}
|
|
|
|
func (m *nfsCollector) initStats() error {
|
|
cmd := exec.Command(m.config.Nfsstats, `-l`, `--all`)
|
|
|
|
// Wait for cmd end
|
|
if err := cmd.Wait(); err != nil {
|
|
return fmt.Errorf("initStats(): %w", err)
|
|
}
|
|
|
|
buffer, err := cmd.Output()
|
|
if err == nil {
|
|
for line := range strings.Lines(string(buffer)) {
|
|
lf := strings.Fields(line)
|
|
if len(lf) != 5 {
|
|
continue
|
|
}
|
|
if lf[1] == m.version {
|
|
name := strings.Trim(lf[3], ":")
|
|
if _, exist := m.data[name]; !exist {
|
|
value, err := strconv.ParseInt(lf[4], 0, 64)
|
|
if err == nil {
|
|
x := m.data[name]
|
|
x.current = value
|
|
x.last = value
|
|
m.data[name] = x
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (m *nfsCollector) updateStats() error {
|
|
cmd := exec.Command(m.config.Nfsstats, `-l`, `--all`)
|
|
|
|
// Wait for cmd end
|
|
if err := cmd.Wait(); err != nil {
|
|
return fmt.Errorf("updateStats(): %w", err)
|
|
}
|
|
|
|
buffer, err := cmd.Output()
|
|
if err == nil {
|
|
for line := range strings.Lines(string(buffer)) {
|
|
lf := strings.Fields(line)
|
|
if len(lf) != 5 {
|
|
continue
|
|
}
|
|
if lf[1] == m.version {
|
|
name := strings.Trim(lf[3], ":")
|
|
if _, exist := m.data[name]; exist {
|
|
value, err := strconv.ParseInt(lf[4], 0, 64)
|
|
if err == nil {
|
|
x := m.data[name]
|
|
x.last = x.current
|
|
x.current = value
|
|
m.data[name] = x
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (m *nfsCollector) MainInit(config json.RawMessage) error {
|
|
m.config.Nfsstats = string(NFSSTAT_EXEC)
|
|
// Read JSON configuration
|
|
if len(config) > 0 {
|
|
err := json.Unmarshal(config, &m.config)
|
|
if err != nil {
|
|
log.Print(err.Error())
|
|
return err
|
|
}
|
|
}
|
|
m.meta = map[string]string{
|
|
"source": m.name,
|
|
"group": "NFS",
|
|
}
|
|
m.tags = map[string]string{
|
|
"type": "node",
|
|
}
|
|
// Check if nfsstat is in executable search path
|
|
_, err := exec.LookPath(m.config.Nfsstats)
|
|
if err != nil {
|
|
return fmt.Errorf("NfsCollector.Init(): Failed to find nfsstat binary '%s': %v", m.config.Nfsstats, err)
|
|
}
|
|
m.data = make(map[string]NfsCollectorData)
|
|
if err := m.initStats(); err != nil {
|
|
return fmt.Errorf("NfsCollector.Init(): %w", err)
|
|
}
|
|
m.init = true
|
|
m.parallel = true
|
|
return nil
|
|
}
|
|
|
|
func (m *nfsCollector) Read(interval time.Duration, output chan lp.CCMessage) {
|
|
if !m.init {
|
|
return
|
|
}
|
|
timestamp := time.Now()
|
|
|
|
if err := m.updateStats(); err != nil {
|
|
cclog.ComponentError(
|
|
m.name,
|
|
fmt.Sprintf("Read(): updateStats() failed: %v", err),
|
|
)
|
|
return
|
|
}
|
|
prefix := ""
|
|
switch m.version {
|
|
case "v3":
|
|
prefix = "nfs3"
|
|
case "v4":
|
|
prefix = "nfs4"
|
|
default:
|
|
prefix = "nfs"
|
|
}
|
|
|
|
for name, data := range m.data {
|
|
if slices.Contains(m.config.ExcludeMetrics, name) {
|
|
continue
|
|
}
|
|
value := data.current - data.last
|
|
y, err := lp.NewMessage(fmt.Sprintf("%s_%s", prefix, name), m.tags, m.meta, map[string]interface{}{"value": value}, timestamp)
|
|
if err == nil {
|
|
y.AddMeta("version", m.version)
|
|
output <- y
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *nfsCollector) Close() {
|
|
m.init = false
|
|
}
|
|
|
|
type Nfs3Collector struct {
|
|
nfsCollector
|
|
}
|
|
|
|
type Nfs4Collector struct {
|
|
nfsCollector
|
|
}
|
|
|
|
func (m *Nfs3Collector) Init(config json.RawMessage) error {
|
|
m.name = "Nfs3Collector"
|
|
m.version = `v3`
|
|
if err := m.setup(); err != nil {
|
|
return fmt.Errorf("%s Init(): setup() call failed: %w", m.name, err)
|
|
}
|
|
return m.MainInit(config)
|
|
}
|
|
|
|
func (m *Nfs4Collector) Init(config json.RawMessage) error {
|
|
m.name = "Nfs4Collector"
|
|
m.version = `v4`
|
|
if err := m.setup(); err != nil {
|
|
return fmt.Errorf("%s Init(): setup() call failed: %w", m.name, err)
|
|
}
|
|
return m.MainInit(config)
|
|
}
|