-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-collect-metrics.sh
More file actions
executable file
·326 lines (278 loc) · 8.39 KB
/
auto-collect-metrics.sh
File metadata and controls
executable file
·326 lines (278 loc) · 8.39 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
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
#!/bin/bash
# Auto-Collect Metrics Script
# Automated data collection for crosspost performance monitoring
# Usage: ./auto-collect-metrics.sh [interval_minutes]
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_FILE="$SCRIPT_DIR/auto-collect.log"
METRICS_FILE="$SCRIPT_DIR/crosspost-metrics.json"
MONITOR_SCRIPT="$SCRIPT_DIR/crosspost-monitor.js"
INTERVAL_MINUTES=${1:-30} # Default 30 minutes if not specified
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging function
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# Error handling
handle_error() {
log "❌ ERROR: $1"
echo -e "${RED}ERROR: $1${NC}"
exit 1
}
# Success logging
log_success() {
log "✅ SUCCESS: $1"
echo -e "${GREEN}SUCCESS: $1${NC}"
}
# Warning logging
log_warning() {
log "⚠️ WARNING: $1"
echo -e "${YELLOW}WARNING: $1${NC}"
}
# Info logging
log_info() {
log "ℹ️ INFO: $1"
echo -e "${BLUE}INFO: $1${NC}"
}
# Check dependencies
check_dependencies() {
log_info "Checking dependencies..."
if ! command -v node &> /dev/null; then
handle_error "Node.js is not installed or not in PATH"
fi
if [ ! -f "$MONITOR_SCRIPT" ]; then
handle_error "Monitor script not found at $MONITOR_SCRIPT"
fi
if ! node -e "require('fs')" 2>/dev/null; then
handle_error "Node.js fs module not available"
fi
log_success "Dependencies check passed"
}
# Collect metrics
collect_metrics() {
log_info "Starting metrics collection..."
# Run the metrics collection
if node "$MONITOR_SCRIPT" collect >> "$LOG_FILE" 2>&1; then
log_success "Metrics collection completed"
return 0
else
log_warning "Metrics collection failed or returned warnings"
return 1
fi
}
# Generate report
generate_report() {
log_info "Generating performance report..."
if node "$MONITOR_SCRIPT" report >> "$LOG_FILE" 2>&1; then
log_success "Performance report generated"
return 0
else
log_warning "Report generation failed"
return 1
fi
}
# Check metrics file age
check_metrics_freshness() {
if [ -f "$METRICS_FILE" ]; then
# Get file modification time in seconds since epoch
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
FILE_TIME=$(stat -f %m "$METRICS_FILE")
else
# Linux
FILE_TIME=$(stat -c %Y "$METRICS_FILE")
fi
CURRENT_TIME=$(date +%s)
AGE_SECONDS=$((CURRENT_TIME - FILE_TIME))
AGE_MINUTES=$((AGE_SECONDS / 60))
if [ $AGE_MINUTES -gt $((INTERVAL_MINUTES * 2)) ]; then
log_warning "Metrics file is $AGE_MINUTES minutes old (stale data)"
return 1
else
log_info "Metrics file is $AGE_MINUTES minutes old (fresh)"
return 0
fi
else
log_warning "Metrics file does not exist"
return 1
fi
}
# Health check
health_check() {
log_info "Performing health check..."
local issues=0
# Check if metrics file exists and is readable
if [ ! -r "$METRICS_FILE" ]; then
log_warning "Metrics file is not readable or does not exist"
((issues++))
fi
# Check disk space
local disk_usage=$(df "$SCRIPT_DIR" | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$disk_usage" -gt 90 ]; then
log_warning "Disk usage is high: ${disk_usage}%"
((issues++))
fi
# Check log file size
if [ -f "$LOG_FILE" ]; then
local log_size=$(wc -c < "$LOG_FILE")
if [ "$log_size" -gt 10485760 ]; then # 10MB
log_warning "Log file is large (${log_size} bytes), consider rotating"
((issues++))
fi
fi
if [ $issues -eq 0 ]; then
log_success "Health check passed"
return 0
else
log_warning "Health check found $issues issue(s)"
return 1
fi
}
# Cleanup old logs
cleanup_logs() {
if [ -f "$LOG_FILE" ]; then
local log_lines=$(wc -l < "$LOG_FILE")
if [ "$log_lines" -gt 1000 ]; then
log_info "Rotating log file (${log_lines} lines)"
tail -500 "$LOG_FILE" > "${LOG_FILE}.tmp"
mv "${LOG_FILE}.tmp" "$LOG_FILE"
log_success "Log file rotated"
fi
fi
}
# Main execution function
run_collection_cycle() {
log_info "Starting collection cycle (interval: ${INTERVAL_MINUTES} minutes)"
# Health check
health_check
# Check if we need to collect new metrics
if ! check_metrics_freshness; then
log_info "Metrics need updating, collecting fresh data..."
collect_metrics
else
log_info "Metrics are fresh, skipping collection"
fi
# Generate report periodically (every 4 cycles)
local cycle_file="$SCRIPT_DIR/.collection_cycle"
local cycle_count=0
if [ -f "$cycle_file" ]; then
cycle_count=$(cat "$cycle_file")
fi
cycle_count=$((cycle_count + 1))
echo "$cycle_count" > "$cycle_file"
if [ $((cycle_count % 4)) -eq 0 ]; then
log_info "Generating periodic report (cycle $cycle_count)"
generate_report
fi
# Cleanup
cleanup_logs
log_success "Collection cycle completed"
}
# Continuous monitoring mode
start_continuous_monitoring() {
log_info "Starting continuous monitoring mode (every ${INTERVAL_MINUTES} minutes)"
log_info "Press Ctrl+C to stop"
# Create PID file
echo $$ > "$SCRIPT_DIR/.monitor_pid"
# Trap signals for graceful shutdown
trap 'log_info "Stopping continuous monitoring..."; rm -f "$SCRIPT_DIR/.monitor_pid"; exit 0' INT TERM
while true; do
run_collection_cycle
log_info "Sleeping for ${INTERVAL_MINUTES} minutes..."
sleep $((INTERVAL_MINUTES * 60))
done
}
# Stop continuous monitoring
stop_monitoring() {
local pid_file="$SCRIPT_DIR/.monitor_pid"
if [ -f "$pid_file" ]; then
local pid=$(cat "$pid_file")
if kill -0 "$pid" 2>/dev/null; then
log_info "Stopping monitoring process (PID: $pid)"
kill "$pid"
rm -f "$pid_file"
log_success "Monitoring stopped"
else
log_warning "Process $pid not found, cleaning up PID file"
rm -f "$pid_file"
fi
else
log_warning "No monitoring process found"
fi
}
# Check if monitoring is running
check_monitoring_status() {
local pid_file="$SCRIPT_DIR/.monitor_pid"
if [ -f "$pid_file" ]; then
local pid=$(cat "$pid_file")
if kill -0 "$pid" 2>/dev/null; then
echo -e "${GREEN}Monitoring is running (PID: $pid)${NC}"
return 0
else
echo -e "${RED}PID file exists but process not running${NC}"
rm -f "$pid_file"
return 1
fi
else
echo -e "${YELLOW}Monitoring is not running${NC}"
return 1
fi
}
# Display usage
usage() {
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo ""
echo "Commands:"
echo " start [minutes] Start continuous monitoring (default: 30 minutes)"
echo " stop Stop continuous monitoring"
echo " status Check monitoring status"
echo " once Run collection once and exit"
echo " health Run health check only"
echo ""
echo "Examples:"
echo " $0 start 15 # Start monitoring every 15 minutes"
echo " $0 once # Run collection once"
echo " $0 health # Check system health"
}
# Main script logic
main() {
# Create log file if it doesn't exist
touch "$LOG_FILE"
case "${1:-start}" in
start)
INTERVAL_MINUTES=${2:-30}
check_dependencies
start_continuous_monitoring
;;
stop)
stop_monitoring
;;
status)
check_monitoring_status
;;
once)
INTERVAL_MINUTES=${2:-30}
check_dependencies
run_collection_cycle
;;
health)
check_dependencies
health_check
;;
help|--help|-h)
usage
;;
*)
echo "Unknown command: $1"
usage
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"