rollback old nginx metrics

This commit is contained in:
Giancarlo Rubio 2017-02-24 13:20:35 +01:00
parent 9eaf7d41de
commit 74e5bcece2
3 changed files with 95 additions and 12 deletions

View file

@ -43,7 +43,7 @@ func (em exeMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
func (n *NGINXController) setupMonitor(args []string) { func (n *NGINXController) setupMonitor(args []string) {
pc, err := newProcessCollector(true, exeMatcher{"nginx", args}) pc, err := newProcessCollector(true, exeMatcher{"nginx", args})
if err != nil { 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) err = prometheus.Register(pc)
if err != nil { if err != nil {
@ -199,17 +199,17 @@ func newProcessCollector(
// Describe implements prometheus.Collector. // Describe implements prometheus.Collector.
func (p *namedProcessCollector) Describe(ch chan<- *prometheus.Desc) { func (p *namedProcessCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- bytesDesc
ch <- cacheDesc
ch <- connectionsDesc
ch <- cpuSecsDesc ch <- cpuSecsDesc
ch <- memResidentbytesDesc ch <- memResidentbytesDesc
ch <- memVirtualbytesDesc ch <- memVirtualbytesDesc
ch <- numprocsDesc ch <- startTimeDesc
ch <- bytesDesc
ch <- cacheDesc
ch <- connectionsDesc
ch <- readBytesDesc ch <- readBytesDesc
ch <- requestDesc ch <- requestDesc
ch <- responseDesc ch <- responseDesc
ch <- startTimeDesc
ch <- writeBytesDesc ch <- writeBytesDesc
ch <- upstreamBackupDesc ch <- upstreamBackupDesc
ch <- upstreamBytesDesc ch <- upstreamBytesDesc
@ -221,6 +221,9 @@ func (p *namedProcessCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upstreamResponsesDesc ch <- upstreamResponsesDesc
ch <- upstreamWeightDesc ch <- upstreamWeightDesc
ch <- numprocsDesc
} }
// Collect implements prometheus.Collector. // Collect implements prometheus.Collector.
@ -249,18 +252,20 @@ func reflectMetrics(value interface{}, desc *prometheus.Desc, ch chan<- promethe
ch <- prometheus.MustNewConstMetric(desc, ch <- prometheus.MustNewConstMetric(desc,
prometheus.CounterValue, float64(val.Field(i).Interface().(float64)), prometheus.CounterValue, float64(val.Field(i).Interface().(float64)),
labels...) labels...)
} }
} }
func (p *namedProcessCollector) scrape(ch chan<- prometheus.Metric) { func (p *namedProcessCollector) scrape(ch chan<- prometheus.Metric) {
nginxMetrics, err := getNginxMetrics() nginxMetrics, err := getNginxMetrics()
if err != nil { if err != nil {
glog.Warningf("unexpected error obtaining nginx status info: %v", err) glog.Warningf("unexpected error obtaining nginx status info: %v", err)
return return
} }
reflectMetrics(&nginxMetrics.Connections, connectionsDesc, ch) reflectMetrics(&nginxMetrics.Connections, connectionsDesc, ch)
for name, zones := range nginxMetrics.UpstreamZones { for name, zones := range nginxMetrics.UpstreamZones {

View file

@ -52,7 +52,7 @@ const (
var ( var (
tmplPath = "/etc/nginx/template/nginx.tmpl" tmplPath = "/etc/nginx/template/nginx.tmpl"
cfgPath = "/etc/nginx/nginx.conf" cfgPath = "/etc/nginx/nginx.conf"
binary = "/usr/sbin/nginx" binary = "/usr/local/bin/nginx"
defIngressClass = "nginx" defIngressClass = "nginx"
) )

View file

@ -21,8 +21,35 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "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 { type Vts struct {
NginxVersion string `json:"nginxVersion"` NginxVersion string `json:"nginxVersion"`
LoadMsec int `json:"loadMsec"` LoadMsec int `json:"loadMsec"`
@ -110,19 +137,39 @@ func (bit BoolToFloat64) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func getNginxMetrics() (*Vts, error) { func getNginxStatus() (*nginxStatus, error) {
resp, err := http.DefaultClient.Get(fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxVtsPath)) data, err := httpBody(fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxStatusPath))
if err != nil { if err != nil {
return nil, fmt.Errorf("unexpected error scraping nginx status page: %v", err) 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) data, err := ioutil.ReadAll(resp.Body)
if err != nil { 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() defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 400 { 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 var vts Vts
@ -133,3 +180,34 @@ func getNginxMetrics() (*Vts, error) {
return &vts, nil 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
}