1/*
2** Zabbix
3** Copyright (C) 2001-2021 Zabbix SIA
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18**/
19
20package memcached
21
22import (
23	"zabbix.com/pkg/metric"
24	"zabbix.com/pkg/plugin"
25	"zabbix.com/pkg/uri"
26)
27
28// handlerFunc defines an interface must be implemented by handlers.
29type handlerFunc func(conn MCClient, params map[string]string) (res interface{}, err error)
30
31// getHandlerFunc returns a handlerFunc related to a given key.
32func getHandlerFunc(key string) handlerFunc {
33	switch key {
34	case keyStats:
35		return statsHandler // memcached.stats[[connString][,user][,password][,type]]
36
37	case keyPing:
38		return pingHandler // memcached.ping[[connString][,user][,password]]
39
40	default:
41		return nil
42	}
43}
44
45const (
46	keyPing  = "memcached.ping"
47	keyStats = "memcached.stats"
48)
49
50// https://github.com/memcached/memcached/blob/master/sasl_defs.c#L26
51var maxEntryLen = 252
52var uriDefaults = &uri.Defaults{Scheme: "tcp", Port: "11211"}
53
54// Common params: [URI|Session][,User][,Password]
55var (
56	paramURI = metric.NewConnParam("URI", "URI to connect or session name.").
57			WithDefault(uriDefaults.Scheme + "://localhost:" + uriDefaults.Port).WithSession().
58			WithValidator(uri.URIValidator{Defaults: uriDefaults, AllowedSchemes: []string{"tcp", "unix"}})
59	paramUser     = metric.NewConnParam("User", "Memcached user.").WithDefault("")
60	paramPassword = metric.NewConnParam("Password", "User's password.").WithDefault("")
61)
62
63var metrics = metric.MetricSet{
64	keyPing: metric.New("Test if connection is alive or not.",
65		[]*metric.Param{paramURI, paramUser, paramPassword}, false),
66
67	keyStats: metric.New("Returns output of stats command.",
68		[]*metric.Param{paramURI, paramUser, paramPassword,
69			metric.NewParam("Type", "One of supported stat types: items, sizes, slabs and settings. "+
70				"Empty by default (returns general statistics).").WithDefault(statsTypeGeneral).
71				WithValidator(metric.SetValidator{
72					Set:             []string{statsTypeGeneral, statsTypeItems, statsTypeSizes, statsTypeSlabs, statsTypeSettings},
73					CaseInsensitive: true,
74				}),
75		}, false),
76}
77
78func init() {
79	plugin.RegisterMetrics(&impl, pluginName, metrics.List()...)
80}
81