|
| 1 | +package collect |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strconv" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/pkg/errors" |
| 14 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 15 | + "k8s.io/klog/v2" |
| 16 | +) |
| 17 | + |
| 18 | +type CollectHostJournald struct { |
| 19 | + hostCollector *troubleshootv1beta2.HostJournald |
| 20 | + BundlePath string |
| 21 | +} |
| 22 | + |
| 23 | +const HostJournaldPath = `host-collectors/journald/` |
| 24 | + |
| 25 | +func (c *CollectHostJournald) Title() string { |
| 26 | + return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "journald") |
| 27 | +} |
| 28 | + |
| 29 | +func (c *CollectHostJournald) IsExcluded() (bool, error) { |
| 30 | + return isExcluded(c.hostCollector.Exclude) |
| 31 | +} |
| 32 | + |
| 33 | +func (c *CollectHostJournald) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { |
| 34 | + |
| 35 | + // collector name check |
| 36 | + collectorName := c.hostCollector.CollectorName |
| 37 | + if collectorName == "" { |
| 38 | + return nil, errors.New("collector name is required") |
| 39 | + } |
| 40 | + |
| 41 | + // timeout check |
| 42 | + timeout, err := getTimeout(c.hostCollector.Timeout) |
| 43 | + if err != nil { |
| 44 | + return nil, errors.Wrap(err, "failed to parse timeout") |
| 45 | + } |
| 46 | + |
| 47 | + // set timeout context |
| 48 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 49 | + defer cancel() |
| 50 | + |
| 51 | + // prepare command options |
| 52 | + cmdOptions, err := generateOptions(c.hostCollector) |
| 53 | + if err != nil { |
| 54 | + return nil, errors.Wrap(err, "failed to generate journalctl options") |
| 55 | + } |
| 56 | + |
| 57 | + // run journalctl and capture output |
| 58 | + klog.V(2).Infof("Running journalctl with options: %v", cmdOptions) |
| 59 | + var stdout, stderr bytes.Buffer |
| 60 | + cmd := exec.CommandContext(ctx, "journalctl", cmdOptions...) |
| 61 | + cmd.Stdout = &stdout |
| 62 | + cmd.Stderr = &stderr |
| 63 | + |
| 64 | + cmdInfo := HostRunInfo{ |
| 65 | + Command: cmd.String(), |
| 66 | + ExitCode: "0", |
| 67 | + } |
| 68 | + |
| 69 | + if err := cmd.Run(); err != nil { |
| 70 | + klog.V(2).Infof("journalctl command failed: %v", err) |
| 71 | + if err == context.DeadlineExceeded { |
| 72 | + cmdInfo.ExitCode = "124" |
| 73 | + cmdInfo.Error = fmt.Sprintf("command timed out after %s", timeout.String()) |
| 74 | + } else if exitError, ok := err.(*exec.ExitError); ok { |
| 75 | + cmdInfo.ExitCode = strconv.Itoa(exitError.ExitCode()) |
| 76 | + cmdInfo.Error = stderr.String() |
| 77 | + } else { |
| 78 | + return nil, errors.Wrap(err, "failed to run journalctl") |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + output := NewResult() |
| 83 | + |
| 84 | + // write info file |
| 85 | + infoJsonBytes, err := json.Marshal(cmdInfo) |
| 86 | + if err != nil { |
| 87 | + return nil, errors.Wrap(err, "failed to marshal journalctl info result") |
| 88 | + } |
| 89 | + infoFileName := getOutputInfoFile(collectorName) |
| 90 | + output.SaveResult(c.BundlePath, infoFileName, bytes.NewBuffer(infoJsonBytes)) |
| 91 | + |
| 92 | + // write actual journalctl output |
| 93 | + outputFileName := getOutputFile(collectorName) |
| 94 | + klog.V(2).Infof("Saving journalctl output to %q in bundle", outputFileName) |
| 95 | + output.SaveResult(c.BundlePath, outputFileName, bytes.NewBuffer(stdout.Bytes())) |
| 96 | + |
| 97 | + return output, nil |
| 98 | +} |
| 99 | + |
| 100 | +func generateOptions(jd *troubleshootv1beta2.HostJournald) ([]string, error) { |
| 101 | + options := []string{} |
| 102 | + |
| 103 | + if jd.System { |
| 104 | + options = append(options, "--system") |
| 105 | + } |
| 106 | + |
| 107 | + if jd.Dmesg { |
| 108 | + options = append(options, "--dmesg") |
| 109 | + } |
| 110 | + |
| 111 | + for _, unit := range jd.Units { |
| 112 | + options = append(options, "-u", unit) |
| 113 | + } |
| 114 | + |
| 115 | + if jd.Since != "" { |
| 116 | + options = append(options, "--since", jd.Since) |
| 117 | + } |
| 118 | + |
| 119 | + if jd.Until != "" { |
| 120 | + options = append(options, "--until", jd.Until) |
| 121 | + } |
| 122 | + |
| 123 | + if jd.Output != "" { |
| 124 | + options = append(options, "--output", jd.Output) |
| 125 | + } |
| 126 | + |
| 127 | + if jd.Lines > 0 { |
| 128 | + options = append(options, "-n", strconv.Itoa(jd.Lines)) |
| 129 | + } |
| 130 | + |
| 131 | + if jd.Reverse { |
| 132 | + options = append(options, "--reverse") |
| 133 | + } |
| 134 | + |
| 135 | + if jd.Utc { |
| 136 | + options = append(options, "--utc") |
| 137 | + } |
| 138 | + |
| 139 | + // opinionated on --no-pager |
| 140 | + options = append(options, "--no-pager") |
| 141 | + |
| 142 | + return options, nil |
| 143 | +} |
| 144 | + |
| 145 | +func getOutputFile(collectorName string) string { |
| 146 | + return filepath.Join(HostJournaldPath, collectorName+".txt") |
| 147 | +} |
| 148 | + |
| 149 | +func getOutputInfoFile(collectorName string) string { |
| 150 | + return filepath.Join(HostJournaldPath, collectorName+"-info.json") |
| 151 | +} |
| 152 | + |
| 153 | +func getTimeout(timeout string) (time.Duration, error) { |
| 154 | + if timeout == "" { |
| 155 | + return 30 * time.Second, nil |
| 156 | + } |
| 157 | + |
| 158 | + return time.ParseDuration(timeout) |
| 159 | +} |
0 commit comments