| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package phpdaemon |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/pkg/stm" |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 10 | ) |
| 11 | |
| 12 | // https://github.com/kakserpom/phpdaemon/blob/master/PHPDaemon/Core/Daemon.php |
| 13 | // see getStateOfWorkers() |
| 14 | |
| 15 | type fullStatus struct { |
| 16 | // Alive is sum of Idle, Busy and Reloading |
| 17 | Alive int64 `json:"alive" stm:"alive"` |
| 18 | Shutdown int64 `json:"shutdown" stm:"shutdown"` |
| 19 | |
| 20 | // Idle that the worker is not in the middle of execution valuable callback (e.g. request) at this moment of time. |
| 21 | // It does not mean that worker not have any pending operations. |
| 22 | // Idle is sum of Preinit, Init and Initialized. |
| 23 | Idle int64 `json:"idle" stm:"idle"` |
| 24 | // Busy means that the worker is in the middle of execution valuable callback. |
| 25 | Busy int64 `json:"busy" stm:"busy"` |
| 26 | Reloading int64 `json:"reloading" stm:"reloading"` |
| 27 | |
| 28 | Preinit int64 `json:"preinit" stm:"preinit"` |
| 29 | // Init means that worker is starting right now. |
| 30 | Init int64 `json:"init" stm:"init"` |
| 31 | // Initialized means that the worker is in Idle state. |
| 32 | Initialized int64 `json:"initialized" stm:"initialized"` |
| 33 | |
| 34 | Uptime *int64 `json:"uptime" stm:"uptime"` |
| 35 | } |
| 36 | |
| 37 | func (c *Collector) collect() (map[string]int64, error) { |
| 38 | req, err := web.NewHTTPRequest(c.RequestConfig) |
| 39 | if err != nil { |
| 40 | return nil, fmt.Errorf("failed to create HTTP request to '%s': %w", c.URL, err) |
| 41 | } |
| 42 | |
| 43 | var st fullStatus |
| 44 | |
| 45 | if err := web.DoHTTP(c.httpClient).RequestJSON(req, &st); err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | |
| 49 | // https://github.com/kakserpom/phpdaemon/blob/master/PHPDaemon/Core/Daemon.php |
| 50 | // see getStateOfWorkers() |
| 51 | st.Initialized = st.Idle - (st.Init + st.Preinit) |
| 52 | |
| 53 | mx := stm.ToMap(st) |
| 54 | |
| 55 | c.once.Do(func() { |
| 56 | if _, ok := mx["uptime"]; ok { |
| 57 | _ = c.charts.Add(uptimeChart.Copy()) |
| 58 | } |
| 59 | }) |
| 60 | |
| 61 | return mx, nil |
| 62 | } |