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 "time" 25 26 "zabbix.com/pkg/uri" 27 "zabbix.com/pkg/zbxerr" 28 29 "zabbix.com/pkg/plugin" 30) 31 32const pluginName = "Memcached" 33 34const hkInterval = 10 35 36// Plugin inherits plugin.Base and store plugin-specific data. 37type Plugin struct { 38 plugin.Base 39 connMgr *ConnManager 40 options PluginOptions 41} 42 43// impl is the pointer to the plugin implementation. 44var impl Plugin 45 46// Export implements the Exporter interface. 47func (p *Plugin) Export(key string, rawParams []string, _ plugin.ContextProvider) (result interface{}, err error) { 48 params, err := metrics[key].EvalParams(rawParams, p.options.Sessions) 49 if err != nil { 50 return nil, err 51 } 52 53 if len(params["User"]+params["Password"]) > maxEntryLen { 54 return nil, zbxerr.ErrorInvalidParams.Wrap( 55 fmt.Errorf("credentials cannot be longer "+"than %d characters", maxEntryLen)) 56 } 57 58 uri, err := uri.NewWithCreds(params["URI"], params["User"], params["Password"], uriDefaults) 59 if err != nil { 60 return nil, err 61 } 62 63 handleMetric := getHandlerFunc(key) 64 if handleMetric == nil { 65 return nil, zbxerr.ErrorUnsupportedMetric 66 } 67 68 result, err = handleMetric(p.connMgr.GetConnection(*uri), params) 69 if err != nil { 70 p.Errf(err.Error()) 71 } 72 73 return result, err 74} 75 76// Start implements the Runner interface and performs initialization when plugin is activated. 77func (p *Plugin) Start() { 78 p.connMgr = NewConnManager( 79 time.Duration(p.options.KeepAlive)*time.Second, 80 time.Duration(p.options.Timeout)*time.Second, 81 hkInterval*time.Second, 82 ) 83} 84 85// Stop implements the Runner interface and frees resources when plugin is deactivated. 86func (p *Plugin) Stop() { 87 p.connMgr.Destroy() 88 p.connMgr = nil 89} 90