-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
145 lines (125 loc) · 3.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/fatih/color"
)
const (
msgContainerKilled = "Pod is marked for graceful deletion, begin teardown"
plegRelist = "GenericPLEG: Relisting"
)
type message struct {
Timestamp float64 `json:"ts"`
Message string `json:"msg"`
Pod string `json:"pod"`
Pods []string `json:"pods"`
Caller string `json:"caller"`
}
func main() {
var (
pod string
stopAfterDeletion bool
)
flag.StringVar(&pod, "pod", "", "the pod to analyze the logs for")
flag.BoolVar(&stopAfterDeletion, "stop-after-deletion", false, "stop log analyzing after seeing a deletion")
flag.Parse()
if pod == "" {
log.Fatalln("No pod provided")
}
fmt.Println("Pod: " + pod)
var msgs []message
var seenFirstPodMessage bool
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
start := strings.Index(line, "{")
if start == -1 {
continue
}
line = line[start:]
var msg message
json.Unmarshal([]byte(line), &msg)
if seenFirstPodMessage && msg.Message == plegRelist {
msgs = append(msgs, msg)
continue
}
if msg.Pod == pod {
if stopAfterDeletion && msg.Message == msgContainerKilled {
break
}
seenFirstPodMessage = true
msgs = append(msgs, msg)
continue
}
for _, podName := range msg.Pods {
if podName == pod {
seenFirstPodMessage = true
msgs = append(msgs, msg)
break
}
}
}
if len(msgs) == 0 {
log.Fatalln("No messages found")
}
sort.Slice(msgs, func(i, j int) bool {
return msgs[i].Timestamp < msgs[j].Timestamp
})
fmt.Println()
fmt.Println("Logs:")
fmt.Println("ELAPSED\tDIFF\tSYSTEM\tMESSAGE")
start := msgs[0].Timestamp
for i, msg := range msgs {
// Figure out which subsystem the line belongs to.
subsystem := color.New(color.Bold).SprintFunc()("MISC")
if strings.HasPrefix(msg.Caller, "volumemanager/") || strings.HasPrefix(msg.Caller, "populator/") || strings.HasPrefix(msg.Caller, "reconciler/") || strings.HasPrefix(msg.Caller, "operationexecutor/") {
subsystem = color.New(color.Bold, color.FgGreen).SprintFunc()("VOLUME")
} else if strings.HasPrefix(msg.Caller, "kuberuntime/") || msg.Message == "syncPod enter" || msg.Message == "syncPod exit" {
subsystem = color.New(color.Bold, color.FgBlue).SprintFunc()("SYNCPOD")
} else if strings.HasPrefix(msg.Caller, "pleg/") {
subsystem = color.New(color.Bold, color.FgRed).SprintFunc()("PLEG")
} else if strings.HasPrefix(msg.Caller, "status/") {
subsystem = color.New(color.Bold, color.FgHiBlue).SprintFunc()("STATUS")
} else if strings.HasPrefix(msg.Caller, "kubelet/kubelet_pods") {
subsystem = color.New(color.Bold, color.FgHiGreen).SprintFunc()("MOUNT")
} else if strings.HasPrefix(msg.Caller, "prober") {
subsystem = color.New(color.Bold, color.FgYellow).SprintFunc()("PROBE")
}
diff := 0
if i > 0 {
diff = int(msg.Timestamp - msgs[i-1].Timestamp)
}
diffStr := fmt.Sprint(diff)
if diff > 10 {
diffStr = color.New(color.FgYellow).SprintFunc()(diff)
}
if diff > 30 {
diffStr = color.New(color.Bold, color.FgYellow).SprintFunc()(diff)
}
if diff > 50 {
diffStr = color.New(color.FgHiYellow).SprintFunc()(diff)
}
if diff > 100 {
diffStr = color.New(color.Bold, color.FgHiYellow).SprintFunc()(diff)
}
if diff > 300 {
diffStr = color.New(color.FgRed).SprintFunc()(diff)
}
if diff > 500 {
diffStr = color.New(color.Bold, color.FgRed).SprintFunc()(diff)
}
fmt.Printf("%d\t%s\t%s\t%s\n", int(msg.Timestamp-start), diffStr, subsystem, truncate(msg.Message, 90))
}
}
func truncate(msg string, max int) string {
if len(msg) < max {
return msg
}
return msg[:max-3] + "..."
}