From 74e5bcece2d927f7e633d95c9a5fa7eaa77bbb66 Mon Sep 17 00:00:00 2001 From: Giancarlo Rubio Date: Fri, 24 Feb 2017 13:20:35 +0100 Subject: [PATCH] rollback old nginx metrics --- .../nginx/pkg/cmd/controller/metrics.go | 19 ++-- controllers/nginx/pkg/cmd/controller/nginx.go | 2 +- .../nginx/pkg/cmd/controller/status.go | 86 ++++++++++++++++++- 3 files changed, 95 insertions(+), 12 deletions(-) diff --git a/controllers/nginx/pkg/cmd/controller/metrics.go b/controllers/nginx/pkg/cmd/controller/metrics.go index 3a5247c1f..b114d1152 100644 --- a/controllers/nginx/pkg/cmd/controller/metrics.go +++ b/controllers/nginx/pkg/cmd/controller/metrics.go @@ -43,7 +43,7 @@ func (em exeMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, string) { func (n *NGINXController) setupMonitor(args []string) { pc, err := newProcessCollector(true, exeMatcher{"nginx", args}) if err != nil { - glog.Fatalf("unexpected error registering nginx collector: %v", err) + glog.Warningf("unexpected error registering nginx collector: %v", err) } err = prometheus.Register(pc) if err != nil { @@ -199,17 +199,17 @@ func newProcessCollector( // Describe implements prometheus.Collector. func (p *namedProcessCollector) Describe(ch chan<- *prometheus.Desc) { - ch <- bytesDesc - ch <- cacheDesc - ch <- connectionsDesc ch <- cpuSecsDesc ch <- memResidentbytesDesc ch <- memVirtualbytesDesc - ch <- numprocsDesc + ch <- startTimeDesc + + ch <- bytesDesc + ch <- cacheDesc + ch <- connectionsDesc ch <- readBytesDesc ch <- requestDesc ch <- responseDesc - ch <- startTimeDesc ch <- writeBytesDesc ch <- upstreamBackupDesc ch <- upstreamBytesDesc @@ -221,6 +221,9 @@ func (p *namedProcessCollector) Describe(ch chan<- *prometheus.Desc) { ch <- upstreamResponsesDesc ch <- upstreamWeightDesc + ch <- numprocsDesc + + } // Collect implements prometheus.Collector. @@ -249,18 +252,20 @@ func reflectMetrics(value interface{}, desc *prometheus.Desc, ch chan<- promethe ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(val.Field(i).Interface().(float64)), labels...) - } } func (p *namedProcessCollector) scrape(ch chan<- prometheus.Metric) { + nginxMetrics, err := getNginxMetrics() if err != nil { glog.Warningf("unexpected error obtaining nginx status info: %v", err) return } + + reflectMetrics(&nginxMetrics.Connections, connectionsDesc, ch) for name, zones := range nginxMetrics.UpstreamZones { diff --git a/controllers/nginx/pkg/cmd/controller/nginx.go b/controllers/nginx/pkg/cmd/controller/nginx.go index ae0b12022..600638c35 100644 --- a/controllers/nginx/pkg/cmd/controller/nginx.go +++ b/controllers/nginx/pkg/cmd/controller/nginx.go @@ -52,7 +52,7 @@ const ( var ( tmplPath = "/etc/nginx/template/nginx.tmpl" cfgPath = "/etc/nginx/nginx.conf" - binary = "/usr/sbin/nginx" + binary = "/usr/local/bin/nginx" defIngressClass = "nginx" ) diff --git a/controllers/nginx/pkg/cmd/controller/status.go b/controllers/nginx/pkg/cmd/controller/status.go index 60304ebde..78e53df51 100644 --- a/controllers/nginx/pkg/cmd/controller/status.go +++ b/controllers/nginx/pkg/cmd/controller/status.go @@ -21,8 +21,35 @@ import ( "fmt" "io/ioutil" "net/http" + "regexp" + "strconv" ) +var ( + ac = regexp.MustCompile(`Active connections: (\d+)`) + sahr = regexp.MustCompile(`(\d+)\s(\d+)\s(\d+)`) + reading = regexp.MustCompile(`Reading: (\d+)`) + writing = regexp.MustCompile(`Writing: (\d+)`) + waiting = regexp.MustCompile(`Waiting: (\d+)`) +) + +type nginxStatus struct { + // Active total number of active connections + Active int + // Accepted total number of accepted client connections + Accepted int + // Handled total number of handled connections. Generally, the parameter value is the same as accepts unless some resource limits have been reached (for example, the worker_connections limit). + Handled int + // Requests total number of client requests. + Requests int + // Reading current number of connections where nginx is reading the request header. + Reading int + // Writing current number of connections where nginx is writing the response back to the client. + Writing int + // Waiting current number of idle client connections waiting for a request. + Waiting int +} + type Vts struct { NginxVersion string `json:"nginxVersion"` LoadMsec int `json:"loadMsec"` @@ -110,19 +137,39 @@ func (bit BoolToFloat64) UnmarshalJSON(data []byte) error { return nil } -func getNginxMetrics() (*Vts, error) { - resp, err := http.DefaultClient.Get(fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxVtsPath)) +func getNginxStatus() (*nginxStatus, error) { + data, err := httpBody(fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxStatusPath)) + if err != nil { return nil, fmt.Errorf("unexpected error scraping nginx status page: %v", err) } + return parse(string(data)), nil +} + +func httpBody(url string) ([]byte, error) { + resp, err := http.DefaultClient.Get(url) + if err != nil { + return nil, fmt.Errorf("unexpected error scraping nginx : %v", err) + } + data, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil, fmt.Errorf("unexpected error scraping nginx status page (%v)", err) + return nil, fmt.Errorf("unexpected error scraping nginx (%v)", err) } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 400 { - return nil, fmt.Errorf("unexpected error scraping nginx status page (status %v)", resp.StatusCode) + return nil, fmt.Errorf("unexpected error scraping nginx (status %v)", resp.StatusCode) + } + + return data, nil + +} +func getNginxVtsMetrics() (*Vts, error) { + data, err := httpBody(fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxVtsPath)) + + if err { + return nil, fmt.Errorf("unexpected error scraping nginx vts (%v)", err) } var vts Vts @@ -133,3 +180,34 @@ func getNginxMetrics() (*Vts, error) { return &vts, nil } + +func parse(data string) *nginxStatus { + acr := ac.FindStringSubmatch(data) + sahrr := sahr.FindStringSubmatch(data) + readingr := reading.FindStringSubmatch(data) + writingr := writing.FindStringSubmatch(data) + waitingr := waiting.FindStringSubmatch(data) + + return &nginxStatus{ + toInt(acr, 1), + toInt(sahrr, 1), + toInt(sahrr, 2), + toInt(sahrr, 3), + toInt(readingr, 1), + toInt(writingr, 1), + toInt(waitingr, 1), + } +} + +func toInt(data []string, pos int) int { + if len(data) == 0 { + return 0 + } + if pos > len(data) { + return 0 + } + if v, err := strconv.Atoi(data[pos]); err == nil { + return v + } + return 0 +}