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

package phpfpm

import (
	"math"

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

func (p *Phpfpm) collect() (map[string]int64, error) {
	st, err := p.client.getStatus()
	if err != nil {
		return nil, err
	}

	mx := stm.ToMap(st)
	if !hasIdleProcesses(st.Processes) {
		return mx, nil
	}

	calcIdleProcessesRequestsDuration(mx, st.Processes)
	calcIdleProcessesLastRequestCPU(mx, st.Processes)
	calcIdleProcessesLastRequestMemory(mx, st.Processes)
	return mx, nil
}

func calcIdleProcessesRequestsDuration(mx map[string]int64, processes []proc) {
	statProcesses(mx, processes, "ReqDur", func(p proc) int64 { return int64(p.Duration) })
}

func calcIdleProcessesLastRequestCPU(mx map[string]int64, processes []proc) {
	statProcesses(mx, processes, "ReqCpu", func(p proc) int64 { return int64(p.CPU) })
}

func calcIdleProcessesLastRequestMemory(mx map[string]int64, processes []proc) {
	statProcesses(mx, processes, "ReqMem", func(p proc) int64 { return p.Memory })
}

func hasIdleProcesses(processes []proc) bool {
	for _, p := range processes {
		if p.State == "Idle" {
			return true
		}
	}
	return false
}

type accessor func(p proc) int64

func statProcesses(m map[string]int64, processes []proc, met string, acc accessor) {
	var sum, count, min, max int64
	for _, proc := range processes {
		if proc.State != "Idle" {
			continue
		}

		val := acc(proc)
		sum += val
		count += 1
		if count == 1 {
			min, max = val, val
			continue
		}
		min = int64(math.Min(float64(min), float64(val)))
		max = int64(math.Max(float64(max), float64(val)))
	}

	m["min"+met] = min
	m["max"+met] = max
	m["avg"+met] = sum / count
}