From 702d7d7fa8dd98976445587925366736633170ce Mon Sep 17 00:00:00 2001 From: Vasantharajan N <51485948+nvprav@users.noreply.github.com> Date: Thu, 30 Oct 2025 21:57:53 +0530 Subject: [PATCH 1/4] Create getMidServerAgentLog.js Code to get Mid server log file --- .../getMidServerAgentLog.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Get Agent log from Mid Server/getMidServerAgentLog.js diff --git a/Server-Side Components/Background Scripts/Get Agent log from Mid Server/getMidServerAgentLog.js b/Server-Side Components/Background Scripts/Get Agent log from Mid Server/getMidServerAgentLog.js new file mode 100644 index 0000000000..b7883ad989 --- /dev/null +++ b/Server-Side Components/Background Scripts/Get Agent log from Mid Server/getMidServerAgentLog.js @@ -0,0 +1,55 @@ +/** + * Script to retrieve a specific log file (e.g., agent0.log.0) + * from a given ServiceNow MID Server. + * + * It creates an ECC Queue output record with the 'grabLog' source + * and 'SystemCommand' topic, which instructs the MID Server to + * return the content of the specified file. + * + * @param {string} midServerName - The name of the target MID Server (e.g., 'My_MID_Server_A'). + * @param {string} logFileName - The name of the log file to retrieve (e.g., 'agent0.log.0'). + * @returns {GlideRecord | null} The ECC Queue record inserted, or null if insertion failed. + */ +function getMidServerAgentLog(midServerName, logFileName) { + if (!midServerName || !logFileName) { + gs.error("MID Server name and log file name are required."); + return null; + } + + var gr = new GlideRecord("ecc_queue"); + gr.initialize(); + gr.source = "grabLog"; // Specific source for file retrieval + gr.topic = "SystemCommand"; // Required topic for this type of command + gr.name = logFileName; // The name of the file to grab (this becomes the command's parameter) + gr.queue = "output"; // Send from ServiceNow to the MID Server + gr.state = "ready"; // Set to ready to be processed by the MID Server + gr.agent = "mid.server." + midServerName; // Full agent string + // Set a high priority to ensure it's processed quickly (optional, but good practice) + gr.priority = 100; + + var sysId = gr.insert(); + + if (sysId) { + gs.info("Request to retrieve log '" + logFileName + "' on MID Server '" + midServerName + "' successfully placed."); + gs.info("ECC Queue Record: https://" + gs.getProperty("instance_name") + ".service-now.com/ecc_queue.do?sys_id=" + sysId); + return gr; // Returns the initialized GlideRecord object + } else { + gs.error("Failed to insert ECC Queue record."); + return null; + } +} + +// --- EXAMPLE USAGE --- + +// 1. Define your target MID Server name (the name *only*, not the 'mid.server.' prefix) +var targetMidServer = "MyMidServerName"; // <== **UPDATE THIS** to your actual MID Server name + +// 2. Define the log file you want to retrieve +var targetLogFile = "agent0.log.0"; + +// 3. Call the function +var eccRecord = getMidServerAgentLog(targetMidServer, targetLogFile); + +if (eccRecord) { + gs.print("Check the ECC Queue Input for a response with the log content."); +} From a81b5417ffa6ce7202e046a4e1953bc27a6af80a Mon Sep 17 00:00:00 2001 From: Vasantharajan N <51485948+nvprav@users.noreply.github.com> Date: Thu, 30 Oct 2025 22:02:03 +0530 Subject: [PATCH 2/4] Create README.md Readme docs about the script used to retrieve the agent log file on demand from the mid server --- .../Get Agent log from Mid Server/README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Get Agent log from Mid Server/README.md diff --git a/Server-Side Components/Background Scripts/Get Agent log from Mid Server/README.md b/Server-Side Components/Background Scripts/Get Agent log from Mid Server/README.md new file mode 100644 index 0000000000..c4068a893f --- /dev/null +++ b/Server-Side Components/Background Scripts/Get Agent log from Mid Server/README.md @@ -0,0 +1,28 @@ +# ServiceNow MID Server Log Retrieval + +This script is used to grab a agent log file from a specified **MID Server** on demand. + +## What Does It Do? + +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. + +--- + +## How to Use the Function + +The function is named `getMidServerAgentLog`. + +| Parameter | What it is | Example Value | +| :--- | :--- | :--- | +| **`midServerName`** | The name of your MID Server. | `"DevMidServer01"` | +| **`logFileName`** | The name of the file you want. | `"agent0.log.0"` | + +### Quick Example: + +```javascript +// Change "YOUR_MID_SERVER" to the actual name! +var mid = "YOUR_MID_SERVER"; +var log = "agent0.log.0"; + +getMidServerAgentLog(mid, log); +// This creates the request in the ECC Queue. From 1d1234817aad8a65c907e79b567713c686169b90 Mon Sep 17 00:00:00 2001 From: Vasantharajan N <51485948+nvprav@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:07:41 +0530 Subject: [PATCH 3/4] Create tableGrowthAnalysis.js Script to assess the given table growth in the instance --- .../tableGrowthAnalysis.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Table Growth Analysis/tableGrowthAnalysis.js diff --git a/Server-Side Components/Background Scripts/Table Growth Analysis/tableGrowthAnalysis.js b/Server-Side Components/Background Scripts/Table Growth Analysis/tableGrowthAnalysis.js new file mode 100644 index 0000000000..f313596910 --- /dev/null +++ b/Server-Side Components/Background Scripts/Table Growth Analysis/tableGrowthAnalysis.js @@ -0,0 +1,34 @@ +// === TABLE SIZE ANALYSIS SCRIPT === +// Log the start of the analysis +gs.info('=== TABLE SIZE ANALYSIS ==='); + +// Define the list of tables to analyze +var tablesToCheck = ['task', 'cmdb_ci', 'sc_cat_item']; + +// Loop through each table in the list +for (var i = 0; i < tablesToCheck.length; i++) { + var tableName = tablesToCheck[i]; // Get current table name + + // Create a GlideAggregate object to count total records in the table + var grCount = new GlideAggregate(tableName); + grCount.addAggregate('COUNT'); // Add COUNT aggregate + grCount.query(); // Execute the query + + // If the query returns a result + if (grCount.next()) { + var recordCount = grCount.getAggregate('COUNT'); // Get total record count + gs.info('Table: ' + tableName + ' | Record count: ' + recordCount); // Log the count + + // Optional: Analyze growth by checking records created in the last 30 days + var grRecent = new GlideAggregate(tableName); + grRecent.addAggregate('COUNT'); // Add COUNT aggregate + grRecent.addQuery('sys_created_on', '>=', gs.daysAgo(30)); // Filter records created in last 30 days + grRecent.query(); // Execute the query + + // If the query returns a result + if (grRecent.next()) { + var recentCount = grRecent.getAggregate('COUNT'); // Get count of recent records + gs.info(' - Records created last 30 days: ' + recentCount); // Log the recent count + } + } +} From 732efeb10a51496ac14dd2606a94f77e267effb0 Mon Sep 17 00:00:00 2001 From: Vasantharajan N <51485948+nvprav@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:10:05 +0530 Subject: [PATCH 4/4] Add README md file Instructions about the usage of the script provided for assessing the given table growth --- .../Table Growth Analysis/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Table Growth Analysis/README.md diff --git a/Server-Side Components/Background Scripts/Table Growth Analysis/README.md b/Server-Side Components/Background Scripts/Table Growth Analysis/README.md new file mode 100644 index 0000000000..c9dd0efdb0 --- /dev/null +++ b/Server-Side Components/Background Scripts/Table Growth Analysis/README.md @@ -0,0 +1,18 @@ +# Table Size Analysis Script + +This script checks the number of records in selected ServiceNow tables and shows how many were created in the last 30 days. + +## Tables Checked +- `task` +- `cmdb_ci` +- `sc_cat_item` + +## What It Does +- Logs the start of the analysis. +- Counts total records in each table. +- Counts records created in the last 30 days. +- Logs both counts to the system log. + +## How to Use +1. Add or remove table names in the `tablesToCheck` list. +2. Run the script in a background script or scheduled job.