Skip to content

Commit

Permalink
use vcr to cache p2pool observer results to avoid spamming servers
Browse files Browse the repository at this point in the history
  • Loading branch information
snex committed Feb 24, 2023
1 parent a7c4c1a commit 1f82f51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ Zabbix Tools to Monitor XMR Mining Stats
## p2pool_stats
### Requirements
* ruby
* vcr gem (built with version 6.1.0)
* webmock gem (built with version 3.18.1)
### Setup
* Place the following in your zabbix-agent.conf
```
AllowKey=system.run[/usr/local/bin/p2pool_stats*]
```
* Add a User Defined Macro in your Zabbix called {$XMR_ADDR} with your XMR mining address for the value
* Ensure that /tmp/p2pool_data exists before running p2pool
* Use the following switches when you start p2pool: ```--data-api /tmp/p2pool_data --local-api```
* Place p2pool_stats into /usr/local/bin and make sure it is executable for the Zabbix user
* Create an Item in Zabbix with Key
* Create an Item (type Text) in Zabbix with Key
```system.run[/usr/local/bin/p2pool_stats {$XMR_ADDR}]```
with type Text and Interval 5m or greater

## xmr_hr
### Requirements
Expand Down
25 changes: 19 additions & 6 deletions p2pool_stats
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
require 'time'
require 'net/http'
require 'json'
require 'fileutils'
require 'vcr'
require 'webmock'

FileUtils.mkdir_p('/tmp/vcr')

VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = '/tmp/vcr'
end

class Time
def readable
Expand All @@ -27,19 +37,22 @@ shares_url = "https://mini.p2pool.observer/api/shares_in_window/#{xmr_addr}?wind
payouts_url = "https://mini.p2pool.observer/api/payouts/#{xmr_addr}"
local_info_file = '/tmp/p2pool_data/local/stats'

uri = URI(info_url)
info_res = JSON.parse(Net::HTTP.get(uri))
info_res = VCR.use_cassette('info', re_record_interval: 300, match_requests_on: [:method, :host, :path]) do
JSON.parse(Net::HTTP.get(URI(info_url)))
end

uri = URI(shares_url)
shares_res = JSON.parse(Net::HTTP.get(uri)).sort { |a,b| a['height'] <=> b['height'] }
shares_res = VCR.use_cassette('shares', re_record_interval: 300, match_requests_on: [:method, :host, :path]) do
JSON.parse(Net::HTTP.get(URI(shares_url))).sort { |a,b| a['height'] <=> b['height'] }
end

cur_time = info_res['sidechain']['timestamp']
cur_height = info_res['sidechain']['height']
prev_day = cur_height - 8640
pplns_window = cur_height - 2160

uri = URI(payouts_url)
payouts_res = JSON.parse(Net::HTTP.get(uri)).sort { |a,b| a['height'] <=> b['height'] }
payouts_res = VCR.use_cassette('payouts', re_record_interval: 300, match_requests_on: [:method, :host, :path]) do
JSON.parse(Net::HTTP.get(URI(payouts_url))).sort { |a,b| a['height'] <=> b['height'] }
end

local_info = JSON.parse(File.readlines(local_info_file).join)

Expand Down

0 comments on commit 1f82f51

Please sign in to comment.