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	"fmt"
24
25	"zabbix.com/pkg/conf"
26	"zabbix.com/pkg/plugin"
27)
28
29type PluginOptions struct {
30	// Timeout is the maximum time for waiting when a request has to be done. Default value equals the global timeout.
31	Timeout int `conf:"optional,range=1:30"`
32
33	// KeepAlive is a time to wait before unused connections will be closed.
34	KeepAlive int `conf:"optional,range=60:900,default=300"`
35
36	// Sessions stores pre-defined named sets of connections settings.
37	Sessions map[string]conf.Session `conf:"optional"`
38}
39
40// Configure implements the Configurator interface.
41// Initializes configuration structures.
42func (p *Plugin) Configure(global *plugin.GlobalOptions, options interface{}) {
43	if err := conf.Unmarshal(options, &p.options); err != nil {
44		p.Errf("cannot unmarshal configuration options: %s", err)
45	}
46
47	if p.options.Timeout == 0 {
48		p.options.Timeout = global.Timeout
49	}
50}
51
52// Validate implements the Configurator interface.
53// Returns an error if validation of a plugin's configuration is failed.
54func (p *Plugin) Validate(options interface{}) error {
55	var (
56		opts PluginOptions
57		err  error
58	)
59
60	err = conf.Unmarshal(options, &opts)
61	if err != nil {
62		return err
63	}
64
65	for name, session := range opts.Sessions {
66		if len(session.Password+session.User) > maxEntryLen {
67			return fmt.Errorf("invalid parameters for session '%s': credentials cannot be longer "+
68				"than %d characters", name, maxEntryLen)
69		}
70	}
71
72	return err
73}
74