send tags and environment with persist module config

This commit is contained in:
Aaron Meihm 2017-09-06 13:57:18 -05:00
Родитель dbb9fcce39
Коммит bcc795666f
6 изменённых файлов: 49 добавлений и 24 удалений

Просмотреть файл

@ -205,7 +205,8 @@ func managePersistModule(ctx *Context, name string) {
// The module is now running, send any configuration parameters we have
// to it.
cm, err := modules.MakeMessageConfig(cfg)
cm, err := modules.MakeMessageConfig(cfg, ctx.Agent.Hostname,
ctx.Agent.Env, ctx.Agent.Tags)
if err != nil {
// This should never happen, but if it does we will just
// kill the executing module as we are unable to send any

Просмотреть файл

@ -48,13 +48,18 @@ func buildResults(e elements, r *modules.Result) (buf []byte, err error) {
var logChan chan string
var alertChan chan string
var handlerErrChan chan error
var configChan chan []byte
var configChan chan modules.ConfigParams
func moduleMain() {
var cfg config
incfg := <-configChan
err := json.Unmarshal(incfg, &cfg)
buf, err := json.Marshal(incfg.Config)
if err != nil {
handlerErrChan <- err
return
}
err = json.Unmarshal(buf, &cfg)
if err != nil {
handlerErrChan <- err
return
@ -111,7 +116,7 @@ func (r *run) RunPersist(in modules.ModuleReader, out modules.ModuleWriter) {
logChan = make(chan string, 64)
regChan := make(chan string, 64)
handlerErrChan = make(chan error, 64)
configChan = make(chan []byte, 1)
configChan = make(chan modules.ConfigParams, 1)
go moduleMain()
l, spec, err := modules.GetPersistListener("audit")

Просмотреть файл

@ -48,7 +48,7 @@ func buildResults(e elements, r *modules.Result) (buf []byte, err error) {
var logChan chan string
var alertChan chan string
var handlerErrChan chan error
var configChan chan []byte
var configChan chan modules.ConfigParams
// messageBuf is a queue used to store incoming messages, and is drained by
// runDispatch
@ -58,7 +58,12 @@ func moduleMain() {
var cfg config
incfg := <-configChan
err := json.Unmarshal(incfg, &cfg)
buf, err := json.Marshal(incfg.Config)
if err != nil {
handlerErrChan <- err
return
}
err = json.Unmarshal(buf, &cfg)
if err != nil {
handlerErrChan <- err
return
@ -140,7 +145,7 @@ func (r *run) RunPersist(in modules.ModuleReader, out modules.ModuleWriter) {
logChan = make(chan string, 64)
regChan := make(chan string, 64)
handlerErrChan = make(chan error, 64)
configChan = make(chan []byte, 1)
configChan = make(chan modules.ConfigParams, 1)
go moduleMain()
l, spec, err := modules.GetPersistListener("dispatch")

Просмотреть файл

@ -67,8 +67,8 @@ var alertChan chan string
var handlerErrChan chan error
// When the agent sends the persistent module it's configuration, it will come in via
// the config channel as a JSON byte slice so we can unmarshal it into our configuration
var configChan chan []byte
// the config channel as a ConfigParams type so we can load it into our configuration
var configChan chan modules.ConfigParams
// An example background task the module will execute while it is being supervised by
// the agent. This example just logs the current time up to the agent every 30
@ -81,7 +81,12 @@ func runSomeTasks() {
// which we can read immediately here. The configuration will come in via
// configChan as a JSON document, which we unmarshal into our config struct.
incfg := <-configChan
err := json.Unmarshal(incfg, &cfg)
buf, err := json.Marshal(incfg.Config)
if err != nil {
handlerErrChan <- err
return
}
err = json.Unmarshal(buf, &cfg)
if err != nil {
handlerErrChan <- err
return
@ -178,7 +183,7 @@ func (r *run) RunPersist(in modules.ModuleReader, out modules.ModuleWriter) {
// and the module to exit.
handlerErrChan = make(chan error, 64)
// Create a config channel we will read our configuration from.
configChan = make(chan []byte, 1)
configChan = make(chan modules.ConfigParams, 1)
// Initialize the alert channel
alertChan = make(chan string, 64)
// Start up an example background task we want our module to run

Просмотреть файл

@ -41,7 +41,7 @@ func buildResults(e elements, r *modules.Result) (buf []byte, err error) {
var logChan chan string
var agentAlertChan chan string
var handlerErrChan chan error
var configChan chan []byte
var configChan chan modules.ConfigParams
var alertChan chan Alert
@ -95,7 +95,12 @@ func moduleMain() {
alertChan = make(chan Alert, 16)
incfg := <-configChan
err := json.Unmarshal(incfg, &cfg)
buf, err := json.Marshal(incfg.Config)
if err != nil {
handlerErrChan <- err
return
}
err = json.Unmarshal(buf, &cfg)
if err != nil {
handlerErrChan <- err
return
@ -143,7 +148,7 @@ func (r *run) RunPersist(in modules.ModuleReader, out modules.ModuleWriter) {
agentAlertChan = make(chan string, 64)
regChan := make(chan string, 64)
handlerErrChan = make(chan error, 64)
configChan = make(chan []byte, 1)
configChan = make(chan modules.ConfigParams, 1)
go moduleMain()
l, spec, err := modules.GetPersistListener("fswatch")

Просмотреть файл

@ -64,7 +64,10 @@ type RegParams struct {
}
type ConfigParams struct {
Config interface{} `json:"config"`
Config interface{} `json:"config"`
Hostname string `json:"hostname"`
Env interface{} `json:"environment"`
Tags map[string]string `json:"tags"`
}
// AlertParams describes the parameters used in an alert message
@ -206,8 +209,14 @@ func MakeMessageRegister(spec string) (rawMsg []byte, err error) {
}
// Creates a new message of class config.
func MakeMessageConfig(cfgdata interface{}) (rawMsg []byte, err error) {
param := ConfigParams{Config: cfgdata}
func MakeMessageConfig(cfgdata interface{}, hostname string, env interface{},
tags map[string]string) (rawMsg []byte, err error) {
param := ConfigParams{
Config: cfgdata,
Hostname: hostname,
Env: env,
Tags: tags,
}
msg := Message{Class: MsgClassConfig, Parameters: param}
rawMsg, err = json.Marshal(&msg)
if err != nil {
@ -377,7 +386,7 @@ func RegisterDispatchFunction(f func(string)) {
// RunPersist function. Looks after replying to ping messages, writing logs, and other
// communication between the agent and the running persistent module.
func DefaultPersistHandlers(in ModuleReader, out ModuleWriter, logch chan string,
errch chan error, regch chan string, alertch chan string, confch chan []byte) {
errch chan error, regch chan string, alertch chan string, confch chan ConfigParams) {
inChan := make(chan Message, 0)
go func() {
for {
@ -470,12 +479,7 @@ func DefaultPersistHandlers(in ModuleReader, out ModuleWriter, logch chan string
failed = true
break
}
buf, err = json.Marshal(confparam.Config)
if err != nil {
failed = true
break
}
confch <- buf
confch <- confparam
case "alert":
if dispatchFunc != nil {
var alparam AlertParams