Add linker flag -Wl,--unresolved-symbols=ignore-in-object-files to build without library. Remove build tags

This commit is contained in:
Thomas Roehl 2022-02-18 11:41:15 +01:00
parent 4e8ee59211
commit e2f78fe1c0
4 changed files with 43 additions and 81 deletions

View File

@ -1,6 +1,3 @@
//go:build ganglia
// +build ganglia
package sinks package sinks
import ( import (

View File

@ -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() {
}

View File

@ -1,11 +1,8 @@
//go:build ganglia
// +build ganglia
package sinks package sinks
/* /*
#cgo CFLAGS: -DGM_PROTOCOL_GUARD #cgo CFLAGS: -DGM_PROTOCOL_GUARD
#cgo LDFLAGS: -L. -lganglia #cgo LDFLAGS: -L. -lganglia -Wl,--unresolved-symbols=ignore-in-object-files
#include <stdlib.h> #include <stdlib.h>
// This is a copy&paste snippet of ganglia.h (BSD-3 license) // This is a copy&paste snippet of ganglia.h (BSD-3 license)
@ -76,18 +73,31 @@ import (
"unsafe" "unsafe"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" 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 { type LibgangliaSinkConfig struct {
defaultSinkConfig defaultSinkConfig
GmondConfig string `json:"gmond_config,omitempty"` GangliaLib string `json:"libganglia_path,omitempty"`
AddGangliaGroup bool `json:"add_ganglia_group,omitempty"` GmondConfig string `json:"gmond_config,omitempty"`
//AddTagsAsDesc bool `json:"add_tags_as_desc,omitempty"` AddGangliaGroup bool `json:"add_ganglia_group,omitempty"`
AddTypeToName bool `json:"add_type_to_name,omitempty"` AddTypeToName bool `json:"add_type_to_name,omitempty"`
AddUnits bool `json:"add_units,omitempty"` AddUnits bool `json:"add_units,omitempty"`
ClusterName string `json:"cluster_name,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 { type LibgangliaSink struct {
@ -124,6 +134,7 @@ func (s *LibgangliaSink) Init(config json.RawMessage) error {
s.config.AddTypeToName = false s.config.AddTypeToName = false
s.config.AddUnits = true s.config.AddUnits = true
s.config.GmondConfig = string(GMOND_CONFIG_FILE) s.config.GmondConfig = string(GMOND_CONFIG_FILE)
s.config.GangliaLib = string(GANGLIA_LIB_NAME)
if len(config) > 0 { if len(config) > 0 {
err = json.Unmarshal(config, &s.config) err = json.Unmarshal(config, &s.config)
if err != nil { if err != nil {
@ -131,6 +142,10 @@ func (s *LibgangliaSink) Init(config json.RawMessage) error {
return err 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 // Set up cache for the C strings
s.cstrCache = make(map[string]*C.char) s.cstrCache = make(map[string]*C.char)
@ -182,16 +197,17 @@ func (s *LibgangliaSink) Write(point lp.CCMetric) error {
} }
// Get metric name // Get metric name
metricname := point.Name()
if s.config.AddTypeToName { if s.config.AddTypeToName {
c_name = lookup(gangliaMetricName(point)) c_name = lookup(gangliaMetricName(point))
} else { } else {
c_name = lookup(point.Name()) c_name = lookup(metricname)
} }
// Get the value C string and lookup the type string in the cache // Get the value C string and lookup the type string in the cache
value, ok := point.GetField("value") value, ok := point.GetField("value")
if !ok { 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) { switch real := value.(type) {
case float64: case float64:
@ -229,14 +245,25 @@ func (s *LibgangliaSink) Write(point lp.CCMetric) error {
c_unit = lookup("") 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 // Create a new Ganglia metric
gmetric := C.Ganglia_metric_create(s.global_context) gmetric := C.Ganglia_metric_create(s.global_context)
rval := C.int(0)
// Set name, value, type and unit in the Ganglia metric // Set name, value, type and unit in the Ganglia metric
// Since we don't have this information from the collectors, // Since we don't have this information from the collectors,
// we assume that the metric value can go up and down (slope), // we assume that the metric value can go up and down (slope),
// and their is no maximum for 'dmax' and 'tmax' // and there 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) // 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 { switch rval {
case 1: case 1:
C.free(unsafe.Pointer(c_value)) C.free(unsafe.Pointer(c_value))

View File

@ -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() {
}