Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
Loading