|
| 1 | +/** |
| 2 | + * Script to retrieve a specific log file (e.g., agent0.log.0) |
| 3 | + * from a given ServiceNow MID Server. |
| 4 | + * |
| 5 | + * It creates an ECC Queue output record with the 'grabLog' source |
| 6 | + * and 'SystemCommand' topic, which instructs the MID Server to |
| 7 | + * return the content of the specified file. |
| 8 | + * |
| 9 | + * @param {string} midServerName - The name of the target MID Server (e.g., 'My_MID_Server_A'). |
| 10 | + * @param {string} logFileName - The name of the log file to retrieve (e.g., 'agent0.log.0'). |
| 11 | + * @returns {GlideRecord | null} The ECC Queue record inserted, or null if insertion failed. |
| 12 | + */ |
| 13 | +function getMidServerAgentLog(midServerName, logFileName) { |
| 14 | + if (!midServerName || !logFileName) { |
| 15 | + gs.error("MID Server name and log file name are required."); |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + var gr = new GlideRecord("ecc_queue"); |
| 20 | + gr.initialize(); |
| 21 | + gr.source = "grabLog"; // Specific source for file retrieval |
| 22 | + gr.topic = "SystemCommand"; // Required topic for this type of command |
| 23 | + gr.name = logFileName; // The name of the file to grab (this becomes the command's parameter) |
| 24 | + gr.queue = "output"; // Send from ServiceNow to the MID Server |
| 25 | + gr.state = "ready"; // Set to ready to be processed by the MID Server |
| 26 | + gr.agent = "mid.server." + midServerName; // Full agent string |
| 27 | + // Set a high priority to ensure it's processed quickly (optional, but good practice) |
| 28 | + gr.priority = 100; |
| 29 | + |
| 30 | + var sysId = gr.insert(); |
| 31 | + |
| 32 | + if (sysId) { |
| 33 | + gs.info("Request to retrieve log '" + logFileName + "' on MID Server '" + midServerName + "' successfully placed."); |
| 34 | + gs.info("ECC Queue Record: https://" + gs.getProperty("instance_name") + ".service-now.com/ecc_queue.do?sys_id=" + sysId); |
| 35 | + return gr; // Returns the initialized GlideRecord object |
| 36 | + } else { |
| 37 | + gs.error("Failed to insert ECC Queue record."); |
| 38 | + return null; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// --- EXAMPLE USAGE --- |
| 43 | + |
| 44 | +// 1. Define your target MID Server name (the name *only*, not the 'mid.server.' prefix) |
| 45 | +var targetMidServer = "MyMidServerName"; // <== **UPDATE THIS** to your actual MID Server name |
| 46 | + |
| 47 | +// 2. Define the log file you want to retrieve |
| 48 | +var targetLogFile = "agent0.log.0"; |
| 49 | + |
| 50 | +// 3. Call the function |
| 51 | +var eccRecord = getMidServerAgentLog(targetMidServer, targetLogFile); |
| 52 | + |
| 53 | +if (eccRecord) { |
| 54 | + gs.print("Check the ECC Queue Input for a response with the log content."); |
| 55 | +} |
0 commit comments