|
| 1 | +// Array of IP addresses to check |
| 2 | +var ipAddressLists = ['192.168.1.35', '192.168.1.27', '192.168.1.15']; |
| 3 | + |
| 4 | +// CMDB table where CI records are stored(Could be Linux/Unix,AIX server,etc) |
| 5 | +var ciTableName = 'cmdb_ci_computer'; |
| 6 | + |
| 7 | +// Array to hold the final output objects |
| 8 | +var ipDiscoveryMapping = []; |
| 9 | + |
| 10 | +// Loop through each IP address in the list |
| 11 | +for (var i = 0; i < ipAddressLists.length; i++) { |
| 12 | + var ip = ipAddressLists[i]; |
| 13 | + |
| 14 | + // Query the CMDB to find the CI record with the given IP address |
| 15 | + var ciRecord = new GlideRecord(ciTableName); |
| 16 | + ciRecord.addEncodedQuery('ip_address=' + ip); |
| 17 | + ciRecord.query(); |
| 18 | + |
| 19 | + // Proceed only if a CI record is found for this IP |
| 20 | + if (ciRecord.next()) { |
| 21 | + |
| 22 | + // Query discovery_device_history to find the related discovery status |
| 23 | + var deviceHistoryGr = new GlideRecord('discovery_device_history'); |
| 24 | + deviceHistoryGr.addEncodedQuery( |
| 25 | + 'cmdb_ci=' + ciRecord.getUniqueValue() + |
| 26 | + '^last_state=Created CI^ORlast_state=Updated CI' |
| 27 | + ); |
| 28 | + deviceHistoryGr.query(); |
| 29 | + |
| 30 | + // If discovery history exists, capture its status number |
| 31 | + if (deviceHistoryGr.next()) { |
| 32 | + // Create an object with IP and discovery status number |
| 33 | + var recordObj = { |
| 34 | + ip_address: ip, |
| 35 | + discovery_status_number: deviceHistoryGr.getDisplayValue('status') |
| 36 | + }; |
| 37 | + ipDiscoveryMapping.push(recordObj); |
| 38 | + } else { |
| 39 | + // Optional: handle case where no discovery status found |
| 40 | + var noStatusObj = { |
| 41 | + ip_address: ip, |
| 42 | + discovery_status_number: 'No discovery record found' |
| 43 | + }; |
| 44 | + ipDiscoveryMapping.push(noStatusObj); |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Optional: handle case where no CI record found |
| 48 | + var noCIObj = { |
| 49 | + ip_address: ip, |
| 50 | + discovery_status_number: 'No CI found for this IP' |
| 51 | + }; |
| 52 | + ipDiscoveryMapping.push(noCIObj); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Log the final array of IP–Discovery Status mappings |
| 57 | +gs.info('IP to Discovery Status mapping: ' + JSON.stringify(ipDiscoveryMapping)); |
0 commit comments