def __acceptOffer(self, offer: Offer): self.__requestHeaders["X-Amz-Date"] = self.__getAmzDate() request = requests.post( FlexUnlimited.routes.get("AcceptOffer"), headers=self.__requestHeaders, json={"offerId": offer.id}) if request.status_code == 200: self.__acceptedOffers.append(offer) if self.twilioClient is not None: self.twilioClient.messages.create( to=self.twilioToNumber, from_=self.twilioFromNumber, body=offer.toString()) Log.info(f"Successfully accepted offer {offer.id}") else: Log.error(f"Unable to accept offer {offer.id}. Request returned status code {request.status_code}")
def run(self): Log.info("Starting job search...") while self.__retryCount < self.retryLimit: if not self.__retryCount % 50: print(self.__retryCount, 'requests attempted\n\n') offersResponse = self.__getOffers() if offersResponse.status_code == 200: currentOffers = offersResponse.json().get("offerList") currentOffers.sort(key=lambda pay: int(pay['rateInfo']['priceAmount']), reverse=True) for offer in currentOffers: offerResponseObject = Offer(offerResponseObject=offer) self.__processOffer(offerResponseObject) self.__retryCount += 1 else: Log.error(offersResponse.json()) break Log.info("Job search cycle ending...") Log.info(f"Accepted {len(self.__acceptedOffers)} offers in {time.time() - self.__startTimestamp} seconds")
class ZenSNPP(IZen, Client): server = None log = None def __init__(self): self.conf = ConfUtil.zen(__file__) daemon = Util.filename(__file__) self.log = Log(daemon) Client.__init__(self, daemon, self.log) self.server = Server( self.conf['snpp']['host'], self.conf['snpp']['port'] ) def get(self): boxes = [] for box in sorted(self.conf['boxes']): url = 'http://%s:81/gsm1.html' % self.conf['boxes'][box] try: response = urlopen(url, timeout=self.conf['timeout']) if response.getcode() != 200: continue except URLError: continue html = response.read() if 'ERROR' in html: continue info = html.split() _, _, _, _, _, _, _, signal, _ = info signal = signal.replace(',', '.') signal = float(signal) signal = int(signal) if signal < 2 or signal == 99: continue boxes.append(box) if len(boxes) == 0: raise MyException return boxes def post(self, box, to, data): to = ';'.join(map(str, to)) url = 'http://%s/source/send_sms.php' % self.conf['boxes'][box] data = { 'username' : 'admin', 'pwd' : 'zenoss', 'from' : 'Zenoss', 'nphone' : to, 'outCh' : 'GSM1', 'testo' : data } data = urlencode(data) request = Request(url, data) try: response = urlopen(request, timeout=self.conf['timeout']) if response.getcode() != 200: raise MyException except URLError: raise MyException def sms(self, host, allow, packet): i = isinstance(packet['to'], int) targets = [packet['to']] if i else packet['to']['phones'] if not allow: debug = DebugUtil.sms(host, targets, packet['data']) self.log.info('Message dropped %s' % debug) return try: boxes = self.get() if all(d not in packet['data'] for d in self.conf['devices']): boxes = [boxes.pop(0)] for box in boxes: self.post(box, targets, packet['data']) smsfrom = { 'host' : host, 'box' : box } debug = DebugUtil.sms(smsfrom, targets, packet['data']) self.log.info('Message sent %s' % debug) except MyException: if i: self.log.critical('Test message not sent') return mail = DictUtil.copy(self.conf['mail']) mail['to'] = packet['to']['mails'] mail['body'] = packet['data'] debug = DebugUtil.mail(host, mail) if Mail.sendraw(mail): self.log.info('Mail sent %s' % debug) else: self.log.critical('Mail not sent %s' % debug) def run(self): try: self.log.clear() while True: read = self.server.select() for socket in read: if self.server.accept(socket): try: client, ip, packet = self.server.recv() client = ClientProxy(client) self.server.attach(client) host = Util.hostname(ip) d = {'host': host} self.send(d) pkt = self.recv() self.sms(host, pkt['allow'], packet) except MyException: pass else: client = ClientProxy(socket) self.server.dettach(client) except KeyboardInterrupt: pass
class ZenStatus(IZen, Client): down = [] hosts = {} log = None def __init__(self): zen = ConfUtil.zen() conf = ConfUtil.zen(__file__) conf['localhost'] = zen['localhost'] self.conf = conf daemon = Util.filename(__file__) self.log = Log(daemon) Client.__init__(self, daemon, self.log) def mail(self, host, success): status = 'up' if success else 'down' mail = DictUtil.copy(self.conf['mail']) mail['subject'] %= status.upper() mail['body'] %= host debug = DebugUtil.mail(self.conf['localhost']['name'], mail) if Mail.sendraw(mail): self.log.info('Mail sent %s' % debug) else: self.log.critical('Mail not sent %s' % debug) def start(self, ip): up = False host = Util.hostname(ip) for _ in range(self.conf['start']['retries']): if 'backup' in self.hosts[host] and self.hosts[host]['backup']: up = True break cmd = ['ssh', ip, 'service', 'serviced', 'start'] check_call(cmd, stderr=PIPE) cmd = ['ssh', ip, 'service', 'serviced', 'status'] output = check_output(cmd, stderr=PIPE) if 'running' not in output: sleep(self.conf['start']['timeout']) continue cmd = ['ssh', ip, 'serviced', 'service', 'start', 'Zenoss.core'] output = check_output(cmd) if 'started' not in output: sleep(self.conf['start']['timeout']) continue up = True break if host in self.down: if up: self.mail(host, True) self.down.remove(host) else: if not up: self.mail(host, False) self.down.append(host) return up def status(self): _hosts = self.hosts.keys() hosts = {h: {} for h in _hosts} for host in sorted(_hosts): if not self.hosts[host]['ping']: hosts[host]['status'] = False self.log.error(host) continue if self.start(self.hosts[host]['ip']): hosts[host]['status'] = True self.log.success(host) else: hosts[host]['status'] = False self.log.error(host) d = {'hosts': hosts} self.send(d) def run(self): try: worker = None self.log.clear() while True: packet = self.recv() self.hosts = packet['hosts'] if worker is not None and worker.is_alive(): continue self.log.clear() worker = Thread(target=self.status) worker.start() except KeyboardInterrupt: pass
class ZenSMTP(IZen, Client): log = None def __init__(self): self.conf = ConfUtil.zen(__file__) daemon = Util.filename(__file__) self.log = Log(daemon) Client.__init__(self, daemon, self.log) def process_message(self, peer, mailfrom, rcpttos, data): ip, _ = peer host = Util.hostname(ip) fr = '(%s, %s)' % (Util.repr(host), Util.repr(mailfrom)) fr = '%s: %s' % (Util.repr('From'), fr) to = ', '.join(map(Util.repr, rcpttos)) if len(rcpttos) > 1: to = '(%s)' % to to = '%s: %s' % (Util.repr('To'), to) msg = message_from_string(data) subject = Util.repr(msg['Subject']) subject = '%s: %s' % (Util.repr('Subject'), subject) debug = '(%s, %s, %s)' % (fr, to, subject) d = {'host': host} self.send(d) packet = self.recv() if packet['allow']: if Mail.send(mailfrom, rcpttos, data): self.log.info('Mail sent %s' % debug) else: self.log.critical('Mail not sent %s' % debug) else: self.log.info('Mail dropped %s' % debug) def run(self): try: self.log.clear() server = SMTPServer( (self.conf['smtp']['host'], self.conf['smtp']['port']), None ) server.process_message = self.process_message loop() except KeyboardInterrupt: pass
class ZenTrap(IZen, Client): hosts = {} queue = [] log = None udp = None @staticmethod def mac(ip): try: arp = sep + path.join('proc', 'net', 'arp') cmd = ['grep', ip, arp] fields = check_output(cmd).split() if len(fields) != 6: raise MacException _, _, _, mac, _, _ = fields if mac == '00:00:00:00:00:00': raise MacException return mac except CalledProcessError: raise MacException def __init__(self): self.conf = ConfUtil.zen(__file__) daemon = Util.filename(__file__) self.log = Log(daemon) Client.__init__(self, daemon, self.log) def loop(self): while True: try: packet = self.udp.recv() if packet['dst_port'] != self.conf['port']: continue self.queue.append(packet) except UdpException: pass def run(self): try: self.log.clear() packet = self.recv() self.hosts, self.udp = packet['hosts'], UDP() Thread(target=self.loop).start() while True: if len(self.queue) == 0: continue packet, targets = self.queue.pop(0), [] for host in sorted(self.hosts): d = {'host': host} self.send(d) _packet = self.recv() if not _packet['allow']: continue if 'mac' not in self.hosts[host]: try: mac = ZenTrap.mac(self.hosts[host]['ip']) self.hosts[host]['mac'] = mac except MacException: continue packet['dst_mac'] = self.hosts[host]['mac'] packet['dst_ip'] = self.hosts[host]['ip'] self.udp.send(packet) targets.append(host) if len(targets) == 0: self.log.info('Trap dropped') else: to = ', '.join(map(Util.repr, targets)) if len(targets) > 1: to = '(%s)' % to self.log.info('Trap sent (%s: %s)' % (Util.repr('To'), to)) except KeyboardInterrupt: pass
class ZenPing(IZen, Client): hosts = {} mails = { 'up' : [], 'down' : [] } down = [] log = None @staticmethod def ping(ip): try: cmd = ['ping', '-c', '1', '-w', '1', '-q', ip] output = check_output(cmd) return ' 0% packet loss' in output except CalledProcessError: return False def __init__(self): zen = ConfUtil.zen() conf = ConfUtil.zen(__file__) conf['localhost'] = zen['localhost'] self.conf = conf daemon = Util.filename(__file__) self.log = Log(daemon) Client.__init__(self, daemon, self.log) def mail(self, up): status = 'up' if up else 'down' mail = DictUtil.copy(self.conf['mail']) mail['subject'] %= status.upper() body = self.mails[status].pop() if len(self.mails[status]) > 0: hosts = ', '.join(self.mails[status]) body = '%s & %s' % (hosts, body) mail['body'] %= body debug = DebugUtil.mail(self.conf['localhost']['name'], mail) if Mail.sendraw(mail): self.log.info('Mail sent %s' % debug) else: self.log.critical('Mail not sent %s' % debug) def run(self): try: self.log.clear() packet = self.recv() self.hosts = packet['hosts'] hosts = {h: {} for h in self.hosts.keys()} while True: for host in sorted(self.hosts): if ZenPing.ping(self.hosts[host]['ip']): if host in self.down: self.down.remove(host) self.mails['up'].append(host) hosts[host]['ping'] = True self.log.success(host) else: if host not in self.down: self.down.append(host) self.mails['down'].append(host) hosts[host]['ping'] = False self.log.error(host) if len(self.mails['down']) > 0: self.mail(False) self.mails['down'] = [] if len(self.mails['up']) > 0: self.mail(True) self.mails['up'] = [] d = {'hosts': hosts} self.send(d) sleep(self.conf['sleep']) self.log.clear() except KeyboardInterrupt: pass