summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/apache/collect.go
blob: 52bad9fdaa19167e79cb273e3aec341709a0a419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-License-Identifier: GPL-3.0-or-later

package apache

import (
	"bufio"
	"fmt"
	"io"
	"net/http"
	"strconv"
	"strings"

	"github.com/netdata/netdata/go/go.d.plugin/pkg/stm"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
)

func (a *Apache) collect() (map[string]int64, error) {
	status, err := a.scrapeStatus()
	if err != nil {
		return nil, err
	}

	mx := stm.ToMap(status)
	if len(mx) == 0 {
		return nil, fmt.Errorf("nothing was collected from %s", a.URL)
	}

	a.once.Do(func() { a.charts = newCharts(status) })

	return mx, nil
}

func (a *Apache) scrapeStatus() (*serverStatus, error) {
	req, err := web.NewHTTPRequest(a.Request)
	if err != nil {
		return nil, err
	}

	resp, err := a.httpClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
	}
	defer closeBody(resp)

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
	}

	return parseResponse(resp.Body)
}

func parseResponse(r io.Reader) (*serverStatus, error) {
	s := bufio.NewScanner(r)
	var status serverStatus

	for s.Scan() {
		parts := strings.Split(s.Text(), ":")
		if len(parts) != 2 {
			continue
		}

		key, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])

		switch key {
		default:
		case "BusyServers", "IdleServers":
			return nil, fmt.Errorf("found '%s', Lighttpd data", key)
		case "BusyWorkers":
			status.Workers.Busy = parseInt(value)
		case "IdleWorkers":
			status.Workers.Idle = parseInt(value)
		case "ConnsTotal":
			status.Connections.Total = parseInt(value)
		case "ConnsAsyncWriting":
			status.Connections.Async.Writing = parseInt(value)
		case "ConnsAsyncKeepAlive":
			status.Connections.Async.KeepAlive = parseInt(value)
		case "ConnsAsyncClosing":
			status.Connections.Async.Closing = parseInt(value)
		case "Total Accesses":
			status.Total.Accesses = parseInt(value)
		case "Total kBytes":
			status.Total.KBytes = parseInt(value)
		case "Uptime":
			status.Uptime = parseInt(value)
		case "ReqPerSec":
			status.Averages.ReqPerSec = parseFloat(value)
		case "BytesPerSec":
			status.Averages.BytesPerSec = parseFloat(value)
		case "BytesPerReq":
			status.Averages.BytesPerReq = parseFloat(value)
		case "Scoreboard":
			status.Scoreboard = parseScoreboard(value)
		}
	}

	return &status, nil
}

func parseScoreboard(line string) *scoreboard {
	//  “_” Waiting for Connection
	// “S” Starting up
	// “R” Reading Request
	// “W” Sending Reply
	// “K” Keepalive (read)
	// “D” DNS Lookup
	// “C” Closing connection
	// “L” Logging
	// “G” Gracefully finishing
	// “I” Idle cleanup of worker
	// “.” Open slot with no current process
	var sb scoreboard
	for _, s := range strings.Split(line, "") {
		switch s {
		case "_":
			sb.Waiting++
		case "S":
			sb.Starting++
		case "R":
			sb.Reading++
		case "W":
			sb.Sending++
		case "K":
			sb.KeepAlive++
		case "D":
			sb.DNSLookup++
		case "C":
			sb.Closing++
		case "L":
			sb.Logging++
		case "G":
			sb.Finishing++
		case "I":
			sb.IdleCleanup++
		case ".":
			sb.Open++
		}
	}
	return &sb
}

func parseInt(value string) *int64 {
	v, err := strconv.ParseInt(value, 10, 64)
	if err != nil {
		return nil
	}
	return &v
}

func parseFloat(value string) *float64 {
	v, err := strconv.ParseFloat(value, 64)
	if err != nil {
		return nil
	}
	return &v
}

func closeBody(resp *http.Response) {
	if resp != nil && resp.Body != nil {
		_, _ = io.Copy(io.Discard, resp.Body)
		_ = resp.Body.Close()
	}
}