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.
Add comment