Skip to content

iguanesolutions/go-livestatus

 
 

Repository files navigation

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

Packages

No packages published

Languages

  • Go 100.0%