Background Script to Search Failed Pattern Error Messages from the Discovery Pattern Log [sa_discovery_log] Table Using an Encoded QuerySummary<!-- /*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 will get the error messages for any failed Patterns using an encoded query to target specific records from the either the Discovery Pattern Log [sa_discovery_log], the Discovery Pattern Log (History) [sa_discovery_log_history] table, 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. 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: ; } } ///////////////////////////////////////// // Description //////////////////////////////////////// /* This background script will get the error messages for any failed Patterns using an encoded query to target specific records from the either the Discovery Pattern Log [sa_discovery_log], the Discovery Pattern Log (History) [sa_discovery_log_history] table, 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. */ ///////////////////////////////////////// // Instructions //////////////////////////////////////// // 1. Go to table sa_discovery_log and filter the list for the pattern records you want to search from. // 2. Right-Click and copy the Encoded Query from the bread crumb filter on the list. // 3. Set the encQDiscoLog parm/variable with the Encoded Query. var encQDiscoLog = "status!=Success"; // 4. Changing the history variable to 'true' will search the [sa_discovery_log_history] table instead of the [sa_discovery_log] table. var history = false; // 5. Click the 'Run script' button in scope 'global' ///////////////////////////////////////////////////////////////////////////////////////////////// returnPatternLogErrorsEncQry(encQDiscoLog, history); function returnPatternLogErrorsEncQry(encQry, history) { // 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 = []; var source = "SNCDEBUG__" + Math.floor((Math.random() * 90000) + 1).toFixed(0); // Create a link to syslog table to view the logged error messages gs.print("\n\nURL Link to syslog records\nhttps://" + 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=\n"); errors = getErrorMessagesEncQry(encQry, history); 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); } } } function getErrorMessagesEncQry(encQ, history) { var array = []; if (history) { gs.log("Searching Pattern Log History [sa_discovery_log_history].") var gr = new GlideRecord('sa_discovery_log_history'); var table = 'sa_discovery_log_history'; } else { gs.log("Searching Pattern Log [sa_discovery_log].") var gr = new GlideRecord('sa_discovery_log'); var table = 'sa_discovery_log'; } 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); try { var lastIndex = matchedString.length - 1; } catch (error) {} try { array.push("\nPattern = " + name + "\nManaged IP = " + gr.managed_ip + "\nPID = " + gr.pid + "\n" + table + " sys_id = " + gr.sys_id + "\nError Message =\n" + matchedString[lastIndex] + "\n\n"); } catch (error) {} } return array; } 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 KB1585474 - Background Script to Get All Failed Pattern Error Messages from a Discovery Status