private NetworkStatistic getNetworkUtilization(string networkCard)
        {
            NetworkStatistic stats = new NetworkStatistic();
            const int        numberOfIterations = 10;

            PerformanceCounter bandwidthCounter = new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard);
            float bandwidth = bandwidthCounter.NextValue();//valor fixo 10Mb/100Mn/

            PerformanceCounter dataSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);

            PerformanceCounter dataReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkCard);

            float sendSum    = 0;
            float receiveSum = 0;

            for (int index = 0; index < numberOfIterations; index++)
            {
                sendSum    += dataSentCounter.NextValue();
                receiveSum += dataReceivedCounter.NextValue();
            }
            float dataSent     = sendSum;
            float dataReceived = receiveSum;

            stats.Download     = Math.Round(dataReceived / 1000.0, 3);
            stats.Upload       = Math.Round(dataSent / 1000.0, 3);
            stats.CreationDate = DateTime.UtcNow;

            double utilization = (8 * (dataSent + dataReceived)) / (bandwidth * numberOfIterations) * 100;

            stats.TotalUsage    = utilization;
            stats.InterfaceName = networkCard + " " + ((bandwidth / 1000.0) / 1000.0 / 1000.0).ToString() + "GB";

            NetworkInterface[] networkCards = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface card in networkCards)
            {
                var ipProps = card.GetIPProperties();
                if (card.Name == "Local Area Connection")
                {
                    string networkIp = networkCard;
                    networkIp = networkIp.Replace("[", "(");
                    networkIp = networkIp.Replace("]", ")");


                    if (card.Description == networkIp)
                    {
                        foreach (var ip in ipProps.UnicastAddresses)
                        {
                            if ((ip.Address.AddressFamily == AddressFamily.InterNetwork))
                            {
                                Console.Out.WriteLine(ip.Address.ToString() + "|" + card.Description.ToString());
                                stats.IpAddress = ip.Address.ToString();
                            }
                        }
                    }
                }
            }
            return(stats);
        }
Пример #2
0
        public List <NetworkStatistic> getNetworkUtilization()
        {
            PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");

            String[] instancename            = category.GetInstanceNames();
            List <NetworkStatistic> Lststats = new List <NetworkStatistic>();

            List <string> lst = new List <string>();

            NetworkInterface[] networkCards = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface card in networkCards)
            {
                var ipProps = card.GetIPProperties();
                if (card.NetworkInterfaceType == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet)
                {
                    foreach (var ip in ipProps.UnicastAddresses)
                    {
                        if ((ip.Address.AddressFamily == AddressFamily.InterNetwork))
                        {
                            var Name = card.Name.ToString();
                            if (!lst.Any(x => x == Name))
                            {
                                NetworkStatistic stats = new NetworkStatistic();
                                if (card.Speed > 0)
                                {
                                    var beforedownload = Math.Round(card.GetIPv4Statistics().BytesReceived / 1000.0, 3);
                                    var beforeupload   = Math.Round(card.GetIPv4Statistics().BytesSent / 1000.0, 3);
                                    Thread.Sleep(1000);
                                    var afterdownload = Math.Round(card.GetIPv4Statistics().BytesReceived / 1000.0, 3);
                                    var afterupload   = Math.Round(card.GetIPv4Statistics().BytesSent / 1000.0, 3);
                                    stats.Download      = afterdownload - beforedownload;
                                    stats.Upload        = afterupload - beforeupload;
                                    stats.CreationDate  = DateTime.UtcNow;
                                    stats.InterfaceName = card.Name;
                                    string hostNames = Dns.GetHostName();
                                    string myIPs     = Dns.GetHostByName(hostNames).AddressList[0].ToString();
                                    stats.ServerIp = System.Environment.MachineName.ToString();
                                    float utilization = (float)((stats.Upload + stats.Download) / ((card.Speed / 8) / 1000.0)) * 100;
                                    stats.TotalUsage = Math.Round(utilization, 3);
                                    stats.ServerName = Name;
                                    stats.IpAddress  = ip.Address.ToString();
                                    Console.Out.WriteLine("\n" + ip.Address.ToString() + "|  " + card.Description.ToString() + "\n" + "OpeartionalStatus:" + card.OperationalStatus + "\n" + "speed:" + card.Speed + "\n" + "Name" + card.Name + "\n" + "NetworkInterfaceType:" + card.NetworkInterfaceType);
                                    Console.WriteLine("Download:{0}, Upload:{1}, TotalUsage{2}", stats.Download, stats.Upload, stats.TotalUsage);
                                    lst.Add(Name);
                                    Lststats.Add(stats);
                                }
                            }
                            //Console.Out.WriteLine(ip.Address.ToString() + "|" + card.Description.ToString());
                            //stats.IpAddress = ip.Address.ToString();
                        }
                    }
                }
            }
            return(Lststats);
        }
Пример #3
0
 public virtual void CopyFrom(NetworkStatistic other)
 {
     if (other != null)
     {
         this.NetworkStatisticId = other.NetworkStatisticId;
         this.InterfaceName      = other.InterfaceName;
         this.IpAddress          = other.IpAddress;
         this.TotalUsage         = other.TotalUsage;
         this.Download           = other.Download;
         this.Upload             = other.Upload;
         this.CreationDate       = other.CreationDate;
         this.ServerIp           = other.ServerIp;
         this.ServerName         = other.ServerName;
     }
 }
        public virtual NetworkStatistic NetworkStatisticFromDataRow(DataRow dr)
        {
            if (dr == null)
            {
                return(null);
            }
            NetworkStatistic entity = new NetworkStatistic();

            if (dr.Table.Columns.Contains("NetworkStatisticId"))
            {
                entity.NetworkStatisticId = (System.Int32)dr["NetworkStatisticId"];
            }
            if (dr.Table.Columns.Contains("InterfaceName"))
            {
                entity.InterfaceName = dr["InterfaceName"].ToString();
            }
            if (dr.Table.Columns.Contains("IPAddress"))
            {
                entity.IpAddress = dr["IPAddress"].ToString();
            }
            if (dr.Table.Columns.Contains("TotalUsage"))
            {
                entity.TotalUsage = (System.Double)dr["TotalUsage"];
            }
            if (dr.Table.Columns.Contains("Download"))
            {
                entity.Download = (System.Double)dr["Download"];
            }
            if (dr.Table.Columns.Contains("Upload"))
            {
                entity.Upload = (System.Double)dr["Upload"];
            }
            if (dr.Table.Columns.Contains("CreationDate"))
            {
                entity.CreationDate = (System.DateTime)dr["CreationDate"];
            }
            if (dr.Table.Columns.Contains("ServerIp"))
            {
                entity.ServerIp = dr["ServerIp"].ToString();
            }
            if (dr.Table.Columns.Contains("ServerName"))
            {
                entity.ServerName = dr["ServerName"].ToString();
            }
            return(entity);
        }
        public override NetworkStatistic InsertNetworkStatistic(NetworkStatistic entity)
        {
            NetworkStatistic other = new NetworkStatistic();

            other = entity;
            if (entity.IsTransient())
            {
                string         sql            = @"Insert into NetworkStatistic ( [InterfaceName]
				,[IPAddress]
				,[TotalUsage]
				,[Download]
				,[Upload]
				,[CreationDate]
				,[ServerIp]
				,[ServerName] )
				Values
				( @InterfaceName
				, @IPAddress
				, @TotalUsage
				, @Download
				, @Upload
				, @CreationDate
				, @ServerIp
				, @ServerName );
				Select scope_identity()"                ;
                SqlParameter[] parameterArray = new SqlParameter[] {
                    new SqlParameter("@InterfaceName", entity.InterfaceName)
                    , new SqlParameter("@IPAddress", entity.IpAddress)
                    , new SqlParameter("@TotalUsage", entity.TotalUsage)
                    , new SqlParameter("@Download", entity.Download)
                    , new SqlParameter("@Upload", entity.Upload)
                    , new SqlParameter("@CreationDate", entity.CreationDate)
                    , new SqlParameter("@ServerIp", entity.ServerIp ?? (object)DBNull.Value)
                    , new SqlParameter("@ServerName", entity.ServerName ?? (object)DBNull.Value)
                };
                var identity = SqlHelper.ExecuteScalar(this.ConnectionString, CommandType.Text, sql, parameterArray);
                if (identity == DBNull.Value)
                {
                    throw new DataException("Identity column was null as a result of the insert operation.");
                }
                return(GetNetworkStatistic(Convert.ToInt32(identity)));
            }
            return(entity);
        }
 public override NetworkStatistic InsertNetworkStatistic(NetworkStatistic entity)
 {
     NetworkStatistic other = new NetworkStatistic();
     other = entity;
     if (entity.IsTransient())
     {
         string sql = @"Insert into NetworkStatistic ( [InterfaceName]
         ,[IPAddress]
         ,[TotalUsage]
         ,[Download]
         ,[Upload]
         ,[CreationDate]
         ,[ServerIp]
         ,[ServerName] )
         Values
         ( @InterfaceName
         , @IPAddress
         , @TotalUsage
         , @Download
         , @Upload
         , @CreationDate
         , @ServerIp
         , @ServerName );
         Select scope_identity()";
         SqlParameter[] parameterArray = new SqlParameter[]{
              new SqlParameter("@InterfaceName",entity.InterfaceName)
             , new SqlParameter("@IPAddress",entity.IpAddress)
             , new SqlParameter("@TotalUsage",entity.TotalUsage)
             , new SqlParameter("@Download",entity.Download)
             , new SqlParameter("@Upload",entity.Upload)
             , new SqlParameter("@CreationDate",entity.CreationDate)
             , new SqlParameter("@ServerIp",entity.ServerIp ?? (object)DBNull.Value)
             , new SqlParameter("@ServerName",entity.ServerName ?? (object)DBNull.Value)};
         var identity = SqlHelper.ExecuteScalar(this.ConnectionString, CommandType.Text, sql, parameterArray);
         if (identity == DBNull.Value) throw new DataException("Identity column was null as a result of the insert operation.");
         return GetNetworkStatistic(Convert.ToInt32(identity));
     }
     return entity;
 }
        public virtual NetworkStatistic UpdateNetworkStatistic(NetworkStatistic entity)
        {
            if (entity.IsTransient())
            {
                return(entity);
            }
            NetworkStatistic other = GetNetworkStatistic(entity.NetworkStatisticId);

            if (entity.Equals(other))
            {
                return(entity);
            }
            string sql = @"Update NetworkStatistic set  [InterfaceName]=@InterfaceName
							, [IPAddress]=@IPAddress
							, [TotalUsage]=@TotalUsage
							, [Download]=@Download
							, [Upload]=@Upload
							, [CreationDate]=@CreationDate
							, [ServerIp]=@ServerIp
							, [ServerName]=@ServerName 
							 where NetworkStatisticId=@NetworkStatisticId"                            ;

            SqlParameter[] parameterArray = new SqlParameter[] {
                new SqlParameter("@InterfaceName", entity.InterfaceName)
                , new SqlParameter("@IPAddress", entity.IpAddress)
                , new SqlParameter("@TotalUsage", entity.TotalUsage)
                , new SqlParameter("@Download", entity.Download)
                , new SqlParameter("@Upload", entity.Upload)
                , new SqlParameter("@CreationDate", entity.CreationDate)
                , new SqlParameter("@ServerIp", entity.ServerIp ?? (object)DBNull.Value)
                , new SqlParameter("@ServerName", entity.ServerName ?? (object)DBNull.Value)
                , new SqlParameter("@NetworkStatisticId", entity.NetworkStatisticId)
            };
            SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, sql, parameterArray);
            return(GetNetworkStatistic(entity.NetworkStatisticId));
        }
Пример #8
0
        public void InsertNetworkStatistic(NetworkStatistic stats)
        {
            NetworkStatisticRepository repo = new NetworkStatisticRepository();

            repo.InsertNetworkStatistic(stats);
        }
 public void InsertNetworkStatistic(NetworkStatistic stats)
 {
     NetworkStatisticRepository repo = new NetworkStatisticRepository();
     repo.InsertNetworkStatistic(stats);
 }
        public List<NetworkStatistic> getNetworkUtilization()
        {
            PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
            String[] instancename = category.GetInstanceNames();
            List<NetworkStatistic> Lststats = new List<NetworkStatistic>();

            List<string> lst = new List<string>();
            NetworkInterface[] networkCards = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface card in networkCards)
            {
                var ipProps = card.GetIPProperties();
                if (card.NetworkInterfaceType == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet)
                {
                    foreach (var ip in ipProps.UnicastAddresses)
                    {
                        if ((ip.Address.AddressFamily == AddressFamily.InterNetwork))
                        {
                            var Name = card.Name.ToString();
                            if (!lst.Any(x => x == Name))
                            {
                                NetworkStatistic stats = new NetworkStatistic();
                                if (card.Speed > 0)
                                {
                                    var beforedownload = Math.Round(card.GetIPv4Statistics().BytesReceived / 1000.0, 3);
                                    var beforeupload = Math.Round(card.GetIPv4Statistics().BytesSent / 1000.0, 3);
                                    Thread.Sleep(1000);
                                    var afterdownload = Math.Round(card.GetIPv4Statistics().BytesReceived / 1000.0, 3);
                                    var afterupload = Math.Round(card.GetIPv4Statistics().BytesSent / 1000.0, 3);
                                    stats.Download = afterdownload - beforedownload;
                                    stats.Upload = afterupload - beforeupload;
                                    stats.CreationDate = DateTime.UtcNow;
                                    stats.InterfaceName = card.Name;
                                    string hostNames = Dns.GetHostName();
                                    string myIPs = Dns.GetHostByName(hostNames).AddressList[0].ToString();
                                    stats.ServerIp = System.Environment.MachineName.ToString();
                                    float utilization = (float)((stats.Upload + stats.Download) / ((card.Speed / 8) / 1000.0)) * 100;
                                    stats.TotalUsage = Math.Round(utilization, 3);
                                    stats.ServerName = Name;
                                    stats.IpAddress = ip.Address.ToString();
                                    Console.Out.WriteLine("\n" + ip.Address.ToString() + "|  " + card.Description.ToString() + "\n" + "OpeartionalStatus:" + card.OperationalStatus + "\n" + "speed:" + card.Speed + "\n" + "Name" + card.Name + "\n" + "NetworkInterfaceType:" + card.NetworkInterfaceType);
                                    Console.WriteLine("Download:{0}, Upload:{1}, TotalUsage{2}", stats.Download, stats.Upload, stats.TotalUsage);
                                    lst.Add(Name);
                                    Lststats.Add(stats);
                                }
                            }
                            //Console.Out.WriteLine(ip.Address.ToString() + "|" + card.Description.ToString());
                            //stats.IpAddress = ip.Address.ToString();
                        }

                    }
                }

            }
            return Lststats;
        }
        private NetworkStatistic getNetworkUtilization(string networkCard)
        {
            NetworkStatistic stats = new NetworkStatistic();
            const int numberOfIterations = 10;

            PerformanceCounter bandwidthCounter = new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard);
            float bandwidth = bandwidthCounter.NextValue();//valor fixo 10Mb/100Mn/

            PerformanceCounter dataSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);

            PerformanceCounter dataReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkCard);

            float sendSum = 0;
            float receiveSum = 0;

            for (int index = 0; index < numberOfIterations; index++)
            {
                sendSum += dataSentCounter.NextValue();
                receiveSum += dataReceivedCounter.NextValue();
            }
            float dataSent = sendSum;
            float dataReceived = receiveSum;
            stats.Download = Math.Round(dataReceived / 1000.0, 3);
            stats.Upload = Math.Round(dataSent / 1000.0, 3);
            stats.CreationDate = DateTime.UtcNow;

            double utilization = (8 * (dataSent + dataReceived)) / (bandwidth * numberOfIterations) * 100;
            stats.TotalUsage = utilization;
            stats.InterfaceName = networkCard + " " + ((bandwidth / 1000.0) / 1000.0 / 1000.0).ToString() + "GB";

            NetworkInterface[] networkCards = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface card in networkCards)
            {
                var ipProps = card.GetIPProperties();
                if (card.Name == "Local Area Connection")
                {
                    string networkIp = networkCard;
                    networkIp = networkIp.Replace("[", "(");
                    networkIp = networkIp.Replace("]", ")");

                    if (card.Description == networkIp)
                    {
                        foreach (var ip in ipProps.UnicastAddresses)
                        {
                            if ((ip.Address.AddressFamily == AddressFamily.InterNetwork))
                            {
                                Console.Out.WriteLine(ip.Address.ToString() + "|" + card.Description.ToString());
                                stats.IpAddress = ip.Address.ToString();
                            }
                        }
                    }

                }
            }
            return stats;
        }
 public virtual NetworkStatistic DeleteNetworkStatistic(NetworkStatistic entity)
 {
     this.DeleteNetworkStatistic(entity.NetworkStatisticId);
     return entity;
 }
 public virtual NetworkStatistic UpdateNetworkStatistic(NetworkStatistic entity)
 {
     if (entity.IsTransient()) return entity;
     NetworkStatistic other = GetNetworkStatistic(entity.NetworkStatisticId);
     if (entity.Equals(other)) return entity;
     string sql=@"Update NetworkStatistic set  [InterfaceName]=@InterfaceName
                     , [IPAddress]=@IPAddress
                     , [TotalUsage]=@TotalUsage
                     , [Download]=@Download
                     , [Upload]=@Upload
                     , [CreationDate]=@CreationDate
                     , [ServerIp]=@ServerIp
                     , [ServerName]=@ServerName
                      where NetworkStatisticId=@NetworkStatisticId";
     SqlParameter[] parameterArray=new SqlParameter[]{
              new SqlParameter("@InterfaceName",entity.InterfaceName)
             , new SqlParameter("@IPAddress",entity.IpAddress)
             , new SqlParameter("@TotalUsage",entity.TotalUsage)
             , new SqlParameter("@Download",entity.Download)
             , new SqlParameter("@Upload",entity.Upload)
             , new SqlParameter("@CreationDate",entity.CreationDate)
             , new SqlParameter("@ServerIp",entity.ServerIp ?? (object)DBNull.Value)
             , new SqlParameter("@ServerName",entity.ServerName ?? (object)DBNull.Value)
             , new SqlParameter("@NetworkStatisticId",entity.NetworkStatisticId)};
     SqlHelper.ExecuteNonQuery(this.ConnectionString,CommandType.Text,sql,parameterArray);
     return GetNetworkStatistic(entity.NetworkStatisticId);
 }
 public virtual NetworkStatistic NetworkStatisticFromDataRow(DataRow dr)
 {
     if(dr==null) return null;
     NetworkStatistic entity=new NetworkStatistic();
     if (dr.Table.Columns.Contains("NetworkStatisticId"))
     {
     entity.NetworkStatisticId = (System.Int32)dr["NetworkStatisticId"];
     }
     if (dr.Table.Columns.Contains("InterfaceName"))
     {
     entity.InterfaceName = dr["InterfaceName"].ToString();
     }
     if (dr.Table.Columns.Contains("IPAddress"))
     {
     entity.IpAddress = dr["IPAddress"].ToString();
     }
     if (dr.Table.Columns.Contains("TotalUsage"))
     {
     entity.TotalUsage = (System.Double)dr["TotalUsage"];
     }
     if (dr.Table.Columns.Contains("Download"))
     {
     entity.Download = (System.Double)dr["Download"];
     }
     if (dr.Table.Columns.Contains("Upload"))
     {
     entity.Upload = (System.Double)dr["Upload"];
     }
     if (dr.Table.Columns.Contains("CreationDate"))
     {
     entity.CreationDate = (System.DateTime)dr["CreationDate"];
     }
     if (dr.Table.Columns.Contains("ServerIp"))
     {
     entity.ServerIp = dr["ServerIp"].ToString();
     }
     if (dr.Table.Columns.Contains("ServerName"))
     {
     entity.ServerName = dr["ServerName"].ToString();
     }
     return entity;
 }
 public virtual NetworkStatistic DeleteNetworkStatistic(NetworkStatistic entity)
 {
     this.DeleteNetworkStatistic(entity.NetworkStatisticId);
     return(entity);
 }