Last active
February 25, 2020 12:17
-
-
Save muxueqz/6ecbe7a08beb1e113ff504cf08d3ccc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import json | |
import urllib2 | |
import base64 | |
#from time import time | |
from datetime import date | |
import sys | |
#ckb_rpc = 'http://127.0.0.1:18114' | |
explorer_server_api = sys.argv[1] | |
ckb_rpc = sys.argv[2] | |
def get_current_epoch(): | |
data = '{"jsonrpc":"2.0","method":"get_current_epoch","params":[],"id":2}' | |
req = urllib2.Request(ckb_rpc, data) | |
req.add_header('content-type', 'application/json') | |
r = urllib2.urlopen(req) | |
result = json.load(r) | |
return int(result['result']['number'], 16) | |
# urllib2.urlopen('ep') | |
def get_data(url): | |
req = urllib2.Request(url) | |
req.add_header('User-Agent', 'Nervos Monitor Agent') | |
req.add_header('content-type', 'application/vnd.api+json') | |
req.add_header('Accept', 'application/vnd.api+json') | |
r = urllib2.urlopen(req) | |
data = json.load(r) | |
return data['data'] | |
def check_chart_by_epoch(metric): | |
url = '%s/epoch_statistics/%s' % (explorer_server_api, metric) | |
epochs = {} | |
for i in get_data(url): | |
epoch = int(i['attributes']['epoch_number']) | |
epochs[epoch] = i | |
data_epoch = max(epochs.keys()) | |
current_epoch = get_current_epoch() | |
if current_epoch - data_epoch > 2: | |
return False | |
return True | |
def check_chart_by_day(metric): | |
url = '%s/daily_statistics/%s' % (explorer_server_api, metric) | |
today_timestamp = int(date.today().strftime('%s')) | |
yesterday_timestamp = today_timestamp - 86400 | |
yesterday_timestamp = today_timestamp - 1 | |
timestamps = {} | |
for i in get_data(url): | |
ts = int(i['attributes']['created_at_unixtimestamp']) | |
timestamps[ts] = i | |
data_timestamp = max(timestamps.keys()) | |
if data_timestamp < yesterday_timestamp: | |
return False | |
return True | |
def main(): | |
epoch_metrics = 'difficulty-hash_rate difficulty-uncle_rate'.split() | |
for i in epoch_metrics: | |
status = check_chart_by_epoch(i) | |
if status != True: | |
print('epoch-%s is failure' % i) | |
exit() | |
break | |
daily_metrics = ''' | |
avg_difficulty avg_hash_rate uncle_rate addresses_count | |
transactions_count total_depositors_count-total_dao_deposit live_cells_count-dead_cells_count | |
'''.split() | |
for i in daily_metrics: | |
status = check_chart_by_day(i) | |
if status != True: | |
print('daily-%s is failure' % i) | |
exit() | |
break | |
print('success') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment