...
1
2
3
18
19 package kubelet
20
21 import (
22 "context"
23 "fmt"
24 "os/exec"
25 "strings"
26 )
27
28 const powershellExe = "PowerShell.exe"
29
30
31 func getLoggingCmd(n *nodeLogQuery, services []string) (string, []string, error) {
32 args := []string{
33 "-NonInteractive",
34 "-ExecutionPolicy", "Bypass",
35 "-Command",
36 }
37
38 psCmd := "Get-WinEvent -FilterHashtable @{LogName='Application'"
39 if n.SinceTime != nil {
40 psCmd += fmt.Sprintf("; StartTime='%s'", n.SinceTime.Format(dateLayout))
41 }
42 if n.UntilTime != nil {
43 psCmd += fmt.Sprintf("; EndTime='%s'", n.UntilTime.Format(dateLayout))
44 }
45 var providers []string
46 for _, service := range services {
47 if len(service) > 0 {
48 providers = append(providers, "'"+service+"'")
49 }
50 }
51 if len(providers) > 0 {
52 psCmd += fmt.Sprintf("; ProviderName=%s", strings.Join(providers, ","))
53 }
54 psCmd += "}"
55 if n.TailLines != nil {
56 psCmd += fmt.Sprintf(" -MaxEvents %d", *n.TailLines)
57 }
58 psCmd += " | Sort-Object TimeCreated"
59 if len(n.Pattern) > 0 {
60 psCmd += fmt.Sprintf(" | Where-Object -Property Message -Match '%s'", n.Pattern)
61 }
62 psCmd += " | Format-Table -AutoSize -Wrap"
63
64 args = append(args, psCmd)
65
66 return powershellExe, args, nil
67 }
68
69
70 func checkForNativeLogger(ctx context.Context, service string) bool {
71 cmd := exec.CommandContext(ctx, powershellExe, []string{
72 "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command",
73 fmt.Sprintf("Get-WinEvent -ListProvider %s | Format-Table -AutoSize", service)}...)
74
75 _, err := cmd.CombinedOutput()
76 if err != nil {
77
78 if _, ok := err.(*exec.ExitError); ok {
79 return false
80 }
81
82
83
84 }
85 return true
86 }
87
View as plain text