Beispiel #1
0
 def get_service_status(service_class):
     """ Get a service_class (eg HaproxyService) 
     :return  a dict {'name':service_name, 'status': status} """
     service_inst = service_class()
     service_status = ServiceStatus.objects.filter(name=service_inst.service_name).first() \
                      or ServiceStatus(name=service_inst.service_name)
     service_status.status = service_inst.status()[0]
     service_status.friendly_name = service_inst.friendly_name
     service_status.save()
     return service_status
Beispiel #2
0
def monitor():

    node = Cluster.get_current_node()

    def get_service_status(service_class):
        """ Get a service_class (eg HaproxyService) 
        :return  a dict {'name':service_name, 'status': status} """
        service_inst = service_class()
        service_status = ServiceStatus.objects.filter(name=service_inst.service_name).first() \
                         or ServiceStatus(name=service_inst.service_name)
        service_status.status = service_inst.status()[0]
        service_status.friendly_name = service_inst.friendly_name
        service_status.save()
        return service_status

    """ Initialize date and Monitor object """
    date = datetime.now().strftime('%Y-%m-%d %H:%M:00')
    date = datetime.strptime(date, '%Y-%m-%d %H:%M:00')
    mon = Monitor(date=make_aware(date), node=node)
    mon.services_id = set()

    for service in [
            HaproxyService, DarwinService, PFService, NetdataService,
            StrongswanService, OpenvpnService, RsyslogService
    ]:

        # Get some statuses outside for reusing variable later
        if service == StrongswanService:
            strongswan_status = get_service_status(StrongswanService)
        elif service == OpenvpnService:
            openvpn_status = get_service_status(OpenvpnService)
        elif service == RsyslogService:
            rsyslogd_status = get_service_status(RsyslogService)

        mon.services.add(get_service_status(service))
    """ Get status of Redis, Mongod and Sshd """
    # Instantiate mother class to get status easily
    service = Service()
    for service_name in ("redis", "mongod", "sshd"):
        service_status = ServiceStatus.objects.filter(name=service_name).first() \
                         or ServiceStatus(name=service_name)
        service_status.status = service.status(service_name)[0]
        mon.services.add(service_status)

    mon.save()
    """ HAPROXY """
    frontends = Frontend.objects.all().only('name', 'status', 'enabled',
                                            'mode', 'listening_mode')
    backends = Backend.objects.all().only('name', 'status', 'enabled')
    if frontends.count() > 0 or backends.count() > 0:
        statuses = {}
        try:
            # Return a dict { frontend_name: frontend_status, backend_name: backend_status, ... }
            statuses = get_stats()

        except ServiceError as e:
            logger.error(str(e))
        except Exception as e:
            logger.error("Failed to retrieve status of HAProxy: {}".format(
                str(e)))
            logger.exception(e)
        """ FRONTENDS """
        for frontend in frontends:
            if node in frontend.get_nodes():
                if not frontend.enabled:
                    status = "DISABLED"
                elif frontend.rsyslog_only_conf:
                    status = {
                        'UP': "OPEN",
                        'DOWN': "STOP"
                    }.get(rsyslogd_status.status, rsyslogd_status.status)
                else:
                    status = statuses.get("FRONTEND",
                                          {}).get(frontend.name, "ERROR")
                logger.debug("Status of frontend '{}': {}".format(
                    frontend.name, status))

                if status != frontend.status.get(node.name):
                    frontend.status[node.name] = status
                    frontend.save()

            elif frontend.status.get(node.name):
                frontend.status.pop(node.name, None)
                frontend.save()
        """ BACKENDS """
        for backend in backends:
            status = "DISABLED" if not backend.enabled else statuses.get(
                "BACKEND", {}).get(backend.name, "ERROR")
            logger.debug("Status of backend '{}': {}".format(
                backend.name, status))
            if backend.status[node.name] != status:
                backend.status[node.name] = status
                backend.save()
    """ STRONGSWAN """
    try:
        strongswan = Strongswan.objects.get(node=node)

    except ObjectDoesNotExist:
        # If there is no IPSEC conf on that node, pass
        pass
    else:
        default = ("STOP", "")

        try:
            statusall, tunnel_statuses, ups, connectings = get_ipsec_tunnels_stats(
            )
        except ServiceError as e:
            logger.exception(e)
            default = ("ERROR", str(e))
            statusall, tunnel_statuses, ups, connectings = "ERROR", {}, 0, 0

        for network in strongswan.ipsec_rightsubnet.split(','):
            strongswan.tunnels_status[network] = tunnel_statuses.get(
                network, default)
            logger.debug("Status of IPSEC Tunnel '{}' : {}".format(
                network, strongswan.tunnels_status[network]))

        strongswan.status = strongswan_status.status
        strongswan.statusall = statusall
        strongswan.tunnels_up = ups
        strongswan.tunnels_connecting = connectings
        strongswan.save()
    """ OPENVPN """
    try:
        openvpn = Openvpn.objects.get(node=node)
    except ObjectDoesNotExist:
        # If there is no VPNSSL conf on that node, pass
        pass
    else:

        openvpn.tunnels_status = get_ssl_tunnels_stats()
        openvpn.status = openvpn_status.status
        openvpn.save()
    """ DARWIN """
    filters = FilterPolicy.objects.all()
    if filters.count() > 0:
        filter_statuses = {}
        default = "DOWN"
        try:
            filter_statuses = monitor_darwin_filters()

        except ServiceError as e:
            logger.error(str(e))
            default = "ERROR"

        for dfilter in filters:
            dfilter.status[node.name] = default

            filter_status = filter_statuses.get(dfilter.name, False)
            if not dfilter.enabled:
                dfilter.status[node.name] = "DISABLED"
            elif filter_status is None:
                dfilter.status[node.name] = "ERROR"
            elif filter_statuses.get(dfilter.name,
                                     {}).get('status') is not None:
                dfilter.status[node.name] = filter_statuses.get(
                    dfilter.name).get('status').upper()
            dfilter.save()

    # Delete old monitoring
    last_date = (timezone.now() - timedelta(days=30))
    for m in Monitor.objects.filter(date__lte=last_date):
        m.delete()

    #Update Bhyve status
    vm_update_status()

    return True