livestatus

package module
v0.0.0-...-3ac215c Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2016 License: BSD-3-Clause Imports: 11 Imported by: 0

README

go-livestatus

This package implements a MK Livestatus binding for Go.

The source code is available at Github, licensed under the terms of the BSD license.

Usage

package main

import (
	"fmt"
	"os"

	livestatus "github.com/vbatoufflet/go-livestatus"
)

func main() {
	l := livestatus.NewLivestatus("tcp", "192.168.0.10:6557")
	// or l := livestatus.NewLivestatus("unix", "/var/run/nagios/livestatus.sock")

	q := l.Query("hosts")
	q.Columns("name", "state", "last_time_down")
	q.Filter("name ~ ^db[0-9]+\\.")
	// or q := l.Query("hosts").Columns("name", "state", "last_time_down").Filter("name ~ ^db[0-9]+\\.")

	resp, err := q.Exec()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s", err)
		os.Exit(1)
	}

	for _, r := range resp.Records {
		name, err := r.GetString("name")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Warning: %s", err)
		}

		state, err := r.GetInt("state")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Warning: %s", err)
		}

		lastDown, err := r.GetTime("last_time_down")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Warning: %s", err)
		}

		fmt.Printf("Host: %s, State: %d, Last time down: %s\n", name, state, lastDown)
	}
}

Output example:

Host: db1.example.net, State: 0, Last time down: 2015-04-03 06:54:32 +0200 CEST
Host: db2.example.net, State: 0, Last time down: 2015-06-07 12:34:56 +0200 CEST

See GoDoc for further details.

Documentation

Overview

Package livestatus provides a binding to MK Livestatus sockets.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownColumn = errors.New("unknown record column")
	ErrInvalidValue  = errors.New("invalid record value")
)

Record retrieval errors

Functions

This section is empty.

Types

type Command

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

Command is a binding command instance.

func (*Command) Arg

func (c *Command) Arg(v interface{})

func (*Command) Exec

func (c *Command) Exec() (*Response, error)

Exec executes the query.

func (*Command) Op

func (c *Command) Op(op CommandOpFunc)

func (*Command) Raw

func (c *Command) Raw(cmd string)

type CommandOpFunc

type CommandOpFunc func(*Command)

type Livestatus

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

Livestatus is a binding instance.

func NewLivestatus

func NewLivestatus(network, address string) *Livestatus

NewLivestatus creates a new binding instance.

func NewLivestatusWithDialer

func NewLivestatusWithDialer(dialer func() (net.Conn, error)) *Livestatus

NewLivestatusWithDialer creates a new binding that uses the net.Conn returned by the provided dialer function.

func (*Livestatus) Close

func (l *Livestatus) Close() error

Close any open connection from a KeepAlive

func (*Livestatus) Command

func (l *Livestatus) Command() *Command

Command creates a new command instanc.

func (*Livestatus) Query

func (l *Livestatus) Query(table string) *Query

Query creates a new query instance on a spacific table.

type Query

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

Query is a binding query instance.

func (*Query) And

func (q *Query) And(n int) *Query

And combines the n last filters into a new filter using a `And` operation.

func (*Query) Columns

func (q *Query) Columns(names ...string) *Query

Columns sets the names of the columns to retrieve when executing a query.

func (*Query) Exec

func (q *Query) Exec() (*Response, error)

Exec executes the query.

func (*Query) Filter

func (q *Query) Filter(rule string) *Query

Filter sets a new filter to apply to the query.

func (*Query) KeepAlive

func (q *Query) KeepAlive() *Query

KeepAlive keeps the connection open after the query, for re-use

func (*Query) KeepAliveOff

func (q *Query) KeepAliveOff() *Query

KeepAliveOff disables the default keepalive from Command

func (*Query) Limit

func (q *Query) Limit(n int) *Query

Limit the query to n responses.

func (*Query) Negate

func (q *Query) Negate() *Query

Negate negates the most recent filter.

func (*Query) Or

func (q *Query) Or(n int) *Query

Or combines the n last filters into a new filter using a `Or` operation.

func (*Query) WaitCondition

func (q *Query) WaitCondition(rule string) *Query

WaitCondition sets a new wait condition to apply to the query.

func (*Query) WaitConditionAnd

func (q *Query) WaitConditionAnd(n int) *Query

WaitConditionAnd combines the n last wait conditions into a new wait condition using a `And` operation.

func (*Query) WaitConditionNegate

func (q *Query) WaitConditionNegate() *Query

WaitConditionNegate negates the most recent wait condition.

func (*Query) WaitConditionOr

func (q *Query) WaitConditionOr(n int) *Query

WaitConditionOr combines the n last wait condition into a new wait condition using a `Or` operation.

func (*Query) WaitObject

func (q *Query) WaitObject(obj string) *Query

WaitObject sets the object within the queried table to wait on. For the table hosts, hostgroups, servicegroups, contacts and contactgroups this is simply the name of the object. For the table services it is the hostname followed by a space followed by the service description

func (*Query) WaitTimeout

func (q *Query) WaitTimeout(t time.Duration) *Query

WaitTimeout set a timeout for the wait condition.

func (*Query) WaitTrigger

func (q *Query) WaitTrigger(event string) *Query

WaitTrigger sets the nagios event that will trigger a check of the wait condition.

type Record

type Record map[string]interface{}

Record is query response entry.

func (Record) Columns

func (r Record) Columns() []string

Columns returns the names of the columns present in the record.

func (Record) Get

func (r Record) Get(name string) (interface{}, error)

Get returns an interface value for a specific column

Returns an error if the column is unknown.

func (Record) GetBool

func (r Record) GetBool(name string) (bool, error)

GetBool returns a boolean value for a specific column.

Returns an error if the column is unknown or if the value can't be represented as a boolean.

func (Record) GetFloat

func (r Record) GetFloat(name string) (float64, error)

GetFloat returns a float value for a specific column.

Returns an error if the column is unknown or if the value can't be represented as a float.

func (Record) GetInt

func (r Record) GetInt(name string) (int64, error)

GetInt returns an integer value for a specific column.

Returns an error if the column is unknown or if the value can't be represented as an integer.

func (Record) GetSlice

func (r Record) GetSlice(name string) ([]interface{}, error)

GetSlice returns a slice of interfaces for a specific column.

Returns an error if the column is unknown or if the value can't be represented as a slice.

func (Record) GetString

func (r Record) GetString(name string) (string, error)

GetString returns a string value for a specific column.

Returns an error if the column is unknown or if the value can't be represented as a string.

func (Record) GetTime

func (r Record) GetTime(name string) (time.Time, error)

GetTime returns a time struct for a specific column.

Returns an error if the column is unknown or if the value can't be represented as a time struct.

func (Record) Len

func (r Record) Len() int

Len returns the number of columns present in the record.

type Response

type Response struct {
	Status  int
	Records []Record
}

Response is a query response.

func (Response) Len

func (r Response) Len() int

Len returns the number of records present in the response.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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