Guest User

VM/HOST/CLUSTER Query on VMM

a guest
Jul 24th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.94 KB | Source Code | 0 0
  1. /* *** */
  2. /* MSSQL query on VMMs VirtualManagerDB */
  3. /* *** */
  4.  
  5. /* VMs */
  6. SELECT
  7.         /* (rownum = 1) for showing only newest entry, tbl_WLC_VMInstance.computername can be NULL */
  8.         ROW_NUMBER() OVER(PARTITION BY COALESCE(tbl_WLC_VMInstance.computername,tbl_WLC_VObject.name) ORDER BY tbl_WLC_VObject.creationtime DESC) AS RowNum,
  9.  
  10.         /* name and/or description */
  11.         tbl_WLC_VMInstance.computername,
  12.         tbl_WLC_VObject.name,
  13.         tbl_WLC_HWProfile.description,
  14.  
  15.         /* type */
  16.         CASE
  17.             WHEN tbl_ADHC_Host.OperatingSystemName LIKE '%Microsoft%' THEN 'microsoft_virtual_machine'
  18.             ELSE 'vmware_virtual_machine'
  19.         END AS device_type,
  20.  
  21.         /* inventory date */
  22.         tbl_WLC_VObject.creationtime AS inventory_date,
  23.  
  24.         /* operating system */
  25.         tbl_IL_OS.name AS operating_system,
  26.  
  27.         /* state, 2 seems to be related to VMHosts, 102 seems to be deprecated/non-existant */
  28.         CASE tbl_WLC_VObject.ObjectState
  29.             WHEN   0 THEN 'running'
  30.             WHEN   1 THEN 'stopped'
  31.             WHEN 107 THEN 'update failed'
  32.             WHEN 201 THEN 'migration failed'
  33.             WHEN 220 THEN 'missing'
  34.             WHEN 223 THEN 'incomplete configuration'
  35.             WHEN 225 THEN 'unsupported cluster configuration'
  36.             ELSE CONCAT('unknown:', COALESCE(tbl_WLC_VObject.ObjectState,'NULL'))
  37.         END AS object_status,
  38.  
  39.         /* cpu/ram infos */
  40.         tbl_WLC_HWProfile.ram AS ram,
  41.         tbl_WLC_HWProfile.virtualnumanodespersocketmaximum AS cpu_sockets,
  42.         tbl_WLC_HWProfile.ProcessorCount AS cpu_chips,
  43.         NULL AS cpu_core_count,
  44.         tbl_ADHC_Host.ProcessorSpeed AS cpu_speed,
  45.         COALESCE(tbl_ADHC_Host.ProcessorManufacturer, '') + COALESCE(' ' + tbl_ADHC_Host.ProcessorModel, '') AS cpu
  46.  
  47.     FROM tbl_WLC_VObject /* primary object table */
  48.  
  49.     LEFT JOIN tbl_WLC_VMInstance
  50.         ON tbl_WLC_VMInstance.objectid = tbl_WLC_VObject.objectid
  51.  
  52.     LEFT JOIN tbl_IL_OS
  53.         ON tbl_IL_OS.osid = tbl_WLC_VObject.OSID
  54.  
  55.     LEFT JOIN tbl_ADHC_Host
  56.         ON tbl_ADHC_Host.hostid = tbl_WLC_VObject.hostid
  57.  
  58.     LEFT JOIN tbl_WLC_HWProfile
  59.         ON tbl_WLC_HWProfile.HWProfileId = tbl_WLC_VObject.hwprofileid
  60.  
  61. /* Object Type 1 -> "normal"; 3 -> "VM template"; 46 -> "Snapshot" */
  62.     WHERE tbl_WLC_VObject.objecttype = 1
  63.  
  64. UNION ALL
  65.  
  66. /* Hosts */
  67. SELECT
  68.         /* only needed for UNION */
  69.         '1' AS RowNum,
  70.  
  71.         /* name and/or fqdn and/or description */
  72.         View_ADHC_HostData.computername,
  73.  
  74.         /* might contain fqdn */
  75.         SUBSTRING(CONCAT(View_ADHC_HostData.computername,'.'), 1, CHARINDEX('.',CONCAT(View_ADHC_HostData.computername,'.'))-1) AS name,               
  76.  
  77.         /* only needed for UNION */
  78.         '' AS description,
  79.  
  80.         /* type */
  81.         CASE
  82.             WHEN View_ADHC_HostData.OperatingSystemName LIKE '%Microsoft%' THEN 'microsoft_hyperv_host'
  83.             WHEN View_ADHC_HostData.OperatingSystemName LIKE '%VMware%' THEN 'vmware_esx_host'
  84.             ELSE NULL
  85.         END AS device_type,
  86.  
  87.         /* last update time stamp */
  88.         View_ADHC_HostData.LastUpdatedDateTime AS inventory_date,
  89.  
  90.         /* operating system */
  91.         View_ADHC_HostData.OperatingSystemName AS operating_system,
  92.  
  93.         /* state, 2 seems to be related to VMHosts, 102 seems to be deprecated/non-existant */
  94.         CASE View_ADHC_HostData.State
  95.             WHEN   0 THEN 'running'
  96.             WHEN   1 THEN 'stopped'
  97.             WHEN 107 THEN 'update failed'
  98.             WHEN 201 THEN 'migration failed'
  99.             WHEN 220 THEN 'missing'
  100.             WHEN 223 THEN 'incomplete configuration'
  101.             WHEN 225 THEN 'unsupported cluster configuration'
  102.             ELSE CONCAT('unknown:', COALESCE(View_ADHC_HostData.State,'NULL'))
  103.         END AS object_status,
  104.  
  105.         /* cpu/ram infos */
  106.         FLOOR(CAST(View_ADHC_HostData.TotalMemory AS bigint)/1024) AS ram,
  107.         View_ADHC_HostData.PhysicalProcessorCount AS cpu_socket_count,
  108.         View_ADHC_HostData.PhysicalProcessorCount AS cpu_chip_count,
  109.         View_ADHC_HostData.LogicalProcessorCount AS cpu_core_count,
  110.         View_ADHC_HostData.ProcessorSpeed AS cpu_speed,
  111.         COALESCE(ProcessorManufacturer, '') + COALESCE(' ' + ProcessorModel, '') AS cpu
  112.  
  113.     FROM View_ADHC_HostData /* primary table */
  114.    
  115. UNION ALL
  116.  
  117.  
  118. /* Cluster */
  119. SELECT DISTINCT
  120.         /* only needed for UNION */
  121.         '1' AS RowNum,
  122.  
  123.         /* cluster name */
  124.         tbl_ADHC_HostCluster.name AS computername,
  125.         /* might contain fqdn */
  126.         SUBSTRING(CONCAT(tbl_ADHC_HostCluster.name,'.'), 1, CHARINDEX('.',CONCAT(tbl_ADHC_HostCluster.name,'.'))-1) AS name,   
  127.        
  128.         /* only needed for UNION */
  129.         '' AS description,
  130.  
  131.         /* type */
  132.         CASE
  133.             WHEN tbl_ADHC_Host.OperatingSystemName LIKE '%Microsoft%' THEN 'microsoft_hyperv_cluster'
  134.             WHEN tbl_ADHC_Host.OperatingSystemName LIKE '%VMware%' THEN 'vmware_esx_cluster'
  135.             ELSE NULL
  136.         END AS device_type,
  137.  
  138.         /* rest not applicable */
  139.         NULL AS inventory_date,
  140.         NULL AS operating_system,
  141.         NULL AS object_status,
  142.         NULL AS ram,
  143.         NULL AS cpu_socket_count,
  144.         NULL AS cpu_chip_count,
  145.         NULL AS cpu_core_count,
  146.         NULL AS cpu_speed,
  147.         NULL AS cpu
  148.  
  149.     FROM tbl_WLC_VObject
  150.     LEFT JOIN tbl_ADHC_Host
  151.         ON tbl_ADHC_Host.hostid = tbl_WLC_VObject.hostid
  152.     LEFT JOIN tbl_ADHC_HostCluster
  153.         ON tbl_ADHC_HostCluster.clusterid = tbl_ADHC_Host.HostClusterID
  154. WHERE tbl_ADHC_HostCluster.name IS NOT NULL
  155.     AND tbl_ADHC_HostCluster.name <> ''
  156.  
Advertisement
Add Comment
Please, Sign In to add comment