예제 #1
0
def _connect_multiple_sites():
    global _live
    enabled_sites, disabled_sites = _get_enabled_and_disabled_sites()
    _set_initial_site_states(enabled_sites, disabled_sites)
    _live = livestatus.MultiSiteConnection(enabled_sites, disabled_sites)

    # Fetch status of sites by querying the version of Nagios and livestatus
    # This may be cached by a proxy for up to the next configuration reload.
    _live.set_prepend_site(True)
    for site_id, v1, v2, ps, num_hosts, num_services in _live.query(
            "GET status\n"
            "Cache: reload\n"
            "Columns: livestatus_version program_version program_start num_hosts num_services"
    ):
        _update_site_status(
            site_id, {
                "state": "online",
                "livestatus_version": v1,
                "program_version": v2,
                "program_start": ps,
                "num_hosts": num_hosts,
                "num_services": num_services,
                "core": v2.startswith("Check_MK") and "cmc" or "nagios",
            })
    _live.set_prepend_site(False)

    # Get exceptions in case of dead sites
    for site_id, deadinfo in _live.dead_sites().items():
        shs = deadinfo.get("status_host_state")
        _update_site_status(
            site_id, {
                "exception": deadinfo["exception"],
                "status_host_state": shs,
                "state": _status_host_state_name(shs),
            })
예제 #2
0
파일: sites.py 프로젝트: m4c3/checkMK
def _connect_multiple_sites():
    global _live
    enabled_sites, disabled_sites = _get_enabled_and_disabled_sites()
    _set_initial_site_states(enabled_sites, disabled_sites)

    if cmk.is_managed_edition():
        import cmk.gui.cme.managed as managed
        _live = managed.CMEMultiSiteConnection(enabled_sites, disabled_sites)
    else:
        _live = livestatus.MultiSiteConnection(enabled_sites, disabled_sites)

    # Fetch status of sites by querying the version of Nagios and livestatus
    # This may be cached by a proxy for up to the next configuration reload.
    _live.set_prepend_site(True)
    for response in _live.query(
            "GET status\n"
            "Cache: reload\n"
            "Columns: livestatus_version program_version program_start num_hosts num_services"
    ):

        try:
            site_id, v1, v2, ps, num_hosts, num_services = response
        except ValueError:
            e = livestatus.MKLivestatusQueryError(
                "Invalid response to status query: %s" % response)

            site_id = response[0]
            _update_site_status(
                site_id, {
                    "exception": e,
                    "status_host_state": None,
                    "state": _status_host_state_name(None),
                })
            continue

        _update_site_status(
            site_id, {
                "state": "online",
                "livestatus_version": v1,
                "program_version": v2,
                "program_start": ps,
                "num_hosts": num_hosts,
                "num_services": num_services,
                "core": v2.startswith("Check_MK") and "cmc" or "nagios",
            })
    _live.set_prepend_site(False)

    # TODO(lm): Find a better way to make the Livestatus object trigger the update
    # once self.deadsites is updated.
    update_site_states_from_dead_sites()
예제 #3
0
def connect_to_livestatus(html):
    html.site_status = {}
    # site_status keeps a dictionary for each site with the following
    # keys:
    # "state"              --> "online", "disabled", "down", "unreach", "dead" or "waiting"
    # "exception"          --> An error exception in case of down, unreach, dead or waiting
    # "status_host_state"  --> host state of status host (0, 1, 2 or None)
    # "livestatus_version" --> Version of sites livestatus if "online"
    # "program_version"    --> Version of Nagios if "online"

    # If there is only one site (non-multisite), than
    # user cannot enable/disable.
    if config.is_multisite():
        # do not contact those sites the user has disabled.
        # Also honor HTML-variables for switching off sites
        # right now. This is generally done by the variable
        # _site_switch=sitename1:on,sitename2:off,...
        switch_var = html.var("_site_switch")
        if switch_var:
            for info in switch_var.split(","):
                sitename, onoff = info.split(":")
                d = config.user_siteconf.get(sitename, {})
                if onoff == "on":
                    d["disabled"] = False
                else:
                    d["disabled"] = True
                config.user_siteconf[sitename] = d
            config.save_site_config()

        # Make lists of enabled and disabled sites
        enabled_sites = {}
        disabled_sites = {}

        for sitename, site in config.allsites().items():
            siteconf = config.user_siteconf.get(sitename, {})
            if siteconf.get("disabled", False):
                html.site_status[sitename] = {
                    "state": "disabled",
                    "site": site
                }
                disabled_sites[sitename] = site
            else:
                html.site_status[sitename] = {"state": "dead", "site": site}
                enabled_sites[sitename] = site

        html.live = livestatus.MultiSiteConnection(enabled_sites,
                                                   disabled_sites)

        # Fetch status of sites by querying the version of Nagios and livestatus
        html.live.set_prepend_site(True)
        for sitename, v1, v2, ps, num_hosts, num_services in html.live.query(
                "GET status\n"
                "Columns: livestatus_version program_version program_start num_hosts num_services"
        ):
            html.site_status[sitename].update({
                "state": "online",
                "livestatus_version": v1,
                "program_version": v2,
                "program_start": ps,
                "num_hosts": num_hosts,
                "num_services": num_services,
            })
        html.live.set_prepend_site(False)

        # Get exceptions in case of dead sites
        for sitename, deadinfo in html.live.dead_sites().items():
            html.site_status[sitename]["exception"] = deadinfo["exception"]
            shs = deadinfo.get("status_host_state")
            html.site_status[sitename]["status_host_state"] = shs
            if shs == None:
                statename = "dead"
            else:
                statename = {
                    1: "down",
                    2: "unreach",
                    3: "waiting",
                }.get(shs, "unknown")
            html.site_status[sitename]["state"] = statename

    else:
        html.live = livestatus.SingleSiteConnection(
            "unix:" + defaults.livestatus_unix_socket)
        html.live.set_timeout(10)  # default timeout is 10 seconds
        html.site_status = {'': {"state": "dead", "site": config.site('')}}
        v1, v2, ps = html.live.query_row(
            "GET status\nColumns: livestatus_version program_version program_start"
        )
        html.site_status[''].update({
            "state": "online",
            "livestatus_version": v1,
            "program_version": v2,
            "program_start": ps
        })

    # If Multisite is retricted to data user is a nagios contact for,
    # we need to set an AuthUser: header for livestatus
    if not config.may("general.see_all"):
        html.live.set_auth_user('read', config.user_id)
        html.live.set_auth_user('action', config.user_id)

    # May the user see all objects in BI aggregations or only some?
    if not config.may("bi.see_all"):
        html.live.set_auth_user('bi', config.user_id)

    # Default auth domain is read. Please set to None to switch off authorization
    html.live.set_auth_domain('read')
예제 #4
0
    },
    "sitea": {
        "alias": "Augsburg",
        "socket": "tcp:sitea:6557",
        "nagios_url": "/nagios/",
        "timeout": 2,
    },
    "siteb": {
        "alias": "Berlin",
        "socket": "tcp:siteb:6557",
        "nagios_url": "/nagios/",
        "timeout": 10,
    },
}

c = livestatus.MultiSiteConnection(sites)  # type: ignore[arg-type]
c.set_prepend_site(True)
print(c.query("GET hosts\nColumns: name state\n"))
c.set_prepend_site(False)
print(c.query("GET hosts\nColumns: name state\n"))

# Beware: When doing stats, you need to aggregate yourself:
print(sum(c.query_column("GET hosts\nStats: state >= 0\n")))

# Detect errors:
sites = {
    "muc": {
        "socket": "unix:/var/run/nagios/rw/live",
        "alias": "Munich",
    },
    "sitea": {
예제 #5
0
    },
    "sitea": {
        "alias": "Augsburg",
        "socket": "tcp:sitea:6557",
        "nagios_url": "/nagios/",
        "timeout": 2,
    },
    "siteb": {
        "alias": "Berlin",
        "socket": "tcp:siteb:6557",
        "nagios_url": "/nagios/",
        "timeout": 10,
    },
}

c = livestatus.MultiSiteConnection(sites)
c.set_prepend_site(True)
print(c.query("GET hosts\nColumns: name state\n"))
c.set_prepend_site(False)
print(c.query("GET hosts\nColumns: name state\n"))

# Beware: When doing stats, you need to aggregate yourself:
print(sum(c.query_column("GET hosts\nStats: state >= 0\n")))

# Detect errors:
sites = {
    "muc": {
        "socket": "unix:/var/run/nagios/rw/live",
        "alias": "Munich",
    },
    "sitea": {