bashpool

package
v0.0.0-...-7301b9a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2021 License: MIT Imports: 14 Imported by: 0

README

BashPool

A service to provide a pool of shells of linux bash to run scripts in designed linux user accounts, and relay stdout to output plugin

Why write this plugin other than using the original "exec" plugin

  • "exec" creates a sh process and releases it each interval. When the script needs su to another user to run, and the environment preparation of the su-ed user is complicated, it will take a long time.
  • BashPool adopts a strategy like a database pool. It starts bash shell with specific user environment, and recept script sent by defined items, and run and return output.

config

There are 2 kinds of object definitions for this plugin:

  • user_shell: a unique id, start bash by which os user.
  • cmd_define: a unique id, run on which user_shell, the cmd line content, gathering interval in seconds.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BashPool

type BashPool struct {
	OutCh          chan MSGLine
	ConfUserShells []*UserShell          `toml:"user_shell"`
	ConfCmdDefs    []*CmdDef             `toml:"cmd_define"`
	UserShells     map[string]*UserShell //key by id
	CmdDefs        map[string]*CmdDef

	Log telegraf.Logger

	ScriptDir string
	// contains filtered or unexported fields
}

BashPool is an input plugin that collects external metrics sent via HTTP

func (*BashPool) Description

func (B *BashPool) Description() string

Description -- input interface function

func (*BashPool) Gather

func (B *BashPool) Gather(_ telegraf.Accumulator) error

Gather -- input interface function. Not use it in this service.

func (*BashPool) SampleConfig

func (B *BashPool) SampleConfig() string

SampleConfig -- input interface function

func (*BashPool) SetParser

func (B *BashPool) SetParser(parser parsers.Parser)

SetParser -- invoked by output plugin

func (*BashPool) StartContext

func (B *BashPool) StartContext(ctx context.Context, acc telegraf.Accumulator) error

StartContext -- start the service. invoked by telegraf framwork

func (*BashPool) Stop

func (B *BashPool) Stop()

Stop cleans up all resources

type CmdDef

type CmdDef struct {
	ID          string   `toml:"id"` //unique
	ShellID     string   `toml:"shell_id"`
	IntervalSec uint     `toml:"interval_second"`
	CmdLine     string   `toml:"cmd_line"`
	Tags        []string `toml:"tags"`
	// contains filtered or unexported fields
}

CmdDef -- bash script define

type MSGLine

type MSGLine struct {
	CmdDefID string
	Type     MSGType
	Line     string
}

MSGLine -- for sending msg to telegraf output or internal communication

type MSGType

type MSGType byte

MSGType -- for send msg by channels of different kinds

const (
	//OTStdout -- out type for stdout. in current, only support influxdb format
	OTStdout MSGType = iota
	//OTStderr -- out type for stderr. maybe will not be used
	OTStderr
	//OTEOFStdOut -- end of stdout after finish executing command
	OTEOFStdOut
	//OTEOFStdErr -- end of stderr after finish executing command
	OTEOFStdErr
	//OTProbe -- for probe to make sure shell is working
	OTProbe

	//ProbeStr --
	ProbeStr = `!@#PROBE#@!`

	SEPERATOR = `[@S#P#T@]`
)

type UserShell

type UserShell struct {
	ID       string `toml:"id"`
	OSUser   string `toml:"os_user"`
	Cmd      *exec.Cmd
	CmdReady bool //ok after su - user

	StdinPipe  io.WriteCloser
	StdoutPipe io.ReadCloser
	StderrPipe io.ReadCloser

	*sync.Mutex
	// contains filtered or unexported fields
}

UserShell -- exec.Cmd with os user

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL