Esempio n. 1
0
        //######################################
        // AddObjectWithExcessiveValue
        //######################################

        private void AddObjectWithExcessiveValue(List <KeyValuePair <Parameter, object> > lengthLimit, ICommandParameters internalParams)
        {
            var limitParam = lengthLimit.First();

            var limit = limitParam.Key.GetEnumAttribute <LengthLimitAttribute>().Length;

            if (limitParam.Value.IsIEnumerable())
            {
                var list = limitParam.Value.ToIEnumerable().ToList();

                var count = list.Count();

                if (count > limit)
                {
                    for (int i = 0; i < count; i += limit)
                    {
                        var thisRequest = list.Skip(i).Take(limit);

                        internalParams[limitParam.Key] = thisRequest;

                        RequestEngine.ExecuteRequest(internalParams);
                    }
                }
                else
                {
                    RequestEngine.ExecuteRequest(internalParams);
                }
            }
            else
            {
                throw new NotImplementedException($"Don't know how to handle {nameof(LengthLimitAttribute)} applied to value of type {limitParam.Value.GetType()}");
            }
        }
        private XElement GetNotificationActionProperties(Either <IPrtgObject, int> objectOrId, CancellationToken token)
        {
            var xml = RequestEngine.ExecuteRequest(new GetObjectPropertyParameters(objectOrId, ObjectType.Notification), HtmlParser.Default.GetXml, token);

            xml = ResponseParser.GroupNotificationActionProperties(xml);

            return(xml);
        }
Esempio n. 3
0
        private XElement GetNotificationActionProperties(int id, CancellationToken token)
        {
            var xml = RequestEngine.ExecuteRequest(new GetObjectPropertyParameters(id, ObjectType.Notification), ObjectSettings.GetXml, token);

            xml = ResponseParser.GroupNotificationActionProperties(xml);

            return(xml);
        }
Esempio n. 4
0
        protected virtual TEntity MakeRequest <TEntity>(string path, Method method, string rootElement = null, object payload = null, List <Parameter> parameters = null)
        {
            var request = CreateRestRequest(path, method, rootElement, payload, parameters);

            var client = RequestEngine.CreateClient(_AccessInfo);

            var response = RequestEngine.ExecuteRequest <TEntity>(client, request);

            return(response);
        }
Esempio n. 5
0
        private string GetPassHash(string password)
        {
            var response = RequestEngine.ExecuteRequest(new PassHashParameters(password), m => m.Content.ReadAsStringAsync().Result).StringValue;

            if (!Regex.Match(response, "^[0-9]+$").Success)
            {
                throw new PrtgRequestException($"Could not retrieve PassHash from PRTG Server. PRTG responded '{response}'");
            }

            return(response);
        }
Esempio n. 6
0
        private List <NotificationTrigger> GetNotificationTriggersInternal(int objectId, CancellationToken token)
        {
            var xmlResponse = RequestEngine.ExecuteRequest(new NotificationTriggerParameters(objectId), token: token);

            var parsed = ResponseParser.ParseNotificationTriggerResponse(objectId, xmlResponse);

            UpdateTriggerChannels(parsed, token);
            UpdateTriggerActions(parsed, token);

            return(parsed);
        }
Esempio n. 7
0
        private void RestartCoreInternal(bool waitForRestart, Func <RestartCoreStage, bool> progressCallback, CancellationToken token)
        {
            var restartTime = waitForRestart ? (DateTime?)GetStatus().DateTime : null;

            RequestEngine.ExecuteRequest(new CommandFunctionParameters(CommandFunction.RestartServer));

            if (waitForRestart)
            {
                WaitForCoreRestart(restartTime.Value, progressCallback, token);
            }
        }
Esempio n. 8
0
        private void RefreshSystemInfoInternal(int deviceId, SystemInfoType[] types, CancellationToken token)
        {
            if (types == null || types.Length == 0)
            {
                types = typeof(SystemInfoType).GetEnumValues().Cast <SystemInfoType>().ToArray();
            }

            foreach (var type in types)
            {
                RequestEngine.ExecuteRequest(new RefreshSystemInfoParameters(deviceId, type), token: token);
            }
        }
Esempio n. 9
0
        private string GetObjectPropertyRawInternal(int objectId, string property, bool text)
        {
            var parameters = new GetObjectPropertyRawParameters(objectId, property, text);

            var response = RequestEngine.ExecuteRequest(
                parameters,
                responseParser: m => ResponseParser.ParseGetObjectPropertyResponse(
                    m.Content.ReadAsStringAsync().Result,
                    property
                    )
                );

            return(ResponseParser.ValidateRawObjectProperty(response, parameters));
        }
Esempio n. 10
0
        //######################################
        // WaitForSensorTargetResolution
        //######################################

        private string WaitForSensorTargetResolution(int deviceId, int tmpId, Func <int, bool> progressCallback)
        {
            var parameters = new SensorTargetProgressParameters(deviceId, tmpId);

            SensorTargetProgress p;
            bool continueQuery = true;

            var stopwatch = new Stopwatch();
            var timeout   = TimeSpan.FromSeconds(60);

            stopwatch.Start();

            do
            {
                p = ObjectEngine.GetObject <SensorTargetProgress>(parameters);

                if (progressCallback != null)
                {
                    continueQuery = progressCallback(p.Percent);
                }

                if (p.Percent < 100)
                {
                    if (!continueQuery)
                    {
                        break;
                    }

                    if (stopwatch.Elapsed > timeout)
                    {
                        throw new TimeoutException("Failed to retrieve sensor information within a reasonable period of time. Check target device is accessible and that valid credentials have been supplied");
                    }

#if !DEBUG
                    Thread.Sleep(1000);
#endif
                }
            } while (p.Percent < 100);

            if (!continueQuery)
            {
                return(null);
            }

            ResponseParser.ValidateSensorTargetProgressResult(p);

            var page = RequestEngine.ExecuteRequest(new SensorTargetCompletedParameters(deviceId, tmpId));

            return(page);
        }
Esempio n. 11
0
        private void AddObjectInternal(int objectId, NewObjectParameters parameters)
        {
            var lengthLimit = RequestParser.ValidateObjectParameters(parameters);

            var internalParams = RequestParser.GetInternalNewObjectParameters(objectId, parameters);

            if (lengthLimit.Count > 0)
            {
                AddObjectWithExcessiveValue(lengthLimit, internalParams);
            }
            else
            {
                RequestEngine.ExecuteRequest(internalParams);
            }
        }
Esempio n. 12
0
        private string GetSensorTargetsResponse(int deviceId, SensorTargetParameters parameters, Func <int, bool> progressCallback)
        {
            Func <HttpResponseMessage, string> getSensorTargetTmpId = ResponseParser.GetSensorTargetTmpId;

            var tmpIdStr = RequestEngine.ExecuteRequest(parameters, getSensorTargetTmpId);

            int tmpId;

            if (!int.TryParse(tmpIdStr, out tmpId))
            {
                throw new PrtgRequestException($"Failed to resolve sensor targets for sensor type '{parameters[Parameter.SensorType]}': type was not valid");
            }

            var response = WaitForSensorTargetResolution(deviceId, tmpId, progressCallback);

            return(response);
        }
Esempio n. 13
0
        //######################################
        // GetChannelsInternal
        //######################################

        internal List <Channel> GetChannelsInternal(int sensorId, Func <string, bool> nameFilter = null, Func <int, bool> idFilter = null)
        {
            if (nameFilter == null)
            {
                nameFilter = n => true;
            }

            if (idFilter == null)
            {
                idFilter = i => true;
            }

            var response = RequestEngine.ExecuteRequest(new ChannelParameters(sensorId));

            response.Descendants("item").Where(item => item.Element("objid").Value == "-4").Remove();

            var items = response.Descendants("item").ToList();

            items.Where(e =>
                        !nameFilter(e.Element("name").Value?.ToString()) ||
                        !idFilter(Convert.ToInt32(e.Element("objid").Value.ToString()))
                        ).Remove();

            items = response.Descendants("item").ToList();

            foreach (var item in items)
            {
                var id = Convert.ToInt32(item.Element("objid").Value);

                var properties = GetChannelProperties(sensorId, id);

                item.Add(properties.Nodes());
                item.Add(new XElement("injected_sensorId", sensorId));
            }

            if (items.Count > 0)
            {
                return(XmlDeserializer <Channel> .DeserializeList(response).Items);
            }

            return(new List <Channel>());
        }
Esempio n. 14
0
        private async Task RestartProbeInternalAsync(int[] probeIds, bool waitForRestart, Func <ProbeRestartProgress[], bool> progressCallback, CancellationToken token)
        {
            var restartTime = waitForRestart ? (DateTime?)(await GetStatusAsync(token).ConfigureAwait(false)).DateTime : null;

            if (probeIds != null && probeIds.Length > 1)
            {
                var tasks = probeIds.Select(probeId => RequestEngine.ExecuteRequestAsync(new RestartProbeParameters(probeId), token: token));

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
            else
            {
                RequestEngine.ExecuteRequest(new RestartProbeParameters(probeIds?.Cast <int?>().FirstOrDefault()), token: token);
            }

            if (waitForRestart)
            {
                var probe = probeIds == null || probeIds.Length == 0 ? await GetProbesAsync(token).ConfigureAwait(false) : await GetProbesAsync(Property.Id, probeIds, token).ConfigureAwait(false);
                await WaitForProbeRestartAsync(restartTime.Value, probe, progressCallback, token).ConfigureAwait(false);
            }
        }
Esempio n. 15
0
        private void RestartProbeInternal(int[] probeIds, bool waitForRestart, Func <ProbeRestartProgress[], bool> progressCallback, CancellationToken token)
        {
            var restartTime = waitForRestart ? (DateTime?)GetStatus().DateTime : null;

            if (probeIds != null && probeIds.Length > 1)
            {
                foreach (var probeId in probeIds)
                {
                    RequestEngine.ExecuteRequest(new RestartProbeParameters(probeId), token: token);
                }
            }
            else
            {
                RequestEngine.ExecuteRequest(new RestartProbeParameters(probeIds?.Cast <int?>().FirstOrDefault()), token: token);
            }

            if (waitForRestart)
            {
                var probe = probeIds == null || probeIds.Length == 0 ? GetProbes(new ProbeParameters(), token) : GetProbes(Property.Id, probeIds, token);
                WaitForProbeRestart(restartTime.Value, probe, progressCallback, token);
            }
        }
Esempio n. 16
0
        //######################################
        // GetNotificationActionsInternal
        //######################################

        internal List <NotificationAction> GetNotificationActionsInternal(NotificationActionParameters parameters)
        {
            var response = RequestEngine.ExecuteRequest(parameters);

            var items = response.Descendants("item").ToList();

            foreach (var item in items)
            {
                var id = Convert.ToInt32(item.Element("objid").Value);

                var properties = GetNotificationActionProperties(id);

                item.Add(properties.Nodes());
            }

            var actions = XmlDeserializer <NotificationAction> .DeserializeList(response).Items;

            var actionsWithSchedules = ResponseParser.GroupActionSchedules(actions).ToList();

            UpdateActionSchedules(actionsWithSchedules);

            return(actions);
        }
Esempio n. 17
0
 internal void ApproveProbeInternal(int probeId, ProbeApproval action) =>
 RequestEngine.ExecuteRequest(new ApproveProbeParameters(probeId, action));
Esempio n. 18
0
 internal void FoldObject(int objectId, bool fold) =>
 RequestEngine.ExecuteRequest(new FoldParameters(objectId, fold));
 internal void ApproveProbeInternal(Either <Probe, int> probeOrId, ProbeApproval action) =>
 RequestEngine.ExecuteRequest(new ApproveProbeParameters(probeOrId, action));
 internal void SetObjectProperty <T>(BaseSetObjectPropertyParameters <T> parameters, int numObjectIds, CancellationToken token) =>
 RequestEngine.ExecuteRequest(parameters, m => ResponseParser.ParseSetObjectPropertyUrl(numObjectIds, m), token);
Esempio n. 21
0
        private XElement GetChannelProperties(int sensorId, int channelId, CancellationToken token)
        {
            var parameters = new ChannelPropertiesParameters(sensorId, channelId);

            return(RequestEngine.ExecuteRequest(parameters, r => ChannelSettings.GetChannelXml(r, channelId), token));
        }
Esempio n. 22
0
 internal void SetObjectProperty <T>(BaseSetObjectPropertyParameters <T> parameters, int numObjectIds) =>
 RequestEngine.ExecuteRequest(parameters, m => ResponseParser.ParseSetObjectPropertyUrl(numObjectIds, m));
Esempio n. 23
0
 private PrtgResponse GetObjectPropertiesRawInternal(int objectId, object objectType, CancellationToken token = default(CancellationToken)) =>
 RequestEngine.ExecuteRequest(new GetObjectPropertyParameters(objectId, objectType), token: token);
Esempio n. 24
0
 private int CloneObject(CloneParameters parameters, CancellationToken token) =>
 ResponseParser.Amend(RequestEngine.ExecuteRequest(parameters, ResponseParser.CloneRequestParser, token), ResponseParser.CloneResponseParser);
Esempio n. 25
0
        private void SetNotificationTriggerInternal(TriggerParameters parameters, CancellationToken token)
        {
            ValidateTriggerParameters(parameters, token);

            RequestEngine.ExecuteRequest(parameters, token: token);
        }