How to Manage Monitis Monitors with VBScript
Posted by Mikayel Vardanyan | Posted in Management Scripts, Monitis API | Posted on 13-07-2011
Tags: VBScript
Did you know that, using VBScript and powerful Monitis API, you can not only create monitors and feed them with data, but you can also manage your monitors and retrieve information? This group of scripts allows you to perform some tasks without using the Web Console.
All scripts share a common configuration file where your API Key and Secret Key are stored. You can store your keys with the command:
cscript KeyManager.vbs /cmd:set /APIKey:3TE4KFEAJICQDKRANI3IPTBLQK /SecretKey:5CK8P2176A21Q9ARDDRGEWSAK
You can check that your key is correctly stored with the command:
cscript KeyManager.vbs /cmd:get
Suspend external monitors
Sometimes your monitored websites may undergo maintenance, and during that period of time, you may prefer to go without receiving alerts. You can quickly suspend all or some of your monitors using a script, without accessing the web console. You first have to gather the list of monitors this can be done for all external monitors:
Function GetAllExtMon
url = "http://www.monitis.com/api?action=tests&apikey=" + apiKey + "&output=xml"
objHTTP.open "GET", url, False
objHTTP.send
Set GetAllExtMon = CreateObject("Microsoft.XMLDOM")
GetAllExtMon.async = False
GetAllExtMon.LoadXML(objHTTP.responseText)
End Function
Or you can do it by filtering them by tag. See below:
Function GetExtMonByTag(tag) url = "http://www.monitis.com/api?action=tagtests&apikey=" + apiKey + "&output=xml&tag=" + tag objHTTP.open "GET", url, False objHTTP.send
Set GetExtMonByTag = CreateObject("Microsoft.XMLDOM")
GetExtMonByTag.async = False
GetExtMonByTag.LoadXML(objHTTP.responseText)
End Function
One function or the other is called based on the parameters supplied by the user. See below:
if tag > "" then Set oResp = GetExtMonByTag(tag) else Set oResp = GetAllExtMon() end if
Once you have the list of monitors in an XML object, check to see if any are already suspended, if not, its ID will be added to a list:
for each oNode in oResp.documentElement.childnodes
if oNode.Attributes.getNamedItem("isSuspended").text = "0" then
IDs = AddList(IDs, oNode.Attributes.getNamedItem("id").text)
end if
next
if IDs > "" then SuspendExt IDs
Function AddList(list, add) if list > "" then AddList = list + "," + add else AddList = add end if End Function
Once we have the list of the monitors to suspend another API call is made. See instructions here:
Sub SuspendExt(IDs) url = "http://www.monitis.com/api" objHTTP.open "POST", url, False objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" postData = "apikey=" + apiKey + "&validation=token&authToken=" + token + "×tamp=" + FmtDate(dtGMT) + "&action=suspendExternalMonitor&monitorIds=" + IDs objHTTP.send postData resp = objHTTP.responseText End sub
Script with this command line:
cscript SuspendExt.vbs [/tag:<tag>]
Or, you can use this:
cscript SuspendExt.vbs /tag:"MyTag"
The /tag: parameter is optional. When not specified, all other monitors are suspended except those with the specified tag.
Here is what you will get:
Resume external monitors
In the same way, we can resume the monitors once maintenance is done. In this case, the list of monitors will be filled based on currently suspended monitors:
for each oNode in oResp.documentElement.childnodes
if oNode.Attributes.getNamedItem("isSuspended").text = "1" then
IDs = AddList(IDs, oNode.Attributes.getNamedItem("id").text)
end if
next
And an API call will be made with that list:
Sub ResumeExt(IDs) url = "http://www.monitis.com/api" objHTTP.open "POST", url, False objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" postData = "apikey=" + apiKey + "&validation=token&authToken=" + token + "×tamp=" + FmtDate(dtGMT) + "&action=activateExternalMonitor&monitorIds=" + IDs objHTTP.send postData resp = objHTTP.responseText End sub
You can script with this command line:
cscript ResumeExt.vbs [/tag:<tag>]
for example:
cscript ResumeExt.vbs /tag:"MyTag"
The /tag: parameter is optional. When not specified, all monitors are suspended except those with the specified tag.
Here is what you will get:
List Internal Monitoring Agents
You may have deployed several internal monitoring agents, each monitoring several parameters on different computers. At this point, you may need to take a quick view on what was deployed.
You can get the list of agents and load it into an XML object with this call:
url = "http://www.monitis.com/api?action=agents&apikey=" + apiKey + "&output=xml" objHTTP.open "GET", url, False objHTTP.send
Set oAgents = CreateObject("Microsoft.XMLDOM")
oAgents.async = False
oAgents.LoadXML(objHTTP.responseText)
Then you can cycle into that list and get further information about each agent. See below:
for each oAgent in oAgents.documentElement.childnodes
wscript.echo "Agent ID: ", oAgent.selectSingleNode("id").text,
oAgent.selectSingleNode("key").text
url = "http://www.monitis.com/api?action=agentInfo&apikey=" + apiKey + "&output=xml&agentId=" + oAgent.selectSingleNode("id").text + "&loadTests=true"
objHTTP.open "GET", url, False
objHTTP.send
Set oAgentInfo = CreateObject("Microsoft.XMLDOM")
oAgentInfo.async = False
oAgentInfo.LoadXML(objHTTP.responseText)
for each oInfo in oAgentInfo.documentElement.childnodes ListNames oInfo.childnodes next next
The ListNames sub displays the details of each monitor:
Sub ListNames(oNames)
dim oName, IP, name, str, pos, pos1, computer
for each oName in oNames
if oName.NodeName <> "#text" then
if oName.selectSingleNode("ip") is nothing then IP = "" else IP = " IP: " + oName.selectSingleNode("ip").text
str = oName.selectSingleNode("name").text
pos = InStr(str, "@") -1
name = mid(str, 1, pos)
pos1 = InStr(pos + 2, str, "@") -1
computer = mid(str, pos+ 2, pos1 - pos -1)
wscript.echo " " + name + " on " + computer + IP
end if
next
End Sub
You can script with this command line:
cscript ListInternals.vbs /apiKey:<your API key> /secretKey:<your secret key>
for example:
cscript ListInternals.vbs
Here is what you will get:
Dump External Monitors Results to a CSV file
You may want to export the results of External Monitors into a CSV file in order to analyze them in Excel or in another productivity application. The script DumpExtMonRes.vbs will help you with this task, following are the parameters to run it:
[/tag:<tag>] the tag of the monitors to dump results for. This parameter is optional, by default all monitors results are dumped [/name:<name>] the name of the monitor to dump results for. This parameter is optional, by default all monitors are dumped or all with the specified tag. /dtStart:<YYYYMMMDD> the starting date for the period to export [/dtEnd:<YYYYMMMDD>] the ending date for the period to export, by default ending date is today [/locations:<location [,<location>]>] a comma separated list of locations to export data for, by default all locations are exported [/outdir:<directryname>] the directory where result files will be stored, defaults to current folder
This is how the script works. First we will have to get the list of configured monitors, and this is done the same way using script for suspending/resuming monitors. Once we have the list of monitors, we further process it in order to filter by name, when specified:
for each oNode in oResp.documentElement.childnodes
monitorName = oNode.text
if name > "" then 'if a name is specified dumps only the corresponding monitor
if monitorName = name then
DumpMonRes oNode.Attributes.getNamedItem("id").text
end if
else
DumpMonRes oNode.Attributes.getNamedItem("id").text
end if
next
Another optional parameter is locations, if this is specified the listed location will be translated in the corresponding location IDs:
if locations > "" then
'Gets the current location list to decode locations to corresponding IDs
url = "http://www.monitis.com/api?action=locations&apikey=" + apiKey + "&output=xml"
wscript.echo "Requesting location IDs"
objHTTP.open "GET", url, False
objHTTP.send
Set oLocations = CreateObject("Microsoft.XMLDOM")
oLocations.async = False
oLocations.LoadXML(objHTTP.responseText)
'Populates the locations arrays aLocations = split(locations, ",") redim aLocIDs(UBound(aLocations)) for pos = 0 to UBound(aLocations) aLocIDs(pos) = GetLocID(aLocations(pos)) next end if
The export will also behave slightly different in locations that are specified or not:
Sub DumpMonRes(ID) dim oInfo, oNode, pos if locations = "" then 'if no locations list is specified gets the list from the monitor itself url = "http://www.monitis.com/api?action=testinfo&apikey=" + apiKey + "&testId=" + ID + "&output=xml" objHTTP.open "GET", url, False objHTTP.send
Set oInfo = CreateObject("Microsoft.XMLDOM")
oInfo.async = False
oInfo.LoadXML(objHTTP.responseText)
Set oInfo = oInfo.DocumentElement.SelectSingleNode("locations")
for each oNode in oInfo.childnodes
WriteMonRes ID, oNode.selectSingleNode("id").text, oNode.selectSingleNode("name").text
next
else 'if a locations list is specified gets results for those locations
for pos = 0 to UBound(aLocations)
WriteMonRes ID, aLocIDs(pos), aLocations(pos)
next
end if
End sub
Finally the results data will be retrieved for each date in the specified period and written to the export file:
Sub WriteMonRes(ID, location, locname) dim dt, oRes, filename, oCell, oNode, row, oFile, oLocRes
'Set the output file name
filename = outdir + ID & "-" & locname & "-" & year(dtStart) & right("0" & month(dtStart),2) & right("0" & day(dtStart),2) & "-" & year(dtStart) & right("0" & month(dtStart),2) & right("0" & day(dtStart),2) & ".csv"
Set oFile = oFso.CreateTextFile(filename, True)
dt = dtStart
do while dt <= dtEnd
wscript.echo "Dumping results of", monitorName, "(", ID, ") from", locname, "on", dt
'Requesting the monitor results url = "http://www.monitis.com/api?action=testresult&apikey=" + apiKey + "&testId=" + ID + "&day=" & day(dt) & "&month=" & month(dt) & "&year=" & year(dt) & "&locationIds=" & location & "&output=xml&timezone=" & timezone objHTTP.open "GET", url, False objHTTP.send
Set oRes = CreateObject("Microsoft.XMLDOM")
oRes.async = False
oRes.LoadXML(objHTTP.responseText)
if not oRes.firstchild.firstchild is nothing then
for each oNode in oRes.firstchild.firstchild.childnodes
if oNode.nodename = "row" then
row = ""
for each oCell in oNode.ChildNodes
if row = "" then
row = """" & year(dt) & "-" & right("0" & month(dt),2) & "-" & right("0" & day(dt),2) & " " & oCell.text & """"
else
row = row + ";""" + oCell.text + """"
end if
next
oFile.writeline row
end if
next
end if
dt = DateAdd("d", 1, dt)
loop
oFile.close
Set oFile = oFso.GetFile(filename)
if oFile.Size = 0 then oFso.DeleteFile(filename)
End Sub
Below is what you will wind up with:
Dump Internal Monitor Results to a CSV file
Just as with External Monitors, you may want to export Internal Monitor results to a CSV file in order to analyze them in Excel or in another productivity application. The script DumpIntMonRes.vbs will help you with this task. Following are the parameters to run it:
[/agent:<name>] export data only for the specified agent. This parameter is optional, by default all agent results are dumped /dtStart:<YYYYMMMDD> the starting date for the period to export [/dtEnd:<YYYYMMMDD>] the ending date for the period to export, by default ending date is today [/outdir:<directryname>] the directory where result files will be stored, defaults to current folder
This is how the script works. First you’ll have to get the list of configured agents:
url = "http://www.monitis.com/api?action=agents&apikey=" + apiKey + "&output=xml" objHTTP.open "GET", url, False wscript.echo "Requesting agents list" objHTTP.send
Set oAgents = CreateObject("Microsoft.XMLDOM")
oAgents.async = False
oAgents.LoadXML(objHTTP.responseText)
Then you can browse through the list and process the information for each agent. See below:
for each oAgent in oAgents.documentElement.childnodes
agentname = mid(oAgent.selectSingleNode("key").text, 1, instr(oAgent.selectSingleNode("key").text, "@")-1)
if agent = "" or (agent > "" and agentname = agent) then
'Get the list of monitors for the agent
url = "http://www.monitis.com/api?action=agentInfo&apikey=" + apiKey + "&output=xml&agentId=" + oAgent.selectSingleNode("id").text + "&loadTests=true"
objHTTP.open "GET", url, False
objHTTP.send
Set oAgentInfo = CreateObject("Microsoft.XMLDOM")
oAgentInfo.async = False
oAgentInfo.LoadXML(objHTTP.responseText)
for each oInfo in oAgentInfo.documentElement.childnodes ListNames oInfo.childnodes next end if next
Each agent may have one or more type of monitor configured, each with one or more instances:
Sub ListNames(oNames)
dim oName, name, inst
for each oName in oNames
if oName.NodeName <> "#text" then
inst = mid(oName.selectSingleNode("name").text, 1, Instr(oName.selectSingleNode("name").text, "@")-1)
WriteMonRes oName.nodename, oName.selectSingleNode("id").text, inst
end if
next
End Sub
For each instance you can extract the result for the specified period and write them to a CSV file:
Sub WriteMonRes(name, id, inst) dim dt, oRes, oNode, row, oCell, pos, hdr, oFile, filename
filename = outdir + agentname & "-" & inst & "-" & year(dtStart) & right("0" & month(dtStart),2) & right("0" & day(dtStart),2) & "-" & year(dtEnd) & right("0" & month(dtEnd),2) & right("0" & day(dtEnd),2) & ".csv"
Set oFile = oFso.CreateTextFile(filename, True)
dt = dtStart : pos = 0
do while dt <= dtEnd
wscript.echo "Dumping results for " + agentname + " " + inst, "(" + ID + ") on " & dt
url = "http://www.monitis.com/api?apikey=" & apikey & "&output=xml&action=" & name & "Result&monitorId=" + id + "&day=" & day(dt) & "&month=" & month(dt) & "&year=" & year(dt) & "&timezone=" & timezone
objHTTP.open "GET", url, False
objHTTP.send
Set oRes = CreateObject("Microsoft.XMLDOM")
oRes.async = False
oRes.LoadXML(objHTTP.responseText)
if not oRes.firstchild is nothing then
for each oNode in oRes.firstchild.childnodes
row = """" & year(dt) & "-" & right("0" & month(dt),2) & "-" & right("0" & day(dt),2) & " " & oNode.selectSingleNode("time").text & """"
for each oCell in oNode.ChildNodes
if oCell.nodename <> "time" then
row = row + ";""" + oCell.text + """"
end if
next
if pos = 0 then
hdr = """datetime"""
for each oCell in oNode.ChildNodes
if oCell.nodename <> "time" then
hdr = hdr + ";""" + oCell.nodename + """"
end if
next
oFile.writeline hdr
end if
oFile.writeline row
pos = pos + 1
next
end if
dt = DateAdd("d", 1, dt)
loop
oFile.close
Set oFile = oFso.GetFile(filename)
if oFile.Size = 0 then oFso.DeleteFile(filename)
End Sub
Here is what you will get:
Dump Custom Monitors results to a CSV file
You can do it for both External and Internal Monitors, and you can also dump results of Custom Monitors. You may want to export Custom Monitor results to a CSV file in order to analyze them in Excel or in another productivity application. The script DumpCustMonRes.vbs will help you with this task, following are the parameter to run it:
[/tag:<tag>] the tag of the monitors to dump results for. This parameter is optional, by default all monitors results are dumped [/name:<name>] the name of the monitor to dump results for. This parameter is optional, by default all monitors are dumped or all with the specified tag. /dtStart:<YYYYMMMDD> the starting date for the period to export [/dtEnd:<YYYYMMMDD>] the ending date for the period to export, by default ending date is today [/outdir:<directryname>] the directory where result files will be stored, defaults to current folder
This is how the script works. First you can get the list of configured custom monitors, and once we have the list of monitors, we further process it in order to filter by name, when specified:
url = "http://www.monitis.com/customMonitorApi?apikey=" + apiKey + "&action=getMonitors&&output=xml" if tag > "" then url = url + "&tag=" + tag objHTTP.open "GET", url, False wscript.echo "Requesting custom monitors list" objHTTP.send
Set oResp = CreateObject("Microsoft.XMLDOM")
oResp.async = False
oResp.LoadXML(objHTTP.responseText)
'Process each monitor
for each oNode in oResp.documentElement.childnodes
monitorName = oNode.SelectSingleNode("name").text
if name > "" then 'if a name is specified dumps only the corresponding monitor
if monitorName = name then
WriteMonRes oNode.SelectSingleNode("id").text
end if
else
WriteMonRes oNode.SelectSingleNode("id").text
end if
next
Since Custom Monitors have user-defined values, you need to first determine columns name:
url = "http://www.monitis.com/customMonitorApi?action=getMonitorInfo&apikey=" + apiKey + "&monitorId=" + ID + "&output=xml" objHTTP.open "GET", url, False objHTTP.send
Set oRes = CreateObject("Microsoft.XMLDOM")
oRes.async = False
oRes.LoadXML(objHTTP.responseText)
header = "checktime"
Set oRes = oRes.documentelement.selectsinglenode("resultParams")
for each oNode in oRes.childnodes
header = header + ";" + oNode.selectsingleNode("name").text
next
aCols = split(header, ";")
Then, simply request the result and write the CSV file:
dt = dtStart
do while dt <= dtEnd
wscript.echo "Dumping results of", monitorName, "(", ID, ") on", dt
'Requesting the monitor results
url = "http://www.monitis.com/customMonitorApi?action=getMonitorResults&apikey=" + apiKey + "&monitorId=" + ID + "&day=" & day(dt) & "&month=" & month(dt) & "&year=" & year(dt) & "&output=xml&timezone=" & timezone
objHTTP.open "GET", url, False
objHTTP.send
Set oRes = CreateObject("Microsoft.XMLDOM")
oRes.async = False
oRes.LoadXML(objHTTP.responseText)
if not oRes.firstchild is nothing then
for each oNode in oRes.firstchild.childnodes
if oNode.nodename = "result" then
row = """" & year(dt) & "-" & right("0" & month(dt),2) & "-" & right("0" & day(dt),2) + " " + right(oNode.SelectSingleNode("checkTime").text, 5) + """"
for col = 1 to UBound(aCols)
row = row + ";""" + oNode.SelectSingleNode(aCols(col)).text + """"
next
oFile.writeline row
end if
next
end if
dt = DateAdd("d", 1, dt)
loop
Here is what you will get:
Scripts source code
You can find full source code for the scripts in an article on GitHub.
Was that information helpful? We at Monitis would like to know if the series of posts we’ve been writing lately on how to save time and be more efficient on the job has helped. For example, we recently posted on how to add Monitis monitors in bulk by simply using MS Office Excel. Please let us know if these posts are benefiting you in the comment section of this post.
















