- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
The night we stopped screenshotting SolarWinds into tickets
It started with a Slack complaint: an engineer was still logging into SolarWinds, screenshotting the alert dashboard, and pasting it into a RITM by hand - every single time.
The fix seemed simple: let ServiceNow pull the data and attach a chart automatically. The catch was a rule security asked for early on - SolarWinds data can never be written to a ServiceNow table. It can live in a variable for one transaction, but nothing persists except the finished report on the ticket.
Our first version leaned on an external service to draw the chart image, since ServiceNow's script engine has no native charting library. That quietly broke our own rule - it sent alert data outside the instance just to get a picture. The fix: draw the bars by hand in plain SVG. A bar chart is just rectangles sized to a number - no library needed, nothing leaves the walls except the original call to SolarWinds.
The script
var SolarWindsAlertReport = Class.create();
SolarWindsAlertReport.prototype = {
initialize: function() {},
// STEP 1: Ask SolarWinds API to count active alerts per severity.
// The result is just an array sitting in a variable â never a table.
getAlertCounts: function() {
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('GET');
request.setEndpoint('https://<solarwinds-host>:17778/SolarWinds/InformationService/v3/Json/Query');
request.setBasicAuth('${swis_user}', '${swis_password}'); // Connection & Credential alias
request.setQueryParameter('query',
"SELECT Severity, COUNT(AlertObjectID) AS AlertCount FROM Orion.AlertActive GROUP BY Severity");
var response = request.execute();
if (response.getStatusCode() != 200) {
gs.error('SolarWinds call failed: ' + response.getStatusCode());
return null;
}
return JSON.parse(response.getBody()).results;
},
// STEP 2: Draw the bar chart by hand â no library. For each severity, scale a bar's height to its count (bigger number, taller bar) andwrite out plain SVG: one rectangle plus two text labels.
// Note the xmlns attribute below â without it, some HTML-to-PDF engines mis-parse the inline SVG and can drop nearby text too.
buildBarChartSvg: function(counts) {
var barWidth = 100, gap = 40, maxHeight = 200;
var maxCount = Math.max.apply(null, counts.map(function(c) { return c.AlertCount; })) || 1;
var bars = '';
for (var i = 0; i < counts.length; i++) {
var x = i * (barWidth + gap) + 20;
var h = (counts[i].AlertCount / maxCount) * maxHeight;
var y = maxHeight - h + 30;
bars += '<rect x="' + x + '" y="' + y + '" width="' + barWidth + '" height="' + h + '" fill="#2a78d6"/>';
bars += '<text x="' + (x + barWidth / 2) + '" y="' + (y - 8) + '" text-anchor="middle" font-size="14" fill="#0b0b0b">' + counts[i].AlertCount + '</text>';
bars += '<text x="' + (x + barWidth / 2) + '" y="' + (maxHeight + 50) + '" text-anchor="middle" font-size="13" fill="#0b0b0b">' + counts[i].Severity + '</text>';
}
return '<svg xmlns="http://www.w3.org/2000/svg" width="' + (counts.length * (barWidth + gap) + 20) + '" height="280">' + bars + '</svg>';
},
// STEP 3: Wrap that chart in a minimal HTML page â just a heading, a timestamp, and the SVG. Styles are inline on purpose: PDF engines don't reliably apply an external or <style>-block stylesheet, so relying on defaults is how text quietly disappears.
buildHtml: function(ritmNumber, chartSvg) {
return '<!DOCTYPE html><html><body style="font-family:sans-serif;">' +
'<h2 style="color:#0b0b0b;">Server alert summary</h2>' +
'<p style="color:#0b0b0b;">Generated for ' + ritmNumber + ' on ' + new GlideDateTime().getDisplayValue() + '</p>' +
chartSvg + '</body></html>';
},
// STEP 4: Convert the HTML page to PDF bytes.
convertToPdf: function(html) {
var pdfApi = new global.PDFGenerationAPI();
return pdfApi.convertHtml2Pdf(html);
},
// STEP 5: The only write in the whole flow â attach the finished PDF to the RITM. Everything before this line was in-memory only.
attachToRITM: function(ritmSysId, fileName, pdfBytes) {
var ritmGR = new GlideRecord('sc_req_item');
if (!ritmGR.get(ritmSysId)) return null;
var sa = new GlideSysAttachment();
return sa.write(ritmGR, fileName, 'application/pdf', pdfBytes);
},
// Orchestrator: runs the five steps in order and returns true/false.
generateReport: function(ritmSysId, ritmNumber) {
var counts = this.getAlertCounts();
if (!counts || !counts.length) return false;
var html = this.buildHtml(ritmNumber, this.buildBarChartSvg(counts));
var pdfBytes = this.convertToPdf(html);
if (!pdfBytes) return false;
return !!this.attachToRITM(ritmSysId, 'Alert_Summary_' + ritmNumber + '.pdf', pdfBytes);
},
type: 'SolarWindsAlertReport'
};
A quick note, entire script is written as one Script Include on purpose, so you can see the whole flow in one place for reference. It's not the pattern to actually ship. In production, ServiceNow's recommended approach is:
- Connection & Credential alias instead of hardcoded credentials or inline basic auth
- REST Message record (with its own endpoint/method configured in the platform) instead of a hardcoded URL string
- System Properties for anything environment-specific (host, query defaults) instead of literals baked into the script
- Splitting responsibilities across smaller Script Includes if this grows past a demo
Below, ${swis_user} / ${swis_password} and the hardcoded endpoint are stand-ins for that â swap them for the credential alias and REST Message record before this goes anywhere near production.
Called from a Business Rule or Flow Action on the RITM:
new SolarWindsAlertReport().generateReport(current.sys_id.toString(), current.number.toString());
What lands on the ticket
A one-page PDF, generated in seconds, with active alerts by severity:
No custom table, no external service, no data left behind â just the report the ticket needed in the first place.
Note: the script above is a simple, single-purpose example â one query, one bar chart. It can be adapted to pull separate data sets (e.g. CPU, alerts, response time) and render each as its own graphical representation (bar, line, trend) within the same or separate PDF sections, by repeating steps 1 and 2 for each data set before assembling the final HTML in step 3.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.