Skip to content

Commit

Permalink
Update NGINX monitor function to handle monitoring multiple NGINX err…
Browse files Browse the repository at this point in the history
…or logs (#282)

Update NGINX monitor function to handle monitoring multiple NGINX error logs
  • Loading branch information
dhurley authored Apr 12, 2023
1 parent 2e75edd commit cdc40dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
19 changes: 11 additions & 8 deletions src/plugins/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,40 +569,43 @@ func (n *Nginx) monitor(processInfo []core.Process) error {

logErrorChannel := make(chan string, len(errorLogs))
pidsChannel := make(chan string)
defer close(logErrorChannel)
defer close(pidsChannel)

go n.monitorLogs(errorLogs, logErrorChannel)
go n.monitorPids(processInfo, pidsChannel)

for i := 0; i < 2; i++ {
// Expect to receive one message from the pidsChannel and a message for each NGINX error log file in the logErrorChannel
numberOfExpectedMessages := 1 + len(errorLogs)

for i := 0; i < numberOfExpectedMessages; i++ {
select {
case err := <-pidsChannel:
log.Trace("pidsChannel finished")
log.Tracef("message received in pidsChannel: %s", err)
if err != "" {
errorsFound = append(errorsFound, err)
}
case err := <-logErrorChannel:
log.Trace("logErrorChannel finished")
log.Tracef("message received in logErrorChannel: %s", err)
if err != "" {
errorsFound = append(errorsFound, err)
}
}
}

defer close(logErrorChannel)
defer close(pidsChannel)

log.Info("Finished monitoring post reload")

if len(errorsFound) > 0 {
return errors.New(errorsFound[0])
}

return nil
}

func (n *Nginx) monitorLogs(errorLogs map[string]string, errorChannel chan string) {
if len(errorLogs) == 0 {
log.Trace("No logs so returning monitoring of logs")
errorChannel <- ""
return
}

for _, logFile := range errorLogs {
Expand Down Expand Up @@ -665,7 +668,7 @@ func parseIntList(data []core.Process) []int {
func (n *Nginx) tailLog(logFile string, errorChannel chan string) {
t, err := tailer.NewTailer(logFile)
if err != nil {
log.Errorf("Unable to tail %q: %v", logFile, err)
log.Errorf("Unable to tail error log %q after NGINX reload: %v", logFile, err)
// this is not an error in the logs, ignoring tailing
errorChannel <- ""
return
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cdc40dc

Please sign in to comment.