node_sync_status.sh
Overview
node_sync_status.sh is a Bash script designed to analyze a log file generated by a node process and determine how far behind the node is from the current real-world time based on the timestamp of the last finalized block. The script reads the log file, extracts the timestamp of the most recent finalized block, calculates the difference between this timestamp and the current system time, and outputs the node's synchronization status in a human-readable format.
This utility is particularly useful for monitoring the synchronization progress of nodes in distributed ledger or blockchain systems, where timely block finalization is critical.
Detailed Explanation
Script Behavior
The script receives a single input parameter, which is the path to the log file containing node output.
It verifies the existence of the log file.
Using pattern matching, it extracts the Unix timestamp of the last finalized block from the log.
It calculates the time difference between the current time and the block finalization time.
The output includes the timestamp of the last finalized block in a formatted date/time string and the number of hours the node is behind real time.
Variables
Variable Name | Description |
|---|---|
| The first argument passed to the script, expected to be a log file path. |
| Extracted Unix timestamp (seconds since epoch) of the last finalized block from the log file. |
| Current system time as Unix timestamp. |
| The difference between current time and block time in seconds. |
| The difference converted to hours (integer). |
Step-by-step Breakdown
Log file validation
if [ ! -e $LOG_FILE ]; then echo Log file $LOG_FILE not found. exit 1 fiChecks if the supplied log file exists.
If not, outputs an error message and exits with status code 1.
Extracting last finalized block time
NODE_BLOCK_TIME=$(grep -Po "Last finalized block data: seq_no: ([0-9]*).*time: \K[0-9]{10}" $1 | tail -n 1)Uses
grepwith Perl-compatible regex options:-Penables Perl regex.-ooutputs only the matched part.
The regex looks for the line containing:
"Last finalized block data: seq_no: <number> ... time: <10-digit timestamp>"
Extracts only the 10-digit timestamp.
Uses
tail -n 1to get the last occurrence (most recent block).
Get current time and compute difference
CURRENT_TIME=$(date +%s) TIME_DIFF_SECOND=$(( CURRENT_TIME-NODE_BLOCK_TIME )) TIME_DIFF=$(echo "$TIME_DIFF_SECOND / 3600" | bc)CURRENT_TIMEis the current Unix timestamp.TIME_DIFF_SECONDis the difference in seconds.TIME_DIFFconverts seconds into hours usingbcfor integer division.
Output synchronization status
echo "Synced up to $(echo $NODE_BLOCK_TIME | xargs -I {} date +"%Y-%m-%d %H:%M:%S" -d @{}) ($TIME_DIFF hours behind real time)"Converts the
NODE_BLOCK_TIMEtimestamp into a formatted date/time string.Prints a message indicating the last block time and the lag in hours.
Usage Example
Suppose you have a log file named node.log:
./node_sync_status.sh node.log
Expected output:
Synced up to 2024-06-01 12:34:56 (2 hours behind real time)
This indicates the node's last finalized block timestamp and how many hours it is behind the current time.
Implementation Details and Algorithms
Pattern Matching: The script uses a Perl-compatible regular expression with lookbehind (
\K) to precisely extract the timestamp from complex log lines. This avoids capturing unnecessary context.Time Calculations: It uses Unix timestamps for all time computations to ensure accuracy and simplicity.
Integer Division: The conversion from seconds to hours uses shell arithmetic and
bcto handle integer division, resulting in a whole number of hours.Robustness: The script exits early if the log file does not exist, preventing errors downstream.
Interaction with Other System Components
Log File Dependency: This script depends on the presence of a node log file that contains lines with the pattern
Last finalized block data: seq_no: <number> ... time: <timestamp>. The correctness of its output depends on the log file format.Monitoring Tools: This script can be integrated into monitoring frameworks or cron jobs to periodically check node synchronization status and alert on lag.
Time Synchronization: Assumes the system clock is accurate; discrepancies in system time may affect the reported synchronization lag.
Visual Diagram: Workflow of node_sync_status.sh
flowchart TD
A[Start: Receive log file path] --> B{Log file exists?}
B -- No --> C[Output error and exit]
B -- Yes --> D[Extract last finalized block timestamp using grep]
D --> E[Get current system time]
E --> F[Calculate time difference in seconds]
F --> G[Convert seconds to hours]
G --> H[Format last block timestamp to human-readable date]
H --> I[Output sync status message]
I --> J[End]
This flowchart illustrates the sequential steps taken by the script from receiving input to outputting the synchronization status.