Skip to content

Commit 2d485ed

Browse files
authored
Get Agent log file from mid server on demand using Background script (#2641)
* Create getMidServerAgentLog.js Code to get Mid server log file * Create README.md Readme docs about the script used to retrieve the agent log file on demand from the mid server
1 parent f8d0c75 commit 2d485ed

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ServiceNow MID Server Log Retrieval
2+
3+
This script is used to grab a agent log file from a specified **MID Server** on demand.
4+
5+
## What Does It Do?
6+
7+
It sends a command through the **ECC Queue** to the MID Server, asking it to locate a file (like `agent0.log.0`) and send its contents back to the ServiceNow instance.
8+
9+
---
10+
11+
## How to Use the Function
12+
13+
The function is named `getMidServerAgentLog`.
14+
15+
| Parameter | What it is | Example Value |
16+
| :--- | :--- | :--- |
17+
| **`midServerName`** | The name of your MID Server. | `"DevMidServer01"` |
18+
| **`logFileName`** | The name of the file you want. | `"agent0.log.0"` |
19+
20+
### Quick Example:
21+
22+
```javascript
23+
// Change "YOUR_MID_SERVER" to the actual name!
24+
var mid = "YOUR_MID_SERVER";
25+
var log = "agent0.log.0";
26+
27+
getMidServerAgentLog(mid, log);
28+
// This creates the request in the ECC Queue.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)