Skip to content

Commit

Permalink
Capture the logs from stderr of custom plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
abansal4032 committed Jul 25, 2020
1 parent 061e977 commit ec0fe73
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions pkg/custompluginmonitor/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package plugin
import (
"context"
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"
"sync"
Expand All @@ -30,6 +32,10 @@ import (
"k8s.io/node-problem-detector/pkg/util/tomb"
)

// maxCustomPluginBufferBytes is the max bytes that a custom plugin is allowed to
// send to stdout/stderr. Any bytes exceeding this value will be truncated.
const maxCustomPluginBufferBytes = 1024 * 4

type Plugin struct {
config cpmtypes.CustomPluginConfig
syncChan chan struct{}
Expand Down Expand Up @@ -127,14 +133,50 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
defer cancel()

cmd := exec.CommandContext(ctx, rule.Path, rule.Args...)
stdout, err := cmd.Output()

stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
glog.Errorf("Error creating stdout pipe for plugin %q: error - %v", rule.Path, err)
return cpmtypes.Unknown, "Error creating stdout pipe for plugin. Please check the error log"
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
glog.Errorf("Error creating stderr pipe for plugin %q: error - %v", rule.Path, err)
return cpmtypes.Unknown, "Error creating stderr pipe for plugin. Please check the error log"
}
stdoutReader := io.LimitReader(stdoutPipe, maxCustomPluginBufferBytes)
stderrReader := io.LimitReader(stderrPipe, maxCustomPluginBufferBytes)

if err := cmd.Start(); err != nil {
glog.Errorf("Error in starting plugin %q: error - %v", rule.Path, err)
return cpmtypes.Unknown, "Error in starting plugin. Please check the error log"
}

stdout, err := ioutil.ReadAll(stdoutReader)
if err != nil {
glog.Errorf("Error reading stdout for plugin %q: error - %v", rule.Path, err)
return cpmtypes.Unknown, "Error reading stdout for plugin. Please check the error log"
}

stderr, err := ioutil.ReadAll(stderrReader)
if err != nil {
glog.Errorf("Error reading stderr for plugin %q: error - %v", rule.Path, err)
return cpmtypes.Unknown, "Error reading stderr for plugin. Please check the error log"
}

if err := cmd.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); !ok {
glog.Errorf("Error in running plugin %q: error - %v. output - %q", rule.Path, err, string(stdout))
return cpmtypes.Unknown, "Error in running plugin. Please check the error log"
glog.Errorf("Error in waiting for plugin %q: error - %v. output - %q", rule.Path, err, string(stdout))
return cpmtypes.Unknown, "Error in waiting for plugin. Please check the error log"
}
}

// log the stderr from the plugin
if len(stderr) != 0 {
glog.Infof("Start logs from plugin %q \n %s\n", rule.Path, string(stderr))
glog.Infof("End logs from plugin %q\n", rule.Path)
}

// trim suffix useless bytes
output = string(stdout)
output = strings.TrimSpace(output)
Expand Down

0 comments on commit ec0fe73

Please sign in to comment.