-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-log.sh
More file actions
executable file
·132 lines (114 loc) · 4.23 KB
/
shell-log.sh
File metadata and controls
executable file
·132 lines (114 loc) · 4.23 KB
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
#!/bin/bash
# Simple Shell Logger - Basic nohup with process substitution
# Usage: ./shell-log-simple.sh [app-name] [command] [args...]
set -e
# Configuration
BACKEND_URL="${MCP_LOGGING_BACKEND_URL:-http://localhost:22345}"
APP_NAME="${1:-shell-commands}"
HOST_NAME="${HOSTNAME:-$(hostname)}"
COMMAND="${2:-}"
VERBOSE="${MCP_LOGGING_VERBOSE:-false}"
WORKING_DIR="$(pwd)"
# Generate namespace from command
if [ -n "$COMMAND" ]; then
NAMESPACE="$COMMAND"
if [ ${#NAMESPACE} -gt 30 ]; then
NAMESPACE="${NAMESPACE:0:27}..."
fi
NAMESPACE=$(echo "$NAMESPACE" | sed 's/[^a-zA-Z0-9_-]/_/g')
else
NAMESPACE="shell-output"
fi
# Shift to get remaining args
if [ $# -ge 2 ]; then
shift 2
COMMAND_ARGS=("$@")
else
COMMAND_ARGS=()
fi
if [ -z "$COMMAND" ]; then
echo "📜 Simple Shell Logger - nohup with process substitution"
echo ""
echo "🔧 USAGE:"
echo " $0 [app-name] [command] [args...]"
echo ""
echo "🌟️ FEATURES:"
echo " ✅ Background process capture with nohup"
echo " ✅ Memory-only logging"
echo " ✅ Real-time streaming"
echo ""
exit 1
fi
# Function to send log to MCP backend in standard format
send_log() {
local log_entry="$1"
if [ "$VERBOSE" = "true" ]; then
echo "📤: $log_entry" >&2
fi
# Create a temporary file with the JSON payload to avoid shell expansion issues
local json_payload="{\"app\":\"${APP_NAME}\",\"host\":\"${HOST_NAME}\",\"logs\":{\"${NAMESPACE}\":[${log_entry}]}}"
curl -s -X POST "${BACKEND_URL}/api/logs/submit" \
-H "Content-Type: application/json" \
-d "$json_payload" \
--max-time 5 \
--connect-timeout 3 \
> /dev/null 2>&1 || true
}
# Log command start
START_TIME=$(date +%s)
start_entry="{\"timestamp\":${START_TIME}000,\"level\":\"INFO\",\"message\":\"🚀 Starting: $COMMAND ${COMMAND_ARGS[*]}\",\"namespace\":\"${NAMESPACE}\",\"app\":\"${APP_NAME}\",\"host\":\"${HOST_NAME}\",\"source\":\"shell\"}"
send_log "$start_entry"
if [ "$VERBOSE" = "true" ]; then
echo "🚀 Starting: $COMMAND ${COMMAND_ARGS[*]}"
fi
# Change to working directory
cd "$WORKING_DIR"
# Execute with nohup and process substitution
{
nohup "$COMMAND" "${COMMAND_ARGS[@]}" \
> >(
while IFS= read -r line; do
if [ -n "$line" ]; then
timestamp=$(date +%s)
# Properly escape the message for JSON
escaped_message=$(echo "$line" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')
log_entry="{\"timestamp\":${timestamp}000,\"level\":\"LOG\",\"message\":\"${escaped_message}\",\"namespace\":\"${NAMESPACE}\",\"app\":\"${APP_NAME}\",\"host\":\"${HOST_NAME}\",\"source\":\"shell\"}"
send_log "$log_entry"
if [ "$VERBOSE" = "true" ]; then
echo "[stdout] $line"
fi
fi
done
) \
2> >(
while IFS= read -r line; do
if [ -n "$line" ]; then
timestamp=$(date +%s)
# Properly escape the message for JSON
escaped_message=$(echo "$line" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')
log_entry="{\"timestamp\":${timestamp}000,\"level\":\"ERROR\",\"message\":\"${escaped_message}\",\"namespace\":\"${NAMESPACE}\",\"app\":\"${APP_NAME}\",\"host\":\"${HOST_NAME}\",\"source\":\"shell\"}"
send_log "$log_entry"
if [ "$VERBOSE" = "true" ]; then
echo "[stderr] $line"
fi
fi
done
) &
}
PID=$!
# Wait for completion
wait $PID 2>/dev/null || true
EXIT_CODE=$?
# Log command completion
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
STATUS="✅"
if [ $EXIT_CODE -ne 0 ]; then
STATUS="❌"
fi
completion_entry="{\"timestamp\":${END_TIME}000,\"level\":\"INFO\",\"message\":\"${STATUS} Completed: $COMMAND ${COMMAND_ARGS[*]} (${DURATION}s)\",\"namespace\":\"${NAMESPACE}\",\"app\":\"${APP_NAME}\",\"host\":\"${HOST_NAME}\",\"source\":\"shell\"}"
send_log "$completion_entry"
if [ "$VERBOSE" = "true" ]; then
echo "✅ Completed with exit code: $EXIT_CODE (duration: ${DURATION}s)"
fi
exit $EXIT_CODE