# Path: simple_monitor_alert/sma.py
# class SMA(object):
#     def __init__(self, monitors_dir=None, alerts_dir=None, config_file=None):
#         # noinspection PyTypeChecker
#         results_file = create_file(os.path.join(get_var_directory(), 'results.json'), {
#             'version': __version__,
#             'monitors': {},
#         })
#         self.config = Config(config_file)
#         self.results = Results(results_file, sma=self)
#         self.monitors_info = MonitorsInfo(os.path.join(get_var_directory(), 'monitors.json'))
#         self.monitors = Monitors(monitors_dir, self.config, self)
#         self.alerts = Alerts(self, alerts_dir)
# 
#     def evaluate_and_alert(self):
#         observables = self.evaluate_all()
#         fail_observables = [observable for (observable, result) in observables if not result]
#         self.alert_all(fail_observables)
# 
#     def alert_all(self, observables, fail=True):
#         for observable in observables:
#             self.alerts.send_alerts(observable, fail)
#         self.results.write()
# 
#     def execute_all(self):
#         return self.monitors.execute_all()
# 
#     def evaluate_all(self):
#         observables = self.execute_all()
#         for observable in observables:
#             result = observable.evaluate()
#             self.results.update_observable_result(observable, not result)
#             log_evaluate(observable, result)
#             yield observable, result
#         self.results.write()
# 
# class SMAService(SMA):
#     def start(self):
#         while True:
#             start_t = time.time()
#             self.evaluate_and_alert()
#             wait = WAIT_SECONDS - (time.time() - start_t)
#             time.sleep(wait if wait > 0 else 0)


import argparse
import logging
import sys
from simple_monitor_alert.sma import SMA, SMAService

    parse_service = parser.sub.add_parser('service', help='Run SMA as service (daemon).')
    parse_service.set_defaults(which='service')

    parse_oneshot = parser.sub.add_parser('one-shot', help='Run SMA once and exit')
    parse_oneshot.set_defaults(which='one-shot')

    parse_alerts = parser.sub.add_parser('alerts', help='Alerts options.')
    parse_alerts.set_defaults(which='alerts')
    parse_alerts.add_argument('--test', help = 'Test alert', action='store_true')
    parse_alerts.add_argument('alert_section', nargs='?', help='Alert section to see')

    parse_results = parser.sub.add_parser('results', help='Monitors results')
    parse_results.set_defaults(which='results')

    parser.set_default_subparser('one-shot')
    args = parser.parse_args(argv[1:])

    create_logger('sma', args.loglevel)

    if not getattr(args, 'which', None) or args.which == 'one-shot':
        sma = SMA(args.monitors_dir, args.alerts_dir, args.config)
        sma.evaluate_and_alert()
    elif args.which == 'service':
        sma = SMAService(args.monitors_dir, args.alerts_dir, args.config)
        sma.start()
    elif args.which == 'alerts' and args.test:
        sma = SMA(args.monitors_dir, args.alerts_dir, args.config)
        sma.alerts.test()
    elif args.which == 'results':

# Path: simple_monitor_alert/sma.py
# class SMA(object):
#     def __init__(self, monitors_dir=None, alerts_dir=None, config_file=None):
#         # noinspection PyTypeChecker
#         results_file = create_file(os.path.join(get_var_directory(), 'results.json'), {
#             'version': __version__,
#             'monitors': {},
#         })
#         self.config = Config(config_file)
#         self.results = Results(results_file, sma=self)
#         self.monitors_info = MonitorsInfo(os.path.join(get_var_directory(), 'monitors.json'))
#         self.monitors = Monitors(monitors_dir, self.config, self)
#         self.alerts = Alerts(self, alerts_dir)
# 
#     def evaluate_and_alert(self):
#         observables = self.evaluate_all()
#         fail_observables = [observable for (observable, result) in observables if not result]
#         self.alert_all(fail_observables)
# 
#     def alert_all(self, observables, fail=True):
#         for observable in observables:
#             self.alerts.send_alerts(observable, fail)
#         self.results.write()
# 
#     def execute_all(self):
#         return self.monitors.execute_all()
# 
#     def evaluate_all(self):
#         observables = self.execute_all()
#         for observable in observables:
#             result = observable.evaluate()
#             self.results.update_observable_result(observable, not result)
#             log_evaluate(observable, result)
#             yield observable, result
#         self.results.write()
# 
# class SMAService(SMA):
#     def start(self):
#         while True:
#             start_t = time.time()
#             self.evaluate_and_alert()
#             wait = WAIT_SECONDS - (time.time() - start_t)
#             time.sleep(wait if wait > 0 else 0)


# Path: simple_monitor_alert/management.py
import argparse
import logging
import sys
from simple_monitor_alert.sma import SMA, SMAService


    parse_service = parser.sub.add_parser('service', help='Run SMA as service (daemon).')
    parse_service.set_defaults(which='service')

    parse_oneshot = parser.sub.add_parser('one-shot', help='Run SMA once and exit')
    parse_oneshot.set_defaults(which='one-shot')

    parse_alerts = parser.sub.add_parser('alerts', help='Alerts options.')
    parse_alerts.set_defaults(which='alerts')
    parse_alerts.add_argument('--test', help = 'Test alert', action='store_true')
    parse_alerts.add_argument('alert_section', nargs='?', help='Alert section to see')

    parse_results = parser.sub.add_parser('results', help='Monitors results')
    parse_results.set_defaults(which='results')

    parser.set_default_subparser('one-shot')
    args = parser.parse_args(argv[1:])

    create_logger('sma', args.loglevel)

    if not getattr(args, 'which', None) or args.which == 'one-shot':
        sma = SMA(args.monitors_dir, args.alerts_dir, args.config)
        sma.evaluate_and_alert()
    elif args.which == 'service':
        sma = SMAService(args.monitors_dir, args.alerts_dir, args.config)
        sma.start()
    elif args.which == 'alerts' and args.test:
        sma = SMA(args.monitors_dir, args.alerts_dir, args.config)
        sma.alerts.test()
    elif args.which == 'results':

        print(SMA(args.monitors_dir, args.alerts_dir, args.config).results)