示例#1
0
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            DescribeLogGroupsResponse response = new DescribeLogGroupsResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("logGroups", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <LogGroup, LogGroupUnmarshaller>(LogGroupUnmarshaller.Instance);
                    response.LogGroups = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("nextToken", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.NextToken = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
示例#2
0
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonCloudWatchLogsConfig config = new AmazonCloudWatchLogsConfig();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonCloudWatchLogsClient client = new AmazonCloudWatchLogsClient(creds, config);

            DescribeLogGroupsResponse resp = new DescribeLogGroupsResponse();

            do
            {
                DescribeLogGroupsRequest req = new DescribeLogGroupsRequest
                {
                    NextToken = resp.NextToken
                    ,
                    Limit = maxItems
                };

                resp = client.DescribeLogGroups(req);
                CheckError(resp.HttpStatusCode, "200");

                foreach (var obj in resp.LogGroups)
                {
                    AddObject(obj);
                }
            }while (!string.IsNullOrEmpty(resp.NextToken));
        }
        public async Task DescribeLogGroupsTest_TwoResponses()
        {
            var request       = new DescribeLogGroupsRequest();
            var firstResponse = new DescribeLogGroupsResponse()
            {
                NextToken = "foo"
            };
            var secondResponse = new DescribeLogGroupsResponse()
            {
                NextToken = null
            };
            var token = new CancellationToken();

            _mockClient.SetupSequence(x => x.DescribeLogGroupsAsync(It.IsAny <DescribeLogGroupsRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(firstResponse)
            .ReturnsAsync(secondResponse);

            var numResponses = 0;

            await foreach (var desination in _mockClient.Object.Paginators.DescribeLogGroups(request).Responses.WithCancellation(token))
            {
                numResponses += 1;
            }
            Assert.Equal(2, numResponses);
        }
示例#4
0
        protected virtual void Dispose(bool disposing)
        {
            AmazonCloudWatchLogsClient Client =
                new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2);

            foreach (var logGroupName in LogGroupNameList)
            {
                if (!(string.IsNullOrEmpty(logGroupName)))
                {
                    DescribeLogGroupsResponse describeLogGroupsResponse = Client.DescribeLogGroupsAsync(
                        new DescribeLogGroupsRequest
                    {
                        LogGroupNamePrefix = logGroupName
                    }).Result;

                    if (!(string.IsNullOrEmpty(describeLogGroupsResponse.LogGroups[0].LogGroupName)))
                    {
                        var response = Client.DeleteLogGroupAsync(new DeleteLogGroupRequest
                        {
                            LogGroupName = logGroupName
                        }).Result;
                    }
                }
            }
        }
        public LogGroup GetLogGroup(string logGroup = null)
        {
            List <LogGroup>           allGroups  = new List <LogGroup>();
            DescribeLogGroupsResponse lgResponse = null;

            do
            {
                DebugLogger.WriteLine("Getting logGroups...");
                lgResponse = client.DescribeLogGroups(new DescribeLogGroupsRequest {
                    NextToken = (lgResponse != null ? lgResponse.NextToken : null)
                });
                allGroups.AddRange(lgResponse.LogGroups);
                DebugLogger.WriteLine("Got logGroups, have {0}, {1} more pages", allGroups.Count, (!string.IsNullOrWhiteSpace(lgResponse.NextToken) ? "still" : "no"));
            } while (!string.IsNullOrWhiteSpace(lgResponse.NextToken));

            if (string.IsNullOrWhiteSpace(logGroup) || logGroup[logGroup.Length - 1] == '*')
            {
                if (!string.IsNullOrWhiteSpace(logGroup))
                {
                    if (logGroup.EndsWith("*"))
                    {
                        logGroup = logGroup.Substring(0, logGroup.Length - 1);
                    }
                    allGroups = allGroups.Where(x => x.LogGroupName.StartsWith(logGroup)).ToList();
                }

                for (int i = 0, len = allGroups.Count; i < len; ++i)
                {
                    Console.WriteLine(i + ") " + allGroups[i].LogGroupName);
                }
                int num = ReadIntBetween("Choose log group (-1 to create one): ", -1, allGroups.Count - 1);

                if (num == -1)
                {
                    if (logGroup[logGroup.Length - 1] == '*')
                    {
                        return(CreateLogGroup());
                    }
                }

                Console.Clear();
                Console.WriteLine("You choose LogGroup: " + allGroups[num].LogGroupName);
                return(allGroups[num]);
            }

            var lg = allGroups.FirstOrDefault(x => x.LogGroupName == logGroup);

            if (lg == null)
            {
                Console.WriteLine("The log group '" + logGroup + "' does not exist.");
                lg = CreateLogGroup();
            }

            Console.WriteLine("You choose LogGroup: " + lg.LogGroupName);
            return(lg);
        }
        public async Task DescribeLogGroupsTest__OnlyUsedOnce()
        {
            var request   = new DescribeLogGroupsRequest();
            var response  = new DescribeLogGroupsResponse();
            var token     = new CancellationToken();
            var paginator = _mockClient.Object.Paginators.DescribeLogGroups(request);

            _mockClient.Setup(x => x.DescribeLogGroupsAsync(It.IsAny <DescribeLogGroupsRequest>(), It.IsAny <CancellationToken>())).ReturnsAsync(response);

            // Should work the first time
            await LoopOverLogGroups(paginator, token);

            // Second time should throw an exception
            await Assert.ThrowsAsync <System.InvalidOperationException>(async() => await LoopOverLogGroups(paginator, token));
        }
示例#7
0
        public void awsGetLogGroup() //Pull all LogGroup from a aws account
        {
            bool LogGroupExist = false;

            DescribeLogGroupsResponse respon = awsC.GetLogGroups();

            foreach (var item in respon.LogGroups)
            {
                if (item.LogGroupName.Contains("ecs")) // /ecs/blade-*** is from the ML logs
                {
                    LogGroupExist = true;
                    break;
                }
            }
            Assert.IsTrue(LogGroupExist);
        }
示例#8
0
        public void awsGetLogStream() //Pull all LogStream from a LogGroup on a aws account
        {
            string awsLogGroup = "";

            DescribeLogGroupsResponse LogGroupRespon = awsC.GetLogGroups();

            foreach (var item in LogGroupRespon.LogGroups)
            {
                if (item.LogGroupName.Contains("ecs")) // /ecs/blade-*** is from the ML logs
                {
                    awsLogGroup = item.LogGroupName;
                    break;
                }
            }

            DescribeLogStreamsResponse LogStreamRespon = awsC.GetLogStreams(awsLogGroup);

            Assert.IsNotNull(LogStreamRespon);
        }
示例#9
0
        [TestMethod]                 // May fail if there are no logs within the timespan that are set and may take some time to be done, if there is
        public void awsGetLogsFrom() //Pull all Logs from a Logstream from a timespan on a aws account
        {
            string awsLogGroup = "";
            int    awsLogFrom  = -20;
            int    awsLogTo    = -15;

            DescribeLogGroupsResponse LogGroupRespon = awsC.GetLogGroups();

            foreach (var item in LogGroupRespon.LogGroups)
            {
                if (item.LogGroupName.Contains("/ecs/blade-seg")) // /ecs/blade-seg is from the ML logs
                {
                    awsLogGroup = item.LogGroupName;
                    break;
                }
            }

            DescribeLogStreamsResponse LogStreamRespon = awsC.GetLogStreams(awsLogGroup);

            List <GetLogEventsResponse> Logs = awsC.GetLogsFrom(awsLogGroup, LogStreamRespon, awsLogFrom, awsLogTo);

            bool containdata = false;

            foreach (var log in Logs)
            {
                foreach (var item in log.Events)
                {
                    if (!string.IsNullOrEmpty(item.Message))
                    {
                        containdata = true;
                        break;
                    }
                }
            }

            Assert.IsTrue(containdata);
        }
        public async Task DescribeLogGroupsTest__CancellationToken()
        {
            var request       = new DescribeLogGroupsRequest();
            var firstResponse = new DescribeLogGroupsResponse()
            {
                NextToken = "foo"
            };
            var secondResponse = new DescribeLogGroupsResponse()
            {
                NextToken = null
            };
            var paginator = _mockClient.Object.Paginators.DescribeLogGroups(request);

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            tokenSource.Cancel();

            _mockClient.SetupSequence(x => x.DescribeLogGroupsAsync(It.IsAny <DescribeLogGroupsRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(firstResponse)
            .ReturnsAsync(secondResponse);

            await Assert.ThrowsAsync <OperationCanceledException>(async() => await LoopOverLogGroups(paginator, token));
        }
示例#11
0
        static void Main(string[] args)
        {
            var logClient = new AmazonCloudWatchLogsClient();
            // Add a new log group for testing
            const string newLogGroupName   = "NewLogGroup";
            DescribeLogGroupsResponse dlgr = logClient.DescribeLogGroups();
            var groups = new List <LogGroup> {
            };

            groups = dlgr.LogGroups;
            LogGroup lg = new LogGroup();

            lg.LogGroupName = newLogGroupName;
            // Look for our new log group name to determine if we need to do setup
            LogGroup result = groups.Find(
                delegate(LogGroup bk)
            {
                return(bk.LogGroupName == newLogGroupName);
            }
                );

            if (result != null)
            {
                Console.WriteLine(result.LogGroupName + " found");
            }
            else
            {
                //Haven't seen this log group, set it up
                CreateLogGroupRequest clgr = new CreateLogGroupRequest(newLogGroupName);
                logClient.CreateLogGroup(clgr);
                // Create a file to sace next SequenceToken in
                File.CreateText("..\\..\\" + lg.LogGroupName + ".txt");
                CreateLogStreamRequest csr = new CreateLogStreamRequest(lg.LogGroupName, newLogGroupName);
                logClient.CreateLogStream(csr);
            }

            string tokenFile = "";

            try
            {
                Console.WriteLine(lg.LogGroupName);
                //Pick up the next sequence token from the last run
                tokenFile = lg.LogGroupName;
                StreamReader sr            = File.OpenText("..\\..\\" + tokenFile + ".txt");
                string       sequenceToken = sr.ReadLine();
                sr.Close();
                lg.RetentionInDays = 30;
                string groupName                  = lg.LogGroupName;;
                TestMetricFilterRequest tmfr      = new TestMetricFilterRequest();
                List <InputLogEvent>    logEvents = new List <InputLogEvent>(3);
                InputLogEvent           ile       = new InputLogEvent();
                ile.Message = "Test Event 1";
                //DateTime dt = new DateTime(1394793518000);
                DateTime dt = new DateTime(2017, 01, 11);
                ile.Timestamp = dt;
                logEvents.Add(ile);
                ile.Message = "Test Event 2";
                logEvents.Add(ile);
                ile.Message = "This message also contains an Error";
                logEvents.Add(ile);
                DescribeLogStreamsRequest dlsr = new DescribeLogStreamsRequest(groupName);

                PutLogEventsRequest pler = new PutLogEventsRequest(groupName, tokenFile, logEvents);
                pler.SequenceToken = sequenceToken; //use last sequence token
                PutLogEventsResponse plerp = new PutLogEventsResponse();
                plerp = logClient.PutLogEvents(pler);
                Console.WriteLine("Next sequence token = " + plerp.NextSequenceToken);
                FileStream fs = File.OpenWrite("..\\..\\" + tokenFile + ".txt");
                fs.Position = 0;
                UTF8Encoding utf8         = new UTF8Encoding();
                Byte[]       encodedBytes = utf8.GetBytes(plerp.NextSequenceToken);
                fs.Write(encodedBytes, 0, utf8.GetByteCount(plerp.NextSequenceToken));
                fs.Close();
                List <string> lem = new List <string>(1);
                lem.Add("Error");
                tmfr.LogEventMessages = lem;
                tmfr.FilterPattern    = "Error";
                TestMetricFilterResponse tmfrp = new TestMetricFilterResponse();
                tmfrp = logClient.TestMetricFilter(tmfr);
                var results = new List <MetricFilterMatchRecord> {
                };
                results = tmfrp.Matches;
                Console.WriteLine("Found " + results.Count.ToString() + " match records");
                IEnumerator ie = results.GetEnumerator();
                while (ie.MoveNext())
                {
                    MetricFilterMatchRecord mfmr = (MetricFilterMatchRecord)ie.Current;
                    Console.WriteLine("Event Message = " + mfmr.EventMessage);
                }
                Console.WriteLine("Metric filter test done");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#12
0
        public void CRUDLogGroup()
        {
            var logGroupName = "sdk-dotnet-" + DateTime.Now.Ticks;

            Client.CreateLogGroupAsync(new CreateLogGroupRequest
            {
                LogGroupName = logGroupName
            }).Wait();
            try
            {
                {
                    DescribeLogGroupsResponse response = Client.DescribeLogGroupsAsync(new DescribeLogGroupsRequest
                    {
                        LogGroupNamePrefix = logGroupName
                    }).Result;

                    Assert.AreEqual(1, response.LogGroups.Count);
                    Assert.IsNotNull(response.LogGroups[0].Arn);
                    Assert.IsNull(response.LogGroups[0].RetentionInDays);

                    Client.PutRetentionPolicyAsync(new PutRetentionPolicyRequest
                    {
                        LogGroupName    = logGroupName,
                        RetentionInDays = 1
                    }).Wait();

                    response = Client.DescribeLogGroupsAsync(new DescribeLogGroupsRequest
                    {
                        LogGroupNamePrefix = logGroupName
                    }).Result;

                    Assert.AreEqual(1, response.LogGroups.Count);
                    Assert.AreEqual(1, response.LogGroups[0].RetentionInDays.GetValueOrDefault());
                }

                {
                    Client.CreateLogStreamAsync(new CreateLogStreamRequest
                    {
                        LogGroupName  = logGroupName,
                        LogStreamName = "sample"
                    }).Wait();

                    DescribeLogStreamsResponse describeResponse = Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest
                    {
                        LogGroupName        = logGroupName,
                        LogStreamNamePrefix = "sample"
                    }).Result;

                    Assert.AreEqual(1, describeResponse.LogStreams.Count);
                    Assert.IsNotNull(describeResponse.LogStreams[0].Arn);

                    PutLogEventsResponse putResponse1 = Client.PutLogEventsAsync(new PutLogEventsRequest
                    {
                        LogGroupName  = logGroupName,
                        LogStreamName = "sample",
                        LogEvents     = new List <InputLogEvent>
                        {
                            new InputLogEvent
                            {
                                Message   = "First Data",
                                Timestamp = DateTime.Now
                            }
                        }
                    }).Result;

                    // Pad the time so the 2 events are not at the same time.
                    UtilityMethods.Sleep(TimeSpan.FromSeconds(.1));

                    Client.PutLogEventsAsync(new PutLogEventsRequest
                    {
                        SequenceToken = putResponse1.NextSequenceToken,
                        LogGroupName  = logGroupName,
                        LogStreamName = "sample",
                        LogEvents     = new List <InputLogEvent>
                        {
                            new InputLogEvent
                            {
                                Message   = "Second Data",
                                Timestamp = DateTime.Now
                            }
                        }
                    }).Wait();

                    GetLogEventsResponse getResponse = null;

                    // Doing this in a loop to wait for the eventual consistency of the events
                    // being written to cloudwatch logs.
                    for (int i = 0; i < 20; i++)
                    {
                        getResponse = Client.GetLogEventsAsync(new GetLogEventsRequest
                        {
                            LogGroupName  = logGroupName,
                            LogStreamName = "sample",
                            StartTime     = DateTime.Now.AddDays(-2),
                            EndTime       = DateTime.Now
                        }).Result;

                        if (getResponse.Events.Count == 2)
                        {
                            break;
                        }

                        UtilityMethods.Sleep(TimeSpan.FromSeconds(2));
                    }

                    Assert.AreEqual(2, getResponse.Events.Count);
                    Assert.AreEqual("First Data", getResponse.Events[0].Message);
                    Assert.AreEqual(DateTime.UtcNow.Date, getResponse.Events[0].Timestamp.Date);

                    Assert.AreEqual("Second Data", getResponse.Events[1].Message);
                    Assert.AreEqual(DateTime.UtcNow.Date, getResponse.Events[1].Timestamp.Date);

                    Assert.IsTrue(getResponse.Events[0].Timestamp < getResponse.Events[1].Timestamp);


                    Client.DeleteLogStreamAsync(new DeleteLogStreamRequest
                    {
                        LogGroupName  = logGroupName,
                        LogStreamName = "sample"
                    }).Wait();
                }
            }
            finally
            {
                Client.DeleteLogGroupAsync(new DeleteLogGroupRequest
                {
                    LogGroupName = logGroupName
                }).Wait();
            }
        }