def connect(): init_site_status() # If there is only one site (non-multisite), then user cannot enable/disable. if config.is_multisite(): connect_multiple_sites() else: connect_single_site() set_livestatus_auth()
def may_see(host): if config.may("general.see_all"): return True # FIXME: Or maybe make completely transparent and add pseudo local_connection() to Single livestatus clas? if config.is_multisite(): conn = html.live.local_connection() else: conn = html.live # livestatus connection is setup with AuthUser return conn.query_value("GET hosts\nStats: state >= 0\nFilter: name = %s\n" % lqencode(host)) > 0
def site_selector(html, htmlvar, enforce = True): if not config.is_multisite(): choices = [("", _("(local)"))] else: if enforce: choices = [] else: choices = [("","")] for sitename, state in html.site_status.items(): if state["state"] == "online": choices.append((sitename, config.site(sitename)["alias"])) html.sorted_select(htmlvar, choices)
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')
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, {}) # Convert livestatus-proxy links into UNIX socket s = site["socket"] if type(s) == tuple and s[0] == "proxy": site["socket"] = "unix:" + defaults.livestatus_unix_socket + "proxy/" + 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 use_livestatus_auth = True if html.output_format == 'html': if config.may("general.see_all") and not config.user.get("force_authuser"): use_livestatus_auth = False else: if config.may("general.see_all") and not config.user.get("force_authuser_webservice"): use_livestatus_auth = False if use_livestatus_auth == True: 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')