2022-01-30 15:21:24 +01:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
2022-02-04 18:12:24 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-01-30 15:21:24 +01:00
|
|
|
"fmt"
|
2022-01-30 15:25:57 +01:00
|
|
|
"log"
|
2022-01-30 15:21:24 +01:00
|
|
|
"strings"
|
2022-01-30 15:25:57 +01:00
|
|
|
|
2022-01-30 15:21:24 +01:00
|
|
|
// "time"
|
|
|
|
"os/exec"
|
2022-01-30 15:25:57 +01:00
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
2022-01-30 15:25:57 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2022-01-30 15:21:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const GMETRIC_EXEC = `gmetric`
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
type GangliaSinkConfig struct {
|
|
|
|
defaultSinkConfig
|
|
|
|
GmetricPath string `json:"gmetric_path"`
|
|
|
|
AddGangliaGroup bool `json:"add_ganglia_group"`
|
|
|
|
}
|
|
|
|
|
2022-01-30 15:21:24 +01:00
|
|
|
type GangliaSink struct {
|
2022-02-04 18:12:24 +01:00
|
|
|
sink
|
2022-01-30 15:21:24 +01:00
|
|
|
gmetric_path string
|
2022-02-04 18:12:24 +01:00
|
|
|
config GangliaSinkConfig
|
2022-01-30 15:21:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
func (s *GangliaSink) Init(config json.RawMessage) error {
|
|
|
|
var err error = nil
|
|
|
|
s.name = "GangliaSink"
|
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &s.config)
|
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(s.name, "Error reading config for", s.name, ":", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.gmetric_path = ""
|
|
|
|
if len(s.config.GmetricPath) > 0 {
|
|
|
|
p, err := exec.LookPath(s.config.GmetricPath)
|
|
|
|
if err == nil {
|
|
|
|
s.gmetric_path = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.gmetric_path) == 0 {
|
|
|
|
p, err := exec.LookPath(string(GMETRIC_EXEC))
|
|
|
|
if err == nil {
|
|
|
|
s.gmetric_path = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.gmetric_path) == 0 {
|
|
|
|
err = errors.New("cannot find executable 'gmetric'")
|
2022-01-30 15:25:57 +01:00
|
|
|
}
|
2022-01-30 15:21:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-30 15:25:57 +01:00
|
|
|
func (s *GangliaSink) Write(point lp.CCMetric) error {
|
|
|
|
var err error = nil
|
|
|
|
var tagsstr []string
|
|
|
|
var argstr []string
|
2022-02-01 14:54:34 +01:00
|
|
|
for key, value := range point.Tags() {
|
|
|
|
switch key {
|
2022-01-30 15:25:57 +01:00
|
|
|
case "cluster":
|
2022-02-01 14:54:34 +01:00
|
|
|
argstr = append(argstr, fmt.Sprintf("--cluster=%s", value))
|
2022-01-30 15:25:57 +01:00
|
|
|
case "unit":
|
2022-02-01 14:54:34 +01:00
|
|
|
argstr = append(argstr, fmt.Sprintf("--units=%s", value))
|
2022-01-30 15:25:57 +01:00
|
|
|
case "group":
|
2022-02-04 18:12:24 +01:00
|
|
|
if s.config.AddGangliaGroup {
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--group=%s", value))
|
|
|
|
}
|
2022-01-30 15:25:57 +01:00
|
|
|
default:
|
2022-02-01 14:54:34 +01:00
|
|
|
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value))
|
2022-01-30 15:25:57 +01:00
|
|
|
}
|
2022-01-30 15:21:24 +01:00
|
|
|
}
|
2022-02-04 18:12:24 +01:00
|
|
|
if s.config.MetaAsTags {
|
|
|
|
for key, value := range point.Meta() {
|
|
|
|
switch key {
|
|
|
|
case "cluster":
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--cluster=%s", value))
|
|
|
|
case "unit":
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--units=%s", value))
|
|
|
|
case "group":
|
|
|
|
if s.config.AddGangliaGroup {
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--group=%s", value))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-30 15:21:24 +01:00
|
|
|
if len(tagsstr) > 0 {
|
2022-01-30 15:25:57 +01:00
|
|
|
argstr = append(argstr, fmt.Sprintf("--desc=%q", strings.Join(tagsstr, ",")))
|
|
|
|
}
|
2022-01-30 15:21:24 +01:00
|
|
|
argstr = append(argstr, fmt.Sprintf("--name=%s", point.Name()))
|
|
|
|
for _, f := range point.FieldList() {
|
2022-01-30 15:25:57 +01:00
|
|
|
if f.Key == "value" {
|
|
|
|
switch f.Value.(type) {
|
|
|
|
case float64:
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--value=%v", f.Value.(float64)))
|
|
|
|
argstr = append(argstr, "--type=double")
|
|
|
|
case float32:
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--value=%v", f.Value.(float32)))
|
|
|
|
argstr = append(argstr, "--type=float")
|
|
|
|
case int:
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--value=%d", f.Value.(int)))
|
|
|
|
argstr = append(argstr, "--type=int32")
|
|
|
|
case int64:
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--value=%d", f.Value.(int64)))
|
|
|
|
argstr = append(argstr, "--type=int32")
|
|
|
|
case string:
|
|
|
|
argstr = append(argstr, fmt.Sprintf("--value=%q", f.Value.(string)))
|
|
|
|
argstr = append(argstr, "--type=string")
|
|
|
|
}
|
|
|
|
}
|
2022-01-30 15:21:24 +01:00
|
|
|
}
|
|
|
|
log.Print(s.gmetric_path, " ", strings.Join(argstr, " "))
|
2022-01-30 15:25:57 +01:00
|
|
|
// command := exec.Command(string(GMETRIC_EXEC), strings.Join(argstr, " "))
|
|
|
|
// command.Wait()
|
|
|
|
// _, err := command.Output()
|
2022-01-30 15:21:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GangliaSink) Flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GangliaSink) Close() {
|
|
|
|
}
|