File: registry.go

package info (click to toggle)
zabbix 1%3A7.0.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 272,688 kB
  • sloc: sql: 946,050; ansic: 389,440; php: 292,698; javascript: 83,388; sh: 5,680; makefile: 3,285; java: 1,420; cpp: 694; perl: 64; xml: 56
file content (213 lines) | stat: -rw-r--r-- 5,302 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
** documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all copies or substantial portions
** of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
** WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
** COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**/

package plugin

import (
	"reflect"
	"regexp"
	"unicode"

	"golang.zabbix.com/sdk/errs"
)

type Metric struct {
	Plugin      Accessor
	Key         string
	Description string
	UsrPrm      bool
}

var (
	Metrics map[string]*Metric  = make(map[string]*Metric)
	Plugins map[string]Accessor = make(map[string]Accessor)

	metricKeyRe = regexp.MustCompile(`^[A-Za-z0-9\._-]+$`)
)

// RegisterMetrics registers metrics for the given plugin.
func RegisterMetrics(impl Accessor, name string, params ...string) error {
	if len(params) < 2 {
		return errs.New("expected at least one metric and its description")
	}

	if len(params)%2 != 0 {
		return errs.New(
			"expected even number of metric and description parameters",
		)
	}

	for i := 0; i < len(params); i += 2 {
		err := registerMetric(impl, name, params[i], params[i+1])
		if err != nil {
			return err
		}
	}

	return nil
}

func Get(key string) (acc Accessor, err error) {
	if m, ok := Metrics[key]; ok {
		return m.Plugin, nil
	}

	return nil, UnsupportedMetricError
}

func ClearRegistry() {
	Metrics = make(map[string]*Metric)
	Plugins = make(map[string]Accessor)
}

func GetByName(name string) (acc Accessor, err error) {
	if p, ok := Plugins[name]; ok {
		return p, nil
	}

	return nil, errs.Errorf("unknown plugin %q", name)
}

func ClearUserParamMetrics() map[string]*Metric {
	metricsFallback := make(map[string]*Metric)

	for key, metric := range Metrics {
		if metric.UsrPrm {
			metricsFallback[key] = metric

			delete(Metrics, key)
		}
	}

	return metricsFallback
}

func RestoreUserParamMetrics(metrics map[string]*Metric) {
	for key, metric := range metrics {
		Metrics[key] = metric
	}
}

func registerMetric(plugin Accessor, name, key, description string) error {
	var usrprm bool

	ok := metricKeyRe.MatchString(key)
	if !ok {
		return errs.Errorf(`cannot register metric "%s" having invalid format`, key)
	}

	if description == "" {
		return errs.Errorf(`cannot register metric "%s" with empty description`, key)
	}

	if unicode.IsLower([]rune(description)[0]) {
		return errs.Errorf(
			`cannot register metric "%s" with description without capital first letter: "%s"`,
			key,
			description,
		)
	}

	if description[len(description)-1] != '.' {
		return errs.Errorf(
			"cannot register metric %q without dot at the end of description: %q",
			key, description,
		)
	}

	if _, ok := Metrics[key]; ok {
		return errs.Errorf("cannot register duplicate metric %q", key)
	}

	t := reflect.TypeOf(plugin)
	for i := 0; i < t.NumMethod(); i++ {
		method := t.Method(i)
		switch method.Name {
		case "Export":
			if _, ok := plugin.(Exporter); !ok {
				return errs.Errorf(
					"the %q plugin has %s method, but does implement Exporter interface",
					name,
					method.Name,
				)
			}
		case "Collect", "Period":
			if _, ok := plugin.(Collector); !ok {
				return errs.Errorf(
					"the %q plugin has %s method, but does not implement Collector interface",
					name,
					method.Name,
				)
			}
		case "Watch":
			if _, ok := plugin.(Watcher); !ok {
				return errs.Errorf(
					"the %q plugin has %s method, but does not implement Watcher interface",
					name,
					method.Name,
				)
			}
		case "Configure", "Validate":
			if _, ok := plugin.(Configurator); !ok {
				return errs.Errorf(
					"the %q plugin has %s method, but does not implement Configurator interface",
					name,
					method.Name,
				)
			}
		case "Start", "Stop":
			if _, ok := plugin.(Runner); !ok {
				return errs.Errorf(
					"the %q plugin has %s method, but does not implement Runner interface",
					name,
					method.Name,
				)
			}
		}
	}

	switch plugin.(type) {
	case Exporter, Collector, Runner, Watcher, Configurator:
	default:
		return errs.Errorf("plugin %q does not implement any plugin interfaces", name)
	}

	if p, ok := Plugins[name]; ok {
		if p != plugin {
			return errs.Errorf("plugin name %q has been already registered by other plugin", name)
		}
	} else {
		Plugins[name] = plugin
		plugin.Init(name)
	}

	if name == "UserParameter" {
		usrprm = true
	} else {
		usrprm = false
	}

	Metrics[key] = &Metric{
		Plugin:      plugin,
		Key:         key,
		Description: description,
		UsrPrm:      usrprm,
	}

	return nil
}