Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Traceectl to Tracee #4396

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

ShohamBit
Copy link
Contributor

@ShohamBit ShohamBit commented Nov 25, 2024

Description

This PR introduces traceectl, a new CLI tool for interacting with Tracee. It implements basic commands for communication with Tracee via gRPC.

Key Changes

  • Added traceectl CLI tool.
  • Implemented commands for:
    • version
    • metrics
    • stream
    • event

Testing

  1. Compile and run Tracee with gRPC:

    make tracee
    tracee --grpc-listen-addr unix:/tmp/tracee.sock
  2. Run traceectl:

    go run ./cmd/traceectl/main.go

    This displays the help command.

Current Limitations

  • metrics: JSON output is not yet supported.
  • stream: Only the base stream command is supported. Streams events directly from Tracee.

Related Issues

Note: This PR was accidentally closed previously and is now being resubmitted.

Copy link
Collaborator

@yanivagman yanivagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see comments below.
In addition, please add documentation to traceectl.
As part of the documentation, please also refer to the way the user should start tracee "daemon" (e.g. using NONE output and setting the grpc server address)

@@ -0,0 +1,150 @@
package cmd

//TODO:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Short: "Event management for tracee",
Long: `Event Management for tracee
Let you enable and disable events in tracee.
Get descriptions of events and run them.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Get descriptions of events and run them.
Get descriptions of events.

Comment on lines +39 to +43
listEventCmd.Flags().StringVarP(&eventFormatFlag, "format", "f", formatter.FormatTable, "Output format (json|table|template) ")
listEventCmd.Flags().StringVarP(&eventOutputFlag, "output", "o", "stdout", "Output destination ")

describeEventCmd.Flags().StringVarP(&eventFormatFlag, "format", "f", formatter.FormatTable, "Output format (json|table|template) ")
describeEventCmd.Flags().StringVarP(&eventOutputFlag, "output", "o", "stdout", "Output destination ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output can be made global instead of per command


type ServerInfo struct {
ConnectionType string
ADDR string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename to Addr

Comment on lines +33 to +49
switch serverInfo.ConnectionType {
case PROTOCOL_UNIX:
address := fmt.Sprintf("unix://%s", serverInfo.ADDR)
conn, err = grpc.NewClient(address, opts...)

if err != nil {
log.Fatalf("failed to connect to server: %v", err)
return nil, err
}
case PROTOCOL_TCP:
address := fmt.Sprintf(serverInfo.ADDR)
conn, err = grpc.NewClient(address, opts...)

if err != nil {
log.Fatalf("failed to connect to server: %v", err)
return nil, err
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch case should only initialize address, common code can be below

)

func (f *Formatter) PrintStreamJSON(event *pb.Event) {
f.CMD.Printf("%s", event.String())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use camelCase

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these functions are tightly coupled to the stream and events commands, I suggest to put them in their packages, and leave the formatter out at this stage

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods in this file are coupled to the command - let's move them to the respective commands instead. In the future, we can think about extending traceectl with a formatter package

Comment on lines +47 to +68
func initOutput(cmd *cobra.Command, output string) error {
if (output != "") && (output != "stdout") {
if output == "" || strings.TrimSpace(output) == "" {
return fmt.Errorf("output file path is empty or invalid")
}
dir := filepath.Dir(output)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directories for output file: %v", err)
}
file, err := os.Create(output)
if err != nil {
return fmt.Errorf("failed to open output file: %v", err)
}

cmd.SetOut(file)
cmd.SetErr(file)
cmd.PersistentPostRun = func(cmd *cobra.Command, args []string) {
file.Close()
}
}
return nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't seem to be part of the formatter package, considering it handles output

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants