Exemplo n.º 1
0
 def __init__(self):
     self.syncDB = DatabaseConnection(env.DB_HOST, env.DB_UNAME,
                                      env.DB_PASSWORD, env.DB_NAME)
     # self.statusDB = DatabaseConnection(
     #     env.DB_HOST, env.DB_UNAME, env.DB_PASSWORD, env.DB_NAME)
     self.limitRow = env.LIMIT_PROC_ROW
     self.outbox = Outbox(self.syncDB)
     self.systemlog = SystemLog()
     self.inbox = Inbox(self.syncDB)
     self.outbox = Outbox(self.syncDB)
     self.clientIdStartFrom = 10
     self.updateToZeroHistory = set([])
     self.PKFileName = 'pk'
     self.nextPriToProcess = dict()
     self.PRIFileName = 'pri'
Exemplo n.º 2
0
async def notify(log_level: int = None) -> NoReturn:
    """Notify our users periodically of the number of red metrics."""
    logging.getLogger().setLevel(log_level or logging.ERROR)
    sleep_duration = int(os.environ.get("NOTIFIER_SLEEP_DURATION", 60))
    api_version = "v3"
    reports_url = (
        f"http://{os.environ.get('SERVER_HOST', 'localhost')}:"
        f"{os.environ.get('SERVER_PORT', '5001')}/api/{api_version}/reports")
    data_model = await retrieve_data_model(api_version)
    most_recent_measurement_seen = datetime.max.replace(tzinfo=timezone.utc)
    outbox = Outbox()
    notification_finder = NotificationFinder(data_model)
    while True:
        record_health()
        logging.info("Determining notifications...")
        try:
            async with aiohttp.ClientSession(raise_for_status=True,
                                             trust_env=True) as session:
                response = await session.get(reports_url)
                json = await response.json()
        except Exception as reason:  # pylint: disable=broad-except
            logging.error("Could not get reports from %s: %s", reports_url,
                          reason)
            json = dict(reports=[])
        notifications = notification_finder.get_notifications(
            json, most_recent_measurement_seen)
        outbox.add_notifications(notifications)
        outbox.send_notifications()
        most_recent_measurement_seen = most_recent_measurement_timestamp(json)
        logging.info("Sleeping %.1f seconds...", sleep_duration)
        await asyncio.sleep(sleep_duration)
Exemplo n.º 3
0
 def test_notifications_without_destination(self, mocked_ready):
     """Test that notifications without a destination aren't sent."""
     mocked_ready.side_effect = [True, True]
     notifications = self.notifications
     notifications[0].destination["webhook"] = None
     notifications[1].destination["webhook"] = None
     outbox = Outbox(notifications)
     self.assertEqual(0, outbox.send_notifications())
Exemplo n.º 4
0
    def send(self,
             recipient_list,
             subject='Sem assunto',
             attachment_list='False'):

        emailRegex = re.compile(
            r'''([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+(\.[a-zA-Z]{2,4}))''',
            re.VERBOSE)
        valid_recipients = []
        invalid_recipients = []

        for i in recipient_list:
            email = emailRegex.findall(i)
            if email:
                valid_recipients.append(email)
            else:
                invalid_recipients.append(email)
        try:
            with Outbox(username=smtp_user,
                        password=smtp_pass,
                        server=smtp_server,
                        port=smtp_port,
                        mode='SSL') as outbox:

                if attachment_list == 'False':
                    attachments = []
                    for i in attachment_list:
                        attachments.append(
                            Attachment(str(i), fileobj=open(i, 'rb')))
                    outbox.send(Email(
                        subject=subject,
                        html_body='<b>SOME REALLY NICE SENTIMENT</b>',
                        recipients=valid_recipients),
                                attachments=attachments)
                    response = self.get_mail_sent_ok_message()
                    if len(invalid_recipients) > 0:
                        response = response + '\n' + self.get_invalid_mails_filtered_by_regex(
                        )
                        for invalid in invalid_recipients:
                            response = response + '\n' + invalid
                    return response
                else:
                    outbox.send(
                        Email(subject=subject,
                              html_body='<b>SOME REALLY NICE SENTIMENT</b>',
                              recipients=valid_recipients))
                    response = self.get_mail_sent_ok_message()
                    if len(invalid_recipients) > 0:
                        response = response + '\n' + self.get_invalid_mails_filtered_by_regex(
                        )
                        for invalid in invalid_recipients:
                            response = response + '\n' + invalid
                    return response
        finally:
            pass
Exemplo n.º 5
0
 def __init__(self, dbhost, dbusername, dbpass, dbname, sinkaddr, skey,
              ivkey):
     self.key = skey
     self.iv = ivkey
     self.context = zmq.Context()
     self.receiver = self.context.socket(zmq.PULL)
     self.receiver.bind(sinkaddr)
     self.syslog = SystemLog()
     self.db = DatabaseConnection(dbhost, dbusername, dbpass, dbname)
     self.inbox = Inbox(self.db)
     self.outbox = Outbox(self.db)
Exemplo n.º 6
0
 def __init__(self):
     # self.maxRowPerWorker = env.VEN_MAX_ROW_PER_WORKER
     self.workerAddress = env.VEN_WORKER_ADDR
     self.sinkAddr = env.SINK_ADDR
     self.context = zmq.Context()
     self.sender = self.context.socket(zmq.PUSH)
     self.sender.bind(self.workerAddress)
     self.processId = os.getpid()
     self.system = platform.system()
     self.db = DatabaseConnection(env.DB_HOST, env.DB_UNAME,
                                  env.DB_PASSWORD, env.DB_NAME)
     self.syslog = SystemLog()
     self.outbox = Outbox(self.db)
Exemplo n.º 7
0
 def test_merge_notifications_with_same_destination_but_different_report(
         self):
     """Test that the metrics are merged into the correct notification."""
     report = dict(title="different_title", url="https://differentreport")
     metric1 = dict(metric_name="new_metric 1")
     metric2 = dict(metric_name="new_metric 2")
     metrics1 = [metric1, metric2]
     new_notifications = [Notification(report, metrics1, "uuid1", {})]
     outbox = Outbox(self.notifications)
     outbox.add_notifications(new_notifications)
     self.assertEqual(
         outbox.notifications[0].metrics,
         [self.metric_notification_data1, self.metric_notification_data2],
     )
Exemplo n.º 8
0
def _send_email(config_file, subject, body):
    with open(config_file, 'r') as f:
        config = json.load(f)

    port = config["port"]
    server = config["server"]
    fromaddr = config["fromaddr"]
    toaddrs = config["toaddrs"]
    username = config["username"]
    password = config["password"]

    outbox = Outbox(username=username,
                    password=password,
                    server=server,
                    port=port)
    email = Email(subject=subject, body=body, recipients=toaddrs)
    outbox.send(email)
Exemplo n.º 9
0
def send(account: Account, to: str, subject: str, body: str, attachment,
         attachment_name):
    if not body:
        raise EmptyBody

    with Outbox(server=account.smtp_host,
                port=account.smtp_port,
                username=account.address,
                password=account.password,
                mode='SSL' if account.smtp_ssl else None) as connection:

        email = Email(recipients=to, subject=subject, body=body)

        attachments = []

        if attachment:
            attachments.append(Attachment(attachment_name, attachment))

        try:
            connection.send(email, attachments)
        except SMTPRecipientsRefused:
            raise IncorrectAddress
Exemplo n.º 10
0
def send_report(report):
    message = ''
    for server_key, server_report in report.items():
        message += "SERVER %s: \n\n" % server_key
        if isinstance(server_report, dict):
            remote_report = server_report.get("remote")
            del (server_report["remote"])
            for db_name, db_report in server_report.items():
                message += "\tDB %s: %s\n" % (db_name, db_report)
            if remote_report:
                message += "\n\t[Remote server (scp)]: %s\n" % remote_report
        else:
            message += "%s" % server_report
        message += "\n\n"
    print(message)
    try:
        import socket
        from outbox import Outbox, Email, Attachment
        hostname = socket.gethostname()
        smtp = namedtuple('smtp', settings.smtp.keys())(**settings.smtp)
        attachments = [
            Attachment('backup.log', fileobj=open(settings.logfile))
        ]
        outbox = Outbox(username=smtp.user,
                        password=smtp.password,
                        server=smtp.host,
                        port=smtp.port,
                        mode='SSL')
        message = "HOST: %s\n\n" % hostname + message
        outbox.send(Email(subject='[%s] Daily backup report' % hostname,
                          body=message,
                          recipients=settings.emails),
                    attachments=attachments)
        # if report sent, we can remove log file
        os.unlink(settings.logfile)
    except Exception as e:
        logger.error("Can't send report via email")
        logger.exception(e)
Exemplo n.º 11
0
 def test_merge_nothing_into_nothing(self):
     """Test that notifications are merged, even if the destination is empty."""
     outbox = Outbox()
     outbox.add_notifications([])
     self.assertEqual(outbox.notifications, [])
Exemplo n.º 12
0
 def test_merge_nothing_into_notifications(self):
     """Test that notifications are merged, even if no new metrics are given to be added."""
     outbox = Outbox(self.notifications)
     outbox.add_notifications([])
     self.assertEqual(outbox.notifications, self.notifications)
Exemplo n.º 13
0
 def test_merge_notifications_into_nothing(self):
     """Test that notifications are merged, even if the outbox is currently empty."""
     outbox = Outbox()
     outbox.add_notifications(self.notifications)
     self.assertEqual(outbox.notifications, self.notifications)
Exemplo n.º 14
0
 def test_deletion_of_notifications(self, mocked_send, mocked_ready):
     """Test that notifications are deleted after sending."""
     mocked_ready.side_effect = [True, True]
     mocked_send.side_effect = [None, None]
     outbox = Outbox(self.notifications)
     self.assertEqual(2, outbox.send_notifications())
Exemplo n.º 15
0
 def test_send_notifications(self, mocked_send, mocked_ready):
     """Test that notifications can be sent."""
     mocked_ready.side_effect = [True, False]
     outbox = Outbox(self.notifications)
     outbox.send_notifications()
     mocked_send.assert_called_once()
import time
import zmq
import os
import sys
import json
import env
from encryption import AES256
from DatabaseConnection import DatabaseConnection
import time
from outbox import Outbox
import datetime

uniqueId = env.UNIQUE_ID
db = DatabaseConnection(env.DB_HOST, env.DB_UNAME,
                        env.DB_PASSWORD, env.DB_NAME)
outbox = Outbox(db)
# if(len(sys.argv) == 2):
#     fileName = sys.argv[1]
# else:
#     fileName = 'worker_process.id'

# print(fileName)
context = zmq.Context()
# f = open("process/"+fileName, "a+")
# f.write(str(os.getpid()) + "\n")
# f.close()
# Socket to receive messages on
receiver = context.socket(zmq.PULL)
receiver.connect("tcp://localhost:5557")

Exemplo n.º 17
0
 def __init__(self, config):
     self.outbox = Outbox(server=config.host,
                          username=config.user,
                          password=config.password,
                          port=config.get('port', 25),
                          mode=config.get('mode', None))