1 50558780 2022-01-06 o // package icinga provides a client to the Icinga2 HTTP API.
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:
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
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
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:
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
34 83f51130 2022-01-06 o StateOK State = 0 + iota
41 83f51130 2022-01-06 o HostUp State = 0 + iota
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 {
53 b74ab3b3 2021-12-30 o username string
54 b74ab3b3 2021-12-30 o password string
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
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
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)
78 2c4d16ae 2021-12-23 o return resp, nil