|
| 1 | +import time |
| 2 | +import subprocess |
| 3 | +import json |
| 4 | +from prometheus_client import start_http_server, Gauge |
| 5 | + |
| 6 | +# Define the metrics |
| 7 | +log_line_count = Gauge('syslog_line_count', 'Number of lines in /var/log/syslog') |
| 8 | +archway_balance_metric = Gauge('archway_balance', 'Balance in Archway Relay Wallet') |
| 9 | +icon_balance_metric = Gauge('icon_balance', 'Balance in Icon Relay Wallet') |
| 10 | +service_status = Gauge('relayer_node_status', 'Relayer node status (active or inactive)', ['service_name']) |
| 11 | +icon_chain_metric = Gauge('icon_block_height', 'Extracted values from the log', ['chain_name', 'chain_id']) |
| 12 | +archway_chain_metric = Gauge('archway_block_height', 'Extracted values from the log', ['chain_name', 'chain_id']) |
| 13 | +command_archway = "rly q balance archway default | grep -oP 'balance \{\K[^}]+' | sed 's/aconst//'" |
| 14 | +command_icon = "rly q balance icon relayer_wallet | grep -oP 'balance \{\K[^}]+' | sed 's/ICX//'" |
| 15 | +command_icon_height="grep 'icon' /home/ubuntu/.relayer/relay.log | tail -n 1 | grep -o '{.*}' | sed 's/latest_height/height/g'" |
| 16 | + |
| 17 | +command_archway_height="grep 'archway' /home/ubuntu/.relayer/relay.log | tail -n 1 | grep -o '{.*}' | sed 's/latest_height/height/g'" |
| 18 | + |
| 19 | +# Define a function to fetch and set the metric values |
| 20 | +def fetch_metrics(): |
| 21 | + try: |
| 22 | + # Run the 'wc -l' command on the syslog file and parse the result |
| 23 | + output = subprocess.check_output(['wc', '-l', '/var/log/syslog']) |
| 24 | + line_count = int(output.split()[0]) # Extract the line count |
| 25 | + log_line_count.set(line_count) |
| 26 | + archway_balance_output = subprocess.check_output(command_archway, shell=True, text=True) |
| 27 | + # print(archway_balance_output) |
| 28 | + archway_balance_value = archway_balance_output.strip() |
| 29 | + archway_balance_metric.set(archway_balance_value) # Assuming balance is a numeric value |
| 30 | + icon_balance_output = subprocess.check_output(command_icon, shell=True, text=True) |
| 31 | + # print(icon_balance_output) |
| 32 | + icon_balance_value = icon_balance_output.strip() |
| 33 | + icon_balance_metric.set(icon_balance_value) # Assuming balance is a numeric value |
| 34 | + # Check relayer node status |
| 35 | + status = subprocess.getoutput(f'systemctl is-active ibc-relayer.service') |
| 36 | + service_status.labels(service_name='ibc-relayer.service').set(1 if status == 'active' else 0) |
| 37 | + # Get block height - icon |
| 38 | + icon_output = subprocess.check_output(command_icon_height, shell=True, text=True, executable='/bin/bash') |
| 39 | + # print(icon_output) |
| 40 | + json_data = json.loads(icon_output) |
| 41 | + chain_name = json_data['chain_name'] |
| 42 | + chain_id = json_data['chain_id'] |
| 43 | + height = json_data['height'] |
| 44 | + # print(f'{chain_id},{chain_name},{height}') |
| 45 | + icon_chain_metric.labels(chain_name=chain_name, chain_id=chain_id).set(height) |
| 46 | + # Get block height - archway |
| 47 | + archway_output = subprocess.check_output(command_archway_height, shell=True, text=True, executable='/bin/bash') |
| 48 | + # print(archway_output) |
| 49 | + json_data = json.loads(archway_output) |
| 50 | + if 'error' in json_data: |
| 51 | + error_message = json_data['error'] |
| 52 | + # Handle the error message, for example, by logging it or raising an exception. |
| 53 | + print(f"Error: {error_message}") |
| 54 | + # chain_name = json_data['chain_name'] |
| 55 | + # chain_id = json_data['chain_id'] |
| 56 | + # archway_chain_metric.labels(chain_name=chain_name, chain_id=chain_id).set(2434508) |
| 57 | + |
| 58 | + else: |
| 59 | + chain_name = json_data['chain_name'] |
| 60 | + chain_id = json_data['chain_id'] |
| 61 | + height = json_data['height'] |
| 62 | + # print(f'{chain_id},{chain_name},{height}') |
| 63 | + archway_chain_metric.labels(chain_name=chain_name, chain_id=chain_id).set(height) |
| 64 | + except Exception as e: |
| 65 | + print(f"Failed to fetch metrics: {e}") |
| 66 | + |
| 67 | +if __name__ == '__main__': |
| 68 | + # Start an HTTP server for Prometheus to scrape the metrics |
| 69 | + start_http_server(8000) |
| 70 | + |
| 71 | + # Run the commands and update the metric values every 5 seconds |
| 72 | + while True: |
| 73 | + fetch_metrics() |
| 74 | + time.sleep(5) |
0 commit comments