Version: v0.5.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2020 License: ISC Imports: 8 Imported by: 2



Package exec implements tools to write plugins for collectd's "exec plugin" in Go.

e := NewExecutor()

// simple "value" callback
answer := func() api.Value {
	return api.Gauge(42)
e.ValueCallback(answer, &api.ValueList{
	Identifier: api.Identifier{
		Host:         "example.com",
		Plugin:       "golang",
		Type:         "answer",
		TypeInstance: "live_universe_and_everything",
	Interval: time.Second,

// "complex" void callback
bicycles := func(ctx context.Context, interval time.Duration) {
	vl := &api.ValueList{
		Identifier: api.Identifier{
			Host:   "example.com",
			Plugin: "golang",
			Type:   "bicycles",
		Interval: interval,
		Time:     time.Now(),
		Values:   make([]api.Value, 1),

	data := []struct {
		TypeInstance string
		Value        api.Gauge
		{"beijing", api.Gauge(9000000)},
	for _, d := range data {
		vl.Values[0] = d.Value
		vl.Identifier.TypeInstance = d.TypeInstance
		Putval.Write(ctx, vl)
e.VoidCallback(bicycles, time.Second)

// blocks forever




This section is empty.


Putval is the dispatcher used by the exec package to print ValueLists.


func Hostname

func Hostname() string

Hostname determines the hostname to use from the "COLLECTD_HOSTNAME" environment variable and falls back to os.Hostname() if it is unset. If that also fails an empty string is returned.

func Interval

func Interval() time.Duration

Interval determines the default interval from the "COLLECTD_INTERVAL" environment variable. It falls back to 10s if the environment variable is unset or cannot be parsed.


type Executor

type Executor struct {
	// contains filtered or unexported fields

Executor holds one or more callbacks which are called periodically.

func NewExecutor

func NewExecutor() *Executor

NewExecutor returns a pointer to a new Executor object.

func (*Executor) Run

func (e *Executor) Run(ctx context.Context)

Run starts calling all callbacks periodically and blocks.

func (*Executor) Stop

func (e *Executor) Stop()

Stop sends a signal to all callbacks to exit and returns. This unblocks "Run()" but does not block itself.

func (*Executor) ValueCallback

func (e *Executor) ValueCallback(callback func() api.Value, vl *api.ValueList)

ValueCallback adds a simple "value" callback to the Executor. The callback only returns a Number, i.e. either a api.Gauge or api.Derive, and formatting and printing is done by the executor.

func (*Executor) VoidCallback

func (e *Executor) VoidCallback(callback func(context.Context, time.Duration), interval time.Duration)

VoidCallback adds a "complex" callback to the Executor. While the functions prototype is simpler, all the work has to be done by the callback, i.e. the callback needs to format and print the appropriate lines to "STDOUT". However, this allows cases in which the number of values reported varies, e.g. depending on the system the code is running on.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL