Esempio n. 1
0
 def close_session(self):
     """
     closes imap sesion
     """
     self.imapSession.close()
     self.imapSession.logout()
     l.l('disconnected from gmail')
Esempio n. 2
0
 def dispatcher():
     """
     starts scheduler
     """
     last_hour = datetime.now().strftime("%H")
     print last_hour
     while True:
         schedule.run_pending()
         time.sleep(1)
         this_hour = datetime.now().strftime("%H")
         if this_hour <> last_hour:
             l.l('heartbeat --> still here homey!')
             last_hour = this_hour
Esempio n. 3
0
 def test_missing_emails(self):
     """
     compares list of expected subjects to what is retrieved
     :return: failure_count, results
     """
     l.l('look for emails not received')
     results = []
     failure_count = 0
     for x in self.subjects:
         r ={}
         if x in self.emails_received:
             r[x] = 'ok'
         else:
             r[x] = 'FAILURE!'
             failure_count += 1
         results.append(r)
     return failure_count, results
Esempio n. 4
0
 def __init__(self, subjects, senders, received_after='00:00'):
     """
     :param subjects:
     :param senders:
     :param received_after:  default is midnight
     :return:
     """
     l.l('connecting to gmail')
     username = conf['google_apps']['email']
     password = conf['google_apps']['password']
     self.imapSession = imaplib.IMAP4_SSL('imap.gmail.com')
     typ, accountDetails = self.imapSession.login(username, password)
     if typ != 'OK':
         l.l('Not able to sign in!')
         raise
         exit(1)
     self.subjects = subjects
     self.senders = senders
     self.received_after = received_after
     self.emails_received = []
Esempio n. 5
0
    def trigger_incident(self, pd_service, pd_description, error_detail, check = 'eqx'):
        """
        :param pd_service:
        :param pd_description:
        :param error_detail:
        """
        incident = check + '-' + str(datetime.now().strftime('%Y%m%d-%H'))
        payload = json.dumps({
          "service_key": pd_service,
          "incident_key": incident,
          "event_type": "trigger",
          "description": pd_description,
          "client": "robopager",
          "client_url": "https://monitoring.equinox.com",
          "details": error_detail

        })
        r = requests.post(
                        'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
                        headers=self.headers,
                        data=payload,
        )
        if r.status_code != 200:
            l.l('Error triggering pager duty')
            raise
        l.l(r.status_code)
        l.l(r.text)
Esempio n. 6
0
    def get_emails(self):
        """
        pulls down a list of emails, filters by time, subject, senders
        :return: emails recieved matching criteria
        """
        def parse_gmail_dates(gdate):
            try:
                date =  datetime.strptime(gdate[:-6], '%d %b %Y %H:%M:%S')
            except:
                date =  datetime.strptime(gdate[:-6], '%a, %d %b %Y %H:%M:%S')
            return date
        # select messages
        date_received = (date.today()).strftime("%d-%b-%Y")
        delivery_threshold = datetime.strptime(' '.join((date_received, self.received_after)),"%d-%b-%Y %H:%M")
        imap_search = '(SENTSINCE {date} FROM "{senders}")'.format(date=date_received, senders=','.join(self.senders))

        self.imapSession.select('[Gmail]/All Mail')
        typ, data = self.imapSession.search(None, imap_search)
        if typ != 'OK':
            l.l('Error searching Inbox.')
            raise
        l.l(str(len(data[0].split())) + ' emails found')

        # Iterating over all emails
        for msgId in data[0].split():
            #typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
            typ, messageParts = self.imapSession.fetch(msgId, '(BODY.PEEK[HEADER])')
            msg = email.message_from_string(messageParts[0][1])
            time_recieved =  parse_gmail_dates(msg['Date'])
            if time_recieved < delivery_threshold:
                continue
            self.emails_received.append(msg['Subject'])
            if typ != 'OK':
                l.l('Error fetching mail.')
                raise
                exit(1)
        l.l("emails recieved: " + ','.join(self.emails_received))
        return self.emails_received
Esempio n. 7
0
def run_test(**kwargs):
    """
    a wrapper for running basic email tests
    :param kwargs:
    :return:
    """
    l.l('\n--------------------\nstarting test')
    pd_check = kwargs.get('check')
    subjects = kwargs.get('subjects')
    senders = kwargs.get('senders')
    delivery_time = kwargs.get('delivery_time')
    pd_service = kwargs.get('pd_service')
    pd_description = kwargs.get('pd_description')

    e = CheckEmails(subjects,senders,delivery_time)
    e.get_emails()
    failure_count, results = e.test_missing_emails()
    e.close_session()

    l.l("failure count: " + str(failure_count))
    l.l("results:")
    l.l(results)
    if failure_count > 0:
        l.l('!!!!uh oh, we are missing some reports bro!!!!')
        PDInteraction().trigger_incident(pd_service, pd_description, json.dumps(results, indent=4), pd_check)
    else:
        l.l('everything is ok')
    l.l('\ntest complete!')
Esempio n. 8
0
    l.l("failure count: " + str(failure_count))
    l.l("results:")
    l.l(results)
    if failure_count > 0:
        l.l('!!!!uh oh, we are missing some reports bro!!!!')
        PDInteraction().trigger_incident(pd_service, pd_description, json.dumps(results, indent=4), pd_check)
    else:
        l.l('everything is ok')
    l.l('\ntest complete!')



if __name__ == "__main__":
    # argparse
    l.l('starting')
    parser = argparse.ArgumentParser()
    parser.add_argument('-c','--check', help ="enter a check referencing the checklist.yaml", default='cron')
    args = parser.parse_args()
    check = args.check

    # get list of tests and metadata
    check_list = parse_checklist()

    # check to see if we are running a specific check via external cron or starting as a service
    if check != 'cron':
        l.l('single job mode')
        if check not in check_list:
            l.l("invalid test, please review checklist.yaml")
            raise
        else: