Skip to content

Commit

Permalink
Use collector ID as reference to avoid naming conflicts (#262)
Browse files Browse the repository at this point in the history
Fixes #255
  • Loading branch information
Marius Sturm authored and bernd committed Jul 19, 2018
1 parent acb103f commit fde7741
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions api/graylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ func NewStatusRequest() graylog.StatusRequest {
statusRequest := graylog.StatusRequest{Backends: make([]graylog.StatusRequestBackend, 0)}
combined, count := system.GlobalStatus.Status, 0

for name, runner := range daemon.Daemon.Runner {
for id, runner := range daemon.Daemon.Runner {
backendStatus := runner.GetBackend().Status()
statusRequest.Backends = append(statusRequest.Backends, graylog.StatusRequestBackend{
Name: name,
Id: id,
Status: backendStatus.Status,
Message: backendStatus.Message,
})
Expand Down
2 changes: 1 addition & 1 deletion api/graylog/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type NodeDetailsRequest struct {
}

type StatusRequestBackend struct {
Name string `json:"name"`
Id string `json:"collector_id"`
Status int `json:"status"`
Message string `json:"message"`
}
Expand Down
16 changes: 8 additions & 8 deletions backends/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ type backendStore struct {
}

func (bs *backendStore) SetBackend(backend Backend) {
bs.backends[backend.Name] = &backend
bs.backends[backend.Id] = &backend
executeParameters, err := common.SprintfList(backend.ExecuteParameters, backend.ConfigurationPath)
if err != nil {
log.Errorf("Invalid execute parameters, skip adding backend: %s", backend.Name)
return
}
bs.backends[backend.Name].ExecuteParameters = executeParameters
bs.backends[backend.Id].ExecuteParameters = executeParameters
validationParameters, err := common.SprintfList(backend.ValidationParameters, backend.ConfigurationPath)
if err != nil {
log.Errorf("Invalid validation parameters, skip adding backend: %s", backend.Name)
return
}
bs.backends[backend.Name].ValidationParameters = validationParameters
bs.backends[backend.Id].ValidationParameters = validationParameters
}

func (bs *backendStore) GetBackend(name string) *Backend {
return bs.backends[name]
func (bs *backendStore) GetBackend(id string) *Backend {
return bs.backends[id]
}

func (bs *backendStore) GetBackendById(id string) *Backend {
Expand All @@ -66,11 +66,11 @@ func (bs *backendStore) Update(backends []Backend) {
activeIds = append(activeIds, backend.Id)

// add new backend
if bs.backends[backend.Name] == nil {
if bs.backends[backend.Id] == nil {
bs.SetBackend(backend)
// update if settings did change
} else {
if !bs.backends[backend.Name].EqualSettings(&backend) {
if !bs.backends[backend.Id].EqualSettings(&backend) {
bs.SetBackend(backend)
}
}
Expand All @@ -85,7 +85,7 @@ func (bs *backendStore) Cleanup(validBackendIds []string) {
for _, backend := range bs.backends {
if !common.IsInList(backend.Id, validBackendIds) {
log.Debug("Cleaning up backend: " + backend.Name)
delete(bs.backends, backend.Name)
delete(bs.backends, backend.Id)
}
}
}
20 changes: 10 additions & 10 deletions daemon/action_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ func HandleCollectorActions(actions []graylog.ResponseCollectorAction) {
}

func startAction(backend *backends.Backend) {
for name, runner := range Daemon.Runner {
if name == backend.Name {
for id, runner := range Daemon.Runner {
if id == backend.Id {
if !runner.Running() {
log.Infof("[%s] Staring collector", name)
log.Infof("[%s] Staring collector", backend.Name)
runner.Restart()
} else {
log.Infof("Collector [%s] is already running, skipping start action.", name)
log.Infof("Collector [%s] is already running, skipping start action.", backend.Name)
}
}
}
}

func restartAction(backend *backends.Backend) {
for name, runner := range Daemon.Runner {
if name == backend.Name {
log.Infof("[%s] Restarting collector", name)
for id, runner := range Daemon.Runner {
if id == backend.Id {
log.Infof("[%s] Restarting collector", backend.Name)
runner.Restart()
}
}
}

func stopAction(backend *backends.Backend) {
for name, runner := range Daemon.Runner {
if name == backend.Name {
log.Infof("[%s] Stopping collector", name)
for id, runner := range Daemon.Runner {
if id == backend.Id {
log.Infof("[%s] Stopping collector", backend.Name)
runner.Shutdown()
}
}
Expand Down
24 changes: 12 additions & 12 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ func (dc *DaemonConfig) AddRunner(backend backends.Backend, context *context.Ctx
log.Fatalf("Execution driver %s is not supported on this platform", backend.ServiceType)
}
runner.SetDaemon(dc)
dc.Runner[backend.Name] = runner
dc.Runner[backend.Id] = runner
}

func (dc *DaemonConfig) DeleteRunner(backendName string) {
if dc.Runner[backendName] == nil {
func (dc *DaemonConfig) DeleteRunner(backendId string) {
if dc.Runner[backendId] == nil {
return
}

if dc.Runner[backendName].Running() {
if err := dc.Runner[backendName].Shutdown(); err != nil {
log.Errorf("[%s] Failed to stop backend during deletion: %v", backendName, err)
if dc.Runner[backendId].Running() {
if err := dc.Runner[backendId].Shutdown(); err != nil {
log.Errorf("[%s] Failed to stop backend during deletion: %v", backendId, err)
}
}
delete(dc.Runner, backendName)
delete(dc.Runner, backendId)
}

func (dc *DaemonConfig) GetRunnerByBackendId(id string) Runner {
Expand All @@ -112,8 +112,8 @@ func (dc *DaemonConfig) SyncWithAssignments(context *context.Ctx) {
return
}

for name, runner := range dc.Runner {
backend := backends.Store.GetBackend(name)
for id, runner := range dc.Runner {
backend := backends.Store.GetBackend(id)

// update outdated runner backend
runnerBackend := runner.GetBackend()
Expand All @@ -124,15 +124,15 @@ func (dc *DaemonConfig) SyncWithAssignments(context *context.Ctx) {

// cleanup backends that should not run anymore
if backend == nil || assignments.Store.GetAll()[backend.Id] == "" {
log.Info("Removing process runner: " + name)
dc.DeleteRunner(name)
log.Info("Removing process runner: " + backend.Name)
dc.DeleteRunner(id)
}
}

// add new backends to registry
for backendId := range assignments.Store.GetAll() {
backend := backends.Store.GetBackendById(backendId)
if backend != nil && dc.Runner[backend.Name] == nil {
if backend != nil && dc.Runner[backend.Id] == nil {
log.Info("Adding process runner for: " + backend.Name)
dc.AddRunner(*backend, context)
}
Expand Down
1 change: 1 addition & 0 deletions daemon/exec_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (r *ExecRunner) GetBackend() *backends.Backend {

func (r *ExecRunner) SetBackend(b backends.Backend) {
r.backend = b
r.name = b.Name
r.exec = b.ExecutablePath
r.args = b.ExecuteParameters
r.restartCount = 1
Expand Down
1 change: 1 addition & 0 deletions daemon/svc_runner_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (r *SvcRunner) GetBackend() *backends.Backend {

func (r *SvcRunner) SetBackend(b backends.Backend) {
r.backend = b
r.name = b.Name
r.exec = b.ExecutablePath
r.args = b.ExecuteParameters
}
Expand Down
2 changes: 1 addition & 1 deletion services/periodicals.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func checkForUpdateAndRestart(httpClient *http.Client, checksum string, context
continue
}

if err := daemon.Daemon.Runner[backend.Name].Restart(); err != nil {
if err := daemon.Daemon.Runner[backend.Id].Restart(); err != nil {
msg := "Failed to restart collector"
backend.SetStatus(backends.StatusError, msg)
log.Errorf("[%s] %s: %v", backend.Name, msg, err)
Expand Down

0 comments on commit fde7741

Please sign in to comment.