-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
70 lines (59 loc) · 2.34 KB
/
monitor.py
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
#!/usr/bin/env python
# -----------------------------------------------------------------------------
# monitor.py
#
# Shoutz: All the BTC & LTC guys on the forums: zif, Delarock.
# efnet #innercircle, freenode #give-me-ltc
# Fuckz: MKEGuy <-- scammer!
#
# Author: sk / [email protected]
# -----------------------------------------------------------------------------
import boto
import pprint
import config
import urllib2
import json
import sys
import pickle
import os
class Monitor:
def __init__(self):
self.inmemoryDict = {}
self.loadedDict = {}
self.giveMeLTCAPI = 'https://give-me-ltc.com/api?api_key='
if os.path.exists("workers.p"):
self.loadedDict = pickle.load(open("workers.p", "rb"))
print "[x] Loaded dictionary"
def sendAlert(self, msg):
sns = boto.connect_sns()
mytopic_arn = config.snsTopic
res = sns.publish(mytopic_arn, msg, '')
def heartbeat(self):
req = urllib2.Request(self.giveMeLTCAPI + config.poolKey)
opener = urllib2.build_opener()
for parentKey, subDict in json.loads(opener.open(req).read())['workers'].iteritems():
if 'last_share_timestamp' in subDict:
self.inmemoryDict[parentKey] = subDict['hashrate']
if len(self.loadedDict.keys())< 1:
print "[!] Loaded dictionary is empty, assign (Probably first time running)"
pickle.dump(self.inmemoryDict, open("workers.p", "wb"))
sys.exit()
dictIter = self.loadedDict.iteritems()
for wName, wHash in config.workerDict.iteritems():
for lKey, lHash in dictIter:
if wName not in self.inmemoryDict:
print "[!] Missing worker %s, sending alert" % wName
self.sendAlert("Worker %s has dropped off" % wName)
if lKey in self.inmemoryDict:
print "[x] Worker %s current hash %s, threshold %s" % (lKey, lHash, wHash)
if int(lHash) < wHash:
print "[!] Issue found with worker %s, sending alert" % lKey
self.sendAlert("Worker %s is below %s" % (lKey, wHash))
break
pickle.dump(self.inmemoryDict, open("workers.p", "wb"))
print "[x] Complete\n"
#
# Main
#
monitor = Monitor()
monitor.heartbeat();