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 redis
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 redisClient, 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 keyConfig:
35		return configHandler
36
37	case keyInfo:
38		return infoHandler
39
40	case keyPing:
41		return pingHandler
42
43	case keySlowlog:
44		return slowlogHandler
45
46	default:
47		return nil
48	}
49}
50
51const (
52	keyConfig  = "redis.config"
53	keyInfo    = "redis.info"
54	keyPing    = "redis.ping"
55	keySlowlog = "redis.slowlog.count"
56)
57
58var maxAuthPassLen = 512
59var uriDefaults = &uri.Defaults{Scheme: "tcp", Port: "6379"}
60
61// Common params: [URI|Session][,User][,Password]
62var (
63	paramURI = metric.NewConnParam("URI", "URI to connect or session name.").
64			WithDefault(uriDefaults.Scheme + "://localhost:" + uriDefaults.Port).WithSession().
65			WithValidator(uri.URIValidator{Defaults: uriDefaults, AllowedSchemes: []string{"tcp", "unix"}})
66	paramPassword = metric.NewConnParam("Password", "Redis password.").WithDefault("").
67			WithValidator(metric.LenValidator{Max: &maxAuthPassLen})
68)
69
70var metrics = metric.MetricSet{
71	keyConfig: metric.New("Returns configuration parameters of Redis server.",
72		[]*metric.Param{paramURI, paramPassword,
73			metric.NewParam("Pattern", "Glob-style pattern to filter configuration parameters.").
74				WithDefault("*"),
75		}, false),
76
77	keyInfo: metric.New("Returns output of INFO command.",
78		[]*metric.Param{paramURI, paramPassword,
79			metric.NewParam("Section", "Section of information to return.").WithDefault("default"),
80		}, false),
81
82	keyPing: metric.New("Test if connection is alive or not.",
83		[]*metric.Param{paramURI, paramPassword}, false),
84
85	keySlowlog: metric.New("Returns the number of slow log entries since Redis has been started.",
86		[]*metric.Param{paramURI, paramPassword}, false),
87}
88
89func init() {
90	plugin.RegisterMetrics(&impl, pluginName, metrics.List()...)
91}
92