Example #1
0
        private static string GetCapacityUsed(string authToken, string accountName)
        {
            string capacityUsed = string.Empty;
            CloudStorageAccount storageAccount = GetUserAccount(authToken, accountName);

            if (storageAccount != null)
            {
                CloudAnalyticsClient        storageAnalytics = storageAccount.CreateCloudAnalyticsClient();
                TableQuery <CapacityEntity> capacityQuery    = storageAnalytics.CreateCapacityQuery();
                IQueryable <CapacityEntity> capacityData     = capacityQuery.Where(x => x.RowKey == Constant.Storage.Table.Key.CapacityData);
                try
                {
                    List <CapacityEntity> capacities = capacityData.ToList();
                    if (capacities.Count == 0)
                    {
                        capacityUsed = Storage.MapByteCount(0);
                    }
                    else
                    {
                        capacities.Sort(OrderByLatest);
                        long latestCapacity = capacities.First().Capacity;
                        capacityUsed = Storage.MapByteCount(latestCapacity);
                    }
                }
                catch
                {
                    capacityUsed = Constant.Storage.NotAvailable;
                }
            }
            return(capacityUsed);
        }
Example #2
0
        static void Main(string[] args)
        {
            StorageUri           blobStorageUri  = new StorageUri(new Uri($"https://{args[0]}.blob.core.windows.net"));
            StorageUri           tableStorageUri = new StorageUri(new Uri($"https://{args[0]}.table.core.windows.net"));
            StorageCredentials   credentials     = new StorageCredentials(args[0], args[1]);
            CloudAnalyticsClient client          = new CloudAnalyticsClient(blobStorageUri, tableStorageUri, credentials);

            PrintMinuteMetrics(client);
        }
        protected internal virtual CloudAnalyticsClient CreateAnalyticsClient(IKernel kernel)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException(nameof(kernel));
            }

            CloudStorageAccount  account = GetAccount(kernel);
            CloudAnalyticsClient client  = account.CreateCloudAnalyticsClient();

            return(client);
        }
Example #4
0
        private static void PrintMinuteMetrics(CloudAnalyticsClient analyticsClient)
        {
            var services = Enum.GetValues(typeof(StorageService));

            foreach (StorageService service in services)
            {
                Console.WriteLine($"Minute Metrics for Service {service}");
                var metricsQuery = analyticsClient.CreateMinuteMetricsQuery(service, StorageLocation.Primary);
                var t            = analyticsClient.GetMinuteMetricsTable(service);
                var opContext    = new OperationContext();
                var query        =
                    from entity in metricsQuery
                    select entity;

                var results      = query.ToList().Where(m => m.RowKey.StartsWith("user"));
                var resultString = results.Aggregate(new StringBuilder(), (builder, metrics) => builder.AppendLine(MetricsString(metrics, opContext))).ToString();
                Console.WriteLine($"{resultString}");
            }
        }
Example #5
0
 public AnalyticsRepository(StorageService serviceType)
 {
     analyticsClient = base.storageAccount.CreateCloudAnalyticsClient();
     service         = serviceType;
 }
Example #6
0
        public static void GetAnalyticsLogs(CloudBlobClient blobClient, CloudTableClient tableClient, StorageAccount strAccount)
        {
            try
            {
                DateTime                 time               = DateTime.UtcNow;
                int                      inactivityDays     = Int16.Parse(InactivityDaysForStorageAccount) * -1;
                CloudAnalyticsClient     analyticsClient    = new CloudAnalyticsClient(blobClient.StorageUri, tableClient.StorageUri, tableClient.Credentials);
                IEnumerable <ICloudBlob> results            = analyticsClient.ListLogs(StorageService.Blob, time.AddDays(inactivityDays), null, LoggingOperations.All, BlobListingDetails.Metadata, null, null);
                List <ICloudBlob>        logs               = results.ToList();
                int                      nonLogEntries      = 0;
                int                      onlyListLogEntries = 0;

                //Creating folders for storage account
                string storageName = blobClient.BaseUri.Host.Split('.')[0];
                string tempPath    = Path.GetTempPath();
                string storagePath = (tempPath + "/logs/" + storageName + "/");
                Directory.CreateDirectory(storagePath);

                //Download the log files
                foreach (var item in logs)
                {
                    string             name      = ((CloudBlockBlob)item).Name;
                    CloudBlobContainer container = blobClient.GetContainerReference("$logs");
                    CloudBlockBlob     blockBlob = container.GetBlockBlobReference(name);

                    //specify the directory without file name
                    string sub_folder = name.Remove(name.LastIndexOf("/") + 1);
                    string path       = (storagePath + sub_folder);

                    //create the directory if it does not exist.
                    Directory.CreateDirectory(path);

                    //specify the file full path
                    string file_path = (storagePath + name);

                    using (var fileStream = File.Create(file_path))
                    {
                        blockBlob.DownloadToStream(fileStream);
                    }
                }
                if (logs.Count > 0)
                {
                    foreach (string file in Directory.GetFiles(storagePath, "*.log", SearchOption.AllDirectories))
                    {
                        var contents = File.ReadLines(file);
                        foreach (var line in contents)
                        {
                            string operationType = line.Split(';')[2];
                            //Ignoring the log entries for fetching logs from logs container & BlobPreflightRequest logs also.
                            if (!(line.Contains("$logs") || line.Contains("%24logs") || line.Contains("BlobPreflightRequest")))
                            {
                                if (operationType.Equals("GetBlobServiceProperties") || operationType.Equals("ListBlobs") || operationType.Equals("ListContainers") || operationType.Equals("GetContainerServiceMetadata"))
                                {
                                    onlyListLogEntries++;
                                }
                                else
                                {
                                    nonLogEntries++;
                                }
                            }
                        }
                    }
                }
                ServiceProperties serviceProperties = blobClient.GetServiceProperties();
                bool isLoggingDisabled = serviceProperties.Logging.LoggingOperations.ToString() == "None";
                strAccount.isDiagnosticsEnabled = true;
                if (isLoggingDisabled)
                {
                    Console.WriteLine("Information: Diagnostics Settings are enabled for this account but logging is still disabled. Please change the Diagnostics Settings.");
                }
                else
                {
                    strAccount.isLoggingEnabled = true;
                    if (logs.Count == 0)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Warning: Diagnostics Settings are enabled for this account. Either this storage account has not been used from last " + InactivityDaysForStorageAccount + " days or Logs are not available for this storage account.");
                        Console.ResetColor();
                        strAccount.isNotUsed = true;
                    }
                    else
                    {
                        //If there are log entries other than the list logs means the storage account is in use
                        if (onlyListLogEntries > 0 && nonLogEntries == 0)
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Information: There are only log entries for GetBlobServiceProperties, ListBlobs, ListContainers from last " + InactivityDaysForStorageAccount + " days for this storage account.");
                            Console.ResetColor();
                            strAccount.onlyListLogEntries = true;
                        }
                        else if (onlyListLogEntries == 0 && nonLogEntries == 0)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Warning: Either the storage account has not been used from last " + InactivityDaysForStorageAccount + " days for this storage account or complete logs are not available.");
                            Console.ResetColor();
                            strAccount.isNotUsed = true;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("Storage account is being used in last " + InactivityDaysForStorageAccount + " days!");
                            Console.ResetColor();
                            strAccount.isUsed = true;
                        }
                    }
                }
            }
            catch (StorageException e)
            {
                //If logs container is not present, means diagnostics are not enabled
                if (e.RequestInformation.ErrorCode == "ContainerNotFound")
                {
                    strAccount.isDiagnosticsEnabled = false;
                    Console.WriteLine("Information: Please enable Diagnostics Settings to check the logs/activity of the storage account.");
                }
                else
                {
                    Console.WriteLine("Exception occured:" + e.RequestInformation.ErrorCode);
                }
            }
            finally
            {
                storageAccounts.Add(strAccount);
            }
        }
        //internal const string TableName = "ustormiot20170712003413jul2017at084521201setuplog";
        public static void Main(string[] args)
        {
            Console.WriteLine("Azure Storage Table Sample\n");

            // Create or reference an existing table
            //CloudTable table = CreateTableAsync().Result;

            // Demonstrate basic CRUD functionality
            // BasicTableOperationsAsync(table).Wait();

            // Demonstrate advanced functionality such as batch operations and segmented multi-entity queries
            //  AdvancedTableOperationsAsync(table).Wait();

            // When you delete a table it could take several seconds before you can recreate a table with the same
            // name - hence to enable you to run the demo in quick succession the table is not deleted. If you want
            // to delete the table uncomment the line of code below.
            //DeleteTableAsync(table).Wait();

            CloudStorageAccount  storageAccount  = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            StorageCredentials   cred            = new StorageCredentials("testfuyu", "dbxJcP5LaGhJvOZtLENl8NGoHITRKtSAlB1Hg2b8VwlsP3aphw8SdK9K/KVtkvpWogyDnyzxx2IZ1AYoeV6PQg==");
            StorageUri           bloburi         = new StorageUri(new Uri("https://testfuyu.blob.core.chinacloudapi.cn/testfuyu"));
            StorageUri           tableuri        = new StorageUri(new Uri("https://testfuyu.table.core.chinacloudapi.cn/"));
            CloudAnalyticsClient analyticsClient = new CloudAnalyticsClient(bloburi, tableuri, cred);
            var cloudTableClient = storageAccount.CreateCloudTableClient();
            var table            = cloudTableClient.GetTableReference(TableName);
            // var entities = table.ExecuteQuery(new TableQuery<CapacityEntity>()).ToList();

            TableContinuationToken token = null;
            var entities = new List <CapacityEntity>();

            do
            {
                var queryResult = table.ExecuteQuerySegmented(new TableQuery <CapacityEntity>(), token);
                entities.AddRange(queryResult.Results);
                token = queryResult.ContinuationToken;
            } while (token != null);

            foreach (CapacityEntity entity in entities)
            {
                Console.WriteLine("result is {0}, {1}", entity.Time, entity.Capacity);
            }
            var yesterday     = DateTime.UtcNow.Date.AddDays(-1).ToString("yyyyMMddT0000");
            var capacityQuery = analyticsClient.CreateCapacityQuery();
            var query         = from entity in capacityQuery
                                where entity.PartitionKey.CompareTo(yesterday) >= 0
                                select entity;

            var results = query.ToList();

            //TableQuery < CapacityEntity > query = new TableQuery<CapacityEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.GreaterThan, yesterday));
            //Console.WriteLine("enum is: {0}", table.ExecuteQuery(query).GetEnumerator());

            Console.WriteLine("another way to check result");

            foreach (CapacityEntity entity in results)
            {
                if (entity.PartitionKey.CompareTo(yesterday) >= 0)
                {
                    Console.WriteLine("result is {0}, {1}", entity.Time, entity.Capacity);
                }
            }

            Console.ReadLine();
        }