@@ -13,6 +13,7 @@ import (
1313 "github.com/shirou/gopsutil/v3/disk"
1414 "github.com/shirou/gopsutil/v3/host"
1515 "github.com/shirou/gopsutil/v3/mem"
16+ "github.com/shirou/gopsutil/v3/net"
1617)
1718
1819const (
@@ -31,7 +32,7 @@ func formatBytes(bytes uint64, unit string) string {
3132 }
3233}
3334
34- // TODO: Add support for multi server management
35+ // TODO: Add support for multi server management
3536// solution: create a bridge between the gopsutil and the ssh client
3637func (m * DashboardMonitor ) GetSystemStats () {
3738 osType , err := m .getCommandOutput ("uname -s" )
@@ -48,6 +49,7 @@ func (m *DashboardMonitor) GetSystemStats() {
4849 Memory : MemoryStats {},
4950 Load : LoadStats {},
5051 Disk : DiskStats {AllMounts : []DiskMount {}},
52+ Network : NetworkStats {Interfaces : []NetworkInterface {}},
5153 }
5254
5355 if hostname , err := m .getCommandOutput ("hostname" ); err == nil {
@@ -129,6 +131,8 @@ func (m *DashboardMonitor) GetSystemStats() {
129131 stats .Disk = diskStats
130132 }
131133
134+ stats .Network = m .getNetworkStats ()
135+
132136 m .Broadcast (string (GetSystemStats ), stats )
133137}
134138
@@ -182,6 +186,44 @@ func (m *DashboardMonitor) getCPUStats() CPUStats {
182186 return cpuStats
183187}
184188
189+ func (m * DashboardMonitor ) getNetworkStats () NetworkStats {
190+ networkStats := NetworkStats {
191+ Interfaces : []NetworkInterface {},
192+ }
193+
194+ if ioCounters , err := net .IOCounters (true ); err == nil {
195+ var totalSent , totalRecv , totalPacketsSent , totalPacketsRecv uint64
196+
197+ for _ , counter := range ioCounters {
198+ interfaces := NetworkInterface {
199+ Name : counter .Name ,
200+ BytesSent : counter .BytesSent ,
201+ BytesRecv : counter .BytesRecv ,
202+ PacketsSent : counter .PacketsSent ,
203+ PacketsRecv : counter .PacketsRecv ,
204+ ErrorIn : counter .Errin ,
205+ ErrorOut : counter .Errout ,
206+ DropIn : counter .Dropin ,
207+ DropOut : counter .Dropout ,
208+ }
209+
210+ networkStats .Interfaces = append (networkStats .Interfaces , interfaces )
211+
212+ totalSent += counter .BytesSent
213+ totalRecv += counter .BytesRecv
214+ totalPacketsSent += counter .PacketsSent
215+ totalPacketsRecv += counter .PacketsRecv
216+ }
217+
218+ networkStats .TotalBytesSent = totalSent
219+ networkStats .TotalBytesRecv = totalRecv
220+ networkStats .TotalPacketsSent = totalPacketsSent
221+ networkStats .TotalPacketsRecv = totalPacketsRecv
222+ }
223+
224+ return networkStats
225+ }
226+
185227func (m * DashboardMonitor ) getCommandOutput (cmd string ) (string , error ) {
186228 session , err := m .client .NewSession ()
187229 if err != nil {
0 commit comments