For this scenario, I'll assume you're using ServiceNow as your platform. The solution involves creating a client script that triggers when the variable on the catalog item referring to the cmdb_ci_server table changes. To check for operational virtual servers linked to the selected physical server, you can query the cmdb_rel_ci table using the relevant IP address, filtering by operational status.
Step 1: Create a Client Script for the Catalog Item
- Navigate to
Service Catalog > Catalog Definitions > Catalog Items.
- Select the catalog item you're working with.
- In the related links section at the bottom of the page, click on
Client Scripts.
- Click on
New to create a new client script.
Step 2: Script Logic
This script does the following:
- Gets triggered when the value of the ‘physical server’ variable changes.
- Checks for any operational virtual servers associated with that physical server by traversing the relationship in the
cmdb_rel_ci table and checking the operational status in the cmdb_ci_server table.
- If operational VMs are found, it displays a popup message listing the operational virtual servers' IP addresses.
Example Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Assuming 'physical_server_var' is the name of the catalog variable for the Physical server
var physicalServerSysId = g_form.getValue('physical_server_var');
// AJAX call to the server to find operational virtual servers
var ga = new GlideAjax('VirtualServerChecker');
ga.addParam('sysparm_name', 'checkOperationalVMs');
ga.addParam('sysparm_physical_server_sysid', physicalServerSysId);
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
if (result && result.length > 0) {
var message = "The following operational virtual servers are running on the selected Physical Server. Please make sure that there are no operational virtual servers tagged to the Physical server being decommissioned:\n";
result.forEach(function(vm) {
message += "IP: " + vm.ip + "\n";
});
alert(message);
// Clear the Physical server mandatory variable
g_form.clearValue('physical_server_var');
}
});
}
Step 3: Write a Script Include
The client script makes an AJAX call to VirtualServerChecker, a Script Include you need to create. This Script Include will query the cmdb_rel_ci and cmdb_ci_server tables to find any operational virtual servers associated with the physical server.
VirtualServerChecker Script Include Example:
var VirtualServerChecker = Class.create();
VirtualServerChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkOperationalVMs: function() {
var physicalServerSysId = this.getParameter('sysparm_physical_server_sysid');
var operationalVMs = [];
// Your logic here to query the cmdb_rel_ci and cmdb_ci_server tables
// Example logic, modify according to your relationship and status fields
var relGr = new GlideRecord('cmdb_rel_ci');
relGr.addQuery('parent', physicalServerSysId);
relGr.query();
while (relGr.next()) {
var vmGr = new GlideRecord('cmdb_ci_server');
vmGr.get(relGr.child.toString());
if (vmGr.operational_status == 'Operational' && vmGr.u_is_virtual == 'true') { // Check based on your field names
operationalVMs.push({
ip: vmGr.ip_address.toString()
});
}
}
return JSON.stringify(operationalVMs);
}
});
Ensure you choose the correct field names for your ServiceNow instance, as these can vary based on configuration, additional modules, or customization. This script includes only a simple example, so you may need to adjust queries and filters based on your data model and requirements.
Please Note: Testing and validating the scripts in a sub-production environment before deploying them on a production instance is crucial to avoid any business disruptions.