summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/phpfpm/decode.go
blob: 021e1fb4c5d12197390dbf2ca79a14ffe8ab7090 (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
// SPDX-License-Identifier: GPL-3.0-or-later

package phpfpm

import (
	"bufio"
	"encoding/json"
	"errors"
	"io"
	"strconv"
	"strings"
)

type decoder func(r io.Reader, s *status) error

func decodeJSON(r io.Reader, s *status) error {
	return json.NewDecoder(r).Decode(s)
}

func decodeText(r io.Reader, s *status) error {
	parts := readParts(r)
	if len(parts) == 0 {
		return errors.New("invalid text format")
	}

	part, parts := parts[0], parts[1:]
	if err := readStatus(part, s); err != nil {
		return err
	}

	return readProcesses(parts, s)
}

func readParts(r io.Reader) [][]string {
	sc := bufio.NewScanner(r)

	var parts [][]string
	var lines []string
	for sc.Scan() {
		line := strings.Trim(sc.Text(), "\r\n ")
		// Split parts by star border
		if strings.HasPrefix(line, "***") {
			parts = append(parts, lines)
			lines = []string{}
			continue
		}
		// Skip empty lines
		if line == "" {
			continue
		}
		lines = append(lines, line)
	}

	if len(lines) > 0 {
		parts = append(parts, lines)
	}
	return parts
}

func readStatus(data []string, s *status) error {
	for _, line := range data {
		key, val, err := parseLine(line)
		if err != nil {
			return err
		}

		switch key {
		case "active processes":
			s.Active = parseInt(val)
		case "max active processes":
			s.MaxActive = parseInt(val)
		case "idle processes":
			s.Idle = parseInt(val)
		case "accepted conn":
			s.Requests = parseInt(val)
		case "max children reached":
			s.Reached = parseInt(val)
		case "slow requests":
			s.Slow = parseInt(val)
		}
	}
	return nil
}

func readProcesses(procs [][]string, s *status) error {
	for _, part := range procs {
		var proc proc
		for _, line := range part {
			key, val, err := parseLine(line)
			if err != nil {
				return err
			}

			switch key {
			case "state":
				proc.State = val
			case "request duration":
				proc.Duration = requestDuration(parseInt(val))
			case "last request cpu":
				proc.CPU = parseFloat(val)
			case "last request memory":
				proc.Memory = parseInt(val)
			}
		}
		s.Processes = append(s.Processes, proc)
	}
	return nil
}

func parseLine(s string) (string, string, error) {
	kv := strings.SplitN(s, ":", 2)
	if len(kv) != 2 {
		return "", "", errors.New("invalid text format line")
	}
	return strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1]), nil
}

func parseInt(s string) int64 {
	val, err := strconv.ParseInt(strings.TrimSpace(s), 10, 64)
	if err != nil {
		return 0
	}
	return val
}

func parseFloat(s string) float64 {
	val, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
	if err != nil {
		return 0
	}
	return val
}