Blame


1 50558780 2022-01-06 o // package icinga provides a client to the Icinga2 HTTP API.
2 50558780 2022-01-06 o //
3 50558780 2022-01-06 o // A Client manages interaction with an Icinga2 server.
4 50558780 2022-01-06 o // It is created using Dial:
5 50558780 2022-01-06 o //
6 50558780 2022-01-06 o // client, err := icinga.Dial("icinga.example.com:5665", "icinga", "secret", http.DefaultClient)
7 50558780 2022-01-06 o // if err != nil {
8 50558780 2022-01-06 o // // handle error
9 50558780 2022-01-06 o // }
10 50558780 2022-01-06 o // host, err := icinga.LookupHost("myserver.example.com")
11 50558780 2022-01-06 o // if err != nil {
12 50558780 2022-01-06 o // // handle error
13 50558780 2022-01-06 o // }
14 50558780 2022-01-06 o //
15 50558780 2022-01-06 o // Since Client wraps http.Client, exported methods of http.Client such
16 50558780 2022-01-06 o // as Get and PostForm can be used to implement any extra functionality
17 50558780 2022-01-06 o // not provided by this package. For example:
18 50558780 2022-01-06 o //
19 50558780 2022-01-06 o // resp, err := client.PostForm("https://icinga.example.com:5665", data)
20 50558780 2022-01-06 o // if err != nil {
21 50558780 2022-01-06 o // // handle error
22 50558780 2022-01-06 o // }
23 50558780 2022-01-06 o //
24 2c4d16ae 2021-12-23 o package icinga
25 2c4d16ae 2021-12-23 o
26 2c4d16ae 2021-12-23 o import (
27 2c4d16ae 2021-12-23 o "fmt"
28 2c4d16ae 2021-12-23 o "net/http"
29 2c4d16ae 2021-12-23 o )
30 2c4d16ae 2021-12-23 o
31 83f51130 2022-01-06 o type State int
32 83f51130 2022-01-06 o
33 83f51130 2022-01-06 o const (
34 83f51130 2022-01-06 o StateOK State = 0 + iota
35 83f51130 2022-01-06 o StateWarning
36 83f51130 2022-01-06 o StateCritical
37 83f51130 2022-01-06 o StateUnknown
38 83f51130 2022-01-06 o )
39 83f51130 2022-01-06 o
40 83f51130 2022-01-06 o const (
41 83f51130 2022-01-06 o HostUp State = 0 + iota
42 83f51130 2022-01-06 o HostDown
43 83f51130 2022-01-06 o HostUnknown
44 83f51130 2022-01-06 o )
45 83f51130 2022-01-06 o
46 50558780 2022-01-06 o // A Client represents a client connection to the Icinga2 HTTP API.
47 50558780 2022-01-06 o // It should be created using Dial.
48 50558780 2022-01-06 o // Since Client wraps http.Client, standard methods such as Get and
49 50558780 2022-01-06 o // PostForm can be used to implement any functionality not provided by
50 50558780 2022-01-06 o // methods of Client.
51 2c4d16ae 2021-12-23 o type Client struct {
52 50558780 2022-01-06 o addr string
53 b74ab3b3 2021-12-30 o username string
54 b74ab3b3 2021-12-30 o password string
55 007a3d09 2021-12-27 o *http.Client
56 2c4d16ae 2021-12-23 o }
57 2c4d16ae 2021-12-23 o
58 50558780 2022-01-06 o // Dial returns a new Client connected to the Icinga2 server at addr.
59 50558780 2022-01-06 o // The recommended value for client is http.DefaultClient.
60 50558780 2022-01-06 o // But it may also be a modified client which, for example,
61 50558780 2022-01-06 o // skips TLS certificate verification.
62 50558780 2022-01-06 o func Dial(addr, username, password string, client *http.Client) (*Client, error) {
63 50558780 2022-01-06 o c := &Client{addr, username, password, client}
64 2c4d16ae 2021-12-23 o if _, err := c.Status(); err != nil {
65 2c4d16ae 2021-12-23 o return nil, err
66 2c4d16ae 2021-12-23 o }
67 2c4d16ae 2021-12-23 o return c, nil
68 2c4d16ae 2021-12-23 o }
69 2c4d16ae 2021-12-23 o
70 2c4d16ae 2021-12-23 o func (c *Client) Status() (*http.Response, error) {
71 2c4d16ae 2021-12-23 o resp, err := c.get("/status")
72 2c4d16ae 2021-12-23 o if err != nil {
73 2c4d16ae 2021-12-23 o return nil, err
74 2c4d16ae 2021-12-23 o }
75 2c4d16ae 2021-12-23 o if resp.StatusCode != http.StatusOK {
76 2c4d16ae 2021-12-23 o return resp, fmt.Errorf("status %s", resp.Status)
77 2c4d16ae 2021-12-23 o }
78 2c4d16ae 2021-12-23 o return resp, nil
79 2c4d16ae 2021-12-23 o }