'Query a livestatus backend'

def query_backend(self, backend, host=None):

    hosts_request = Socket(backend['connection']).hosts.columns(backend['host_field'], backend['group_field'])
    if (backend['host_filter'] is not None):
        hosts_request = hosts_request.filter(backend['host_filter'])
    if (host is not None):
        hosts_request = hosts_request.filter(('name    =    ' + host[0]))
    hosts_request._columns += backend['fields']
    hosts = hosts_request.call()
    for host in hosts:
        hostname = host[backend['host_field']]
        hostgroups = host[backend['group_field']]
        if (not isinstance(hostgroups, list)):
            hostgroups = [hostgroups]
        self.add_host(hostname, 'all')
        self.add_host(hostname, backend['name'])
        for group in hostgroups:
            self.add_host(hostname, group)
        for field in backend['fields']:
            var_name = (backend['var_prefix'] + field)
            if (hostname not in self.result['_meta']['hostvars']):
                self.result['_meta']['hostvars'][hostname] = {}
            self.result['_meta']['hostvars'][hostname][var_name] = host[field]
