项目作者: vbatoufflet

项目描述 :
MK Livestatus binding for Go
高级语言: Go
项目地址: git://github.com/vbatoufflet/go-livestatus.git
创建时间: 2015-08-15T10:40:03Z
项目社区:https://github.com/vbatoufflet/go-livestatus

开源协议:BSD 3-Clause "New" or "Revised" License

下载


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

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. livestatus "github.com/vbatoufflet/go-livestatus"
  6. )
  7. func main() {
  8. c := livestatus.NewClient("tcp", "localhost:6557")
  9. // or c := livestatus.NewClient("unix", "/var/run/nagios/livestatus.sock")
  10. defer c.Close()
  11. q := livestatus.NewQuery("hosts")
  12. q.Columns("name", "state", "last_time_down")
  13. q.Filter("name ~ ^db[0-9]+\\.")
  14. // or q := livestatus.Query("hosts").Columns("name", "state", "last_time_down").Filter("name ~ ^db[0-9]+\\.")
  15. resp, err := c.Exec(q)
  16. if err != nil {
  17. fmt.Fprintf(os.Stderr, "Error: %s", err)
  18. os.Exit(1)
  19. }
  20. for _, r := range resp.Records {
  21. name, err := r.GetString("name")
  22. if err != nil {
  23. fmt.Fprintf(os.Stderr, "Warning: %s", err)
  24. }
  25. state, err := r.GetInt("state")
  26. if err != nil {
  27. fmt.Fprintf(os.Stderr, "Warning: %s", err)
  28. }
  29. lastTimeDown, err := r.GetTime("last_time_down")
  30. if err != nil {
  31. fmt.Fprintf(os.Stderr, "Warning: %s", err)
  32. }
  33. fmt.Printf("Host: %s, State: %d, Last time down: %s\n", name, state, lastTimeDown)
  34. }
  35. }

Output example:

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