Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add ibc-relayer custom exporter- prometheus #232

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions scripts/prometheus/collector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import time
import subprocess
import json
from prometheus_client import start_http_server, Gauge

# Define the metrics
log_line_count = Gauge('syslog_line_count', 'Number of lines in /var/log/syslog')
archway_balance_metric = Gauge('archway_balance', 'Balance in Archway Relay Wallet')
icon_balance_metric = Gauge('icon_balance', 'Balance in Icon Relay Wallet')
service_status = Gauge('relayer_node_status', 'Relayer node status (active or inactive)', ['service_name'])
icon_chain_metric = Gauge('icon_block_height', 'Extracted values from the log', ['chain_name', 'chain_id'])
archway_chain_metric = Gauge('archway_block_height', 'Extracted values from the log', ['chain_name', 'chain_id'])
command_archway = "rly q balance archway default | grep -oP 'balance \{\K[^}]+' | sed 's/aconst//'"
command_icon = "rly q balance icon relayer_wallet | grep -oP 'balance \{\K[^}]+' | sed 's/ICX//'"
command_icon_height="grep 'icon' /home/ubuntu/.relayer/relay.log | tail -n 1 | grep -o '{.*}' | sed 's/latest_height/height/g'"

command_archway_height="grep 'archway' /home/ubuntu/.relayer/relay.log | tail -n 1 | grep -o '{.*}' | sed 's/latest_height/height/g'"

# Define a function to fetch and set the metric values
def fetch_metrics():
try:
# Run the 'wc -l' command on the syslog file and parse the result
output = subprocess.check_output(['wc', '-l', '/var/log/syslog'])
line_count = int(output.split()[0]) # Extract the line count
log_line_count.set(line_count)
archway_balance_output = subprocess.check_output(command_archway, shell=True, text=True)
# print(archway_balance_output)
archway_balance_value = archway_balance_output.strip()
archway_balance_metric.set(archway_balance_value) # Assuming balance is a numeric value
icon_balance_output = subprocess.check_output(command_icon, shell=True, text=True)
# print(icon_balance_output)
icon_balance_value = icon_balance_output.strip()
icon_balance_metric.set(icon_balance_value) # Assuming balance is a numeric value
# Check relayer node status
status = subprocess.getoutput(f'systemctl is-active ibc-relayer.service')
service_status.labels(service_name='ibc-relayer.service').set(1 if status == 'active' else 0)
# Get block height - icon
icon_output = subprocess.check_output(command_icon_height, shell=True, text=True, executable='/bin/bash')
# print(icon_output)
json_data = json.loads(icon_output)
chain_name = json_data['chain_name']
chain_id = json_data['chain_id']
height = json_data['height']
# print(f'{chain_id},{chain_name},{height}')
icon_chain_metric.labels(chain_name=chain_name, chain_id=chain_id).set(height)
# Get block height - archway
archway_output = subprocess.check_output(command_archway_height, shell=True, text=True, executable='/bin/bash')
# print(archway_output)
json_data = json.loads(archway_output)
if 'error' in json_data:
error_message = json_data['error']
# Handle the error message, for example, by logging it or raising an exception.
print(f"Error: {error_message}")
# chain_name = json_data['chain_name']
# chain_id = json_data['chain_id']
# archway_chain_metric.labels(chain_name=chain_name, chain_id=chain_id).set(2434508)

else:
chain_name = json_data['chain_name']
chain_id = json_data['chain_id']
height = json_data['height']
# print(f'{chain_id},{chain_name},{height}')
archway_chain_metric.labels(chain_name=chain_name, chain_id=chain_id).set(height)
except Exception as e:
print(f"Failed to fetch metrics: {e}")

if __name__ == '__main__':
# Start an HTTP server for Prometheus to scrape the metrics
start_http_server(8000)

# Run the commands and update the metric values every 5 seconds
while True:
fetch_metrics()
time.sleep(5)
Loading