Skip to content

Commit 9aff3f9

Browse files
author
Jonathan Thurman
committed
Add nginx status info to GetStats
1 parent 1900f9f commit 9aff3f9

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

client/nginx.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (internalError *internalError) Wrap(err string) *internalError {
7979
// Stats represents NGINX Plus stats fetched from the NGINX Plus API.
8080
// https://nginx.org/en/docs/http/ngx_http_api_module.html
8181
type Stats struct {
82+
Status Status
8283
Connections Connections
8384
HTTPRequests HTTPRequests
8485
SSL SSL
@@ -88,6 +89,18 @@ type Stats struct {
8889
StreamUpstreams StreamUpstreams
8990
}
9091

92+
// Status contains basic system status information
93+
type Status struct {
94+
Version string `json:"version"`
95+
Build string `json:"build"`
96+
Address string `json:"address"`
97+
Generation int `json:"generation"`
98+
LoadTimestamp string `json:"load_timestamp"`
99+
CurrentTime string `json:"timestamp"`
100+
ProcessID int `json:"pid"`
101+
ParentProcessID int `json:"ppid"`
102+
}
103+
91104
// Connections represents connection related stats.
92105
type Connections struct {
93106
Accepted uint64
@@ -658,6 +671,11 @@ func determineStreamUpdates(updatedServers []StreamUpstreamServer, nginxServers
658671

659672
// GetStats gets connection, request, ssl, zone, stream zone, upstream and stream upstream related stats from the NGINX Plus API.
660673
func (client *NginxClient) GetStats() (*Stats, error) {
674+
status, err := client.getStatus()
675+
if err != nil {
676+
return nil, fmt.Errorf("failed to get stats %v", err)
677+
}
678+
661679
cons, err := client.getConnections()
662680
if err != nil {
663681
return nil, fmt.Errorf("failed to get stats: %v", err)
@@ -694,6 +712,7 @@ func (client *NginxClient) GetStats() (*Stats, error) {
694712
}
695713

696714
return &Stats{
715+
Status: *status,
697716
Connections: *cons,
698717
HTTPRequests: *requests,
699718
SSL: *ssl,
@@ -704,6 +723,15 @@ func (client *NginxClient) GetStats() (*Stats, error) {
704723
}, nil
705724
}
706725

726+
func (client *NginxClient) getStatus() (*Status, error) {
727+
var status Status
728+
err := client.get("nginx", &status)
729+
if err != nil {
730+
return nil, fmt.Errorf("failed to get status: %v", err)
731+
}
732+
return &status, nil
733+
}
734+
707735
func (client *NginxClient) getConnections() (*Connections, error) {
708736
var cons Connections
709737
err := client.get("connections", &cons)

tests/client_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,32 @@ func TestStats(t *testing.T) {
412412
t.Errorf("Error getting stats: %v", err)
413413
}
414414

415+
// Status info
416+
if stats.Status.Version == "" {
417+
t.Error("Missing version string")
418+
}
419+
if stats.Status.Build == "" {
420+
t.Error("Missing build string")
421+
}
422+
if stats.Status.Address != "127.0.0.1" {
423+
t.Errorf("Bad status address: %v", stats.Status.Address)
424+
}
425+
if stats.Status.Generation < 1 {
426+
t.Errorf("Bad config generation: %v", stats.Status.Generation)
427+
}
428+
if stats.Status.LoadTimestamp == "" {
429+
t.Error("Missing load timestamp")
430+
}
431+
if stats.Status.CurrentTime == "" {
432+
t.Error("Missing current timestamp")
433+
}
434+
if stats.Status.ProcessID < 1 {
435+
t.Errorf("Bad process id: %v", stats.Status.ProcessID)
436+
}
437+
if stats.Status.ParentProcessID < 1 {
438+
t.Errorf("Bad parent process id: %v", stats.Status.ParentProcessID)
439+
}
440+
415441
if stats.Connections.Accepted < 1 {
416442
t.Errorf("Bad connections: %v", stats.Connections)
417443
}

0 commit comments

Comments
 (0)