github.com/wtfutil/wtf@v0.43.0/modules/newrelic/client/servers.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  )
     7  
     8  // ServersFilter is the filtering component of ServersOptions.
     9  type ServersFilter struct {
    10  	Name     string
    11  	Host     string
    12  	IDs      []int
    13  	Labels   []string
    14  	Reported bool
    15  }
    16  
    17  // ServersOptions provides a filtering mechanism for GetServers.
    18  type ServersOptions struct {
    19  	Filter ServersFilter
    20  	Page   int
    21  }
    22  
    23  // ServerSummary describes the summary component of a Server.
    24  type ServerSummary struct {
    25  	CPU             float64 `json:"cpu,omitempty"`
    26  	CPUStolen       float64 `json:"cpu_stolen,omitempty"`
    27  	DiskIO          float64 `json:"disk_io,omitempty"`
    28  	Memory          float64 `json:"memory,omitempty"`
    29  	MemoryUsed      int64   `json:"memory_used,omitempty"`
    30  	MemoryTotal     int64   `json:"memory_total,omitempty"`
    31  	FullestDisk     float64 `json:"fullest_disk,omitempty"`
    32  	FullestDiskFree int64   `json:"fullest_disk_free,omitempty"`
    33  }
    34  
    35  // ServerLinks link Servers to the objects to which they pertain.
    36  type ServerLinks struct {
    37  	AlertPolicy int `json:"alert_policy,omitempty"`
    38  }
    39  
    40  // Server represents a New Relic Server.
    41  type Server struct {
    42  	ID             int           `json:"id,omitempty"`
    43  	AccountID      int           `json:"account_id,omitempty"`
    44  	Name           string        `json:"name,omitempty"`
    45  	Host           string        `json:"host,omitempty"`
    46  	HealthStatus   string        `json:"health_status,omitempty"`
    47  	Reporting      bool          `json:"reporting,omitempty"`
    48  	LastReportedAt time.Time     `json:"last_reported_at,omitempty"`
    49  	Summary        ServerSummary `json:"summary,omitempty"`
    50  	Links          ServerLinks   `json:"links,omitempty"`
    51  }
    52  
    53  // GetServers will return a slice of New Relic Servers, optionally filtered by
    54  // ServerOptions.
    55  func (c *Client) GetServers(opt *ServersOptions) ([]Server, error) {
    56  	resp := &struct {
    57  		Servers []Server `json:"servers,omitempty"`
    58  	}{}
    59  	path := "servers.json"
    60  	err := c.doGet(path, opt, resp)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return resp.Servers, nil
    65  }
    66  
    67  // GetServer will return a single New Relic Server for the given id.
    68  func (c *Client) GetServer(id int) (*Server, error) {
    69  	resp := &struct {
    70  		Server *Server `json:"server,omitempty"`
    71  	}{}
    72  	path := "servers/" + strconv.Itoa(id) + ".json"
    73  	err := c.doGet(path, nil, resp)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	return resp.Server, nil
    78  }
    79  
    80  func (o *ServersOptions) String() string {
    81  	if o == nil {
    82  		return ""
    83  	}
    84  	return encodeGetParams(map[string]interface{}{
    85  		"filter[name]":     o.Filter.Name,
    86  		"filter[host]":     o.Filter.Host,
    87  		"filter[ids]":      o.Filter.IDs,
    88  		"filter[labels]":   o.Filter.Labels,
    89  		"filter[reported]": o.Filter.Reported,
    90  		"page":             o.Page,
    91  	})
    92  }