def do_GET(self): global server_start_time if self.path == '/status': logger.info("mate - status request received from: {addr}".format( addr=self.client_address)) self.send_response(200) self.send_header('Content-type:', 'text/html') self.end_headers() data = {'status': 'running'} self.respond_data(data) elif self.path == '/uptime': logger.info("mate - uptime heartbeat received from: {addr}".format( addr=self.client_address)) self.send_response(200) self.send_header('Content-type:', 'text/html') self.end_headers() now = datetime.datetime.now() uptime = str(now - server_start_time) data = {'status': 'running', 'uptime': uptime} self.respond_data(data) else: logger.error('mate - Received different request')
def __init__(self, server, sender, receivers, subject, text, port=25, user=None, password=None, sender_name=None, tls=True): """ The class initializer. Args: server (str): The Mail server address sender (str): The sender mail address receivers (list): The list of mail receivers subject (str): The subject for the mail text (str): The mail text body. Can be HTML port (int): Port to connect to the mail server. Default 25 user (str): The username to authenticate the mail server. Default None password (str): The password to authenticate the mail server. Default None sender_name (str): The sender name that should appear in the sent mail. Default None """ self.msg = MIMEMultipart('alternative') self.sender = sender self.receivers = receivers self.subject = subject self.user = user self.password = password self.server = server self.port = port self.sender_name = sender_name self.text = text self.tls = tls send_status = False if self.remoteconn() and self.sender != '' and len(receivers) != 0: send_status = self.send() if send_status: logger.info("mail - Mail sent!") else: logger.error("mail - Mail transaction failed...")
def curl_check(address): try: t = urllib.request.urlopen(address) return t except urllib.error.HTTPError as err: logger.error( "webcheck - HTTP error while connecting to {addr}: {err}".format( addr=address, err=str(err))) return False except Exception as err: logger.error( "webcheck - Unknown error while connecting to {addr}: {err}". format(addr=address, err=str(err))) return False
def remoteconn(self): """ Connects to the Mail server using TLS if set Returns: boolean: True if successfull, False otherwise """ try: self.smtpObj = smtplib.SMTP(self.server, self.port) if self.tls: self.smtpObj.starttls() self.smtpObj.login(self.user, self.password) return True except Exception as err: logger.error("Error while authenticating to the server: {0}".format(str(err))) return False
def mate_check_data(address_port): mate_address = 'http://{addr_port}/uptime'.format(addr_port=address_port) logger.info('mate - Checking {0}'.format(mate_address)) try: t = urllib.request.urlopen(mate_address) return t except urllib.error.HTTPError as err: logger.error( 'mate - HTTP error while checking mate {mate}: {err}'.format( mate=mate_address, err=str(err))) return False except Exception as err: logger.error('mate - Error while checking mate {mate}: {err}'.format( mate=mate_address, err=str(err))) return False
def tcp_checker(address, port, close=2): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(close) try: s.connect((address, port)) s.close() logger.info("tcp - Success connecting to {addr}:{port}".format( addr=address, port=port)) return True except socket.error as err: logger.error( "tcp - Socket error connecting to {addr}:{port} - {err}".format( addr=address, port=port, err=str(err))) return False except Exception as err: logger.error( "tcp - Unknown error connecting to {addr}:{port} - {err}".format( addr=address, port=port, err=str(err))) return False
def tcp_check(addresses, close=2, timer=10): while True: for addr_port in addresses: logger.debug("tcp - Checking {0}".format(addr_port)) if ':' not in addr_port: logger.error( 'tcp - Port not specified on address: {addr}'.format( addr=addr_port)) else: try: addr = addr_port.split(':')[0] port = int(addr_port.split(':')[1]) tcp_checker(addr, port, close) except Exception as err: logger.error( "tcp - Failed to check {0} due to an error: {1}". format(addr_port, str(err))) time.sleep(timer)
def send(self): """ Args: subject (string): Email Subject receivers (list): List of receivers ex: ['*****@*****.**', '*****@*****.**'] text (string): Text message to send Returns: boolean: True if successful, False otherwise """ try: COMMASPACE = ", " self.msg['From'] = formataddr((str(Header(self.sender_name, 'utf-8')), self.sender)) self.msg['Subject'] = self.subject to_text = COMMASPACE.join(self.receivers) self.msg['To'] = to_text self.msg.attach(MIMEText(self.text, 'html')) self.smtpObj.sendmail(self.sender, self.receivers, self.msg.as_string()) self.smtpObj.quit() return True except Exception as err: logger.error("Error while sending email: {0}".format(str(err))) return False