Exemplo n.º 1
0
 def test_level_critical(self):
     '''Alert ... if level > 100'''
     now = time.time()
     dt = datetime.datetime.utcfromtimestamp(now)
     data = [(dt - MINUTE, 100.1)]
     level, value = space_weather.process_data(now, data)
     self.assertEqual(level, space_weather.CRITICAL)
Exemplo n.º 2
0
 def test_level_warning(self):
     '''Warning ... if level > 1'''
     now = time.time()
     dt = datetime.datetime.utcfromtimestamp(now)
     data = [(dt - MINUTE, 1.1)]
     level, value = space_weather.process_data(now, data)
     self.assertEqual(level, space_weather.WARNING)
Exemplo n.º 3
0
 def test_level_none(self):
     '''[BAD] Info ... if level < 1 for at least 90 minutes'''
     now = time.time()
     dt = datetime.datetime.utcfromtimestamp(now)
     data = [(dt - 90 * MINUTE, 1.1), (dt - 85 * MINUTE, 0.9)]
     level, value = space_weather.process_data(now, data)
     self.assertEqual(level, space_weather.NOTSET)
Exemplo n.º 4
0
 def test_level_info(self):
     '''[GOOD] Info ... if level < 1 for at least 90 minutes'''
     now = time.time()
     dt = datetime.datetime.utcfromtimestamp(now)
     data = [(dt - 2 * HOUR, 0.9)]
     level, value = space_weather.process_data(now, data)
     self.assertEqual(level, space_weather.INFO)
Exemplo n.º 5
0
def main():
    '''Main function'''
    global last_level
    now = time.time()

    url = config.get('data', 'url')
    data, filename = space_weather.retrieve_data(url)
    filename = space_weather.format_filename(now, filename, 'png')
    level, value = space_weather.process_data(now, data)

    if level > last_level:
        if level in [space_weather.INFO,
                     space_weather.ALERT,
                     space_weather.CRITICAL]:
            headers, body = (space_weather.generate_alert
                             (level, value, url))
            space_weather.call_api(config.get('alert', 'host'),
                                   config.getint('alert', 'port'),
                                   config.get('alert', 'url'),
                                   headers, body)
        if level >= space_weather.INFO:
            space_weather.generate_plot(now, data, filename)
            fromaddr = config.get('email', 'fromaddr')
            toaddrs = config.get('email', 'toaddrs').split(',')
            msg = (space_weather.generate_email
                   (level, value, filename,
                    fromaddr, *toaddrs))
            space_weather.send_email(config.get('email', 'host'),
                                     config.getint('email', 'port'),
                                     config.getboolean('email', 'tls'),
                                     config.get('email', 'username'),
                                     config.get('email', 'password'),
                                     msg, fromaddr, *toaddrs)
    last_level = level

    delay = space_weather.next_notify(now)
    schedule.enter(delay, NORMAL, main, ())