zabbix

package module
v0.0.0-...-fc342e5 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2019 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

zabbix

This Go library implements the Zabbix 2.0 API. The Zabbix API is a JSONRPC based API, although it is not compatable with Go's builtin JSONRPC libraries. So we implement that JSONRPC, and provide data types that mimic Zabbbix's return values.

Connecting to the API

func main() {
    api, err := zabbix.NewAPI("http://zabbix.yourhost.net/api_jsonrpc.php", "User", "Password")
    if err != nil {
        fmt.Println(err)
        return
    }

    versionresult, err := api.Version()
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(versionresult)

    _, err = api.Login()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("Connected to API")
}

Making a call

I typically wrap the actual API call to hide the messy details. If the response has an Error field, and the code is greater than 0, the API will return that Error. Then my wrapper function return a ZabbixError to the caller.

// Find and return a single host object by name
func GetHost(api *zabbix.API, host string) (zabbix.ZabbixHost, error) {
    params := make(map[string]interface{}, 0)
    filter := make(map[string]string, 0)
    filter["host"] = host
    params["filter"] = filter
    params["output"] = "extend"
    params["select_groups"] = "extend"
    params["templated_hosts"] = 1
    ret, err := api.Host("get", params)

    // This happens if there was an RPC error
    if err != nil {
        return nil, err
    }

    // If our call was successful
    if len(ret) > 0 {
        return ret[0], err
    }

    // This will be the case if the RPC call was successful, but
    // Zabbix had an issue with the data we passed.
    return nil, &zabbix.ZabbixError{0,"","Host not found"}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	Client *http.Client
	// contains filtered or unexported fields
}

func NewAPI

func NewAPI(server, user, passwd string) (*API, error)

func (*API) GetAuth

func (api *API) GetAuth() string

func (*API) Graph

func (api *API) Graph(method string, data interface{}) ([]ZabbixGraph, error)

* Interface to the graph.* calls

func (*API) History

func (api *API) History(method string, data interface{}) ([]ZabbixHistoryItem, error)

* Interface to the history.* calls

func (*API) Host

func (api *API) Host(method string, data interface{}) ([]ZabbixHost, error)

* Interface to the host.* calls

func (*API) Hostgroup

func (api *API) Hostgroup(method string, data interface{}) ([]ZabbixHostInterface, error)

func (*API) Interface

func (api *API) Interface(method string, data interface{}) ([]ZabbixHostInterface, error)

func (*API) Login

func (api *API) Login() (bool, error)

func (*API) Logout

func (api *API) Logout() (bool, error)

func (*API) User

func (api *API) User(method string, data interface{}) ([]interface{}, error)

* Interface to the user.* calls

func (*API) Version

func (api *API) Version() (string, error)

func (*API) ZabbixRequest

func (api *API) ZabbixRequest(method string, data interface{}) (JsonRPCResponse, error)

type JsonRPCRequest

type JsonRPCRequest struct {
	Jsonrpc string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params"`

	// Zabbix 2.0:
	// The "user.login" method must be called without the "auth" parameter
	Auth string `json:"auth,omitempty"`
	Id   int    `json:"id"`
}

type JsonRPCResponse

type JsonRPCResponse struct {
	Jsonrpc string      `json:"jsonrpc"`
	Error   ZabbixError `json:"error"`
	Result  interface{} `json:"result"`
	Id      int         `json:"id"`
}

* Zabbix and Go's RPC implementations don't play with each other.. at all. So I've re-created the wheel at bit.

type ZabbixError

type ZabbixError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    string `json:"data"`
}

func (*ZabbixError) Error

func (z *ZabbixError) Error() string

type ZabbixGraph

type ZabbixGraph map[string]interface{}

type ZabbixGraphItem

type ZabbixGraphItem map[string]interface{}

type ZabbixHistoryItem

type ZabbixHistoryItem struct {
	Clock  string `json:"clock"`
	Value  string `json:"value"`
	Itemid string `json:"itemid"`
}

type ZabbixHost

type ZabbixHost map[string]interface{}

type ZabbixHostGroup

type ZabbixHostGroup map[string]interface{}

type ZabbixHostInterface

type ZabbixHostInterface map[string]interface{}

Jump to

Keyboard shortcuts

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