Skip to content

Commit c0472bb

Browse files
committed
Add process collection for command
1 parent 1c8e680 commit c0472bb

File tree

9 files changed

+99
-66
lines changed

9 files changed

+99
-66
lines changed

collector/collector.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"strconv"
78
"strings"
89
"sync"
910

@@ -287,7 +288,7 @@ func (c *Collector) handleSocketCollection(con net.Conn) error {
287288

288289
c.logger.Debug().Msgf("Received: %s", string(buf[:n]))
289290

290-
if len(parts) != 7 {
291+
if len(parts) != 8 {
291292
c.logger.Error().Msg("Invalid command format")
292293
return fmt.Errorf("invalid command format")
293294
}
@@ -321,13 +322,16 @@ func (c *Collector) handleStartCommand(parts []string) error {
321322
c.logger.Error().Err(err).Msg("Failed to get repository name")
322323
}
323324

325+
pid, _ := strconv.ParseInt(parts[5], 10, 64)
326+
324327
command := Command{
325328
Category: ParseCommand(parts[1]),
326329
Command: parts[1],
327330
Directory: parts[2],
328331
User: parts[3],
329332
StartTime: time.Now().UnixMilli(), // TODO: there are some issues with sending time through shell because of ms support on MAC, explore more
330333
Repository: repo,
334+
PID: pid,
331335
}
332336

333337
c.collectionConfig.ongoingCommands[parts[4]] = command
@@ -349,8 +353,8 @@ func (c *Collector) handleEndCommand(parts []string) error {
349353
if command, exists := c.collectionConfig.ongoingCommands[parts[4]]; exists {
350354
command.EndTime = time.Now().UnixMilli()
351355
command.ExecutionTime = command.EndTime - command.StartTime
352-
command.Result = parts[5]
353-
command.Status = parts[6]
356+
command.Result = parts[6]
357+
command.Status = parts[7]
354358

355359
c.logger.Debug().Msgf("Command: %+v", command)
356360
if err := InsertCommand(command); err != nil {

collector/command.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Command struct {
2323
Status string `json:"status" db:"status"`
2424
Result string `json:"result" db:"result"`
2525
Repository string `json:"repository" db:"repository"`
26+
PID int64 `json:"pid" db:"pid"`
2627
}
2728

2829
// GetCommandById fetches a command by its ID
@@ -91,8 +92,8 @@ func DeleteCommandsByDays(days int) error {
9192

9293
// InsertCommand inserts a command into the database
9394
func InsertCommand(command Command) error {
94-
query := `INSERT INTO commands (category, command, user, directory, execution_time, start_time, end_time, status, result, repository)
95-
VALUES (:category, :command, :user, :directory, :execution_time, :start_time, :end_time, :status, :result, :repository)`
95+
query := `INSERT INTO commands (category, command, user, directory, execution_time, start_time, end_time, status, result, repository, pid)
96+
VALUES (:category, :command, :user, :directory, :execution_time, :start_time, :end_time, :status, :result, :repository, :pid)`
9697

9798
_, err := database.DB.NamedExec(query, command)
9899

@@ -139,5 +140,6 @@ func MapCommandToProto(command Command) *gen.Command {
139140
Status: command.Status,
140141
Result: command.Result,
141142
Repository: command.Repository,
143+
Pid: command.PID,
142144
}
143145
}

database/migrations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func createCommandsTable() {
7474
end_time INTEGER,
7575
status TEXT,
7676
result TEXT,
77-
repository TEXT
77+
repository TEXT,
78+
pid INTEGER
7879
);`
7980

8081
_, err := DB.Exec(createCommandsTableSQL)

gen/api/v1/collector.pb.go

Lines changed: 62 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/api/v1/collector.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ message Command {
2929
string result = 9; // Result of executed command => success/failure
3030
string status = 10; // Status of executed command
3131
string repository = 11; // Repository is repository where commands are executed
32+
int64 pid = 12; // PID of the command
3233
}
3334

3435
// Define a message representing a process, including its metadata and resource usage.

shell/scripts/bash.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ generate_uuid() {
22
echo "$(date +%s)-$$-$RANDOM"
33
}
44

5+
generate_ppid() {
6+
echo "$$"
7+
}
8+
59
preexec_invoke_exec() {
610
# Avoid running preexec_invoke_exec for PROMPT_COMMAND
711
if [[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]]; then
812
export UUID=$(generate_uuid)
13+
export PID=$(generate_ppid)
914
export LAST_COMMAND="$BASH_COMMAND"
1015
# Send a start execution message
11-
{{.CommandScriptPath}} "start" "$LAST_COMMAND" "$PWD" "$USER" "$UUID"
16+
{{.CommandScriptPath}} "start" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$PID"
1217
fi
1318
}
1419
trap 'preexec_invoke_exec' DEBUG
@@ -22,7 +27,7 @@ precmd_invoke_cmd() {
2227
fi
2328

2429
# Send an end execution message with the result and exit status
25-
{{.CommandScriptPath}} "end" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$result" "$exit_status"
30+
{{.CommandScriptPath}} "end" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$PID" "$result" "$exit_status"
2631
}
2732

2833
# Update PROMPT_COMMAND to invoke precmd_invoke_cmd

shell/scripts/collector.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# $3 - Working directory
88
# $4 - User who executed the command
99
# $5 - Unique identifier
10+
# #6 - PID of the command
1011
# $6 - Command result (success/failure)
1112
# $7 - Exit status
1213

@@ -46,7 +47,7 @@ send_via_perl() {
4647
}
4748

4849
# Construct the log message including result and exit status
49-
LOG_MESSAGE="$1|$2|$3|$4|$5|$6|$7"
50+
LOG_MESSAGE="$1|$2|$3|$4|$5|$6|$7|$8"
5051

5152
# Send the log message to the Go application via UNIX socket
5253
if command_exists nc && nc_supports_U; then

shell/scripts/fish.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ function generate_uuid
22
echo (date +%s)"-"(echo %self)"-"(random)
33
end
44

5+
function generate_ppid
6+
echo (echo %self)
7+
end
8+
59
function fish_preexec --on-event fish_preexec
610
set -gx LAST_COMMAND $argv[1]
711
set -gx UUID (generate_uuid)
12+
set -gx PID (generate_ppid)
813
# Send a start execution message
9-
{{.CommandScriptPath}} "start" "$LAST_COMMAND" "$PWD" "$USER" "$UUID"
14+
{{.CommandScriptPath}} "start" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$PID"
1015
end
1116

1217
function fish_postexec --on-event fish_postexec
@@ -18,5 +23,5 @@ function fish_postexec --on-event fish_postexec
1823
end
1924

2025
# Send an end execution message with result and exit status
21-
{{.CommandScriptPath}} "end" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" $result $exit_status
26+
{{.CommandScriptPath}} "end" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$PID" "$result" "$exit_status"
2227
end

shell/scripts/zsh.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ generate_uuid() {
22
echo "$(date +%s)-$$-$RANDOM"
33
}
44

5+
generate_ppid() {
6+
echo "$$"
7+
}
8+
59
preexec() {
610
export LAST_COMMAND=$1
711
UUID=$(generate_uuid)
12+
PID=$(generate_ppid)
813
# Send a start execution message
9-
{{.CommandScriptPath}} "start" "$LAST_COMMAND" "$PWD" "$USER" "$UUID"
14+
{{.CommandScriptPath}} "start" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$PID"
1015
}
1116

1217
precmd() {
@@ -18,5 +23,5 @@ precmd() {
1823
fi
1924

2025
# Send an end execution message with result and exit status
21-
{{.CommandScriptPath}} "end" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$result" "$exit_status"
26+
{{.CommandScriptPath}} "end" "$LAST_COMMAND" "$PWD" "$USER" "$UUID" "$PID" "$result" "$exit_status"
2227
}

0 commit comments

Comments
 (0)