Skip to content

Commit d82aa26

Browse files
author
Jonathan Thurman
committed
Add nginx info to GetStats
1 parent d0dec08 commit d82aa26

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+
NginxInfo NginxInfo
8283
Connections Connections
8384
HTTPRequests HTTPRequests
8485
SSL SSL
@@ -88,6 +89,18 @@ type Stats struct {
8889
StreamUpstreams StreamUpstreams
8990
}
9091

92+
// NginxInfo contains general information about NGINX Plus.
93+
type NginxInfo struct {
94+
Version string
95+
Build string
96+
Address string
97+
Generation uint64
98+
LoadTimestamp string `json:"load_timestamp"`
99+
Timestamp string
100+
ProcessID uint64 `json:"pid"`
101+
ParentProcessID uint64 `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+
info, err := client.getNginxInfo()
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+
NginxInfo: *info,
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) getNginxInfo() (*NginxInfo, error) {
727+
var info NginxInfo
728+
err := client.get("nginx", &info)
729+
if err != nil {
730+
return nil, fmt.Errorf("failed to get info: %v", err)
731+
}
732+
return &info, 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+
// NginxInfo
416+
if stats.NginxInfo.Version == "" {
417+
t.Error("Missing version string")
418+
}
419+
if stats.NginxInfo.Build == "" {
420+
t.Error("Missing build string")
421+
}
422+
if stats.NginxInfo.Address == "" {
423+
t.Errorf("Missing server address")
424+
}
425+
if stats.NginxInfo.Generation < 1 {
426+
t.Errorf("Bad config generation: %v", stats.NginxInfo.Generation)
427+
}
428+
if stats.NginxInfo.LoadTimestamp == "" {
429+
t.Error("Missing load timestamp")
430+
}
431+
if stats.NginxInfo.Timestamp == "" {
432+
t.Error("Missing timestamp")
433+
}
434+
if stats.NginxInfo.ProcessID < 1 {
435+
t.Errorf("Bad process id: %v", stats.NginxInfo.ProcessID)
436+
}
437+
if stats.NginxInfo.ParentProcessID < 1 {
438+
t.Errorf("Bad parent process id: %v", stats.NginxInfo.ParentProcessID)
439+
}
440+
415441
if stats.Connections.Accepted < 1 {
416442
t.Errorf("Bad connections: %v", stats.Connections)
417443
}

0 commit comments

Comments
 (0)