From 91ed596ed07b2439290ea5f8608733e5f7db999f Mon Sep 17 00:00:00 2001 From: Jonathan Thurman Date: Wed, 12 Dec 2018 12:11:27 -0800 Subject: [PATCH] Add nginx info to GetStats --- client/nginx.go | 28 ++++++++++++++++++++++++++++ docker/test.conf | 6 +----- tests/client_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/client/nginx.go b/client/nginx.go index 07fdce40..0f9678b7 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -79,6 +79,7 @@ func (internalError *internalError) Wrap(err string) *internalError { // Stats represents NGINX Plus stats fetched from the NGINX Plus API. // https://nginx.org/en/docs/http/ngx_http_api_module.html type Stats struct { + NginxInfo NginxInfo Connections Connections HTTPRequests HTTPRequests SSL SSL @@ -88,6 +89,18 @@ type Stats struct { StreamUpstreams StreamUpstreams } +// NginxInfo contains general information about NGINX Plus. +type NginxInfo struct { + Version string + Build string + Address string + Generation uint64 + LoadTimestamp string `json:"load_timestamp"` + Timestamp string + ProcessID uint64 `json:"pid"` + ParentProcessID uint64 `json:"ppid"` +} + // Connections represents connection related stats. type Connections struct { Accepted uint64 @@ -658,6 +671,11 @@ func determineStreamUpdates(updatedServers []StreamUpstreamServer, nginxServers // GetStats gets connection, request, ssl, zone, stream zone, upstream and stream upstream related stats from the NGINX Plus API. func (client *NginxClient) GetStats() (*Stats, error) { + info, err := client.getNginxInfo() + if err != nil { + return nil, fmt.Errorf("failed to get stats %v", err) + } + cons, err := client.getConnections() if err != nil { return nil, fmt.Errorf("failed to get stats: %v", err) @@ -694,6 +712,7 @@ func (client *NginxClient) GetStats() (*Stats, error) { } return &Stats{ + NginxInfo: *info, Connections: *cons, HTTPRequests: *requests, SSL: *ssl, @@ -704,6 +723,15 @@ func (client *NginxClient) GetStats() (*Stats, error) { }, nil } +func (client *NginxClient) getNginxInfo() (*NginxInfo, error) { + var info NginxInfo + err := client.get("nginx", &info) + if err != nil { + return nil, fmt.Errorf("failed to get info: %v", err) + } + return &info, nil +} + func (client *NginxClient) getConnections() (*Connections, error) { var cons Connections err := client.get("connections", &cons) diff --git a/docker/test.conf b/docker/test.conf index 693b6d6a..1bbb57da 100644 --- a/docker/test.conf +++ b/docker/test.conf @@ -10,10 +10,6 @@ server { } location /api { - limit_except GET { - allow 172.0.0.0/8; - deny all; - } api write=on; } @@ -22,4 +18,4 @@ server { health_check interval=10 fails=3 passes=1; } status_zone test; -} \ No newline at end of file +} diff --git a/tests/client_test.go b/tests/client_test.go index 14fbd186..35dcb146 100644 --- a/tests/client_test.go +++ b/tests/client_test.go @@ -412,6 +412,32 @@ func TestStats(t *testing.T) { t.Errorf("Error getting stats: %v", err) } + // NginxInfo + if stats.NginxInfo.Version == "" { + t.Error("Missing version string") + } + if stats.NginxInfo.Build == "" { + t.Error("Missing build string") + } + if stats.NginxInfo.Address == "" { + t.Errorf("Missing server address") + } + if stats.NginxInfo.Generation < 1 { + t.Errorf("Bad config generation: %v", stats.NginxInfo.Generation) + } + if stats.NginxInfo.LoadTimestamp == "" { + t.Error("Missing load timestamp") + } + if stats.NginxInfo.Timestamp == "" { + t.Error("Missing timestamp") + } + if stats.NginxInfo.ProcessID < 1 { + t.Errorf("Bad process id: %v", stats.NginxInfo.ProcessID) + } + if stats.NginxInfo.ParentProcessID < 1 { + t.Errorf("Bad parent process id: %v", stats.NginxInfo.ParentProcessID) + } + if stats.Connections.Accepted < 1 { t.Errorf("Bad connections: %v", stats.Connections) }