2021-10-08 13:29:57 +02:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2021-11-25 14:04:03 +01:00
|
|
|
"errors"
|
2021-10-08 13:29:57 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
2021-11-25 14:04:03 +01:00
|
|
|
"strings"
|
2021-11-25 15:11:39 +01:00
|
|
|
"time"
|
2022-01-21 14:35:52 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
|
|
|
influx "github.com/influxdata/line-protocol"
|
2021-10-08 13:29:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const CUSTOMCMDPATH = `/home/unrz139/Work/cc-metric-collector/collectors/custom`
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
type CustomCmdCollectorConfig struct {
|
2022-01-25 11:12:06 +01:00
|
|
|
Commands []string `json:"commands"`
|
|
|
|
Files []string `json:"files"`
|
2021-11-25 15:11:39 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-10-08 13:29:57 +02:00
|
|
|
type CustomCmdCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
|
|
|
handler *influx.MetricHandler
|
|
|
|
parser *influx.Parser
|
2021-11-25 15:11:39 +01:00
|
|
|
config CustomCmdCollectorConfig
|
2021-11-25 14:04:03 +01:00
|
|
|
commands []string
|
2021-11-25 15:11:39 +01:00
|
|
|
files []string
|
2021-10-08 13:29:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CustomCmdCollector) Init(config json.RawMessage) error {
|
2021-11-25 15:11:39 +01:00
|
|
|
var err error
|
2021-10-08 13:29:57 +02:00
|
|
|
m.name = "CustomCmdCollector"
|
2022-06-08 15:25:40 +02:00
|
|
|
m.parallel = true
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "Custom"}
|
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 {
|
|
|
|
log.Print(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-10-08 13:29:57 +02:00
|
|
|
m.setup()
|
2022-01-25 11:12:06 +01:00
|
|
|
for _, c := range m.config.Commands {
|
2021-11-25 15:11:39 +01:00
|
|
|
cmdfields := strings.Fields(c)
|
|
|
|
command := exec.Command(cmdfields[0], strings.Join(cmdfields[1:], " "))
|
|
|
|
command.Wait()
|
|
|
|
_, err = command.Output()
|
|
|
|
if err != nil {
|
|
|
|
m.commands = append(m.commands, c)
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2022-01-25 11:12:06 +01:00
|
|
|
for _, f := range m.config.Files {
|
2021-11-25 15:11:39 +01:00
|
|
|
_, err = ioutil.ReadFile(f)
|
|
|
|
if err == nil {
|
|
|
|
m.files = append(m.files, f)
|
|
|
|
} else {
|
|
|
|
log.Print(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
if len(m.files) == 0 && len(m.commands) == 0 {
|
2022-03-15 16:41:11 +01:00
|
|
|
return errors.New("no metrics to collect")
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
m.handler = influx.NewMetricHandler()
|
|
|
|
m.parser = influx.NewParser(m.handler)
|
2021-10-08 13:29:57 +02:00
|
|
|
m.parser.SetTimeFunc(DefaultTime)
|
|
|
|
m.init = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultTime = func() time.Time {
|
|
|
|
return time.Unix(42, 0)
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CustomCmdCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
for _, cmd := range m.commands {
|
|
|
|
cmdfields := strings.Fields(cmd)
|
|
|
|
command := exec.Command(cmdfields[0], strings.Join(cmdfields[1:], " "))
|
2021-10-08 13:29:57 +02:00
|
|
|
command.Wait()
|
|
|
|
stdout, err := command.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
cmdmetrics, err := m.parser.Parse(stdout)
|
2021-10-08 13:29:57 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
for _, c := range cmdmetrics {
|
|
|
|
_, skip := stringArrayContains(m.config.ExcludeMetrics, c.Name())
|
|
|
|
if skip {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-08 10:58:53 +01:00
|
|
|
|
2022-02-21 14:50:11 +01:00
|
|
|
y := lp.FromInfluxMetric(c)
|
2021-11-25 15:11:39 +01:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
output <- y
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
for _, file := range m.files {
|
2021-11-25 15:11:39 +01:00
|
|
|
buffer, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmetrics, err := m.parser.Parse(buffer)
|
2021-11-25 14:04:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
2021-10-08 13:29:57 +02:00
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
for _, f := range fmetrics {
|
|
|
|
_, skip := stringArrayContains(m.config.ExcludeMetrics, f.Name())
|
|
|
|
if skip {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-21 14:50:11 +01:00
|
|
|
y := lp.FromInfluxMetric(f)
|
2021-11-25 15:11:39 +01:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
output <- y
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-08 13:29:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CustomCmdCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|