#!/bin/bash # Configuration LOG_FILE="/usr/local/aigrammar/log/app.log" PY_SCRIPT="/home/ubuntu/projects/devops/tools/send_to_wecom.py" TODAY=$(date +%Y-%m-%d) YESTERDAY=$(date -d "yesterday" +%Y-%m-%d) FILTERED_LOG="${LOG_FILE%.log}_${YESTERDAY}.log" # Check if log file exists if [ ! -f "$LOG_FILE" ]; then echo "Error: Log file not found - $LOG_FILE" >&2 exit 1 fi # Check if Python script exists and is executable if [ ! -f "$PY_SCRIPT" ]; then echo "Error: Python script not found or not executable - $PY_SCRIPT" >&2 exit 1 fi # Create filtered log file echo "Filtering yesterday's logs..." #grep "$TODAY" "$LOG_FILE" > "$FILTERED_LOG" grep "$YESTERDAY" "$LOG_FILE" > "$FILTERED_LOG" # Check if filtered log has content if [ ! -s "$FILTERED_LOG" ]; then echo "Warning: No logs found for $YESTERDAY" content="Date: $YESTERDAY\nNo log records found" python3 "$PY_SCRIPT" "$content" exit 0 fi echo "Filtered log created: $FILTERED_LOG" # Statistics echo "Calculating statistics..." request_total=$(grep -E "\"level\":\"info\"" "$FILTERED_LOG" | grep "\"func\":\"main.parseToken\"" | wc -l) grammar_total=$(grep -v "\"level\":\"debug\"" "$FILTERED_LOG" | grep "\"func\":\"main.GrammarHandler\"" | wc -l) translate_total=$(grep -v "\"level\":\"debug\"" "$FILTERED_LOG" | grep "\"func\":\"main.TranslateHandler\"" | wc -l) words_total=$(grep -v "\"level\":\"debug\"" "$FILTERED_LOG" | grep "\"func\":\"main.WordsHandler\"" | wc -l) free_limit_total=$(grep -E "\"level\":\"warn\"" "$FILTERED_LOG" | grep "\"func\":\"main.queryUserBenefits\"" | wc -l) error_total=$(grep "\"level\":\"error\"" "$FILTERED_LOG" | wc -l) # Generate content to send content="Date: $YESTERDAY Total Requests: $request_total Grammar Check Requests: $grammar_total Translation Requests: $translate_total Words Requests: $words_total Free Quota Exceeded: $free_limit_total Total Errors: $error_total" # Print statistics echo "===== Statistics =====" echo -e "$content" # Send to WeCom echo "Sending to WeChat Work..." python3 "$PY_SCRIPT" "$content" # Check result if [ $? -eq 0 ]; then echo "Successfully sent to WeChat Work" else echo "Error: Failed to send to WeChat Work" >&2 exit 1 fi # remove tmp logfile rm -rf "$FILTERED_LOG" echo "Script execution completed" exit 0