|
| 1 | +//go:build windows |
| 2 | +// +build windows |
| 3 | + |
| 4 | +package checkups |
| 5 | + |
| 6 | +import ( |
| 7 | + "archive/zip" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/kolide/launcher/ee/agent" |
| 16 | + "github.com/kolide/launcher/ee/allowedcmd" |
| 17 | +) |
| 18 | + |
| 19 | +type intuneCheckup struct { |
| 20 | + summary string |
| 21 | +} |
| 22 | + |
| 23 | +func (i *intuneCheckup) Name() string { |
| 24 | + return "Intune" |
| 25 | +} |
| 26 | + |
| 27 | +func (i *intuneCheckup) Run(ctx context.Context, extraWriter io.Writer) error { |
| 28 | + // Other areas of interest: https://learn.microsoft.com/en-us/mem/intune/remote-actions/collect-diagnostics |
| 29 | + |
| 30 | + zipWriter := zip.NewWriter(extraWriter) |
| 31 | + defer zipWriter.Close() |
| 32 | + |
| 33 | + if err := agentLogs(zipWriter); err != nil { |
| 34 | + i.summary += fmt.Sprintf("Failed to collect Intune agent logs: %v. ", err) |
| 35 | + } |
| 36 | + |
| 37 | + if err := installLogs(zipWriter); err != nil { |
| 38 | + i.summary += fmt.Sprintf("Failed to collect Intune install logs: %v. ", err) |
| 39 | + } |
| 40 | + |
| 41 | + if err := diagnostics(ctx, zipWriter); err != nil { |
| 42 | + i.summary += fmt.Sprintf("Failed to collect Intune diagnostics: %v. ", err) |
| 43 | + } |
| 44 | + |
| 45 | + i.summary = strings.TrimSpace(i.summary) |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func agentLogs(zipWriter *zip.Writer) error { |
| 51 | + agentLogsPathPattern := filepath.Join(os.Getenv("SYSTEMROOT"), "ProgramData", "Microsoft", "IntuneManagementExtension", "Logs", "*") |
| 52 | + matches, err := filepath.Glob(agentLogsPathPattern) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("globbing for agent logs at %s: %w", agentLogsPathPattern, err) |
| 55 | + } |
| 56 | + |
| 57 | + for _, match := range matches { |
| 58 | + if err := addFileToZip(zipWriter, match); err != nil { |
| 59 | + return fmt.Errorf("adding %s to zip: %w", match, err) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func installLogs(zipWriter *zip.Writer) error { |
| 67 | + installLogsPathPattern := filepath.Join(os.Getenv("WINDIR"), "System32", "config", "systemprofile", "AppData", "Local", "mdm", "*.log") |
| 68 | + matches, err := filepath.Glob(installLogsPathPattern) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("globbing for install logs at %s: %w", installLogsPathPattern, err) |
| 71 | + } |
| 72 | + |
| 73 | + for _, match := range matches { |
| 74 | + if err := addFileToZip(zipWriter, match); err != nil { |
| 75 | + return fmt.Errorf("adding %s to zip: %w", match, err) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +func diagnostics(ctx context.Context, zipWriter *zip.Writer) error { |
| 83 | + tempDir, err := agent.MkdirTemp("mdm-diagnostics") |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("creating temp dir: %w", err) |
| 86 | + } |
| 87 | + defer os.RemoveAll(tempDir) |
| 88 | + |
| 89 | + tempOutfile := filepath.Join(tempDir, "MdmDiagnosticReport.zip") |
| 90 | + |
| 91 | + cmd, err := allowedcmd.MdmDiagnosticsTool(ctx, "-zip", outfile) |
| 92 | + if cmd == nil { |
| 93 | + return nil |
| 94 | + } else if err != nil { |
| 95 | + return fmt.Errorf("creating diagnostics command: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + return addFileToZip(zipWriter, tempOutfile) |
| 99 | +} |
| 100 | + |
| 101 | +func (i *intuneCheckup) ExtraFileName() string { |
| 102 | + return "intune.zip" |
| 103 | +} |
| 104 | + |
| 105 | +func (i *intuneCheckup) Status() Status { |
| 106 | + return Informational |
| 107 | +} |
| 108 | + |
| 109 | +func (i *intuneCheckup) Summary() string { |
| 110 | + return i.summary |
| 111 | +} |
| 112 | + |
| 113 | +func (i *intuneCheckup) Data() any { |
| 114 | + return nil |
| 115 | +} |
0 commit comments