Quick log to Telegram script

Hi community

Just wanted to pass on a quick script to parse caddy’s JSON logs and get a daily dump of common logs daily into Telegram.
It is assumed that you know how to create a Telegram bot and retrieve token & chat ID:


# Initialise variables
day=$(date +%Y-%m-%d)
ytday=$(date -d "yesterday 13:00" '+%d/%b/%Y')

# Extraction of JSON info
touch $filename
chmod 666 $filename

cat $logfile | jq -r " .. | .common_log? | select(. !=null and contains ("\"$ytday\"") and (contains ("\"<IP_TO_EXCLUDE_FROM_LOG>\"") | not))" >> $filename

sed -i '1i\
### Caddy log ###\
Range processed: yesterday\
Period: day\
Level of Output: INFO\
Type of Output: txt\
Host: $HOSTNAME' $filename

# Telegram commands
curl -F chat_id="$chat_id" -F text="$(head /var/log/caddy/$day-caddy.txt -n 6)" https://api.telegram.org/bot$token/sendMessage >/dev/null 2>&1
curl -F chat_id="$chat_id" -F document=@"/var/log/caddy/$day-caddy.txt" https://api.telegram.org/bot$token/sendDocument >/dev/null 2>&1

Hoping this can be helpful for some. I was lazy enough to parse and filter through the common_log but jq can help filter any field from the JSON file.


This topic was automatically closed after 14 days. New replies are no longer allowed.