-- | Ask luxid about the hypervisors used. As, at the moment, we only
-- have specialised CPU collectors for xen, we're only interested which
-- instances run under the Xen hypervisor.
getXenInstances :: ResultT String IO (Set.Set String)
getXenInstances = do
  let query = L.Query (Qlang.ItemTypeOpCode Qlang.QRInstance)
              ["name", "hypervisor"] Qlang.EmptyFilter
  luxiSocket <- liftIO Path.defaultQuerySocket
  raw <- bracket (mkResultT . liftM (either (Bad . show) Ok)
                   . tryIOError $ L.getLuxiClient luxiSocket)
                 (liftIO . L.closeClient)
                 $ mkResultT' . L.callMethod query
  answer <- L.extractArray raw >>= mapM (mapM L.fromJValWithStatus)
  let getXen [name, hv] | hv `elem` ["xen-pvm", "xen-hvm"] = [name]
      getXen _ = []
  return $ Set.fromList (answer >>= getXen)

-- | Look for an instance in a given report.
getXenInstances :: ResultT String IO (Set.Set String)
getXenInstances = do
  let query = L.Query (Qlang.ItemTypeOpCode Qlang.QRInstance)
              ["name", "hypervisor"] Qlang.EmptyFilter
  luxiSocket <- liftIO Path.defaultQuerySocket
  raw <- bracket (mkResultT . liftM (either (Bad . show) Ok)
                   . tryIOError $ L.getLuxiClient luxiSocket)
                 (liftIO . L.closeClient)
                 $ mkResultT' . L.callMethod query
  answer <- L.extractArray raw >>= mapM (mapM L.fromJValWithStatus)
  let getXen [name, hv] | hv `elem` ["xen-pvm", "xen-hvm"] = [name]
      getXen _ = []
  return $ Set.fromList (answer >>= getXen)

-- | Look for an instance in a given report.
getXenInstances = do
  let query = L.Query (Qlang.ItemTypeOpCode Qlang.QRInstance)
              ["name", "hypervisor"] Qlang.EmptyFilter
  luxiSocket <- liftIO Path.defaultQuerySocket
  raw <- bracket (mkResultT . liftM (either (Bad . show) Ok)
                   . tryIOError $ L.getLuxiClient luxiSocket)
                 (liftIO . L.closeClient)
                 $ mkResultT' . L.callMethod query
  answer <- L.extractArray raw >>= mapM (mapM L.fromJValWithStatus)
  let getXen [name, hv] | hv `elem` ["xen-pvm", "xen-hvm"] = [name]
      getXen _ = []
  return $ Set.fromList (answer >>= getXen)

-- | Look for an instance in a given report.