def test_queue_entries(self): que = QueueEntries() self.assertTrue(que) que.add('test', 'test') que.addbroadcast('qbroad', 'test') que.addalert('msg') self.assertTrue(que.hasentries())
def dooffline(miner: mining.Miner): '''notify user''' entries = QueueEntries() savedminer = OFFLINE.app.getminer(miner) if not savedminer.is_disabled(): if savedminer.is_send_offline_alert(): #update status to offline and alert savedminer.status = mining.MinerStatus.Offline alertmsg = OFFLINE.app.stamp( 'miner {0} is offline! since {1}'.format( savedminer.name, savedminer.laststatuschanged)) OFFLINE.app.putminer(savedminer) entries.addalert(alertmsg) print("Sent offline alert '{0}'".format(alertmsg)) else: #stop annoying the user, disable the miner to stop sending alerts savedminer.status = mining.MinerStatus.Disabled alertmsg = OFFLINE.app.stamp( 'miner {0} is Disabled after many offline alerts. No more alerts will be sent for this miner.' .format(savedminer.name)) OFFLINE.app.putminer(savedminer) entries.addalert(alertmsg) print("Sent disabled alert for {0}".format(savedminer.name)) else: if not savedminer.is_manually_disabled(): print( 'Disabled miner {0} is offline. manually disable miner to get rid of this message' .format(miner.name)) return entries
def doonline(miner): '''then provision the miner''' entries = QueueEntries() savedminer = ONLINE.app.getminer(miner) if savedminer is None: savedminer = miner #update status savedminer.status = mining.MinerStatus.Online #sending message will also save it #just provision the miner and start to monitor entries.add(QueueName.Q_PROVISION, ONLINE.app.messageencode(savedminer)) #tell them something good happened msg = 'miner {0} is back online'.format(savedminer.name) entries.addalert(msg) print(msg) return entries
def doprovision(miner): '''provision/configure a miner''' entries = QueueEntries() poollist = PROVISION.app.pools.get_all_pools() print("{0} pools configured".format(len(poollist))) print('{0} {1}'.format(miner.name, miner.ipaddress)) mineraccess = '' addpools = None minerinfo = None minerpool = None try: minerstats, minerinfo, apicall, minerpool = antminerhelper.stats(miner) miner.setminerinfo(minerinfo) #find the current pool in known pools knownpool = PROVISION.app.pools.findpool(minerpool) if knownpool is not None: minerpool.poolname = knownpool.name miner.minerpool = minerpool PROVISION.app.updateknownminer(miner) #find pools that need to be added and add them addpools = services.poolstoadd(miner, minerpool, poollist) mineraccess = PROVISION.app.antminer.getaccesslevel(miner) except antminerhelper.MinerMonitorException as ex: if ex.istimedout(): mineraccess = MinerAccessLevel.Waiting if mineraccess == MinerAccessLevel.Restricted or mineraccess == MinerAccessLevel.Waiting: if mineraccess == MinerAccessLevel.Restricted: PROVISION.app.antminer.set_privileged(miner) PROVISION.app.antminer.waitforonline(miner) mineraccess = PROVISION.app.antminer.getaccesslevel(miner) if mineraccess == MinerAccessLevel.Restricted: entries.addalert('could not set {0} to privileged access'.format( miner.name)) #try a few more times then give up else: addpoolstominer(miner, addpools) addminerpools(miner) switchtodefaultpool(miner, poollist, minerpool) enforcedefaultpool(miner, poollist, minerpool) entries.add(QueueName.Q_MONITORMINER, PROVISION.app.messageencode(miner)) return entries
def doonline(miner): '''then provision the miner''' entries = QueueEntries() savedminer = ONLINE.app.getminer(miner) if savedminer is None: savedminer = miner #update status savedminer.online_now() ONLINE.app.putminer(savedminer) ONLINE.app.updateknownminer(savedminer) #just provision the miner and start to monitor entries.add(QueueName.Q_PROVISION, ONLINE.app.messageencode(savedminer)) #tell them something good happened msg = ONLINE.app.stamp('miner {0} is back online'.format(savedminer.name)) entries.addalert(msg) print(msg) return entries
def rules(miner, minerstats, minerpool): '''this runs the rules''' entries = QueueEntries() if miner is None or minerstats is None: return entries savedminer = RULES.app.getminer(miner) cmd_restart = MinerCommand('restart', '') broken = [] for ruleparms in RULES.app.ruleparameters(): rule = MinerStatisticsRule(savedminer, minerstats, ruleparms) if rule.isbroken(): broken += rule.brokenrules if broken: #TODO: could raise broken rule event??? for rule in broken: log = MinerLog() log.createdate = datetime.datetime.utcnow() log.minerid = rule.miner.key() log.minername = rule.miner.name log.action = rule.parameter RULES.app.log_mineractivity(log) if rule.action == 'alert': entries.addalert( RULES.addalert(RULES.app.stamp(rule.parameter))) elif rule.action == 'restart': entries.add( QueueName.Q_RESTART, RULES.app.createmessagecommand(rule.miner, cmd_restart)) entries.addalert( RULES.addalert( RULES.app.stamp('Restarted {0}'.format( rule.miner.name)))) else: RULES.app.logerror('did not process broken rule {0}'.format( rule.parameter)) return entries
def doprovision(miner): '''provision/configure a miner''' entries = QueueEntries() poollist = PROVISION.app.pools() print("{0} pools configured".format(len(poollist))) print('{0} {1}'.format(miner.name, miner.ipaddress)) mineraccess = '' addpools = None minerinfo = None minerpool = None try: minerinfo = antminerhelper.getminerinfo(miner) miner.minerinfo = minerinfo minerpool = antminerhelper.pools(miner) #find the current pool in known pools knownpool = PROVISION.app.findpool(minerpool) if knownpool is not None: minerpool.poolname = knownpool.name miner.minerpool = minerpool PROVISION.app.updateknownminer(miner) #find pools that need to be added and add them addpools = services.poolstoadd(miner, minerpool, poollist) mineraccess = PROVISION.app.antminer.getaccesslevel(miner) except antminerhelper.MinerMonitorException as ex: if ex.istimedout(): mineraccess = MinerAccessLevel.Waiting if mineraccess == MinerAccessLevel.Restricted or mineraccess == MinerAccessLevel.Waiting: if mineraccess == MinerAccessLevel.Restricted: PROVISION.app.antminer.set_privileged(miner) PROVISION.app.antminer.waitforonline(miner) mineraccess = PROVISION.app.antminer.getaccesslevel(miner) if mineraccess == MinerAccessLevel.Restricted: entries.addalert('could not set {0} to privileged access'.format( miner.name)) #try a few more times then give up else: for pool in addpools or []: print( Fore.YELLOW + " Add", pool.name, "(addpool|{0},{1},{2})".format(pool.url, pool.user + miner.name, "x")) #this command adds the pool to miner and prints the result result = antminerhelper.addpool(miner, pool) if result.startswith("Access denied"): print(Fore.RED + result) else: print(result) namedpools = PROVISION.app.pools() #process the pools found on the miner. This will pick up any pools added manually for pool in miner.pools_available: #check if pools is a named pool... foundnamed = None for namedpool in namedpools: if namedpool.is_same_as(pool): foundnamed = namedpool break if foundnamed: #pool should take on the cononical attributes of the named pool pool.named_pool = foundnamed pool.user = foundnamed.user PROVISION.app.add_pool(MinerPool(miner, pool.priority, pool)) #enforce default pool if miner has one set up if miner.defaultpool: founddefault = next( (p for p in poollist if p.name == miner.defaultpool), None) if founddefault is not None: switchtopool(miner, founddefault) #enforce default pool if it doesnt have one. find highest priority pool if not miner.defaultpool: def sort_by_priority(j): return j.priority filtered = [ x for x in poollist if miner.miner_type.startswith(x.pool_type) ] filtered.sort(key=sort_by_priority) #foundpriority = next((p for p in poollist if p.priority == 0), None) if filtered: switchtopool(miner, filtered[0]) entries.add(QueueName.Q_MONITORMINER, PROVISION.app.messageencode(miner)) return entries