Thursday, February 22, 2024

 startVirtualComputerCheck():


Initiates the virtual computer check and sets the virtual field to true if certain conditions are met.

Checks if the configuration item is a Solaris Zone, VMWare, HyperV, or falls under credentialless discovery.

Establishes relationships between virtual machines (VMs), instances, and hypervisors.

isSolarisZone():


Checks if the configuration item is a Solaris Zone based on its system class name, correlation_id, and specific conditions.

isVMWare():


Checks if the configuration item is a VMWare VM based on its serial number.

isHyperV():


Checks if the configuration item is a HyperV VM based on its serial number and specific conditions.

handleSolariZone():


Handles the case when the configuration item is identified as a Solaris Zone by querying and setting the corresponding instance.

isCredentialless():


Checks if the discovery is credentialless and attempts to reconcile with a VMWare VM based on IP address.

handleVMWare():


Handles the case when the configuration item is identified as a VMWare VM by querying and setting the corresponding instance.

commonVMWareHandling():


Common handling for VMWare instances and guests, filtering out reconciled relationships.

handleHyperV():


Handles the case when the configuration item is identified as a HyperV VM.

findVMWareByImage():


Finds a VMWare instance based on the relationship with the provided instance.

reconcileRelationVMtoHyperVisor():

Reconciles relationships between VM and HyperVisor, creating or correcting relationships.

setIsVirtualForCloudIPs():

Sets the virtual field to true based on specific IP address patterns.

This script seems to be part of a larger system responsible for identifying and managing virtualized resources within a CMDB. The comments in the code provide insights into the logic and reasoning behind various decisions.

[2:09 PM, 2/21/2024] Parth New ServiceNow Support: function isVMWare() {: This line starts the definition of the isVMWare function.


if (!fVMGr.serial_number.hasValue()): Checks if the serial number of a virtual machine (fVMGr) has a value. If not, it returns false.


return false;: If the serial number is empty, the function immediately returns false.


if (!fVMGr.serial_number.toLowerCase().startsWith("vmware-")): Checks if the lowercase version of the serial number starts with the string "vmware-". This is a common pattern for VMware serial numbers. If not, it returns false.


return false;: If the serial number doesn't match the expected pattern, the function returns false.


return true;: If the serial number has a value and starts with "vmware-", the function returns true, indicating that the device is recognized as a VMware virtual machine.


}: Closes the isVMWare function.


function isHyperV() {: This line starts the definition of the isHyperV function.


var serialNumber = fVMGr.serial_number;: Stores the serial number of the virtual machine in the variable serialNumber for easier reference.


if (!serialNumber.hasValue()): Checks if the serial number has a value. If not, it returns false.


return false;: If the serial number is empty, the function immediately returns false.


var instGr = new GlideRecord("cmdb_ci_hyper_v_instance");: Creates a new GlideRecord object for the "cmdb_ci_hyper_v_instance" table, which likely stores information about Hyper-V instances.


var serials = ['chassis_serial', 'bios_serial', 'baseboard_serial'];: Defines an array serials containing three serial-related fields in the "cmdb_ci_hyper_v_instance" table.


return serials.some( ... );: Uses the some function to iterate over the serials array and check if the serial number matches any of these fields.


function(serial) { ... }: Defines an anonymous function that takes a serial field as a parameter.


instGr.initialize();: Initializes the GlideRecord object for each iteration.


if (instGr.get(serial, serialNumber)) { ... }: Attempts to retrieve a record from the "cmdb_ci_hyper_v_instance" table where the specified serial field matches the virtual machine's serial number.


fInstanceGr = instGr;: If a matching record is found, it assigns the GlideRecord object to the variable fInstanceGr.


return true;: If a match is found for any serial field, the anonymous function returns true, and the some function stops iterating.


});: Closes the anonymous function and the some function.


}: Closes the isHyperV function.


In summary, these functions determine whether a device is a VMware or Hyper-V virtual machine based on its serial number, returning true if it matches the respective criteria and false otherwise. The Hyper-V function also sets a GlideRecord object (fInstanceGr) if a match is found.

[2:13 PM, 2/21/2024] Parth New ServiceNow Support: let's break down each function in detail:


1. function handleSolariZone() {

javascript


var matchingId = fVMGr.correlation_id.substring(5);

var gr = new GlideRecord('cmdb_ci_solaris_instance');

gr.addQuery('correlation_id', matchingId);

gr.query();

if (gr.next())

    fInstanceGr = gr;


This function appears to handle Solaris zones. Here's a step-by-step explanation:


matchingId = fVMGr.correlation_id.substring(5);: Extracts a substring from the correlation_id of the current virtual machine record (fVMGr). It starts from the 6th character, effectively excluding the first 5 characters.


gr = new GlideRecord('cmdb_ci_solaris_instance');: Creates a new GlideRecord object for the "cmdb_ci_solaris_instance" table.


gr.addQuery('correlation_id', matchingId);: Adds a query condition to the GlideRecord, filtering records where the correlation_id matches the previously derived matchingId.


gr.query();: Executes the query on the GlideRecord.


if (gr.next()) fInstanceGr = gr;: If there's at least one record matching the criteria, it advances to the first matching record (gr.next()) and assigns it to the variable fInstanceGr.


[2:14 PM, 2/21/2024] Parth New ServiceNow Support: 2. function isCredentialless() {

javascript


var potentialVmInstances, potentialGuests;


if (!current.ip_address || !current.ip_address.changes() || current.discovery_source != 'CredentiallessDiscovery')

    return;


potentialVmInstances = new GlideRecord('cmdb_ci_vmware_instance');

potentialVmInstances.addQuery('ip_address', current.ip_address);

potentialVmInstances.setLimit(2);

potentialVmInstances.query();


potentialGuests = new GlideRecord('cmdb_ci_computer');

potentialGuests.addQuery('ip_address', current.ip_address);

potentialGuests.addQuery('sys_id', '!=', current.sys_id);

potentialGuests.setLimit(2);

potentialGuests.query();


commonVMWareHandling(potentialVmInstances, potentialGuests);


return !!fInstanceGr;

This function appears to check if the current record is being created by credentialless discovery and attempts to reconcile it with a VMWare VM based on IP. Here's a detailed breakdown:


Initial Variable Declarations:


var potentialVmInstances, potentialGuests;: Declares two GlideRecord variables for potential VMware instances and potential computer guests.

Credentialless Discovery Check:


if (!current.ip_address || !current.ip_address.changes() || current.discovery_source != 'CredentiallessDiscovery') return;: Checks if the IP address is empty, hasn't changed, or if the discovery source is not 'CredentiallessDiscovery'. If any of these conditions are met, the function returns early.

Query Potential VMware Instances:


potentialVmInstances = new GlideRecord('cmdb_ci_vmware_instance');: Creates a GlideRecord object for the "cmdb_ci_vmware_instance" table.

potentialVmInstances.addQuery('ip_address', current.ip_address);: Adds a query condition to find VMware instances with the same IP address as the current record.

potentialVmInstances.setLimit(2);: Sets a limit of 2 records to retrieve.

potentialVmInstances.query();: Executes the query.

Query Potential Computer Guests:


potentialGuests = new GlideRecord('cmdb_ci_computer');: Creates a GlideRecord object for the "cmdb_ci_computer" table.

potentialGuests.addQuery('ip_address', current.ip_address);: Adds a query condition to find computer records with the same IP address as the current record.

potentialGuests.addQuery('sys_id', '!=', current.sys_id);: Excludes the current record from the query.

potentialGuests.setLimit(2);: Sets a limit of 2 records to retrieve.

potentialGuests.query();: Executes the query.

Common VMware Handling Function:


commonVMWareHandling(potentialVmInstances, potentialGuests);: Calls a common function (commonVMWareHandling) with the potential VMware instances and computer guests as parameters.

Return Statement:


return !!fInstanceGr;: Returns a boolean indicating whether fInstanceGr has a value. If it has a value, it returns true; otherwise, it returns false.

[3:01 PM, 2/21/2024] Parth New ServiceNow Support: This JavaScript function, named isHyperV, checks whether a device (presumably a virtual machine) is a Hyper-V virtual machine based on its serial number. Let's break down each part of the function:



function isHyperV() {

    var serialNumber = fVMGr.serial_number;


    if (!serialNumber.hasValue())

        return false;


    var instGr = new GlideRecord("cmdb_ci_hyper_v_instance");

    var serials = ['chassis_serial', 'bios_serial', 'baseboard_serial'];


    return serials.some(

        function(serial) {

            instGr.initialize();

            if (instGr.get(serial, serialNumber)) {

                fInstanceGr = instGr;

                return true;

            }

        }

    );

}

var serialNumber = fVMGr.serial_number;: Retrieves the serial number of the virtual machine from the fVMGr object and stores it in the variable serialNumber.


if (!serialNumber.hasValue()) return false;: Checks if the serialNumber has a value. If not, it immediately returns false, indicating that the device is not a Hyper-V virtual machine.


var instGr = new GlideRecord("cmdb_ci_hyper_v_instance");: Creates a new GlideRecord object for the "cmdb_ci_hyper_v_instance" table, suggesting that information about Hyper-V instances is stored in this table.


var serials = ['chassis_serial', 'bios_serial', 'baseboard_serial'];: Defines an array serials containing three types of serial numbers associated with Hyper-V instances.


return serials.some(...);: Uses the some function to iterate over the serials array and check if any of the Hyper-V serial numbers match the virtual machine's serial number.


function(serial) { ... }: Defines an anonymous function that takes a serial type as a parameter.


instGr.initialize();: Initializes the GlideRecord object instGr for each iteration.


if (instGr.get(serial, serialNumber)) { ... }: Attempts to retrieve a record from the "cmdb_ci_hyper_v_instance" table where the specified serial field matches the virtual machine's serial number.


fInstanceGr = instGr;: If a matching record is found, it assigns the GlideRecord object to the variable fInstanceGr.


return true;: If a match is found for any serial field, the anonymous function returns true, and the some function stops iterating.


});: Closes the anonymous function and the some function.


The entire function returns the result of the some function, indicating whether the virtual machine is identified as a Hyper-V instance based on the provided serial numbers. If true, it sets fInstanceGr to the matching GlideRecord object. If false, it indicates that the device is not recognized as a Hyper-V virtual machine.


No comments:

Post a Comment

Discovery troubleshooting | Error messages

  Discovery troubleshooting | Error messages - Support and Troubleshooting (servicenow.com) Description Learn how to resolve common Discover...