Blob


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