Check on different service status reported by the django-server-status
    library.

    .. code-block:: yaml

        beacons:
          http_status:
            - sites:
                example-site-1:
                  url: "https://example.com/status"
                  timeout: 30
                  content-type: json
                  status:
                    - value: 400
                      comp: <
                    - value: 300
                      comp: '>='
                  content:
                    - path: 'certificate:status'
                      value: down
                      comp: '=='
                    - path: 'status_all'
                      value: down
                      comp: '=='
            - interval: 10

def beacon(config):
    '''
    Check on different service status reported by the django-server-status
    library.

    .. code-block:: yaml

        beacons:
          http_status:
            - sites:
                example-site-1:
                  url: "https://example.com/status"
                  timeout: 30
                  content-type: json
                  status:
                    - value: 400
                      comp: <
                    - value: 300
                      comp: '>='
                  content:
                    - path: 'certificate:status'
                      value: down
                      comp: '=='
                    - path: 'status_all'
                      value: down
                      comp: '=='
            - interval: 10
    '''
    ret = []

    _config = {}
    list(map(_config.update, config))

    for site, site_config in _config.get('sites', {}).items():
        url = site_config.pop('url')
        content_type = site_config.pop('content_type', 'json')
        try:
            r = requests.get(url, timeout=site_config.pop('timeout', 30))
        except requests.exceptions.RequestException as e:
            log.info("Request failed: %s", e)
            if r.raise_for_status:
                log.info('[-] Response from status endpoint was invalid: '
                         '%s', r.status_code)
                _failed = {'status_code': r.status_code,
                           'url': url}
                ret.append(_failed)
                continue

        for attr, checks in site_config.items():
            for check in checks:
                log.debug('[+] response_item: %s', attr)
                attr_path = check.get('path', '')
                comp = comparisons[check['comp']]
                expected_value = check['value']
                if attr_path:
                    received_value = salt.utils.data.traverse_dict_and_list(attr_func_map[attr](r), attr_path)
                else:
                    received_value = attr_func_map[attr](r)
                if received_value is None:
                    log.info('[-] No data found at location %s for url %s', attr_path, url)
                    continue
                log.debug('[+] expected_value: %s', expected_value)
                log.debug('[+] received_value: %s', received_value)
                if not comp(expected_value, received_value):
                    _failed = {'expected': expected_value,
                               'received': received_value,
                               'url': url,
                               'path': attr_path
                               }
                    ret.append(_failed)
    return ret