Pradyumna Das
Tera Expert
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 04-16-2023 10:29 PM
Hi Folks,
Please use the below script in scripted rest API if you wanted to automate CI Discovery Process.
Note: Create one query parameter named as IPAddress so that while calling the endpoint the requester has to pass the IP of the CI in the url parameter.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
function availableMidServer() {
var ecc = new GlideRecord('ecc_agent');
ecc.addQuery('status', 'up');
ecc.query();
while (ecc.next()) {
return ecc.name;
}
return 0;
}
function validIP(target) {
if (!target)
return false;
// Only matches IPV4 dot decimal notation 255.255.255.255
var regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return target.match(regex) != null;
}
var jsonObject = {
"Discovery": {}
};
var message = '';
var mid = '';
var result;
var dis = new Discovery();
var ip = request.queryParams.IPAddress.toString(); // Please create one query parameter named as IPAddress
var status = '';
jsonObject.Discovery['IP'] = ip;
if (!validIP(ip)) {
status = 'Fail';
message = 'IP is not Valid';
} else {
var ips = [];
ips.push(ip);
try {
var params = dis.getScheduleContainingAnyIP(ips);
if (params == null) {
message = 'No Discovery schedule configured for the IP Address provided.';
result = availableMidServer();
if (result == 0) {
status = 'Fail';
message += 'No MID Servers are running currently. Contact ServiceNow Administrator.';
} else {
mid = result;
}
} else {
var mid_type = params.schedule.midSelMethod;
if (mid_type == 'specific_mid') {
var ecc = new GlideRecord('ecc_agent');
if (ecc.get(params.schedule.midServerID)) {
if (ecc.status.toLowerCase() == 'up') {
mid = ecc.name;
} else {
message = 'The MID server assosiated with the schedule is not running. ';
result = availableMidServer();
if (result == 0) {
status = 'Fail';
message += 'No other MID Servers are running currently. Contact ServiceNow Administrator.';
} else {
mid = result;
}
}
}
} else if (mid_type == 'specific_cluster') {
var ecc_cluster = new GlideRecord('ecc_agent_cluster');
if (ecc_cluster.get(params.schedule.midClusterID)) {
var ecc_cluster_mid = new GlideRecord('ecc_agent_cluster_member_m2m');
ecc_cluster_mid.addQuery('cluster', ecc_cluster.sys_id);
ecc_cluster_mid.addQuery('agent.status', 'up');
ecc_cluster_mid.query();
if (ecc_cluster_mid.next()) {
mid = ecc_cluster_mid.agent.name;
} else {
message = 'None of the MID Servers assosiated with the scheduled cluster are running.';
result = availableMidServer();
if (result == 0) {
status = 'Fail';
message += 'No other MID Servers are running currently. Contact ServiceNow Administrator.';
} else {
mid = result;
}
}
}
} else {
result = availableMidServer();
if (result == 0) {
message += 'No MID Servers are running currently. Contact ServiceNow Administrator.';
} else {
mid = result;
}
}
}
if (mid != '') {
var statusID = dis.discoveryFromIP(ip, mid, 'Quick_Discovery');
status = 'Success';
jsonObject.Discovery["Mid Server"] = mid;
if (statusID != '') {
var dis_sta = new GlideRecord('discovery_status');
dis_sta.addQuery('sys_id', statusID.toString());
dis_sta.query();
if (dis_sta.next()) {
jsonObject.Discovery["StatusID"] = dis_sta.number;
}
}
message += ' Discovery has been triggred successfully.';
}
} catch (error) {
status = 'Fail';
message = error;
}
}
jsonObject.Discovery['Status'] = status;
jsonObject.Discovery['Message'] = message;
return jsonObject;
})(request, response);
Please mark it helpful if it helps you in any manner.
Thank You,
Pradyumna Das
- 920 Views