zoukankan      html  css  js  c++  java
  • 以太坊api访问,区块同步监测

    以太坊geth api访问,区块同步监测

    curl查询geth区块高度
    supervisor管理以太坊geth进程
    geth进程健康检查

    # curl访问geth api

    #使用curl访问geth api查询区块高度
    curl -s -X POST -H "Content-Type":application/json 
    --data '{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}' 
     localhost:8545 |awk -F'"' '{print $(NF-1)}'
    
    #如上,查询结果为十六进制
    #在shell终端查看十进制区块高度
    echo $((`curl -s -X POST -H "Content-Type":application/json --data 
     '{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}' 
     localhost:8545 |awk -F'"' '{print $(NF-1)}'`))
    

    # 使用supervisor管理以太坊geth进程

    #安装启动supervisor(ubuntu)
    apt-get install -y supervisor
    systemctl start supervisor
    systemctl enable supervisor
    
    #配置geth
    mkdir -p /var/log/geth
    vim /etc/supervisor/conf.d/geth.conf 
    #配置文件如下 
    
    [program:geth]
    command=/opt/geth/geth --rpc --rpcapi web3,eth,net,db,personal --rpcaddr 0.0.0.0 --rpcport 8545
    directory=/opt/geth
    user=root
    autostart=true
    autorestart=true
    startretries=9999
    exitcodes=0
    stopsignal=KILL
    stopwaitsecs=10
    redirect_stderr=true
    logfile_backups=10
    stdout_logfile_maxbytes=8MB
    stdout_logfile=/var/log/geth/geth.log
    
    #使用supervisor启动geth
    supervisorctl reload
    supervisorctl restart geth
    

    有时geth进程运行正常,区块同步故障,需要检查区块高度是否增长
    使用curl访问api查询区块高度,间隔一段时间在查,对比没增长则重启进程
    shell分享如下:

    #!/bin/bash
    # check.geth.sh
    # By Elven , 2018-11-16
    #区块高度监控
    
    #定时任务
    #check blockchain
    #*/4 * * * * bash /opt/shell/check.geth.sh
    
    #var
    eth_api=localhost:8545
    Stime=180
    [ $1 -gt $Stime ] && { Stime=$1 ; }
    
    H1=$((`curl -s -X POST -H "Content-Type":application/json --data '{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}'  $eth_api |awk -F'"' '{print $(NF-1)}'`))
    
    sleep $Stime
    
    H2=$((`curl -s -X POST -H "Content-Type":application/json --data '{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}'  $eth_api |awk -F'"' '{print $(NF-1)}'`))
    
    if [[ $H1 -eq $H2 ]];then
        echo "geth restart at  $(date +%F" "%T)  block $H1" >>/tmp/geth.restart.log
        supervisorctl restart geth &>/dev/null
    fi
    
    
  • 相关阅读:
    内存的静态分配和动态分配
    C#中堆和栈的区别分析
    NHibernate中的API
    我心中的核心组件(可插拔的AOP)~大话开篇及目录
    简单ORM工具的设计和编写,自己项目中曾经用过的
    NHibernate.3.0.Cookbook第一章第六节Handling versioning and concurrency的翻译
    NHibernate.3.0.Cookbook第一章第五节Setting up a base entity class
    [NHibernate] Guid 作主键速度超慢的背后
    技术分析淘宝的超卖宝贝
    日志打屏对性能有多少影响
  • 原文地址:https://www.cnblogs.com/elvi/p/10205777.html
Copyright © 2011-2022 走看看