Name
global.IPAddressFixup
Description
Grooms the CMDB, ensuring that IP addresses are valid and unique to their containing device CI. After a device has been successfully discovered, ensures two things. 1. If the CI table is not extended from any tables in glide.discovery.exclude_ip_sync_classes property then make sure the IP address field on the base CI is one of the IP addresses on the NICs . 2. Make sure no other device has the IP address that is on this CI being discovered. If there is, then clear the IP address on the other devices. This functionality is off by default.
Script
// Discovery
gs.include('PrototypeServer');
/**
* Grooms the CMDB, ensuring that IP addresses are valid and unique to their containing device CI.
* @author bow.rugerri, tom.dilatush, aleck.lin
*/
var IPAddressFixup = Class.create();
IPAddressFixup.prototype = {
initialize: function(cmdb_ci) {
this.ci = cmdb_ci;
},
fix: function() {
var enforce_ip_sync = GlideProperties.getBoolean('glide.discovery.enforce_ip_sync', true);
// Verify if the Ci class is not extended from any classes in "glide.discovery.exclude_ip_sync_classes" property that we don't want to replace with it with NIC ip-addresses.
var exclude_ip_sync_classes = GlideProperties.get('glide.discovery.exclude_ip_sync_classes').split(',');
var hdGr = new GlideRecord('cmdb_ci_hardware');
var syncClass = true;
if (hdGr.get(this.ci)){
var ciTable = hdGr.getValue('sys_class_name');
var heirarchy = GlideDBObjectManager.getTables(ciTable);
for (var i = 0; i < exclude_ip_sync_classes.length && syncClass ; i++)
if (heirarchy.contains(exclude_ip_sync_classes[i]))
syncClass = false;
}
if (enforce_ip_sync && syncClass)
this.syncIP();
var enforce_unique_ip = GlideProperties.getBoolean('glide.discovery.enforce_unique_ips', false);
if (enforce_unique_ip)
this.enforceUniqueIP();
},
// Ensure that ip_address field on the base CI is set to one the IP addresses (from cmdb_ci_ip_address) on the NICs, or leave it alone if there were no IPs...
syncIP: function() {
//If the ip is already in one of the IPs, then don't go through the IP fix-up...
var IPs = [];
var parentIP = this.getParentIP();
var gr = new GlideRecord('cmdb_ci_ip_address');
gr.addQuery('nic.cmdb_ci', this.ci);
gr.addQuery('ip_version', 4);
gr.addQuery('install_status', '!=', 100);
gr.orderBy('ip_address');
gr.query();
while (gr.next()) {
var ip = gr.ip_address + '';
if (ip == parentIP)
return;
IPs.push(ip);
}
// If there's no IPs, then nuthin' to do...
if (IPs.length == 0)
return;
// If we found a non-localhost IP, then we're done
for (var i = 0; i < IPs.length; i++) {
var nicIP;
try {
nicIP = new SNC.IPAddressV4(IPs[i]);
} catch (e) {
continue;
}
if (!nicIP.isLocalhost() && !nicIP.isLinkLocalAddress()) {
this.setParentIP(nicIP.toString());
return;
}
}
//If we get here, then no good IP is available... so no need to update
},
setParentIP: function(ip) {
var gr = new GlideRecord('cmdb_ci_hardware');
if (this.ci && gr.get('sys_id', this.ci)) {
gr.ip_address = ip;
gr.update();
}
},
getParentIP: function() {
var gr = new GlideRecord('cmdb_ci_hardware');
if (this.ci && gr.get('sys_id', this.ci))
return gr.ip_address + '';
},
enforceUniqueIP: function() {
var ciGR = new GlideRecord('cmdb_ci_hardware');
ciGR.get(this.ci);
var ipStr = ciGR.ip_address + '';
var ourIP = new SNC.IPAddressV4(ipStr);
if ((!ourIP.isValid()) || (ourIP.isLocalhost()) ||(ourIP.isLinkLocalAddress()))
return;
this.wipeIP('cmdb_ci_hardware', ipStr);
},
wipeIP: function(tablename, ip) {
var ciGR = new GlideRecord(tablename);
ciGR.addQuery('ip_address', ip);
ciGR.addQuery('sys_id', '!=', this.ci);
ciGR.query();
while (ciGR.next()) {
ciGR.ip_address = '';
ciGR.update();
}
},
type: 'IPAddressFixup'
}
Sys ID
350895ad4a36231d00eb160120869c2e