ethereumをeth-netstatで状態監視する

完成イメージ

仕組みはこんな感じ


※dockerコンテナをホスト側から操作するので各gethのポート番号は異なります。

監視対象はこちらで作成したdockerコンテナ
API(eth-net-intelligence-api)とWeb(eth-netstats)はdockerのホスト上に設置します(dockerコンテナ化出来たらまた刷新するかも)

共通

・npmのパッケージインストール
sudo npm install -g grunt-cli
sudo npm install -g pm2
sudo npm install -g primus

eth-net-intelligence-apiのインストール

cd ~/
git clone https://github.com/cubedro/eth-net-intelligence-api
cd eth-net-intelligence-api
bin/update.sh
※rootの実行権限が必要なのでsudoできるユーザかrootで実行要

cd eth-net-inteligence-api
npm install
cat - > process.json
---
[

  {
    "name"              : "node01",
    "cwd"               : "/home/yosshi/eth-net-intelligence-api",
    "script"            : "app.js",
    "log_date_format"   : "YYYY-MM-DD HH:mm Z",
    "log_file"          : "/home/yosshi/logs/node01-app-log.log",
    "out_file"          : "/home/yosshi/logs/node01-app-out.log",
    "error_file"        : "/home/yosshi/logs/node01-app-err.log",
    "merge_logs"        : false,
    "watch"             : false,
    "max_restarts"      : 10,
    "exec_interpreter"  : "node",
    "exec_mode"         : "cluster_mode",
    "env":
    {
      "NODE_ENV"        : "test",
      "RPC_HOST"        : "localhost",
      "RPC_PORT"        : "8546",
      "LISTENING_PORT"  : "30304",
      "INSTANCE_NAME"   : "node01",
      "CONTACT_DETAILS" : "",
      "WS_SERVER"       : "http://127.0.0.1:53000",
      "WS_SECRET"       : "secret",
      "VERBOSITY"       : 2
    }
  },
  {
    "name"              : "node02",
    "cwd"               : "/home/yosshi/eth-net-intelligence-api",
    "script"            : "app.js",
    "log_date_format"   : "YYYY-MM-DD HH:mm Z",
    "log_file"          : "/home/yosshi/logs/node02-app-log.log",
    "out_file"          : "/home/yosshi/logs/node02-app-out.log",
    "error_file"        : "/home/yosshi/logs/node02-app-err.log",
    "merge_logs"        : false,
    "watch"             : false,
    "max_restarts"      : 10,
    "exec_interpreter"  : "node",
    "exec_mode"         : "cluster_mode",
    "env":
    {
      "NODE_ENV"        : "test",
      "RPC_HOST"        : "localhost",
      "RPC_PORT"        : "8547",
      "LISTENING_PORT"  : "30305",
      "INSTANCE_NAME"   : "node02",
      "CONTACT_DETAILS" : "",
      "WS_SERVER"       : "http://127.0.0.1:53000",
      "WS_SECRET"       : "secret",
      "VERBOSITY"       : 2
    }
  }
]
---
※各pathやWS_SECRETは環境にあわせてください

pm2 start process.json

・その他のコマンド
pm2 list
pm2 monit
pm2 show node01
pm2 restart node01
pm2 kill

eth-netstats

cd ~/
git clone https://github.com/cubedro/eth-netstats
cd eth-netstats
npm install
grunt all
export PORT=53000; export WS_SECRET=secret; npm start

ブラウザで http://localhost:53000/ で接続できる。

docker環境のethereumをプライベートで接続する

環境

node1とnode2を接続する

IPアドレスを調べる

% sudo docker inspect ethereum-node-01 | grep \"IPAddress |grep -v \"\"
                    "IPAddress": "172.22.0.2",

% sudo docker inspect ethereum-node-02 | grep \"IPAddress |grep -v \"\"
                    "IPAddress": "172.22.0.3",

※admin.addPeerする際にホスト名ではなく、IPアドレスが必要

疎通を確認

% sudo docker exec -it ethereum-node-01 ping ethereum-node-02
PING ethereum-node-02 (172.22.0.3): 56 data bytes
64 bytes from 172.22.0.3: seq=0 ttl=64 time=0.232 ms
64 bytes from 172.22.0.3: seq=1 ttl=64 time=0.246 ms

% sudo docker exec -it ethereum-node-02 ping ethereum-node-01
PING ethereum-node-01 (172.22.0.2): 56 data bytes
64 bytes from 172.22.0.2: seq=0 ttl=64 time=0.480 ms
64 bytes from 172.22.0.2: seq=1 ttl=64 time=0.203 ms

node02のnodeInfoを取得

% sudo docker exec -it ethereum-node-02 geth attach rpc:http://localhost:8545

> admin.nodeInfo.enode
"enode://cfb24fe963ad802e261619235033e38dea0e0f2de63064bfd494e58fc7bb5fb86d9a8e4bed2d4664b35ded123e33b5f006db08df1333ab4a82bb1ed2ba1d767b@[::]:30303?discport=0"

node01からnode02へ接続

% sudo docker exec -it ethereum-node-01 geth attach rpc:http://localhost:8545

> admin.addPeer("enode://cfb24fe963ad802e261619235033e38dea0e0f2de63064bfd494e58fc7bb5fb86d9a8e4bed2d4664b35ded123e33b5f006db08df1333ab4a82bb1ed2ba1d767b@172.22.0.3:30303?discport=0")
true
※[::]だったところを、node02のIPアドレスに変更する

> admin.peers
[{
    caps: ["eth/62", "eth/63"],
    id: "cfb24fe963ad802e261619235033e38dea0e0f2de63064bfd494e58fc7bb5fb86d9a8e4bed2d4664b35ded123e33b5f006db08df1333ab4a82bb1ed2ba1d767b",
    name: "Geth/v1.8.0-unstable-0662384d/linux-amd64/go1.9.3",
    network: {
      localAddress: "172.22.0.2:35260",
      remoteAddress: "172.22.0.3:30303"
    },
    protocols: {
      eth: {
        difficulty: 278121932,
        head: "0x80c11e45770fa315c24d56469bbf52f7a635d2a404ffee2a19ef90408f625151",
        version: 63
      }
    }
}]

> net.peerCount
1

> eth.blockNumber
2229

node02で接続を確認する

% sudo docker exec -it ethereum-node-02 geth attach rpc:http://localhost:8545

> admin.peers
[{
    caps: ["eth/62", "eth/63"],
    id: "021b697ab806c28ec99aa9dc601308b8b77d6659d8cb7fcde758bb2b1af813f3cc036476ed59e95246e85a5ff7b66ac2d1ec8ef4a9dcceb9bb0a245ff7089af0",
    name: "Geth/v1.8.0-unstable-0662384d/linux-amd64/go1.9.3",
    network: {
      localAddress: "172.22.0.3:30303",
      remoteAddress: "172.22.0.2:35748"
    },
    protocols: {
      eth: {
        difficulty: 433531171,
        head: "0xd1510332bc8cc51396d7b07861ad9d2e6e568da1980684cba917345f649f0ffb",
        version: 63
      }
    }
}]

> net.peerCount
1

> eth.blockNumber
2229
※同期できていれば同じ値になっている