/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
package external
import (
"bytes"
"errors"
"fmt"
"net"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"golang.zabbix.com/sdk/conf"
"golang.zabbix.com/sdk/errs"
"golang.zabbix.com/sdk/log"
"golang.zabbix.com/sdk/plugin"
"golang.zabbix.com/sdk/plugin/comms"
)
var (
_ plugin.Runner = (*Plugin)(nil)
_ plugin.Configurator = (*Plugin)(nil)
_ plugin.Exporter = (*Plugin)(nil)
)
// startLock is used to ensure that only one plugin is started at a time.
// view startPlugin method for more details.
var startLock sync.Mutex //nolint:gochecknoglobals
// Plugin represents an external plugin.
type Plugin struct {
plugin.Base
Path string
name string // name of plugin
socket string
interfaces uint32
listener net.Listener
timeout time.Duration
cmd *exec.Cmd
cmdWait chan error // cmd.Wait() result
pluginStopped chan struct{} // triggered by plugin.Stop() request
broker *pluginBroker
logr log.Logger
}
// NewPlugin created a new external plugin accessor instance.
func NewPlugin(
name, path, socket string,
timeout time.Duration,
listener net.Listener,
) *Plugin {
base := plugin.Base{
Logger: log.New(name),
}
base.SetExternal(true)
base.SetHandleTimeout(true)
return &Plugin{
Base: base,
name: name,
Path: path,
socket: socket,
listener: listener,
timeout: timeout,
cmdWait: make(chan error),
pluginStopped: make(chan struct{}),
logr: base.Logger, // set as a neat separate field.
}
}
// RegisterMetrics starts the plugin process, sends register and validate
// (if supported by plugin) requests and stops plugin.
func (p *Plugin) RegisterMetrics(config any) error {
pluginExit, err := p.startPlugin(true)
if err != nil {