File: statusd_bitcoin.lua

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (55 lines) | stat: -rw-r--r-- 1,268 bytes parent folder | download | duplicates (6)
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
-- Authors: Voker57 <voker57@gmail.com>
-- License: Public domain
-- Last Changed: Unknown
--
-- Bitcoin (฿) daemon monitor

-- %bitcoin_speed - current speed in khash/s
-- %bitcoin_balance - current balance

-- by Voker57 <voker57@gmail.com>
-- Public domain

local defaults={
    update_interval=30 * 1000
}

local settings=table.join(statusd.get_config("bitcoin"), defaults)

local bitcoin_timer

local function get_bitcoin_speed()
	local hps = io.popen("bitcoind gethashespersec 2>/dev/null")
	if not hps then
		return "N/A"
	end
	local answ = hps:read("*a")
	if not answ  or answ == "" then
		return "N/A"
	end
	return string.format("%0.2f", answ / 1000)
end

local function get_bitcoin_balance()
	local bl = io.popen("bitcoind getbalance 2>/dev/null")
	if not bl then
		return "N/A"
	end
	local answ = bl:read("*a")
	if not answ or answ == "" then
		return "N/A"
	end
	return string.format("%0.2f", answ)
end

local function update_bitcoin()
	local bitcoin_speed = get_bitcoin_speed()
	local bitcoin_balance = get_bitcoin_balance()
	statusd.inform("bitcoin_balance", bitcoin_balance)
	statusd.inform("bitcoin_speed", bitcoin_speed)
	bitcoin_timer:set(settings.update_interval, update_bitcoin)
end

-- Init
bitcoin_timer=statusd.create_timer()
update_bitcoin()