Not a question, just wanted to share how I'm getting the uptime/downtime or sensors for a certain time period in maps. For example, I was tasked with generating a dashboard that had certain sensor's uptime/downtime for the last month. By default the <#objectstatus name="uptime" id="<@objectid>"> provides the uptime for the life of the sensor, but I want to provide my own start and end date (like using a report) but in a map. Using the 'historicdata_totals.xml' api and writing a custom map object, this was possible...
<!--Custom Map Objects: Sensor Stats Up/Downtime Last Month--> <div class="map_object" id="<@itemid>" objectid="<@objectid>" subid="<@subid>" style="<#mapobject type="topleftcoordinates" subid="<@subid>" mode="<@editmode>">"> <#checkobjecttype objecttype="sensor" nicemessage="true" id="<@objectid>"> <#mapobject type="objectgrip" mode="<@editmode>"> <#mapobject type="htmlbefore" subid="<@subid>"> <font face="arial;" size="2"><b><#objectstatus name="name" id="<@objectid>"></b></font> <#objectstatus name="downsens" id="<@objectid>"> <font color="green"><b><div id="<@objectid>uptime"></div></font> <font color="red"><b><div id="<@objectid>downtime"></div></font> <#mapobject type="htmlafter" subid="<@subid>"> </div> <SCRIPT LANGUAGE="JavaScript"> // set start date to 1 month before current date var startDate = new Date(); startDate.setMonth(startDate.getMonth()-1); // date format required for historicdata_totals.xml = 2017-02-12 (yyyy-mm-dd) var d = new Date(startDate), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; startDate = [year, month, day].join('-'); // set end date to today var endDate = new Date(); // date format required for historicdata_totals.xml = 2017-02-12 (yyyy-mm-dd) var d = new Date(endDate), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; endDate = [year, month, day].join('-'); //console.log(startDate); //console.log(endDate); var xhr = new XMLHttpRequest(); // false makes the request synchronous, doesn't work async when used multiple times in a single map xhr.open('GET', 'https://YOURPRTGSERVER/api/historicdata_totals.xml?id=<@objectid>&sdate='+startDate+'-00-00-00&edate='+endDate+'-23-59-59', false); xhr.onload = function () { if (xhr.readyState === xhr.DONE) { if (xhr.status === 200) { console.log(xhr.responseText); parser = new DOMParser(); xmlDoc = parser.parseFromString(xhr.responseText,"text/xml"); var UptimePercent = xmlDoc.getElementsByTagName("uptimepercent")[0].childNodes[1].nodeValue; var Uptime = xmlDoc.getElementsByTagName("uptime")[0].childNodes[1].nodeValue; document.getElementById("<@objectid>uptime").innerHTML = "<b>Uptime:</b> " + UptimePercent; var DowntimePercent = xmlDoc.getElementsByTagName("downtimepercent")[0].childNodes[1].nodeValue; var Downtime = xmlDoc.getElementsByTagName("downtime")[0].childNodes[1].nodeValue; document.getElementById("<@objectid>downtime").innerHTML = "<b>Downtime:</b> " + DowntimePercent + " ["+Downtime+"]"; } } }; xhr.send(null); </SCRIPT>
This may not be the most elegant, or best way to accomplish this; and I'd be glad to hear what may be a better solution, but for the time being this is working great. Hopefully someone might find it handy.
Article Comments
@Steve: Thank you for sharing this, we appreciate it.
@Matthew: You basically take the code portion from Steve's post and save it as a file with file extension "*.htm" to PRTG's installation path, subfolder "webroot\mapobjects". You need to adjust potion https://YOURPRTGSERVER in the file accordingly with your PRTG server (and custom port if necessary).
You will have this object then at your disposal in PRTG's map editor in section "Custom Map Objects" to drag&drop into the map and then drop a sensor onto it from the left pane.
Kind regards,
Erhard
May, 2018 - Permalink
I'm no expert but it looks to me like the script auto-populates the start/end date and sensor id. I did add the address of my PRTG server into the script where it says 'https://YOURPRTGSERVER ' I managed to get the custom sensor to show in the properties section of the map designer however I cannot get it to show on the map.
May, 2018 - Permalink
Hi Matthew,
Ah, yes, you are right, actually adjusting the url for your PRTG server, I will correct that, that is the only thing you need to do. Drag&drop the object to the map and then you can drag&drop the sensor from the left pane onto it in order to display the data.
Kind regards,
Erhard
May, 2018 - Permalink
Hi Matthew,
Send me an email (support@paessler.com) with more details like screenshots of what you're doing and which browser you use.
Kind regards,
Erhard
May, 2018 - Permalink
How would I go about using this in my PRTG application?
May, 2018 - Permalink