I've been using the
/api/table.json?content=devices&output=json&columns=objid,probe...
call to get a device with all containing sensors data on it. It works very well, but I now need to get the data for all devices (and their sensors) in a specific group. when I use the "content=groups" I only get the information about the group, not the devices data or it's sensor data. How do I achieve this? I only want to use a group id and get all containing data.
Thanks in advance! Martin Johansson
Article Comments
Hi Felix! Thanks for the response!
I've tried using the groupID instead of deviceID in a "Content=devices"-call, but it seems that I only get a list of the devices, but no information about the individual sensors that's on those devices. I've looked at the sensortree-call, and that seems to be more what I need, since the actual data that I present is the sensor-details, but I needed sensors from all devices in a group.
I might be able to use the call you recommended to get all devices in a group and from there get the devices with sensors from the device-id's that I get from the first call. But this seems to be inefficient since I then have to make atleast 2 calls (depending on how many devices the group has).
Any recommendations?
Thanks again! Martin Johansson
May, 2015 - Permalink
Hi Martin,
Please try to replace the devices with sensors and add device to the columns to see to which device the sensor belongs
/api/table.json?ID=50&content=sensors&output=json&columns=objid,device,name
Is this what you are looking for?
May, 2015 - Permalink
Hello again! Before I saw you response I had gotten "SensorTree" to work perfectly. It gave me full information about everything I need, Group, Devices and Sensors.
I'm guessing that your last answer would give me approximately the same things, but that I only could get the device-name from the sensor, and group-name from that device etc.
With SensorTree I can go the other way around with Group as base and access the devices and sensors through the group. It's a more natural class structure for what I'm trying to do IMO.
I used the query that's used in the API-demo:
/api/table.xml?content=sensortree&output=xml&&id=$GroupId$
You must of course add your authorization after this.
I'm doing this in C# and the class structure that I created to deserialize the response too looks like this (It's a rather large response with multiple XmlRoots):
using System.Collections.Generic; using System.Xml.Serialization; namespace YourOwnNamespace { [XmlRoot(ElementName = "usertimezone")] public class Usertimezone { [XmlElement(ElementName = "bias")] public string Bias { get; set; } [XmlElement(ElementName = "standardbias")] public string StandardBias { get; set; } [XmlElement(ElementName = "daylightbias")] public string DaylightBias { get; set; } [XmlElement(ElementName = "standarddatemonth")] public string StandardDateMonth { get; set; } [XmlElement(ElementName = "standarddatedayofweek")] public string StandardDateDayOfWeek { get; set; } [XmlElement(ElementName = "standarddateday")] public string StandardDateDay { get; set; } [XmlElement(ElementName = "standarddatehour")] public string StandardDateHour { get; set; } [XmlElement(ElementName = "standarddateminute")] public string StandardDateMinute { get; set; } [XmlElement(ElementName = "daylightdatemonth")] public string DaylightDateMonth { get; set; } [XmlElement(ElementName = "daylightdatedayofweek")] public string DaylightDateDayOfWeek { get; set; } [XmlElement(ElementName = "daylightdateday")] public string DaylightDateDay { get; set; } [XmlElement(ElementName = "daylightdatehour")] public string DaylightDateHour { get; set; } [XmlElement(ElementName = "daylightdateminute")] public string DaylightDateMinute { get; set; } } [XmlRoot(ElementName = "userdatesettings")] public class Userdatesettings { [XmlElement(ElementName = "shorttimeformat")] public string ShortTimeformat { get; set; } [XmlElement(ElementName = "longtimeformat")] public string LongTimeformat { get; set; } [XmlElement(ElementName = "shortdateformat")] public string ShortDateformat { get; set; } [XmlElement(ElementName = "longdateformat")] public string LongDateformat { get; set; } [XmlElement(ElementName = "dateseparator")] public string DateSeparator { get; set; } [XmlElement(ElementName = "timeseparator")] public string TimeSeparator { get; set; } } [XmlRoot(ElementName = "graphs")] public class Graphs { [XmlElement(ElementName = "live")] public string Live { get; set; } [XmlElement(ElementName = "graph1")] public string Graph1 { get; set; } [XmlElement(ElementName = "graph2")] public string Graph2 { get; set; } [XmlElement(ElementName = "graph3")] public string Graph3 { get; set; } } [XmlRoot(ElementName = "tasks")] public class Tasks { [XmlElement(ElementName = "autodisco")] public string Autodisco { get; set; } [XmlElement(ElementName = "histcalc")] public string Histcalc { get; set; } [XmlElement(ElementName = "correlationstatus")] public string CorrelationStatus { get; set; } [XmlElement(ElementName = "correlationrunning")] public string CorrelationRunning { get; set; } [XmlElement(ElementName = "autoupdate")] public string AutoUpdate { get; set; } [XmlElement(ElementName = "maintenance")] public string Maintenance { get; set; } [XmlElement(ElementName = "trialdaysleft")] public string TrialDaysLeft { get; set; } [XmlElement(ElementName = "showchanneltab")] public string ShowChannelTab { get; set; } [XmlElement(ElementName = "showfollowlinks")] public string ShowFollowLinks { get; set; } [XmlElement(ElementName = "showfeedbacklinks")] public string ShowFeedbackLinks { get; set; } [XmlElement(ElementName = "probeerrors")] public string ProbeErrors { get; set; } [XmlElement(ElementName = "recommenderrunning")] public string RecommenderRunning { get; set; } } [XmlRoot(ElementName = "options")] public class PrtgOptions { [XmlElement(ElementName = "usertimezone")] public Usertimezone UserTimezone { get; set; } [XmlElement(ElementName = "userdatesettings")] public Userdatesettings UserdateSettings { get; set; } [XmlElement(ElementName = "graphs")] public Graphs Graphs { get; set; } [XmlElement(ElementName = "sensortypes")] public string SensorTypes { get; set; } [XmlElement(ElementName = "usedtags")] public string UsedTags { get; set; } [XmlElement(ElementName = "tasks")] public Tasks Tasks { get; set; } } [XmlRoot(ElementName = "sensor")] public class Sensor { [XmlElement(ElementName = "name")] public string Name { get; set; } [XmlElement(ElementName = "id")] public string Id { get; set; } [XmlAttribute(AttributeName = "id")] public string _Id { get; set; } [XmlElement(ElementName = "url")] public string Url { get; set; } [XmlElement(ElementName = "tags")] public string Tags { get; set; } [XmlElement(ElementName = "priority")] public string Priority { get; set; } [XmlElement(ElementName = "fixed")] public string Fixed { get; set; } [XmlElement(ElementName = "hascomment")] public string Hascomment { get; set; } [XmlElement(ElementName = "sensortype")] public string Sensortype { get; set; } [XmlElement(ElementName = "sensorkind")] public string Sensorkind { get; set; } [XmlElement(ElementName = "interval")] public string Interval { get; set; } [XmlElement(ElementName = "status_raw")] public string StatusRaw { get; set; } [XmlElement(ElementName = "status")] public string Status { get; set; } [XmlElement(ElementName = "datamode")] public string Datamode { get; set; } [XmlElement(ElementName = "lastvalue")] public string LastValue { get; set; } [XmlElement(ElementName = "lastvalue_raw")] public string LastValueRaw { get; set; } [XmlElement(ElementName = "statusmessage")] public string StatusMessage { get; set; } [XmlElement(ElementName = "statussince_raw_utc")] public string StatusSinceRawUtc { get; set; } [XmlElement(ElementName = "lasttime_raw_utc")] public string LastTimeRawUtc { get; set; } [XmlElement(ElementName = "lastok_raw_utc")] public string LastOkRawUtc { get; set; } [XmlElement(ElementName = "lasterror_raw_utc")] public string LastErrorRawUtc { get; set; } [XmlElement(ElementName = "lastup_raw_utc")] public string LastUpRawUtc { get; set; } [XmlElement(ElementName = "lastdown_raw_utc")] public string LastDownRawUtc { get; set; } [XmlElement(ElementName = "cumulateddowntime_raw")] public string CumulateDownimeRaw { get; set; } [XmlElement(ElementName = "cumulateduptime_raw")] public string CumulatedUptimeRaw { get; set; } [XmlElement(ElementName = "cumulatedsince_raw")] public string CumulatedSinceRaw { get; set; } [XmlElement(ElementName = "active")] public string Active { get; set; } } [XmlRoot(ElementName = "device")] public class Device { [XmlElement(ElementName = "summary")] public string Summary { get; set; } [XmlElement(ElementName = "name")] public string Name { get; set; } [XmlElement(ElementName = "deviceicon")] public string DeviceIcon { get; set; } [XmlElement(ElementName = "id")] public string Id { get; set; } [XmlAttribute(AttributeName = "id")] public string _Id { get; set; } [XmlElement(ElementName = "url")] public string Url { get; set; } [XmlElement(ElementName = "tags")] public string Tags { get; set; } [XmlElement(ElementName = "priority")] public string Priority { get; set; } [XmlElement(ElementName = "fixed")] public string Fixed { get; set; } [XmlElement(ElementName = "hascomment")] public string HasComment { get; set; } [XmlElement(ElementName = "host")] public string Host { get; set; } [XmlElement(ElementName = "status_raw")] public string StatusRaw { get; set; } [XmlElement(ElementName = "active")] public string Active { get; set; } [XmlElement(ElementName = "sensor")] public List<Sensor> Sensor { get; set; } } [XmlRoot(ElementName = "group")] public class Group { [XmlElement(ElementName = "name")] public string Name { get; set; } [XmlElement(ElementName = "id")] public string Id { get; set; } [XmlAttribute(AttributeName = "id")] public string _Id { get; set; } [XmlElement(ElementName = "url")] public string Url { get; set; } [XmlElement(ElementName = "tags")] public string Tags { get; set; } [XmlElement(ElementName = "priority")] public string Priority { get; set; } [XmlElement(ElementName = "fixed")] public string Fixed { get; set; } [XmlElement(ElementName = "hascomment")] public string HasComment { get; set; } [XmlElement(ElementName = "status_raw")] public string StatusRaw { get; set; } [XmlElement(ElementName = "active")] public string Active { get; set; } [XmlElement(ElementName = "device")] public List<Device> Device { get; set; } } [XmlRoot(ElementName = "nodes")] public class Nodes { [XmlElement(ElementName = "group")] public Group Group { get; set; } } [XmlRoot(ElementName = "sensortree")] public class Sensortree { [XmlElement(ElementName = "nodes")] public Nodes Nodes { get; set; } } [XmlRoot(ElementName = "prtg")] public class BasePrtgSensorTree { [XmlElement(ElementName = "prtg-version")] public string Prtgversion { get; set; } [XmlElement(ElementName = "options")] public PrtgOptions Options { get; set; } [XmlElement(ElementName = "sensortree")] public Sensortree Sensortree { get; set; } } }
You should be able to just copy and paste this code into a .cs file. The base-class you deserialize against is BasePrtgSensorTree.
Here's some examples on how to handle this structure (base-class is here called base:
Get Group:
var group = base.SensorTree.Nodes.Group;
Loop through each Device in Group:
foreach(var device in base.SensorTree.Nodes.Group.Device){ //Perform actions here}
Inside this foreach loop you can loop through each Sensor that's in the current Device.
I hope this will help someone that's trying to do the same thing.
Thanks again!
Martin Johansson
May, 2015 - Permalink
Hi, I won't argue with you on that one! :)
Could you add the call to the post above so that other community members can use it in the future? Thanks!
Edit: Thank you very much for sharing your script with us!
May, 2015 - Permalink
Hi Martin,
You can attach ID=ObjectId to filter the call. The ID of the group is listed on the Overview Tab of the group. You call can look like this:
Best regards
May, 2015 - Permalink