Hi Thwack community, I'm attempting to use a SWQL query within JavaScript to perform some rather specific data manipulations and comparisons as a QOL for our monitoring team, in this case identifying the mismatch in a checksum for readability.
Right now, the HTML code will produce a result when simulating the email action while the message is set to 1. Plain Text (This is backwards)??? 2. is the first simulation of the email alert since opening and editing the specific alert (closing a opening the specific alert means I can run another simulation successful). See attached photos.
(Settings of Email Alert Body) - https://us.v-cdn.net/6038570/uploads/communityserver-discussions-components-files/47/Screenshot-2024_2D00_08_2D00_26-165330.png
(Results from first simulation as expected) - https://us.v-cdn.net/6038570/uploads/communityserver-discussions-components-files/47/Screenshot-2024_2D00_08_2D00_26-165754.png
(Results from second simulation, message is blank) - https://us.v-cdn.net/6038570/uploads/communityserver-discussions-components-files/47/Screenshot-2024_2D00_08_2D00_26-170211.png
Additionally, executing this results in an email of plain text to be sent to me (as expected). And executing as HTML results in a blank bodied email.
Any insight as to how to properly achieve what I am trying to do would be helpful, would love to stick to JavaScript for this as it has the variable control that makes what I'm doing (a specific data cleaning and manipulation) possible.
I've attached the code to achieve this below as well, all it is missing is an SWQL query that returns 6 variables in one row. (Not looking for code critiquing, just for how to make this functional and work out what is happening in the alerts within SolarWinds)
// SWQL Query that returns an array of 6 entries ensure the 3rd and 6th entry are the same length.
const DataQuery="SWQL query that returns 6 entries where entry 3 and 6 are equal length, actual data is irrelevant";
// Convert the above query into a single line and add any parameters (required by the SolarWinds Information Service, even if empty)
const DataToSwis = JSON.stringify({
query: DataQuery,
parameters: {}
});
// AJAX POST call to SW DB
$.ajax({
type: 'POST',
url: '/Orion/Services/Information.asmx/QueryWithParameters',
data: DataToSwis,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// Empty array to populate with arrays of entriesPerItem length
const items = [];
// How any entries per item are expected to be broken into each row
const entriesPerItem = 3;
// Break 1D array into 2D array of response.d.Rows[0].length / entriesPerItem rows and entriesPerItem columns
let iterator = response.d.Rows[0].length / entriesPerItem;
for (let i = 0; i < iterator; i ++) {
items.push(response.d.Rows[0].splice(0, entriesPerItem));
}
// Break the string into an array of chars
let splitStrings = [];
for (let i = 0; i < items.length; i++) {
splitStrings.push(items[i][2]);
}
// Empty array to store the XOR results
let xorMatch = [];
// Ensure data is even so XOR can be performed on pairs of data
if(!(splitStrings.length % 2)) {
for (let i = 0; i < splitStrings.length / 2; i += 2) {
let split1 = splitStrings[i];
let split2 = splitStrings[i+1];
for (let j = 0; j < split1.length; j++) {
if (split1[j] == split2[j]) {
xorMatch.push('0');
} else {
xorMatch.push(split1[j]);
}
}
}
}
// Reconstruct the char array into a string
let xorString = xorMatch.join('');
// Add the resulting XOR match to the items 2D array
items.push(["Mismatched entries in checksum:", "", xorString]);
// Populate HTML table with data from items.
let table = document.getElementById("table");
for (let i = 0; i < items.length; i++ ) {
let row = table.insertRow(i);
for (let j = 0; j < items[i].length; j++) {
let cell = row.insertCell(j);
cell.innerHTML = items[i][j];
if (j == 2) {
document.getElementById("table").rows[i].cells[j].style.fontFamily = "Courier New";
}
}
}
}
});