From 690403dc6c1e5480a0bdee779eb3fbc6f30e0d94 Mon Sep 17 00:00:00 2001 From: franks883 Date: Fri, 24 Oct 2025 21:10:45 +0100 Subject: [PATCH 1/2] Create README.md --- .../MID Server status JSON endpoint/README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Integration/Scripted REST Api/MID Server status JSON endpoint/README.md diff --git a/Integration/Scripted REST Api/MID Server status JSON endpoint/README.md b/Integration/Scripted REST Api/MID Server status JSON endpoint/README.md new file mode 100644 index 0000000000..e2d8d78170 --- /dev/null +++ b/Integration/Scripted REST Api/MID Server status JSON endpoint/README.md @@ -0,0 +1,26 @@ +# MID Server status JSON endpoint + +## What this solves +Operations teams often need a quick machine-readable view of MID Server health for dashboards and monitors. This Scripted REST API returns a compact JSON array of MID Servers with their status, last update time, and a simple "stale" flag if the record has not changed recently. + +## Where to use +Create a Scripted REST API with a single Resource and paste this script as the Resource Script. Call it from monitoring tools, dashboards, or widgets. + +## How it works +- Queries `ecc_agent` for active MID Servers +- Returns `name`, `status`, `sys_id`, `sys_updated_on`, and a computed `stale` boolean based on a configurable `minutes_stale` query parameter (default 15) +- Uses `gs.dateDiff` to compute minutes since last update + +## Configure +- Pass `minutes_stale` as a query parameter to override the default, for example `...?minutes_stale=30` +- Extend the payload as needed (for example add `version`, `ip_address`) if available in your instance + +## References +- Scripted REST APIs + https://www.servicenow.com/docs/bundle/zurich-application-development/page/build/applications/task/create-scripted-rest-api.html +- MID Server overview + https://www.servicenow.com/docs/bundle/zurich-servicenow-platform/page/product/mid-server/concept/c_MIDServer.html +- GlideRecord API + https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideRecord/concept/c_GlideRecordAPI.html +- GlideDateTime and dateDiff + https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideDateTime/concept/c_GlideDateTimeAPI.html From 0b29d7687da1d962ab4d2146515457fcf83b81dd Mon Sep 17 00:00:00 2001 From: franks883 Date: Fri, 24 Oct 2025 21:11:12 +0100 Subject: [PATCH 2/2] Create mid_server_status_api.js --- .../mid_server_status_api.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Integration/Scripted REST Api/MID Server status JSON endpoint/mid_server_status_api.js diff --git a/Integration/Scripted REST Api/MID Server status JSON endpoint/mid_server_status_api.js b/Integration/Scripted REST Api/MID Server status JSON endpoint/mid_server_status_api.js new file mode 100644 index 0000000000..46c91e1876 --- /dev/null +++ b/Integration/Scripted REST Api/MID Server status JSON endpoint/mid_server_status_api.js @@ -0,0 +1,44 @@ +// Scripted REST API Resource Script: MID Server status JSON endpoint +// Method: GET +// Path: /mid/status + +(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { + try { + // Configurable staleness threshold in minutes via query param + var q = request.queryParams || {}; + var minutesStale = parseInt((q.minutes_stale && q.minutes_stale[0]) || '15', 10); + if (!isFinite(minutesStale) || minutesStale <= 0) minutesStale = 15; + + var now = new GlideDateTime(); + + var out = []; + var gr = new GlideRecord('ecc_agent'); // MID Server table + gr.addActiveQuery(); + gr.orderBy('name'); + gr.query(); + + while (gr.next()) { + var updated = String(gr.getValue('sys_updated_on') || ''); + var minutesSince = 0; + if (updated) { + // gs.dateDiff returns seconds when third arg is true + minutesSince = Math.floor(gs.dateDiff(updated, now.getValue(), true) / 60); + } + + out.push({ + sys_id: gr.getUniqueValue(), + name: gr.getDisplayValue('name') || gr.getValue('name'), + status: gr.getDisplayValue('status') || gr.getValue('status'), // Up, Down, etc. + sys_updated_on: gr.getDisplayValue('sys_updated_on'), + minutes_since_update: minutesSince, + stale: minutesSince >= minutesStale + }); + } + + response.setStatus(200); + response.setBody(out); + } catch (e) { + response.setStatus(500); + response.setBody({ error: String(e) }); + } +})(request, response);