gocollectd

package module
v0.0.0-...-8f48e45 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2013 License: MIT Imports: 8 Imported by: 2

README

gocollectd

This is an implementation of the Collectd Binary Protocol in Go.

Installation

Install with go get:

go get github.com/paulhammond/gocollectd

You probably want to import gocollectd into your code with a different name:

import (
  collectd "github.com/paulhammond/gocollectd"
)
// now you can call collectd.Parse() etc

Usage

In short, gocollectd will parse a binary stream of data and return values. However there are some details.

Some collectd plugins send multiple values for the same timestamp at once. For example, the interface plugin sends both tx and rx in one packet. Some applications will want to mirror the behavior of collectd's RRD plugin and store both values in one file. To allow that, parsing data returns Packets instead of Values.

b := []byte{…}
packets := collectd.Parse(b)

packet = packets[0]
fmt.Println(packet.Hostname)       // "laptop.lan"
fmt.Println(packet.Time)           // A go time value
fmt.Println(packet.Plugin)         // "Load"
fmt.Println(packet.ValueCount)     // 3
fmt.Println(packet.ValueNames())   // { "load1", "load5", "load15" }

Collectd values are sent as one of the RRD types: Counter, Gauge, Derive or Absolute. This, in turn, means that they are sent as an int64, uint64 or float64. You have a few options on how to handle this:

// get the bytes
fmt.Println(packet.ValueBytes())   // [][]byte{ … }

// or use the Number interface type
numbers := packet.ValueNumbers()
fmt.Println(numbers) // { 1.13, 0.89, 0.60 }

// if you don't care about exact precision because you're about to average
// lots of numbers, you can convert everything to a float:
f := numbers[0].Float64()  // f == float64(1.13)

// or you can use go type assertions
if i, ok := numbers[0].(int64); ok {
   // do something with i, knowing it's an int64
}

// this is all also available using Values()
value := packet.Values()[0]
fmt.Println(value.Number) // 1.13
fmt.Println(value.Bytes)  // []byte{ … }

The most common use case is to read collectd data directly from the network. A basic server implementation is provided that sends received packets on a channel:

c := make(chan collectd.Packet)
go collectd.Listen("127.0.0.1:25827", c)
for {
  packet := <-c
  // do something with the packet
}

An example is of using this server is provided in gocollectd-example

Known issues

Signed and encrypted collectd packets are not currently supported.

References

License

Copyright (c) 2013 Paul Hammond. gocollectd is available under the MIT license, see LICENSE.txt for details

Documentation

Overview

gocollectd parses the collectd binary protocol.

Index

Constants

View Source
const (
	TypeCounter  = 0
	TypeGauge    = 1
	TypeDerive   = 2
	TypeAbsolute = 3
)

Variables

View Source
var ErrorInvalid = errors.New("Invalid collectd packet recieved")

The error returned if an invalid packet is recieved

View Source
var ErrorUnsupported = errors.New("Unsupported collectd packet recieved")

The error returned if a valid but unsupported packet is recieved

Functions

func Listen

func Listen(addr string, c chan Packet)

Listen creates a UDP server that parses collectd data into packets and sends them over a channel.

func Parse

func Parse(b []byte) (*[]Packet, error)

Parse parses some bytes into packets.

Types

type Absolute

type Absolute uint64

A collectd Absolute value

func (Absolute) CollectdType

func (v Absolute) CollectdType() uint8

CollectdType returns TypeAbsolute

func (Absolute) Float64

func (v Absolute) Float64() float64

Float64 converts this number to a float to avoid type assertions.

type Counter

type Counter uint64

A collectd Counter value

func (Counter) CollectdType

func (v Counter) CollectdType() uint8

CollectdType returns TypeCounter

func (Counter) Float64

func (v Counter) Float64() float64

Float64 converts this number to a float to avoid type assertions.

type Derive

type Derive int64

A collectd Derive value

func (Derive) CollectdType

func (v Derive) CollectdType() uint8

CollectdType returns TypeDerive

func (Derive) Float64

func (v Derive) Float64() float64

Float64 converts this number to a float to avoid type assertions.

type Gauge

type Gauge float64

A collectd Gauge value

func (Gauge) CollectdType

func (v Gauge) CollectdType() uint8

CollectdType returns TypeGauge

func (Gauge) Float64

func (v Gauge) Float64() float64

Float64 converts this number to a float to avoid type assertions.

type Number

type Number interface {
	// CollectdType gives the Collectd Type of this number.
	CollectdType() uint8

	// Float64 converts this number to a float to avoid type assertions.
	Float64() float64
}

A collectd value is sent as a int64, uint64, or float64. Number provides some useful functions to help handle this flexibility.

type Packet

type Packet struct {
	Hostname       string
	Plugin         string
	PluginInstance string
	Type           string
	TypeInstance   string
	CdTime         uint64
	CdInterval     uint64
	DataTypes      []uint8
	Bytes          []byte
}

A packet is a set of collectd values that were sent at once by a collectd plugin.

func (Packet) Name

func (p Packet) Name() (name string)

ValueNames attempts to reformat collectd's plugin/type/instance heirarchy into a string for this packet.

func (Packet) Time

func (p Packet) Time() time.Time

Time returns the measurement time as a go time.

func (Packet) TimeUnix

func (p Packet) TimeUnix() int64

TimeUnix returns the measurement time in seconds since unix epoch.

func (Packet) TimeUnixNano

func (p Packet) TimeUnixNano() int64

TimeUnixNano returns the measurement time in nanoseconds since unix epoch.

func (Packet) ValueBytes

func (p Packet) ValueBytes() [][]byte

ValueBytes returns the raw bytes for each value.

func (Packet) ValueCount

func (p Packet) ValueCount() int

ValueCount returns the number of values in this packet.

func (Packet) ValueNames

func (p Packet) ValueNames() []string

ValueNames attempts to reformat collectd's plugin/type/instance heirarchy into a strings for each value in this packet.

func (Packet) ValueNumbers

func (p Packet) ValueNumbers() ([]Number, error)

ValueNumbers returns the values as Numbers

func (Packet) Values

func (p Packet) Values() []Value

Values returns the data in this packet as a []Value.

type Value

type Value struct {
	// contains filtered or unexported fields
}

A Value is what a packet contains.

func (Value) Bytes

func (v Value) Bytes() []byte

This value's raw bytes.

func (Value) Number

func (v Value) Number() (Number, error)

Number converts this value into a Number.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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