inspektor-gadget

Форк
0
100 строк · 2.6 Кб
1
// Copyright 2022-2023 The Inspektor Gadget authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package tracer
16

17
import (
18
	"fmt"
19
	"strings"
20

21
	gadgetregistry "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-registry"
22
	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
23
	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/snapshot/process/types"
24
	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
25
	"github.com/inspektor-gadget/inspektor-gadget/pkg/parser"
26
)
27

28
const (
29
	ParamThreads = "threads"
30
)
31

32
type GadgetDesc struct{}
33

34
func (g *GadgetDesc) Name() string {
35
	return "process"
36
}
37

38
func (g *GadgetDesc) Category() string {
39
	return gadgets.CategorySnapshot
40
}
41

42
func (g *GadgetDesc) Type() gadgets.GadgetType {
43
	return gadgets.TypeOneShot
44
}
45

46
func (g *GadgetDesc) Description() string {
47
	return "Gather information about running processes"
48
}
49

50
func (g *GadgetDesc) ParamDescs() params.ParamDescs {
51
	return params.ParamDescs{
52
		{
53
			Key:          ParamThreads,
54
			Title:        "All threads",
55
			Description:  "Show all threads (by default, only processes are shown)",
56
			Alias:        "", // TODO: was t, clashes with timeout
57
			DefaultValue: "false",
58
			TypeHint:     params.TypeBool,
59
		},
60
	}
61
}
62

63
func (g *GadgetDesc) Parser() parser.Parser {
64
	return parser.NewParser[types.Event](types.GetColumns())
65
}
66

67
func (g *GadgetDesc) EventPrototype() any {
68
	return &types.Event{}
69
}
70

71
func (g *GadgetDesc) OutputFormats() (gadgets.OutputFormats, string) {
72
	return gadgets.OutputFormats{
73
		"tree": gadgets.OutputFormat{
74
			Name:        "Tree",
75
			Description: "A pstree like output",
76
			Transform: func(data any) ([]byte, error) {
77
				processes, ok := data.([]*types.Event)
78
				if !ok {
79
					return nil, fmt.Errorf("type must be []*types.Event and is: %T", data)
80
				}
81

82
				var builder strings.Builder
83
				err := types.WriteTree(&builder, processes)
84
				if err != nil {
85
					return nil, err
86
				}
87

88
				return []byte(builder.String()), nil
89
			},
90
		},
91
	}, "columns"
92
}
93

94
func (g *GadgetDesc) SortByDefault() []string {
95
	return []string{"k8s.node", "k8s.namespace", "k8s.pod", "k8s.container", "comm", "pid", "tid", "ppid"}
96
}
97

98
func init() {
99
	gadgetregistry.Register(&GadgetDesc{})
100
}
101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.