| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package phpfpm |
| 4 | |
| 5 | import ( |
| 6 | "math" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/pkg/stm" |
| 9 | ) |
| 10 | |
| 11 | func (c *Collector) collect() (map[string]int64, error) { |
| 12 | st, err := c.client.getStatus() |
| 13 | if err != nil { |
| 14 | return nil, err |
| 15 | } |
| 16 | |
| 17 | mx := stm.ToMap(st) |
| 18 | if !hasIdleProcesses(st.Processes) { |
| 19 | return mx, nil |
| 20 | } |
| 21 | |
| 22 | calcIdleProcessesRequestsDuration(mx, st.Processes) |
| 23 | calcIdleProcessesLastRequestCPU(mx, st.Processes) |
| 24 | calcIdleProcessesLastRequestMemory(mx, st.Processes) |
| 25 | return mx, nil |
| 26 | } |
| 27 | |
| 28 | func calcIdleProcessesRequestsDuration(mx map[string]int64, processes []proc) { |
| 29 | statProcesses(mx, processes, "ReqDur", func(p proc) int64 { return int64(p.Duration) }) |
| 30 | } |
| 31 | |
| 32 | func calcIdleProcessesLastRequestCPU(mx map[string]int64, processes []proc) { |
| 33 | statProcesses(mx, processes, "ReqCpu", func(p proc) int64 { return int64(p.CPU) }) |
| 34 | } |
| 35 | |
| 36 | func calcIdleProcessesLastRequestMemory(mx map[string]int64, processes []proc) { |
| 37 | statProcesses(mx, processes, "ReqMem", func(p proc) int64 { return p.Memory }) |
| 38 | } |
| 39 | |
| 40 | func hasIdleProcesses(processes []proc) bool { |
| 41 | for _, p := range processes { |
| 42 | if p.State == "Idle" { |
| 43 | return true |
| 44 | } |
| 45 | } |
| 46 | return false |
| 47 | } |
| 48 | |
| 49 | type accessor func(p proc) int64 |
| 50 | |
| 51 | func statProcesses(m map[string]int64, processes []proc, met string, acc accessor) { |
| 52 | var sum, count, minv, maxv int64 |
| 53 | for _, proc := range processes { |
| 54 | if proc.State != "Idle" { |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | val := acc(proc) |
| 59 | sum += val |
| 60 | count += 1 |
| 61 | if count == 1 { |
| 62 | minv, maxv = val, val |
| 63 | continue |
| 64 | } |
| 65 | minv = int64(math.Min(float64(minv), float64(val))) |
| 66 | maxv = int64(math.Max(float64(maxv), float64(val))) |
| 67 | } |
| 68 | |
| 69 | m["min"+met] = minv |
| 70 | m["max"+met] = maxv |
| 71 | m["avg"+met] = sum / count |
| 72 | } |