Background Script to Get All Failed Pattern Error Messages from a Discovery Status Summary<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } span { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } h2 { font-size: 24pt; font-family: Lato; color: var(--now-color--text-primary, black); } h3 { font-size: 18pt; font-family: Lato; color: var(--now-color--text-primary, black); } h4 { font-size: 14pt; font-family: Lato; color: var(--now-color--text-primary, black); } a { font-size: 12pt; font-family: Lato; color: var(--now-color--link-primary, #00718F); } a:hover { font-size: 12pt; color: var(--now-color--link-primary, #024F69); } a:target { font-size: 12pt; color: var(--now-color--link-primary, #032D42); } a:visited { font-size: 12pt; color: var(--now-color--link-primary, #00718f); } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } This Background Script can be used to get all error messages for all patterns that failed in a Discovery Status.For each failed pattern a syslog record will be created and the error message will be set to the 'message' field on the syslog record.The Script will generate a URL to the syslog table where all the pattern error messages can be viewed at once.In addition to this, the Script will also print the pattern error messages to the background scripts output.This is useful for troubleshooting specific or larger Discovery Schedules where patterns are failing. Release<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } span { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } h2 { font-size: 24pt; font-family: Lato; color: var(--now-color--text-primary, black); } h3 { font-size: 18pt; font-family: Lato; color: var(--now-color--text-primary, black); } h4 { font-size: 14pt; font-family: Lato; color: var(--now-color--text-primary, black); } a { font-size: 12pt; font-family: Lato; color: var(--now-color--link-primary, #00718F); } a:hover { font-size: 12pt; color: var(--now-color--link-primary, #024F69); } a:target { font-size: 12pt; color: var(--now-color--link-primary, #032D42); } a:visited { font-size: 12pt; color: var(--now-color--link-primary, #00718f); } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } All Instructions<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } span { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } h2 { font-size: 24pt; font-family: Lato; color: var(--now-color--text-primary, black); } h3 { font-size: 18pt; font-family: Lato; color: var(--now-color--text-primary, black); } h4 { font-size: 14pt; font-family: Lato; color: var(--now-color--text-primary, black); } a { font-size: 12pt; font-family: Lato; color: var(--now-color--link-primary, #00718F); } a:hover { font-size: 12pt; color: var(--now-color--link-primary, #024F69); } a:target { font-size: 12pt; color: var(--now-color--link-primary, #032D42); } a:visited { font-size: 12pt; color: var(--now-color--link-primary, #00718f); } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } ///////////////////////////////////////// // Instructions //////////////////////////////////////// /* This background script will get the error messages for all failed Patterns from a single Discovery Status, or the error messages for specific Pattern type from a single Discovery Status. A syslog record for each error message will be created and a url linking to them will be returned in the scripts output. * Note, this script requires that the ECC Queue records for the Discovery Status being searched have not rotated out yet. ** To get all failed Pattern error messages from a single Discovery Status Enter only Parameter A discoveryStatusSysId into the function inputs. returnPatternLogErrors(discoveryStatusSysId); ** To get all Errors from a specific Pattern from a single Discovery Status Enter both Parameter A discoveryStatusSysId and Parameter B pattern into the function inputs. returnPatternLogErrors(discoveryStatusSysId,pattern); */ // 1. Set the sys_id of the Discovery Status to the value of the discoveryStatusSysId variable var discoveryStatusSysId = '0668615f47173550d2e9ce44846d4345'; // 2. (Optional) Set they sys_id of the pattern from the sa_pattern table to the pattern variable var pattern = '96f0186ea2684e35a14a7ca4aa97f163'; // 3. Set the input parameters in the returnPatternLogErrors() function returnPatternLogErrors(discoveryStatusSysId, pattern); // 4. Click the 'Run script' button in scope 'global' ///////////////////////////////////////////////////////////////////////////////////////////////// function returnPatternLogErrors(discoveryStatusSysID, pattern) { // Get GDT StartTime to create link to syslog var gdt = new GlideDateTime(); currentTime = gdt.getDisplayValue(); timeSplit = currentTime.split(" "); startDate = timeSplit[0]; startTime = timeSplit[1]; // Add 5 Minutes to the GDT var gt = new GlideDateTime(currentTime); gt.addSeconds(300); endSplit = gt.toString().split(" "); endDate = endSplit[0]; endTime = endSplit[1]; var errors = []; // Check that a valid Sys ID was entered before continuing if (!isRecordValid(discoveryStatusSysID, 'discovery_status')) { gs.info("Discovery Status Sys ID " + discoveryStatusSysID + " is not a Valid Record"); } else if (pattern) { var source = 'SNCDEBUG_' + getDiscoveryStatusNumber(discoveryStatusSysID) + "_" + Math.floor((Math.random() * 90000) + 1).toFixed(0); // Create a link to syslog table to view the logged error messages gs.print("https://" + gs.getProperty('instance_name') + ".service-now.com/syslog_list.do?sysparm_query=sys_created_onBETWEENjavascript:gs.dateGenerate('" + startDate + "','" + startTime + "')@javascript:gs.dateGenerate('" + endDate + "','" + endTime + "')%5Esource%3D" + source + "&sysparm_view="); errors = getErrorMessages(discoveryStatusSysID, pattern); } else { var source = 'SNCDEBUG_' + getDiscoveryStatusNumber(discoveryStatusSysID) + "_" + Math.floor((Math.random() * 90000) + 1).toFixed(0); // Create a link to syslog table to view the logged error messages gs.print("https://" + gs.getProperty('instance_name') + ".service-now.com/syslog_list.do?sysparm_query=sys_created_onBETWEENjavascript:gs.dateGenerate('" + startDate + "','" + startTime + "')@javascript:gs.dateGenerate('" + endDate + "','" + endTime + "')%5Esource%3D" + source + "&sysparm_view="); errors = getErrorMessages(discoveryStatusSysID); } if (!errors) { gs.info('Errors array is empty. Check if the related ECC Queue records are still available.'); } else { for (i = 0; i < errors.length; i++) { gs.log(errors[i], source); } } } // Uses Discovery Status and ECC Queue Output sys_ids to create query to be used on the sa_discovery_log_history table // Returns an array of Errors when called by main functions function getErrorMessages(discoStat, pattern) { var array = []; var createdOn = getCreatedOn(discoStat); var outputs = getECCOutputs(discoStat, createdOn); for (i = 0; i < outputs.length; i++) { if (pattern) { var encQ = "pattern_name=" + pattern + "^ecc_output_id=" + outputs[i] + "^sys_created_onON" + createdOn + "@javascript:gs.dateGenerate('" + createdOn + "','start')@javascript:gs.dateGenerate('" + createdOn + "','end')"; } else { var encQ = "ecc_output_id=" + outputs[i] + "^sys_created_onON" + createdOn + "@javascript:gs.dateGenerate('" + createdOn + "','start')@javascript:gs.dateGenerate('" + createdOn + "','end')"; } var gr = new GlideRecord('sa_discovery_log_history'); gr.addEncodedQuery(encQ); gr.query(); while (gr.next()) { var logMsg = gr.log; var regex = /\"FAILURE\".*\n.*\n.*\n.*/gm; var name = gr.pattern_name.getDisplayValue(); var matchedString = logMsg.match(regex); var lastIndex = matchedString.length - 1; array.push("<<<<<[" + gr.managed_ip + "] " + name + " - ECC Queue Output ID = " + gr.ecc_output_id + " >>>>> " + matchedString[lastIndex]); } } return array; } // Used by main functions function getECCOutputs(discoStatusID, created) { var eccOutputs = []; var gr = new GlideRecord('discovery_log'); var encQuery = "status=" + discoStatusID + "^level=2^sys_created_onON" + created + "@javascript:gs.dateGenerate('" + created + "','start')@javascript:gs.dateGenerate('" + created + "','end')" gr.addEncodedQuery(encQuery); gr.query(); while (gr.next()) { eccOutputs.push(gr.sensor.response_to); } return eccOutputs; } // Called by function returnPatternLogErrors(); function getCreatedOn(discoStatus) { var created = ''; var gr = new GlideRecord('discovery_status'); gr.addQuery('sys_id', discoStatus); gr.query(); while (gr.next()) { created = gr.sys_created_on; } created = created.substring(0, 10); return created; } // Verifies the Discovery Status record is a valid record function isRecordValid(sysID, table) { var gr = new GlideRecord(table); gr.get(sysID); return gr.isValidRecord(); } // Uses Discovery Status sys_id to return the Discovery Status Number function getDiscoveryStatusNumber(sysID) { var gr = new GlideRecord('discovery_status'); gr.get(sysID); return gr.number; } Related Links<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } span { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } h2 { font-size: 24pt; font-family: Lato; color: var(--now-color--text-primary, black); } h3 { font-size: 18pt; font-family: Lato; color: var(--now-color--text-primary, black); } h4 { font-size: 14pt; font-family: Lato; color: var(--now-color--text-primary, black); } a { font-size: 12pt; font-family: Lato; color: var(--now-color--link-primary, #00718F); } a:hover { font-size: 12pt; color: var(--now-color--link-primary, #024F69); } a:target { font-size: 12pt; color: var(--now-color--link-primary, #032D42); } a:visited { font-size: 12pt; color: var(--now-color--link-primary, #00718f); } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } Additional Background Scripts to Search Pattern Logs: KB1585964 - Background Script to Search Pattern Log Messages from the Discovery Pattern Log [sa_discovery_log] Table Using Regular Expressions KB1585475 - Background Script to Search Failed Pattern Error Messages from the Discovery Pattern Log [sa_discovery_log] Table