Skip to content

Instantly share code, notes, and snippets.

@naamahdaemon
Last active August 6, 2023 13:23
Show Gist options
  • Save naamahdaemon/9035f0964c5c472759cb83629ff6437e to your computer and use it in GitHub Desktop.
Save naamahdaemon/9035f0964c5c472759cb83629ff6437e to your computer and use it in GitHub Desktop.
Get colored logs from mina logs
#!/bin/bash
# Default values for optional arguments
start_date="1 day ago"
end_date=""
fulllog=false
blockonly=false
block=""
log=""
live=false
mtail=""
hash_values=()
while getopts ":S:E:h:fbt" opt; do
case $opt in
S) start_date="$OPTARG" ;;
E) end_date="$OPTARG" ;;
f | fulllog) fulllog=true ;;
b | block) blockonly=true ;;
t | tail) live=true ;;
h) IFS=',' read -r -a hash_values <<< "$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
:) echo "Option -$OPTARG requires an argument." >&2; exit 1 ;;
esac
done
# Shift to the next argument after the options have been processed
shift "$((OPTIND-1))"
# Now you can access the arguments using $start_date, $end_date, and ${hash_values[@]}
echo "Start Date: $start_date"
echo "End Date: $end_date"
echo "Hash Values: ${hash_values[@]}"
# Use the variable in your script logic
if $fulllog; then
echo "Full logging enabled."
log=""
else
echo "Full logging disabled."
log=" /.*[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+/!d; "
fi
if $blockonly; then
block=" /.*block.*/!d; /.*Received a block from .*/d; /.*Saw block with state hash.*/d; /.*Duplicate producer and slot:.*/d; /.*Not rebroadcasting block.*/d; s/(.*Error when sending block with state hash.*)/\x1b[1;37;41m\1\x1b[0m/g; "
fi
if $live; then
mtail="-f"
fi
until=""
if [ "$end_date" != "" ]; then
until="--until"
echo "$end_date"
until=" --until \"$end_date\" "
fi
hash_string=""
for hash in "${hash_values[@]}"; do
hash_string+=' s/'"$hash"'/\x1b[32m&\x1b[0m/g; '
done
echo "Hash String: ${hash_string}"
if [ -n "$end_date" ]; then
journalctl --user -u mina --no-pager -S "$start_date" --until "$end_date" $mtail | sed -E 's/(.*synced.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Bootstrap state: complete.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Mina daemon is synced.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Not rebroadcasting block.*)/\x1b[32m\1\x1b[0m/g; s/(.*Generated from consensus at slot.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Block producer won slot.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Producing a block in.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Producing new block with parent.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Successfully produced a new block.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Build breadcrumb on produced block.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Generated transition \$state_hash was accepted into transition frontier.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Generating crash report.*)/\x1b[31m\1\x1b[0m/g; s/(.*Internally generated block \$state_hash cannot be rebroadcast because it.s not a valid time to do so.*)/\x1b[1;97;41m\1\x1b[0m/g; s/(.*\[Error\].*)/\x1b[31m\1\x1b[0m/g; s/(.*mina\[[0-9]+\]:.[^0-9].*)/\x1b[90m\1\x1b[0m/g; s/(.*Syncing local state;.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Synchronizing consensus local state.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Saw block with state.*)/\x1b[90m\1\x1b[0m/g; s/(.*Updating new available work took.*)/\x1b[90m\1\x1b[0m/g; s/(.*Internally generated block \$state_hash cannot be rebroadcast because it.s not a valid time to do so.*)/\x1b[1;97;41m\1\x1b[0m/g; '"$hash_string"' '"$log"' '"$block"''
else
journalctl --user -u mina --no-pager -S "$start_date" $mtail | sed -E 's/(.*synced.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Bootstrap state: complete.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Mina daemon is synced.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Not rebroadcasting block.*)/\x1b[32m\1\x1b[0m/g; s/(.*Generated from consensus at slot.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Block producer won slot.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Producing a block in.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Producing new block with parent.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Successfully produced a new block.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Build breadcrumb on produced block.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Generated transition \$state_hash was accepted into transition frontier.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Generating crash report.*)/\x1b[31m\1\x1b[0m/g; s/(.*Internally generated block \$state_hash cannot be rebroadcast because it.s not a valid time to do so.*)/\x1b[1;97;41m\1\x1b[0m/g; s/(.*\[Error\].*)/\x1b[31m\1\x1b[0m/g; s/(.*mina\[[0-9]+\]:.[^0-9].*)/\x1b[90m\1\x1b[0m/g; s/(.*Syncing local state;.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Synchronizing consensus local state.*)/\x1b[37m\x1b[48;5;22m\1\x1b[0m/g; s/(.*Saw block with state.*)/\x1b[90m\1\x1b[0m/g; s/(.*Updating new available work took.*)/\x1b[90m\1\x1b[0m/g; s/(.*Internally generated block \$state_hash cannot be rebroadcast because it.s not a valid time to do so.*)/\x1b[1;97;41m\1\x1b[0m/g; '"$hash_string"' '"$log"' '"$block"''
fi
@naamahdaemon
Copy link
Author

naamahdaemon commented Aug 5, 2023

Description

This simple shell script outputs colored/formatted logs from the mina daemon making it easier to read and outline important information.
Script takes several input parameters :

  • -S : Start date in systemd journalctl format
  • -E: End date in systemd journalctl format
  • -h : Array of special keyword to be highlighted : all keeywords specified here (comma separated values) will be highlighted in green in log output.
  • -f flag display full logs (only timestamped logs are displayed if you omit this flag)
  • -b flag displays only logs related to block production or uptime.

Example - looking for a specific time frame logs with specific block hashes highlighted

./check_logs.sh -h "418077,3NKvXDSqoNongt2e8pcTTXakDnMEkPqAnJpa6HPFgTFB7cyjqXPU,3NKvbSTFuBrism68rRSPEh3Pcm5RPPnAArucgTb4RT2arTUz3vXC" -S "2023-08-05 01:49:00" -E "2023-08-05 2:00:00"

Will display formatted logs of the mina daemon between 2023/08/05 01:49 and 2023/08/05 2:00 and will highlight some block hash for slot 418077.

image

Example 2 - looking for a bloc production details in a specific time frame to check everything went well

Use a block production time frame and check everything is ok

./check_logs.sh -S "2023-08-04 11:28:00" -E "2023-08-04 11:40:00"

Block production specific lines will be automatically highlighted :

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment