Forum Discussion

clinton_buss's avatar
3 years ago

VCenter - What VM's have ISOS mounted?

With https://kb.vmware.com/s/article/87249 announced the other day, it seemed like a perfect use-case for LM since we use it to monitor all our vcenter instances, but I don't see anything specific to ISOS & VCenter VM's. Apologies in advance if I have overlooked the obvious.

  • You would likely need to write a custom DataSource but sounds doable. Perhaps clone VMware_vCenter_VMStatus and modify the groovy code to query mounted media and connected state. You can also try asking LM support if they have anything unofficial or tips.

  • 2 minutes ago, Michael Rodrigues said:

    @Michael Baker you will need to toggle it to "Public" in the "My Repository" tab of the Exchange for others to download it.

    We default everything to "Private" to avoid accidental publish.

    Done!

  • Round 2 this is much faster:

    <?xml version="1.0" encoding="UTF-8" ?>
    <feed  version="1.0" hasPendingRequests="false" >
      <company></company>
      <status>200</status>
      <errmsg>OK</errmsg>
      <interval>0</interval>
        <entry type="predatasource">
            <version>1641510208</version>
            <name>VMware_vCenter_Check_If_ISO_Mounted</name>
            <displayedas>VMWAre VM Check If ISO Mounted</displayedas>
            <description>Detect possible CVE</description>
            <collector>batchscript</collector>
            <hasMultiInstances>true</hasMultiInstances>
            <useWildValueAsUniqueIdentifier>false</useWildValueAsUniqueIdentifier>
            <schedule>120</schedule>
            <appliesTo>system.virtualization =~ &#34;VMware ESX vcenter&#34;</appliesTo>
            <wildcardauto>true</wildcardauto>
            <wildcardpersist>false</wildcardpersist>
            <wildcardlinuxscript>ad_script</wildcardlinuxscript>
            <wildcardlinuxcmdline>type=&#34;embeded&#34; </wildcardlinuxcmdline>
            <wildcardwinscript>ad_script</wildcardwinscript>
            <wildcardwincmdline>type=&#34;embeded&#34; </wildcardwincmdline>
            <wildcardgroovyscript>/*******************************************************************************
     * © 2007-2019 - LogicMonitor, Inc. All rights reserved.
     ******************************************************************************/
    
    import com.vmware.vim25.*;
    import com.vmware.vim25.mo.*;
    import java.security.cert.*;
    import org.apache.http.auth.*;
    import org.apache.http.conn.ssl.*;
    import org.apache.http.impl.client.*;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.client.methods.HttpGet;
    import com.santaba.agent.groovyapi.esx.ESX
    import groovy.json.JsonSlurper;
    
    def host = hostProps.get(&#34;system.hostname&#34;)
    def user = hostProps.get(&#34;esx.user&#34;);
    def pass = hostProps.get(&#34;esx.pass&#34;);
    def addr = hostProps.get(&#34;esx.url&#34;) ?: &#34;https://${host}/sdk&#34;;
    def keyNamespace = hostProps.get(hostProps.get(&#34;topo.namespace&#34;, &#34;&#34;), &#34;&#34;)
    def keyBlacklist = hostProps.get(&#34;topo.blacklist&#34;, &#34;&#34;).tokenize(&#34;,&#34;)
    
    // Open a connection to the vSphere API, get a service instance and root folder
    
    def svc = new ESX();
    svc.open(addr, user, pass, 10 * 1000); // timeout in 10 seconds
    
    def si = svc.getServiceInstance();
    def rootFolder = si.getRootFolder();
    
    // Get VMs
    def vms = new InventoryNavigator(rootFolder).searchManagedEntities(&#34;VirtualMachine&#34;);
    
    // Get ESX Hosts
    def hostsystems = new InventoryNavigator(rootFolder).searchManagedEntities(&#34;HostSystem&#34;)
    
    // iterate over vms
    vms.each { vm -&#62;
    
        // Get AD info
        def wildvalue = vm.MOR.val;
        def wildalias = vm.name;
        def config = vm.config.hardware.device
            for(dev in config)
            {
                if(dev instanceof com.vmware.vim25.VirtualCdrom) {
                    if(dev.backing instanceof com.vmware.vim25.VirtualCdromIsoBackingInfo) {
                        if(dev.connectable.connected) {
                            println &#34;${wildvalue}##${wildalias}&#34;;
                        }
                    }
                }
            }
    }
    
    return 0
    </wildcardgroovyscript>
            <wildcardschedule>60</wildcardschedule>
            <wildcarddisable>false</wildcarddisable>
            <wildcarddeleteinactive>false</wildcarddeleteinactive>
            <agdmethod>none</agdmethod>
            <agdparams></agdparams>
            <group></group>
            <tags>application,vsphere,virtualization,vm,vmware</tags>
            <technology>:: replaces old ESX_VM_Status ::
    - define esx.user and esx.pass for authentication
    - optionally override vSphere API URL with esx.url property
    - optionally enable vSphere Tags -&#62; instance level properties by setting esx.tags to &#34;true&#34; (vCenter only)
    - url-encode ILPs that are strings in AD script</technology>
            <adlist><![CDATA[{"agdmethod":"none","method":"ad_script","agdparams":"","id":0,"filters":[],"params":{"type":"embeded","groovyscript":"/*******************************************************************************\n * © 2007-2019 - LogicMonitor, Inc. All rights reserved.\n ******************************************************************************/\n\nimport com.vmware.vim25.*;\nimport com.vmware.vim25.mo.*;\nimport java.security.cert.*;\nimport org.apache.http.auth.*;\nimport org.apache.http.conn.ssl.*;\nimport org.apache.http.impl.client.*;\nimport org.apache.http.util.EntityUtils;\nimport org.apache.http.client.methods.HttpGet;\nimport com.santaba.agent.groovyapi.esx.ESX\nimport groovy.json.JsonSlurper;\n\ndef host = hostProps.get(\"system.hostname\")\ndef user = hostProps.get(\"esx.user\");\ndef pass = hostProps.get(\"esx.pass\");\ndef addr = hostProps.get(\"esx.url\") ?: \"https://${host}/sdk\";\ndef keyNamespace = hostProps.get(hostProps.get(\"topo.namespace\", \"\"), \"\")\ndef keyBlacklist = hostProps.get(\"topo.blacklist\", \"\").tokenize(\",\")\n\n// Open a connection to the vSphere API, get a service instance and root folder\n\ndef svc = new ESX();\nsvc.open(addr, user, pass, 10 * 1000); // timeout in 10 seconds\n\ndef si = svc.getServiceInstance();\ndef rootFolder = si.getRootFolder();\n\n// Get VMs\ndef vms = new InventoryNavigator(rootFolder).searchManagedEntities(\"VirtualMachine\");\n\n// Get ESX Hosts\ndef hostsystems = new InventoryNavigator(rootFolder).searchManagedEntities(\"HostSystem\")\n\n// iterate over vms\nvms.each { vm ->\n\n    // Get AD info\n    def wildvalue = vm.MOR.val;\n    def wildalias = vm.name;\n    def config = vm.config.hardware.device\n        for(dev in config)\n        {\n            if(dev instanceof com.vmware.vim25.VirtualCdrom) {\n                if(dev.backing instanceof com.vmware.vim25.VirtualCdromIsoBackingInfo) {\n                    if(dev.connectable.connected) {\n                        println \"${wildvalue}##${wildalias}\";\n                    }\n                }\n            }\n        }\n}\n\nreturn 0\n"}}]]></adlist>
            <schemaVersion>2</schemaVersion>
            <dataSourceType>1</dataSourceType>
            <attributes>
            <attribute>
                <name>scripttype</name>
                <value>embed</value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>scriptgroovy</name>
                <value>import com.vmware.vim25.mo.*;
    import com.santaba.agent.groovyapi.esx.ESX;
    
    def host = hostProps.get(&#34;system.hostname&#34;)
    def user = hostProps.get(&#34;esx.user&#34;);
    def pass = hostProps.get(&#34;esx.pass&#34;);
    def addr = hostProps.get(&#34;esx.url&#34;) ?: &#34;https://${host}/sdk&#34;;
    
    def svc = new ESX();
    svc.open(addr, user, pass, 10 * 1000); // timeout in 10 seconds
    
    def si = svc.getServiceInstance();
    def rootFolder = si.getRootFolder();
    
    def vms = new InventoryNavigator(rootFolder).searchManagedEntities(&#34;VirtualMachine&#34;);
    
    
    vms.each
    {
        vm -&#62;
            def wildvalue = vm.MOR.val;
            def config = vm.config.hardware.device
            for(dev in config)
            {
              if(dev instanceof com.vmware.vim25.VirtualCdrom) {
                if(dev.backing instanceof com.vmware.vim25.VirtualCdromIsoBackingInfo) {
                  if(dev.connectable.connected) {
                     println &#34;${wildvalue}.isoconnected=1&#34;;
                  }
                }
              }
            }       
            
    }
    
    return 0;</value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>windowsscript</name>
                <value></value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>linuxscript</name>
                <value></value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>windowscmdline</name>
                <value></value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>linuxcmdline</name>
                <value></value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>eridetectioninterval</name>
                <value>60</value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>eriscripttype</name>
                <value>embeded</value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>eriscriptgroovy</name>
                <value>import com.santaba.agent.util.esx.EsxVcService
    import com.vmware.vim25.VirtualDisk
    import com.vmware.vim25.mo.Folder
    import com.vmware.vim25.mo.InventoryNavigator
    import com.vmware.vim25.mo.ManagedEntity
    import com.vmware.vim25.VirtualEthernetCard
    import com.vmware.vim25.VirtualDevice
    import groovy.json.JsonSlurper
    import org.apache.http.auth.AuthScope
    import org.apache.http.auth.UsernamePasswordCredentials
    import org.apache.http.client.methods.HttpGet
    import org.apache.http.conn.ssl.AllowAllHostnameVerifier
    import org.apache.http.conn.ssl.SSLContexts
    import org.apache.http.conn.ssl.TrustStrategy
    import org.apache.http.impl.client.BasicCredentialsProvider
    import org.apache.http.impl.client.HttpClients
    import org.apache.http.impl.client.LaxRedirectStrategy
    import org.apache.http.util.EntityUtils
    import java.security.cert.CertificateException
    import java.security.cert.X509Certificate
    def host = hostProps.get(&#34;system.hostname&#34;)
    def user = hostProps.get(&#34;esx.user&#34;);
    def pass = hostProps.get(&#34;esx.pass&#34;);
    def custom_url = hostProps.get(&#34;esx.url&#34;);
    def vmMorVal = instanceProps.get(&#34;wildvalue&#34;);
    def vmName = instanceProps.get(&#34;wildalias&#34;);
    def eri = host+&#34;--&#34;+vmMorVal;
    def ExternalResourceIDs = [eri]
    // Open a connection to the vSphere API, get a service instance and root folder
    def url = custom_url ?: String.format(&#34;https://%s/sdk&#34;, host);
    def svc = EsxVcService.connect(url, user, pass, 10 * 1000); // timeout in 10 seconds
    def si = svc.getServiceInstance();
    def rootFolder = si.getRootFolder();
    ManagedEntity[] vms;
    // Get VMs
    vms = new InventoryNavigator(rootFolder).searchManagedEntities(&#34;VirtualMachine&#34;);
    // iterate over vms
    vms.each
    { vm -&#62;
        // Get AD info
        def aVMMorVal = vm.MOR.val
        def aVMName = vm.name
        if(aVMMorVal == vmMorVal) {
            for (VirtualDevice vd : vm.getConfig().getHardware().getDevice()) {
                try {
                    if (vd instanceof VirtualEthernetCard) {
                        VirtualEthernetCard vEth = (VirtualEthernetCard) vd;
                        ExternalResourceIDs += vEth.macAddress;
                    }
                } catch (Exception e) {
                }
            }
        }
    }
    println &#34;predef.externalResourceID=&#34; + ExternalResourceIDs.join(&#34;,&#34;);
    println &#34;predef.externalResourceType=&#34; + &#34;VirtualMachine&#34;
    return 0;</value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>eriwinscript</name>
                <value></value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>erilinuxscript</name>
                <value></value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>eriwincmdline</name>
                <value></value>
                <comment></comment>
            </attribute>
            <attribute>
                <name>erilinuxcmdline</name>
                <value></value>
                <comment></comment>
            </attribute>
            </attributes>
            <datapoints>
            <datapoint>
                <name>isoconnected</name>
                <dataType>7</dataType>
                <type>2</type>
                <postprocessormethod>namevalue</postprocessormethod>
                <postprocessorparam>isconnected</postprocessorparam>
                <usevalue>output</usevalue>
                <alertexpr></alertexpr>
                <alertmissing>1</alertmissing>
                <alertsubject></alertsubject>
                <alertbody></alertbody>
                <enableanomalyalertsuppression></enableanomalyalertsuppression>
                <adadvsettingenabled>false</adadvsettingenabled>
                <warnadadvsetting></warnadadvsetting>
                <erroradadvsetting></erroradadvsetting>
                <criticaladadvsetting></criticaladadvsetting>
                <description></description>
                <maxvalue>1</maxvalue>
                <minvalue>0</minvalue>
                <userparam1></userparam1>
                <userparam2></userparam2>
                <userparam3></userparam3>
                <iscomposite>false</iscomposite>
                <rpn></rpn>
                <alertTransitionIval>0</alertTransitionIval>
                <alertClearTransitionIval>0</alertClearTransitionIval>
            </datapoint>
            </datapoints>
            <graphs>
            </graphs>
            <overviewgraphs>
            </overviewgraphs>
            <scripts>
            </scripts>
        </entry>
    </feed>

     

  • Just now, Michael Rodrigues said:

    Awesome turnaround @Michael Baker. If you publish that to the Exchange I can put it through Security Review for you.

    Minor bigfix then I will push we have sooo many VC's doing it by hand would be nasty

  • @Michael Baker you will need to toggle it to "Public" in the "My Repository" tab of the Exchange for others to download it.

    We default everything to "Private" to avoid accidental publish.

  • Anonymous's avatar
    Anonymous

    Would be good to bundle a TopoSource with this so that VMs can be mapped to ISOs.