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
|
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
package scheduler
import (
"errors"
"fmt"
"sort"
"strings"
"zabbix.com/pkg/plugin"
)
type pluginMetrics struct {
ref *pluginAgent
metrics []*plugin.Metric
}
// getStatus() returns a list of plugins with their metrics and statuses in a plain text format.
func (m *Manager) getStatus() (result string) {
var status strings.Builder
agents := make(map[plugin.Accessor]*pluginMetrics)
infos := make([]*pluginMetrics, 0, len(m.plugins))
for _, p := range m.plugins {
if _, ok := agents[p.impl]; !ok {
info := &pluginMetrics{ref: p, metrics: make([]*plugin.Metric, 0)}
infos = append(infos, info)
agents[p.impl] = info
}
}
for _, metric := range plugin.Metrics {
if info, ok := agents[metric.Plugin]; ok {
info.metrics = append(info.metrics, metric)
}
}
sort.Slice(infos, func(i, j int) bool {
return infos[i].ref.name() < infos[j].ref.name()
})
for _, info := range infos {
status.WriteString(fmt.Sprintf("[%s]\nactive: %t\ncapacity: %d/%d\ncheck on start: %d\ntasks: %d\n",
info.ref.name(), info.ref.active(), info.ref.usedCapacity, info.ref.maxCapacity,
info.ref.forceActiveChecksOnStart, len(info.ref.tasks)))
sort.Slice(info.metrics, func(l, r int) bool { return info.metrics[l].Key < info.metrics[r].Key })
for _, metric := range info.metrics {
status.WriteString(metric.Key)
status.WriteString(": ")
status.WriteString(metric.Description)
status.WriteString("\n")
}
status.WriteString("\n")
}
return status.String()
}
// processQuery handles internal queries like list of plugins with their metrics
// (accessed from status page or remote command).
func (m *Manager) processQuery(r *queryRequest) (text string, err error) {
switch r.command {
case "metrics":
return m.getStatus(), nil
default:
return "", errors.New("unknown request")
}
}
|