From e2f78fe1c0325ed64c3e0c0dc5592479b1ace2eb Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Fri, 18 Feb 2022 11:41:15 +0100 Subject: [PATCH] Add linker flag -Wl,--unresolved-symbols=ignore-in-object-files to build without library. Remove build tags --- sinks/gangliaSink.go | 3 -- sinks/gangliaSink_disabled.go | 32 ----------------- sinks/libgangliaSink.go | 59 +++++++++++++++++++++++--------- sinks/libgangliaSink_disabled.go | 30 ---------------- 4 files changed, 43 insertions(+), 81 deletions(-) delete mode 100644 sinks/gangliaSink_disabled.go delete mode 100644 sinks/libgangliaSink_disabled.go diff --git a/sinks/gangliaSink.go b/sinks/gangliaSink.go index 2675d5c..c53b11a 100644 --- a/sinks/gangliaSink.go +++ b/sinks/gangliaSink.go @@ -1,6 +1,3 @@ -//go:build ganglia -// +build ganglia - package sinks import ( diff --git a/sinks/gangliaSink_disabled.go b/sinks/gangliaSink_disabled.go deleted file mode 100644 index 836e8fb..0000000 --- a/sinks/gangliaSink_disabled.go +++ /dev/null @@ -1,32 +0,0 @@ -//go:build !ganglia -// +build !ganglia - -package sinks - -import ( - "encoding/json" - "errors" - - // "time" - - lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" -) - -type GangliaSink struct { - sink -} - -func (s *GangliaSink) Init(config json.RawMessage) error { - return errors.New("sink 'ganglia' not implemented, rebuild with tag 'ganglia'") -} - -func (s *GangliaSink) Write(point lp.CCMetric) error { - return errors.New("sink 'ganglia' not implemented, rebuild with tag 'ganglia'") -} - -func (s *GangliaSink) Flush() error { - return errors.New("sink 'ganglia' not implemented, rebuild with tag 'ganglia'") -} - -func (s *GangliaSink) Close() { -} diff --git a/sinks/libgangliaSink.go b/sinks/libgangliaSink.go index 085f10c..f6baef9 100644 --- a/sinks/libgangliaSink.go +++ b/sinks/libgangliaSink.go @@ -1,11 +1,8 @@ -//go:build ganglia -// +build ganglia - package sinks /* #cgo CFLAGS: -DGM_PROTOCOL_GUARD -#cgo LDFLAGS: -L. -lganglia +#cgo LDFLAGS: -L. -lganglia -Wl,--unresolved-symbols=ignore-in-object-files #include // This is a copy&paste snippet of ganglia.h (BSD-3 license) @@ -76,18 +73,31 @@ import ( "unsafe" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" + "github.com/NVIDIA/go-nvml/pkg/dl" ) -const GMOND_CONFIG_FILE = `/etc/ganglia/gmond.conf` +const ( + GANGLIA_LIB_NAME = "libganglia.so" + GANGLIA_LIB_DL_FLAGS = dl.RTLD_LAZY | dl.RTLD_GLOBAL + GMOND_CONFIG_FILE = `/etc/ganglia/gmond.conf` +) + +type LibgangliaSinkSpecialMetric struct { + MetricName string `json:"metric_name,omitempty"` + NewName string `json:"new_name,omitempty"` + Slope string `json:"slope,omitempty"` +} type LibgangliaSinkConfig struct { defaultSinkConfig - GmondConfig string `json:"gmond_config,omitempty"` - AddGangliaGroup bool `json:"add_ganglia_group,omitempty"` - //AddTagsAsDesc bool `json:"add_tags_as_desc,omitempty"` - AddTypeToName bool `json:"add_type_to_name,omitempty"` - AddUnits bool `json:"add_units,omitempty"` - ClusterName string `json:"cluster_name,omitempty"` + GangliaLib string `json:"libganglia_path,omitempty"` + GmondConfig string `json:"gmond_config,omitempty"` + AddGangliaGroup bool `json:"add_ganglia_group,omitempty"` + AddTypeToName bool `json:"add_type_to_name,omitempty"` + AddUnits bool `json:"add_units,omitempty"` + ClusterName string `json:"cluster_name,omitempty"` + SpecialMetrics map[string]LibgangliaSinkSpecialMetric `json:"rename_metrics,omitempty"` // Map to rename metric name from key to value + //AddTagsAsDesc bool `json:"add_tags_as_desc,omitempty"` } type LibgangliaSink struct { @@ -124,6 +134,7 @@ func (s *LibgangliaSink) Init(config json.RawMessage) error { s.config.AddTypeToName = false s.config.AddUnits = true s.config.GmondConfig = string(GMOND_CONFIG_FILE) + s.config.GangliaLib = string(GANGLIA_LIB_NAME) if len(config) > 0 { err = json.Unmarshal(config, &s.config) if err != nil { @@ -131,6 +142,10 @@ func (s *LibgangliaSink) Init(config json.RawMessage) error { return err } } + lib := dl.New(s.config.GangliaLib, GANGLIA_LIB_DL_FLAGS) + if lib == nil { + return fmt.Errorf("error instantiating DynamicLibrary for %s", s.config.GangliaLib) + } // Set up cache for the C strings s.cstrCache = make(map[string]*C.char) @@ -182,16 +197,17 @@ func (s *LibgangliaSink) Write(point lp.CCMetric) error { } // Get metric name + metricname := point.Name() if s.config.AddTypeToName { c_name = lookup(gangliaMetricName(point)) } else { - c_name = lookup(point.Name()) + c_name = lookup(metricname) } // Get the value C string and lookup the type string in the cache value, ok := point.GetField("value") if !ok { - return fmt.Errorf("metric %s has no 'value' field", point.Name()) + return fmt.Errorf("metric %s has no 'value' field", metricname) } switch real := value.(type) { case float64: @@ -229,14 +245,25 @@ func (s *LibgangliaSink) Write(point lp.CCMetric) error { c_unit = lookup("") } + // Determine the slope of the metric. Ganglia's own collector mostly use + // 'both' but the mem and swap total uses 'zero'. + slope_type := C.GANGLIA_SLOPE_BOTH + switch metricname { + case "mem_total": + slope_type = C.GANGLIA_SLOPE_ZERO + case "swap_total": + slope_type = C.GANGLIA_SLOPE_ZERO + } + // Create a new Ganglia metric gmetric := C.Ganglia_metric_create(s.global_context) - rval := C.int(0) // Set name, value, type and unit in the Ganglia metric // Since we don't have this information from the collectors, // we assume that the metric value can go up and down (slope), - // and their is no maximum for 'dmax' and 'tmax' - rval = C.Ganglia_metric_set(gmetric, c_name, c_value, c_type, c_unit, C.GANGLIA_SLOPE_BOTH, 0, 0) + // and there is no maximum for 'dmax' and 'tmax'. + // Ganglia's collectors set 'tmax' but not 'dmax' + rval := C.int(0) + rval = C.Ganglia_metric_set(gmetric, c_name, c_value, c_type, c_unit, C.uint(slope_type), 0, 0) switch rval { case 1: C.free(unsafe.Pointer(c_value)) diff --git a/sinks/libgangliaSink_disabled.go b/sinks/libgangliaSink_disabled.go deleted file mode 100644 index 01886b9..0000000 --- a/sinks/libgangliaSink_disabled.go +++ /dev/null @@ -1,30 +0,0 @@ -//go:build !ganglia -// +build !ganglia - -package sinks - -import ( - "encoding/json" - "errors" - - lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" -) - -type LibgangliaSink struct { - sink -} - -func (s *LibgangliaSink) Init(config json.RawMessage) error { - return errors.New("sink 'libganglia' not implemented, rebuild with tag 'ganglia'") -} - -func (s *LibgangliaSink) Write(point lp.CCMetric) error { - return errors.New("sink 'libganglia' not implemented, rebuild with tag 'ganglia'") -} - -func (s *LibgangliaSink) Flush() error { - return errors.New("sink 'ganglia' not implemented, rebuild with tag 'ganglia'") -} - -func (s *LibgangliaSink) Close() { -}