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 mysql
21
22import (
23	"zabbix.com/pkg/conf"
24	"zabbix.com/pkg/plugin"
25)
26
27// Session is a general structure for storing sessions' configuration.
28type Session struct {
29	URI         string `conf:"name=Uri,optional"`
30	Password    string `conf:"optional"`
31	User        string `conf:"optional"`
32	TLSConnect  string `conf:"name=TLSConnect,optional"`
33	TLSCAFile   string `conf:"name=TLSCAFile,optional"`
34	TLSCertFile string `conf:"name=TLSCertFile,optional"`
35	TLSKeyFile  string `conf:"name=TLSKeyFile,optional"`
36}
37
38// PluginOptions option from config file
39type PluginOptions struct {
40	// Timeout is the maximum time in seconds for waiting when a connection has to be established.
41	// Default value equals to the global timeout.
42	Timeout int `conf:"optional,range=1:30"`
43
44	// CallTimeout is the maximum time in seconds for waiting when a request has to be done.
45	// Default value equals to the global agent timeout.
46	CallTimeout int `conf:"optional,range=1:30"`
47
48	// KeepAlive is a time to wait before unused connections will be closed.
49	KeepAlive int `conf:"optional,range=60:900,default=300"`
50
51	// Sessions stores pre-defined named sets of connections settings.
52	Sessions map[string]Session `conf:"optional"`
53}
54
55// Configure implements the Configurator interface.
56// Initializes configuration structures.
57func (p *Plugin) Configure(global *plugin.GlobalOptions, options interface{}) {
58	if err := conf.Unmarshal(options, &p.options); err != nil {
59		p.Errf("cannot unmarshal configuration options: %s", err)
60	}
61
62	if p.options.Timeout == 0 {
63		p.options.Timeout = global.Timeout
64	}
65
66	if p.options.CallTimeout == 0 {
67		p.options.CallTimeout = global.Timeout
68	}
69}
70
71// Validate implements the Configurator interface.
72// Returns an error if validation of a plugin's configuration is failed.
73func (p *Plugin) Validate(options interface{}) error {
74	var opts PluginOptions
75
76	return conf.Unmarshal(options, &opts)
77}
78