on 07-Mar-2015 15:03
Problem this snippet solves:
This C# application will display the current connections for all of the default pools. There was found to be too much overhead when sending in the list of all pools, some of which were empty so this app strips out the virtual servers with non-defined default pools and batches up requests for statistics.
Code :
using System; using System.Collections.Generic; using System.Text; using iControl; using System.Diagnostics; namespace PRVD.iControl.GetAllPoolCurrentConnections { class Program { static void Main(string[] args) { if (args.Length < 3 || string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) || string.IsNullOrEmpty(args[2])) { Console.WriteLine( "Usage: f5GetAllPoolCurrentConnections [username] [password] [hostname or ip address] optional [partition]"); return; } string user = args[0]; string pass = args[1]; string ipAddress = args[2]; string partition = ""; if (args.Length > 3 && !string.IsNullOrEmpty(args[3])) partition = args[3]; Interfaces m_interfaces = new Interfaces(); m_interfaces.initialize(ipAddress, user, pass); //Check if inialized if (!m_interfaces.initialized) Environment.Exit(Environment.ExitCode); //Get the partition list to drop into PROD bool partitionFound = false; ManagementPartitionAuthZPartition[] partitionList = m_interfaces.ManagementPartition.get_partition_list(); for (int i = 0; i < partitionList.Length; i++) { Console.WriteLine("Partition found: {0}", partitionList[ i ].partition_name); if (partitionList[ i ].partition_name == partition) { m_interfaces.ManagementPartition.set_active_partition(partitionList[ i ].partition_name); partitionFound = true; } } if (!partitionFound) { Console.WriteLine("The partition '{0}' specified was not found.", partition); return; } //Get all virtual servers string[] localVirtualServers = m_interfaces.LocalLBVirtualServer.get_list(); //PrintInfo(localVirtualServers); //Get all the active default pools string[] localDefaultPools = m_interfaces.LocalLBVirtualServer.get_default_pool_name(localVirtualServers); //PrintInfo(localDefaultPools); //Get the pool stats LocalLBPoolPoolStatistics poolStats = new LocalLBPoolPoolStatistics(); //Get the pool stat entries LocalLBPoolPoolStatisticEntry[] poolStatEntries; //Load the localDefaultPools into a Listobject to prune and perform batch get_statistics with List localDefaultPoolsList = RemoveNoNamePools(localDefaultPools); string[] poolBatch; int batchCount = 1; Stopwatch timer = new Stopwatch(); timer.Start(); //Break the statistics rquests into batchs of XX while(localDefaultPoolsList.Count > 0) { if(localDefaultPoolsList.Count >= 10) { poolBatch = localDefaultPoolsList.GetRange(localDefaultPoolsList.Count - 10, 10).ToArray(); localDefaultPoolsList.RemoveRange(localDefaultPoolsList.Count - 10, 10); poolStats = m_interfaces.LocalLBPool.get_statistics(poolBatch); poolStatEntries = poolStats.statistics; PrintStatistics(poolStatEntries); } else { poolBatch = localDefaultPoolsList.ToArray(); localDefaultPoolsList.Clear(); poolStats = m_interfaces.LocalLBPool.get_statistics(poolBatch); poolStatEntries = poolStats.statistics; PrintStatistics(poolStatEntries); } //Console.WriteLine("Batch {0} time elapsed: {1}", batchCount.ToString(), timer.Elapsed.ToString()); batchCount++; } Console.WriteLine("Total time elapsed: {1}", batchCount.ToString(), timer.Elapsed.ToString()); timer.Stop(); } private static List RemoveNoNamePools(string[] localDefaultPools) { List localDefaultPoolsList = new List (); foreach (string poolName in localDefaultPools) { if (poolName != "") localDefaultPoolsList.Add(poolName); } return localDefaultPoolsList; } public static void PrintInfo(string[] info) { for (int i = 0; i > info.Length; i++) { Console.WriteLine(info[ i ].ToString()); } } public static void PrintStatistics(LocalLBPoolPoolStatisticEntry[] poolStatEntries) { //Loop through each pool and print out the server side current connection stats for (int i = 0; i < poolStatEntries.Length; i++) { CommonStatistic[] commonStats = poolStatEntries[ i ].statistics; for (int x = 0; x < commonStats.Length; x++) { if (commonStats[x].type == CommonStatisticType.STATISTIC_SERVER_SIDE_CURRENT_CONNECTIONS && poolStatEntries[ i ].pool_name != "") { Console.WriteLine("Pool name: {0} Current connections: {1}", poolStatEntries[ i ].pool_name, commonStats[x].value.low.ToString()); } } } } } }