-
Notifications
You must be signed in to change notification settings - Fork 3
/
zbx-bitcoin-status.sh
executable file
·184 lines (161 loc) · 6.08 KB
/
zbx-bitcoin-status.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bash
bitcoin_cli="bitcoin-cli"
bitcoin_cli_options=""
zabbix_sender="zabbix_sender -c /etc/zabbix/zabbix_agentd.conf"
measurement_rate="60s"
estimatesmartfee_targets=(
1 # 10 minutes
2 # 20 minutes
3 # 30 minutes
4 # 40 minutes
6 # 1 hour
12 # 2 hours
24 # 4 hours
48 # 8 hours
72 # 12 hours
108 # 18 hours
144 # 1 day
504 # 3 days
1008 # 1 week
)
# 30 fee rate groups is the limit
mempool_fee_histogram_rate_groups=(
0
2
3
4
5
6
7
8
10
12
14
17
20
25
30
40
50
60
70
80
100
120
140
170
200
250
300
400
500
600
)
lockdir="/tmp/$(basename "$0").lock"
if mkdir "$lockdir" 2> /dev/null; then
trap 'rm -rf "$lockdir"' 0
trap "exit 2" 1 2 3 15
else
exit 1
fi
echo "$$" > "$lockdir"/pid 2> /dev/null || exit 2
log_with_date()
{
echo "[$(date -u +"%Y-%m-%dT%H:%M:%SZ")] $*"
}
log_with_date "Starting"
# assume all first parameters beginning with dash are bitcoin-cli options
while (( ${#} > 0 )) && [[ ${1:0:1} == "-" ]]; do
bitcoin_cli_options="$bitcoin_cli_options $1"
shift
done
bitcoin_cli="$bitcoin_cli$bitcoin_cli_options"
log_with_date "$bitcoin_cli"
# check / wait for bitcoind to start
while ! $bitcoin_cli echo hello > /dev/null; do sleep 1s; done
# Check is fee histogram supported. If it is not, there will be error if
# [] argument is given and empty stdout output.
check="$($bitcoin_cli getmempoolinfo [0] 2> /dev/null)"
if [[ -n $check ]]; then
# create comma separated string from array
mempool_fee_histogram_rate_groups_str="${mempool_fee_histogram_rate_groups[*]}"
mempool_fee_histogram_rate_groups_arg="[${mempool_fee_histogram_rate_groups_str// /,}]"
else
mempool_fee_histogram_rate_groups_arg=""
fi
while :; do
log_with_date "Requesting bitcoind status"
blockchain_info="$($bitcoin_cli getblockchaininfo)"
if [[ -z "$blockchain_info" ]]; then
# empty response here means bitcoind is not responding, retry after 1 sec.
log_with_date "... empty response"
sleep 1s
continue
fi
blockchain_tip_blocks="$(jq ".blocks" <<< "$blockchain_info")"
blockchain_tip_headers="$(jq ".headers" <<< "$blockchain_info")"
blockchain_verificationprogress="$(jq ".verificationprogress" <<< "$blockchain_info")"
blockchain_size_on_disk="$(jq ".size_on_disk" <<< "$blockchain_info")"
network_info="$($bitcoin_cli getnetworkinfo)"
bitcoind_version="$(jq ".version" <<< "$network_info")"
bitcoind_subversion="$(jq ".subversion" <<< "$network_info")"
bitcoind_protocolversion="$(jq ".protocolversion" <<< "$network_info")"
bitcoind_connections="$(jq ".connections" <<< "$network_info")"
bitcoind_connections_in="$(jq ".connections_in" <<< "$network_info")"
bitcoind_connections_out="$(jq ".connections_out" <<< "$network_info")"
if [[ -n $mempool_fee_histogram_rate_groups_arg ]]; then
mempool_info="$($bitcoin_cli getmempoolinfo "$mempool_fee_histogram_rate_groups_arg")"
mempool_fee_histogram="$(jq ".fee_histogram" <<< "$mempool_info")"
else
mempool_info="$($bitcoin_cli getmempoolinfo)"
mempool_fee_histogram=""
fi
mempool_tx_count="$(jq ".size" <<< "$mempool_info")"
mempool_size_vbytes="$(jq ".bytes" <<< "$mempool_info")"
mempool_usage_bytes="$(jq ".usage" <<< "$mempool_info")"
mempool_maxmempool_bytes="$(jq ".maxmempool" <<< "$mempool_info")"
mempool_mempoolminfee="$(bc <<< "$(echo "$mempool_info" | grep ".mempoolminfee" | grep -Eo "[0-9]+\.[0-9]+") * 100000")"
rpc_active_commands="$($bitcoin_cli getrpcinfo | jq ".active_commands | length")"
# -1, to not count us
rpc_active_commands=$(( rpc_active_commands - 1 ))
set -x
$zabbix_sender -k bitcoin.blockchain.tip.blocks -o "$blockchain_tip_blocks"
$zabbix_sender -k bitcoin.blockchain.tip.headers -o "$blockchain_tip_headers"
$zabbix_sender -k bitcoin.blockchain.verificationprogress -o "$blockchain_verificationprogress"
$zabbix_sender -k bitcoin.blockchain.size_on_disk -o "$blockchain_size_on_disk"
$zabbix_sender -k bitcoin.version -o "$bitcoind_version"
$zabbix_sender -k bitcoin.subversion -o "$bitcoind_subversion"
$zabbix_sender -k bitcoin.protocolversion -o "$bitcoind_protocolversion"
$zabbix_sender -k bitcoin.connections.num -o "$bitcoind_connections"
$zabbix_sender -k bitcoin.connections_in.num -o "$bitcoind_connections_in"
$zabbix_sender -k bitcoin.connections_out.num -o "$bitcoind_connections_out"
$zabbix_sender -k bitcoin.mempool.tx_count -o "$mempool_tx_count"
$zabbix_sender -k bitcoin.mempool.size_vbytes -o "$mempool_size_vbytes"
$zabbix_sender -k bitcoin.mempool.usage_bytes -o "$mempool_usage_bytes"
$zabbix_sender -k bitcoin.mempool.maxmempool_bytes -o "$mempool_maxmempool_bytes"
$zabbix_sender -k bitcoin.mempool.mempoolminfee -o "$mempool_mempoolminfee"
$zabbix_sender -k bitcoin.rpc.active_commands.num -o "$rpc_active_commands"
set +x
if [[ -n $mempool_fee_histogram ]]; then
readarray -t sizes < <(echo "$mempool_fee_histogram" | grep sizes | grep -Eo "[0-9]+")
for i in $(seq 0 $(( ${#mempool_fee_histogram_rate_groups[@]} - 1 )) ); do
set -x
$zabbix_sender \
-k bitcoin.mempool.fee_group_vbytes["${mempool_fee_histogram_rate_groups[$i]}"] \
-o "${sizes[$i]}"
set +x
done
fi
for i in $(seq 0 $(( ${#estimatesmartfee_targets[@]} - 1 )) ); do
# Can't use jq here as it converts some numbers to scientific notation
# which bc does not like. So use grep instead.
feerate="$(bc <<< "$($bitcoin_cli estimatesmartfee "${estimatesmartfee_targets[$i]}" | grep ".feerate" | grep -Eo "[0-9]+\.[0-9]+") * 100000")"
set -x
$zabbix_sender \
-k bitcoin.estimatesmartfee["${estimatesmartfee_targets[$i]}"] \
-o "$feerate"
set +x
done
log_with_date "Sleeping for $measurement_rate"
sleep "$measurement_rate"
done