icinga

Icinga2 API client in Go
git clone https://git.olowe.co/icinga
Log | Files | Refs | README | LICENSE

host.go (2424B)


      1 package icinga
      2 
      3 import (
      4 	"encoding/json"
      5 	"fmt"
      6 	"time"
      7 )
      8 
      9 // Host represents a Host object. To create a Host, the Name and CheckCommand
     10 // fields must be set.
     11 type Host struct {
     12 	Name            string      `json:"-"`
     13 	Address         string      `json:"address"`
     14 	Address6        string      `json:"address6"`
     15 	Groups          []string    `json:"groups,omitempty"`
     16 	State           HostState   `json:"state,omitempty"`
     17 	StateType       StateType   `json:"state_type,omitempty"`
     18 	CheckCommand    string      `json:"check_command"`
     19 	DisplayName     string      `json:"display_name,omitempty"`
     20 	LastCheck       time.Time   `json:",omitempty"`
     21 	LastCheckResult CheckResult `json:"last_check_result,omitempty"`
     22 	Acknowledgement bool        `json:",omitempty"`
     23 	Notes           string      `json:"notes,omitempty"`
     24 	NotesURL        string      `json:"notes_url,omitempty"`
     25 }
     26 
     27 type HostGroup struct {
     28 	Name        string `json:"-"`
     29 	DisplayName string `json:"display_name"`
     30 }
     31 
     32 type HostState int
     33 
     34 const (
     35 	HostUp HostState = 0 + iota
     36 	HostDown
     37 	HostUnreachable
     38 )
     39 
     40 func (state HostState) String() string {
     41 	switch state {
     42 	case HostUp:
     43 		return "Up"
     44 	case HostDown:
     45 		return "Down"
     46 	}
     47 	return "HostUnreachable"
     48 }
     49 
     50 func (h Host) name() string {
     51 	return h.Name
     52 }
     53 
     54 func (h Host) path() string {
     55 	return "/objects/hosts/" + h.Name
     56 }
     57 
     58 func (hg HostGroup) name() string {
     59 	return hg.Name
     60 }
     61 
     62 func (hg HostGroup) path() string {
     63 	return "/objects/hostgroups/" + hg.Name
     64 }
     65 
     66 // UnmarhsalJSON unmarshals host attributes into more meaningful Host field types.
     67 func (h *Host) UnmarshalJSON(data []byte) error {
     68 	type alias Host
     69 	aux := &struct {
     70 		Acknowledgement interface{} `json:"acknowledgement"`
     71 		State           interface{} `json:"state"`
     72 		StateType       interface{} `json:"state_type"`
     73 		LastCheck       float64     `json:"last_check"`
     74 		*alias
     75 	}{
     76 		alias: (*alias)(h),
     77 	}
     78 	if err := json.Unmarshal(data, &aux); err != nil {
     79 		fmt.Println("uh oh!")
     80 		return err
     81 	}
     82 	switch v := aux.Acknowledgement.(type) {
     83 	case int:
     84 		if v != 0 {
     85 			h.Acknowledgement = true
     86 		}
     87 	case float64:
     88 		if int(v) != 0 {
     89 			h.Acknowledgement = true
     90 		}
     91 	}
     92 	switch v := aux.State.(type) {
     93 	case int:
     94 		h.State = HostState(v)
     95 	case float64:
     96 		h.State = HostState(v)
     97 	}
     98 	switch v := aux.StateType.(type) {
     99 	case int:
    100 		h.StateType = StateType(v)
    101 	case float64:
    102 		h.StateType = StateType(v)
    103 	}
    104 	h.LastCheck = time.Unix(int64(aux.LastCheck), 0)
    105 	return nil
    106 }