Skip to content

Commit 4ddef2b

Browse files
authored
Integration: Scripted REST API to expose MID Server status as JSON (#2467)
1 parent bc2d789 commit 4ddef2b

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# MID Server status JSON endpoint
2+
3+
## What this solves
4+
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.
5+
6+
## Where to use
7+
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.
8+
9+
## How it works
10+
- Queries `ecc_agent` for active MID Servers
11+
- Returns `name`, `status`, `sys_id`, `sys_updated_on`, and a computed `stale` boolean based on a configurable `minutes_stale` query parameter (default 15)
12+
- Uses `gs.dateDiff` to compute minutes since last update
13+
14+
## Configure
15+
- Pass `minutes_stale` as a query parameter to override the default, for example `...?minutes_stale=30`
16+
- Extend the payload as needed (for example add `version`, `ip_address`) if available in your instance
17+
18+
## References
19+
- Scripted REST APIs
20+
https://www.servicenow.com/docs/bundle/zurich-application-development/page/build/applications/task/create-scripted-rest-api.html
21+
- MID Server overview
22+
https://www.servicenow.com/docs/bundle/zurich-servicenow-platform/page/product/mid-server/concept/c_MIDServer.html
23+
- GlideRecord API
24+
https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideRecord/concept/c_GlideRecordAPI.html
25+
- GlideDateTime and dateDiff
26+
https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideDateTime/concept/c_GlideDateTimeAPI.html
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Scripted REST API Resource Script: MID Server status JSON endpoint
2+
// Method: GET
3+
// Path: /mid/status
4+
5+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
6+
try {
7+
// Configurable staleness threshold in minutes via query param
8+
var q = request.queryParams || {};
9+
var minutesStale = parseInt((q.minutes_stale && q.minutes_stale[0]) || '15', 10);
10+
if (!isFinite(minutesStale) || minutesStale <= 0) minutesStale = 15;
11+
12+
var now = new GlideDateTime();
13+
14+
var out = [];
15+
var gr = new GlideRecord('ecc_agent'); // MID Server table
16+
gr.addActiveQuery();
17+
gr.orderBy('name');
18+
gr.query();
19+
20+
while (gr.next()) {
21+
var updated = String(gr.getValue('sys_updated_on') || '');
22+
var minutesSince = 0;
23+
if (updated) {
24+
// gs.dateDiff returns seconds when third arg is true
25+
minutesSince = Math.floor(gs.dateDiff(updated, now.getValue(), true) / 60);
26+
}
27+
28+
out.push({
29+
sys_id: gr.getUniqueValue(),
30+
name: gr.getDisplayValue('name') || gr.getValue('name'),
31+
status: gr.getDisplayValue('status') || gr.getValue('status'), // Up, Down, etc.
32+
sys_updated_on: gr.getDisplayValue('sys_updated_on'),
33+
minutes_since_update: minutesSince,
34+
stale: minutesSince >= minutesStale
35+
});
36+
}
37+
38+
response.setStatus(200);
39+
response.setBody(out);
40+
} catch (e) {
41+
response.setStatus(500);
42+
response.setBody({ error: String(e) });
43+
}
44+
})(request, response);

0 commit comments

Comments
 (0)