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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
package main
import (
"context"
"coredump-tools/types"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/alexeyco/simpletable"
"github.com/google/uuid"
"github.com/urfave/cli/v2"
"golang.org/x/term"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
var configs []types.Coredump_config
// WalkDirectory search file with suffix name ".info"
func WalkDirectory(dir string) {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && strings.HasPrefix(info.Name(), "coredump_") {
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".info") {
data, err := os.ReadFile(path)
if err != nil {
return err
}
var config types.Coredump_config
err = json.Unmarshal(data, &config)
if err != nil {
return err
}
if config.Container_id == "" {
config.Container_id = "NULL"
}
if config.Image_name == "" {
config.Image_name = "NULL"
}
if _, err := os.Stat(config.Storage); err != nil {
config.Storage = "MISS"
}
configs = append(configs, config)
}
return nil
})
if err != nil {
fmt.Printf("Error walking directory %s: %v\n", path, err)
}
}
return nil
})
sort.Slice(configs, func(i, j int) bool {
return configs[i].Timestamp < configs[j].Timestamp
})
if err != nil {
fmt.Printf("Error walking directory %s: %v\n", dir, err)
}
}
func list(pid string) {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Text: "PID"},
{Text: "EXE"},
{Text: "IMAGE"},
{Text: "TIMESTAMP"},
},
}
total := 0
// output the config's info
for _, c := range configs {
if pid != "" && strings.Compare(c.Initial_ns_pid, pid) != 0 {
continue
}
coreTime := time.Unix(c.Timestamp, 0).Format("2006-01-02 15:04:05")
r := []*simpletable.Cell{
{Text: c.Initial_ns_pid},
{Text: c.Process_exe_path},
{Text: c.Image_name},
{Text: coreTime},
}
table.Body.Cells = append(table.Body.Cells, r)
total += 1
}
fmt.Println(table.String())
fmt.Println("Total", total, "coredumps")
}
func info(pid string) {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Text: "PID"},
{Text: "UID"},
{Text: "GID"},
{Text: "SIG"},
{Text: "EXE"},
{Text: "CONTAINER"},
{Text: "IMAGE"},
{Text: "HOSTNAME"},
{Text: "STORAGE"},
{Text: "TIMESTAMP"},
},
}
total := 0
// output the config's info
for _, c := range configs {
if pid != "" && strings.Compare(c.Initial_ns_pid, pid) != 0 {
continue
}
coreTime := time.Unix(c.Timestamp, 0).Format("2006-01-02 15:04:05")
r := []*simpletable.Cell{
{Text: c.Initial_ns_pid},
{Text: c.UID},
{Text: c.GID},
{Text: strconv.Itoa(c.Signal)},
{Text: c.Process_exe_path},
{Text: c.Container_id},
{Text: c.Image_name},
{Text: c.Hostname},
{Text: c.Storage},
{Text: coreTime},
}
table.Body.Cells = append(table.Body.Cells, r)
total += 1
}
fmt.Println(table.String())
fmt.Println("Total", total, "coredumps")
}
func debug(config types.Coredump_config, command string) error {
if strings.HasSuffix(config.Storage, ".minidump") {
corefile := strings.Replace(config.Storage, ".minidump", ".coredump", -1)
cmd := exec.Command("minidump-2-core", "-o", corefile, config.Storage)
err := cmd.Run()
if err != nil {
return err
}
config.Storage = corefile
defer os.Remove(corefile)
}
if config.Image_name != "NULL" {
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" {
kubeconfig = os.Getenv("HOME") + "/.kube/config"
}
// Creates the kubernetes client using the specified kubeconfig
conf, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(conf)
if err != nil {
return err
}
podName, err := debugInpod(conf, clientset, config, command)
if err != nil {
fmt.Println(err)
}
// Delete the Pod
fmt.Printf("Deleting Pod %q...\n", podName)
err = clientset.CoreV1().Pods("default").Delete(context.Background(), podName, metav1.DeleteOptions{})
if err != nil {
return err
}
fmt.Printf("Deleted Pod %q.\n", podName)
return nil
} else { // using gdb to debug the coredump file
cmd := exec.Command("gdb", config.Process_exe_path, config.Storage, "-cd", filepath.Dir(config.Process_exe_path))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Println(cmd.String())
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
func debugInpod(conf *rest.Config, clientset *kubernetes.Clientset, config types.Coredump_config, command string) (string, error) {
// Define the Pod object
id := uuid.New()
fmt.Println(id.String())
podName := fmt.Sprintf("node-debug-%s", id.String())
containerName := "debug"
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: "default",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "debug",
Image: config.Image_name,
ImagePullPolicy: "IfNotPresent",
Command: []string{
"tail",
"-f",
},
VolumeMounts: []v1.VolumeMount{
{
Name: "host-dir",
MountPath: "/host",
},
{
Name: "lib-debuginfo-dir",
MountPath: "/usr/lib/debug",
},
{
Name: "src-debuginfo-dir",
MountPath: "/usr/src/debug",
},
{
Name: "mrzcpd",
MountPath: "/opt/tsg/mrzcpd",
},
},
SecurityContext: &v1.SecurityContext{
Privileged: &[]bool{true}[0],
},
},
},
Volumes: []v1.Volume{
{
Name: "host-dir",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/",
},
},
},
{
Name: "lib-debuginfo-dir",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/usr/lib/debug",
},
},
},
{
Name: "src-debuginfo-dir",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/usr/src/debug",
},
},
},
{
Name: "mrzcpd",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/opt/tsg/mrzcpd",
},
},
},
},
RestartPolicy: v1.RestartPolicyNever,
},
}
// Create the Pod
fmt.Println("Creating Pod...")
fmt.Printf("Creating Pod %q...\n", podName)
result, err := clientset.CoreV1().Pods("default").Create(context.Background(), pod, metav1.CreateOptions{})
if err != nil {
return podName, err
}
fmt.Printf("Created Pod %q.\n", result.GetObjectMeta().GetName())
// Wait for the Pod to be running and ready
fmt.Printf("Waiting for Pod %q to be ready...\n", podName)
ready := false
for i := 0; i < 10; i++ {
result, err := clientset.CoreV1().Pods("default").Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
return podName, err
}
status := result.Status
if status.Phase == v1.PodRunning && len(status.ContainerStatuses) > 0 && status.ContainerStatuses[0].Ready {
ready = true
break
}
time.Sleep(2 * time.Second)
}
if !ready {
return podName, errors.New("create pod timeout")
}
// Disable canonical mode and enable echo mode.
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return podName, err
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
// Create exec request
var cmd []string
if command == "gdb" {
cmd = []string{"gdb", config.Process_exe_path, "/host" + config.Storage, "-cd", filepath.Dir(config.Process_exe_path)}
} else {
cmd = []string{command}
}
req := clientset.CoreV1().RESTClient().Post().Resource("pods").
Name(podName).Namespace("default").
SubResource("exec").
VersionedParams(&v1.PodExecOptions{
Container: containerName,
Command: cmd,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: true,
}, scheme.ParameterCodec)
// Create exec executor
executor, err := remotecommand.NewSPDYExecutor(conf, "POST", req.URL())
if err != nil {
return podName, err
}
// Start exec
err = executor.StreamWithContext(context.Background(), remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: true,
})
if err != nil {
return podName, err
}
return podName, nil
}
func main() {
var (
pid string
dirPath string
command string
)
app := &cli.App{
Name: "coredump",
Usage: "Manage coredump files in Kubernetes clusters",
Commands: []*cli.Command{
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List all coredump files",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pid",
Aliases: []string{"p"},
Usage: "Pid to match",
Value: "",
Destination: &pid,
},
&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Usage: "Coredump directory path(default: /var/lib/coredump)",
Value: "/var/lib/coredump",
Destination: &dirPath,
},
},
Action: func(c *cli.Context) error {
WalkDirectory(dirPath)
list(pid)
return nil
},
},
{
Name: "info",
Aliases: []string{"i"},
Usage: "Display all coredump files's info",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pid",
Aliases: []string{"p"},
Usage: "Pid to match",
Value: "",
Destination: &pid,
},
&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Usage: "Coredump directory path(default: /var/lib/coredump)",
Value: "/var/lib/coredump",
Destination: &dirPath,
},
},
Action: func(c *cli.Context) error {
WalkDirectory(dirPath)
info(pid)
return nil
},
},
{
Name: "debug",
Usage: "Start a debugging session for a coredump",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pid",
Aliases: []string{"p"},
Usage: "Pid to match",
Value: "",
Destination: &pid,
},
&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Usage: "Coredump directory path(default: /var/lib/coredump)",
Value: "/var/lib/coredump",
Destination: &dirPath,
},
&cli.StringFlag{
Name: "command",
Usage: "Debugger command (default: gdb)",
Value: "gdb",
Destination: &command,
},
},
Action: func(c *cli.Context) error {
WalkDirectory(dirPath)
for _, config := range configs {
if strings.Compare(config.Initial_ns_pid, pid) == 0 || pid == "" {
err := debug(config, command)
if err != nil {
fmt.Println(err)
} else {
break
}
}
}
return nil
},
},
{
Name: "help",
Usage: "Show help message",
Action: func(c *cli.Context) error {
cli.ShowAppHelp(c)
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
}
}
|