livestatus

package module
v0.0.0-...-65182dd Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: BSD-3-Clause Imports: 11 Imported by: 13

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() {
	c := livestatus.NewClient("tcp", "localhost:6557")
	// or c := livestatus.NewClient("unix", "/var/run/nagios/livestatus.sock")
	defer c.Close()

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

	resp, err := c.Exec(q)
	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)
		}

		lastTimeDown, 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, lastTimeDown)
	}
}

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidQuery represents an invalid query error.
	ErrInvalidQuery = errors.New("invalid query")
	// ErrInvalidType represents an invalid type error.
	ErrInvalidType = errors.New("invalid type")
	// ErrUnknownColumn represents an unknown column error.
	ErrUnknownColumn = errors.New("unknown column")
)

Functions

This section is empty.

Types

type Client

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

Client represents a Livestatus client instance.

func NewClient

func NewClient(network, address string) *Client

NewClient creates a new Livestatus client instance.

func NewClientWithDialer

func NewClientWithDialer(network, address string, dialer *net.Dialer) *Client

NewClientWithDialer creates a new Livestatus client instance using a provided network dialer.

func (*Client) Close

func (c *Client) Close()

Close closes any remaining connection.

func (*Client) Exec

func (c *Client) Exec(r Request) (*Response, error)

Exec executes a given Livestatus query.

type Command

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

Command represents a Livestatus command instance.

func NewCommand

func NewCommand(name string, args ...string) *Command

NewCommand creates a new Livestatus command instance.

func (*Command) Arg

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

Arg appends a new argument to the command.

func (Command) String

func (c Command) String() string

String returns a string representation of the Livestatus command.

func (*Command) WriteTimeout

func (c *Command) WriteTimeout(timeout time.Duration) *Command

WriteTimeout sets the connection timeout for write operations. A value of 0 disables the timeout.

type ParseError

type ParseError struct {
	Message    string
	FailedData []byte
	Buffer     []byte
}

ParseError embeded an error with some states to help debugging

func (ParseError) Error

func (pe ParseError) Error() string

type Query

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

Query represents a Livestatus query instance.

func NewQuery

func NewQuery(table string) *Query

NewQuery creates a new Livestatus 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 selects which columns to retrieve.

func (*Query) Filter

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

Filter appends a new filter to the query.

func (*Query) KeepAlive

func (q *Query) KeepAlive() *Query

KeepAlive keeps the connection open to reuse for additional requests.

func (*Query) Limit

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

Limit sets the limit of datasets to retrieve.

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) ReadTimeout

func (q *Query) ReadTimeout(timeout time.Duration) *Query

ReadTimeout sets the connection timeout for read operations. Be careful when using a read timeout in conjunction with wait conditions. A value of 0 disables the timeout.

func (Query) String

func (q Query) String() string

String returns a string representation of the Livestatus query.

func (*Query) WaitCondition

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

WaitCondition appends a new wait condition 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 conditions into a new wait condition using a `Or` operation.

func (*Query) WaitObject

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

WaitObject specifies an object from the table to wait on.

For `hosts`, `hostgroups`, `servicegroups`, `contacts` and `contactgroups` tables this is simply the name of the object. For the `services` table it is the `hostname` and the service `description` separated by a space.

func (*Query) WaitTimeout

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

WaitTimeout sets the upper limit on the time to wait before executing the query.

func (*Query) WaitTrigger

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

WaitTrigger appends a new wait trigger to the query, waiting for a specific event broker message to recheck condition.

func (*Query) WriteTimeout

func (q *Query) WriteTimeout(timeout time.Duration) *Query

WriteTimeout sets the connection timeout for write operations. A value of 0 disables the timeout.

type Record

type Record map[string]interface{}

Record represents a Livestatus response entry.

func (Record) Columns

func (r Record) Columns() []string

Columns returns the list of the record columns.

func (Record) Get

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

Get returns an interface value for a specific column.

func (Record) GetBool

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

GetBool returns a boolean value for a specific column.

func (Record) GetFloat

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

GetFloat returns a float value for a specific column.

func (Record) GetInt

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

GetInt returns an integer value for a specific column.

func (Record) GetSlice

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

GetSlice returns a slice of interface value for a specific column.

func (Record) GetString

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

GetString returns a string value for a specific column.

func (Record) GetTime

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

GetTime returns a time struct value for a specific column.

func (Record) Len

func (r Record) Len() int

Len returns the number of columns present in the record.

type Request

type Request interface {
	String() string
	// contains filtered or unexported methods
}

Request represents Livestatus request interface.

type Response

type Response struct {
	Status  int
	Message string
	Records []Record
}

Response represents a Livestatus 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