|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Monitor Cosmos RPC abci_info endpoint at regular intervals |
| 4 | + |
| 5 | +show_help() { |
| 6 | + cat << EOF |
| 7 | +Usage: $(basename "$0") [OPTIONS] |
| 8 | +
|
| 9 | +Monitor Cosmos RPC abci_info endpoint and display results with timing information. |
| 10 | +
|
| 11 | +OPTIONS: |
| 12 | + --block Track only block height changes from abci_info response |
| 13 | + --node <URL> Override default RPC endpoint (default: https://shannon-grove-rpc.mainnet.poktroll.com) |
| 14 | + --help Show this help message and exit |
| 15 | +
|
| 16 | +EXAMPLES: |
| 17 | + $(basename "$0") |
| 18 | + Monitor full abci_info response using default endpoint |
| 19 | +
|
| 20 | + $(basename "$0") --block |
| 21 | + Monitor only block height changes |
| 22 | +
|
| 23 | + $(basename "$0") --node http://localhost:26657 |
| 24 | + Use custom RPC endpoint |
| 25 | +
|
| 26 | + $(basename "$0") --block --node http://localhost:26657 |
| 27 | + Monitor block height using custom endpoint |
| 28 | +
|
| 29 | +EOF |
| 30 | + exit 0 |
| 31 | +} |
| 32 | + |
| 33 | +# Parse command line arguments |
| 34 | +MONITOR_BLOCK=false |
| 35 | +RPC_ENDPOINT="https://shannon-grove-rpc.mainnet.poktroll.com" |
| 36 | + |
| 37 | +while [[ $# -gt 0 ]]; do |
| 38 | + case $1 in |
| 39 | + --block) |
| 40 | + MONITOR_BLOCK=true |
| 41 | + shift |
| 42 | + ;; |
| 43 | + --node) |
| 44 | + if [ -z "$2" ] || [[ "$2" == --* ]]; then |
| 45 | + echo "Error: --node requires a URL argument" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + RPC_ENDPOINT="$2" |
| 49 | + shift 2 |
| 50 | + ;; |
| 51 | + --help) |
| 52 | + show_help |
| 53 | + ;; |
| 54 | + *) |
| 55 | + echo "Unknown option: $1" |
| 56 | + echo "Use --help for usage information" |
| 57 | + exit 1 |
| 58 | + ;; |
| 59 | + esac |
| 60 | +done |
| 61 | + |
| 62 | +# Print configuration mode at the beginning |
| 63 | +if [ "$MONITOR_BLOCK" = true ]; then |
| 64 | + echo "📊 MODE: BLOCK HEIGHT MONITORING" |
| 65 | +else |
| 66 | + echo "🔍 MODE: ABCI INFO MONITORING" |
| 67 | +fi |
| 68 | +echo "🌐 ENDPOINT: $RPC_ENDPOINT" |
| 69 | +echo "" |
| 70 | + |
| 71 | +echo "Starting Cosmos RPC abci_info monitor..." |
| 72 | +echo "Press Ctrl+C to stop" |
| 73 | +echo "" |
| 74 | + |
| 75 | +# Initialize variables for delta calculation |
| 76 | +last_request_time=0 |
| 77 | +last_block_height="" |
| 78 | + |
| 79 | +while true; do |
| 80 | + # Get current timestamp for display and delta calculation |
| 81 | + current_time=$(date +%s.%N) |
| 82 | + timestamp=$(date '+%Y-%m-%d %H:%M:%S') |
| 83 | + |
| 84 | + # Calculate delta from last request |
| 85 | + if [ "$last_request_time" != "0" ]; then |
| 86 | + delta=$(echo "$current_time - $last_request_time" | bc -l) |
| 87 | + delta_formatted=$(printf "%.2f" "$delta") |
| 88 | + |
| 89 | + # Color code based on delta (assuming 0.5s target interval) |
| 90 | + # Green: <= 0.7s (quick), Yellow: 0.7-1.5s (moderate), Red: > 1.5s (slow) |
| 91 | + if (( $(echo "$delta <= 0.7" | bc -l) )); then |
| 92 | + delta_color="\033[32m" # Green |
| 93 | + delta_status="⚡" |
| 94 | + elif (( $(echo "$delta <= 1.5" | bc -l) )); then |
| 95 | + delta_color="\033[33m" # Yellow |
| 96 | + delta_status="⏱️" |
| 97 | + else |
| 98 | + delta_color="\033[31m" # Red |
| 99 | + delta_status="🐌" |
| 100 | + fi |
| 101 | + reset_color="\033[0m" |
| 102 | + delta_display=" ${delta_color}[Δ${delta_formatted}s ${delta_status}]${reset_color}" |
| 103 | + else |
| 104 | + delta_display="" |
| 105 | + fi |
| 106 | + |
| 107 | + # Make the curl request to abci_info endpoint |
| 108 | + response=$(curl -s "$RPC_ENDPOINT/abci_info" 2>&1) |
| 109 | + |
| 110 | + # Check if curl command was successful |
| 111 | + curl_exit_code=$? |
| 112 | + |
| 113 | + if [ $curl_exit_code -ne 0 ]; then |
| 114 | + echo -e "❌ [$timestamp] ERROR: curl failed with exit code $curl_exit_code: $response$delta_display" |
| 115 | + else |
| 116 | + # Check if response contains an error |
| 117 | + if echo "$response" | grep -q '"error"'; then |
| 118 | + echo -e "❌ [$timestamp] API ERROR: $response$delta_display" |
| 119 | + else |
| 120 | + if [ "$MONITOR_BLOCK" = true ]; then |
| 121 | + # Extract block height from abci_info response |
| 122 | + block_height=$(echo "$response" | jq -r '.result.response.last_block_height // empty' 2>/dev/null) |
| 123 | + |
| 124 | + if [ -z "$block_height" ] || [ "$block_height" = "null" ]; then |
| 125 | + echo -e "❌ [$timestamp] PARSE ERROR: Could not extract block height from response$delta_display" |
| 126 | + else |
| 127 | + # Check if block height changed |
| 128 | + if [ "$last_block_height" != "" ] && [ "$block_height" != "$last_block_height" ]; then |
| 129 | + height_change="📈" |
| 130 | + height_diff=$((block_height - last_block_height)) |
| 131 | + height_info=" (↑$height_diff)" |
| 132 | + else |
| 133 | + height_change="📊" |
| 134 | + height_info="" |
| 135 | + fi |
| 136 | + |
| 137 | + echo -e "✅ [$timestamp] $height_change Block Height: $block_height$height_info$delta_display" |
| 138 | + last_block_height="$block_height" |
| 139 | + fi |
| 140 | + else |
| 141 | + # Display full abci_info response details |
| 142 | + app_name=$(echo "$response" | jq -r '.result.response.data // "N/A"' 2>/dev/null) |
| 143 | + version=$(echo "$response" | jq -r '.result.response.version // "N/A"' 2>/dev/null) |
| 144 | + block_height=$(echo "$response" | jq -r '.result.response.last_block_height // "N/A"' 2>/dev/null) |
| 145 | + |
| 146 | + if [ "$app_name" = "null" ] || [ "$app_name" = "" ]; then |
| 147 | + app_name="N/A" |
| 148 | + fi |
| 149 | + if [ "$version" = "null" ] || [ "$version" = "" ]; then |
| 150 | + version="N/A" |
| 151 | + fi |
| 152 | + if [ "$block_height" = "null" ] || [ "$block_height" = "" ]; then |
| 153 | + block_height="N/A" |
| 154 | + fi |
| 155 | + |
| 156 | + echo -e "✅ [$timestamp] ABCI Info - App: $app_name, Version: $version, Height: $block_height$delta_display" |
| 157 | + fi |
| 158 | + fi |
| 159 | + fi |
| 160 | + |
| 161 | + # Update last request time for next iteration |
| 162 | + last_request_time="$current_time" |
| 163 | + |
| 164 | + # Wait 0.5 seconds |
| 165 | + sleep 0.5 |
| 166 | +done |
0 commit comments