netstat.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. )
  8. type NetStat struct {
  9. // TcpExt
  10. SyncookiesSent uint64 `json:"syncookie_sent"`
  11. SyncookiesRecv uint64 `json:"syncookies_recv"`
  12. SyncookiesFailed uint64 `json:"syncookies_failed"`
  13. EmbryonicRsts uint64 `json:"embryonic_rsts"`
  14. PruneCalled uint64 `json:"prune_called"`
  15. RcvPruned uint64 `json:"rcv_pruned"`
  16. OfoPruned uint64 `json:"ofo_pruned"`
  17. OutOfWindowIcmps uint64 `json:"out_of_window_icmps"`
  18. LockDroppedIcmps uint64 `json:"lock_dropped_icmps"`
  19. ArpFilter uint64 `json:"arp_filter"`
  20. TW uint64 `json:"tw"`
  21. TWRecycled uint64 `json:"tw_recycled"`
  22. TWKilled uint64 `json:"tw_killed"`
  23. PAWSPassive uint64 `json:"paws_passive"`
  24. PAWSActive uint64 `json:"paws_active"`
  25. PAWSEstab uint64 `json:"paws_estab"`
  26. DelayedACKs uint64 `json:"delayed_acks"`
  27. DelayedACKLocked uint64 `json:"delayed_ack_locked"`
  28. DelayedACKLost uint64 `json:"delayed_ack_lost"`
  29. ListenOverflows uint64 `json:"listen_overflows"`
  30. ListenDrops uint64 `json:"listen_drops"`
  31. TCPPrequeued uint64 `json:"tcp_prequeued"`
  32. TCPDirectCopyFromBacklog uint64 `json:"tcp_direct_copy_from_backlog"`
  33. TCPDirectCopyFromPrequeue uint64 `json:"tcp_direct_copy_from_prequeue"`
  34. TCPPrequeueDropped uint64 `json:"tcp_prequeue_dropped"`
  35. TCPHPHits uint64 `json:"tcp_hp_hits"`
  36. TCPHPHitsToUser uint64 `json:"tcp_hp_hits_to_user"`
  37. TCPPureAcks uint64 `json:"tcp_pure_acks"`
  38. TCPHPAcks uint64 `json:"tcp_hp_acks"`
  39. TCPRenoRecovery uint64 `json:"tcp_reno_recovery"`
  40. TCPSackRecovery uint64 `json:"tcp_sack_recovery"`
  41. TCPSACKReneging uint64 `json:"tcp_sack_reneging"`
  42. TCPFACKReorder uint64 `json:"tcp_fack_reorder"`
  43. TCPSACKReorder uint64 `json:"tcp_sack_reorder"`
  44. TCPRenoReorder uint64 `json:"tcp_reno_reorder"`
  45. TCPTSReorder uint64 `json:"tcp_ts_reorder"`
  46. TCPFullUndo uint64 `json:"tcp_full_undo"`
  47. TCPPartialUndo uint64 `json:"tcp_partial_undo"`
  48. TCPDSACKUndo uint64 `json:"tcp_dsack_undo"`
  49. TCPLossUndo uint64 `json:"tcp_loss_undo"`
  50. TCPLoss uint64 `json:"tcp_loss"`
  51. TCPLostRetransmit uint64 `json:"tcp_lost_retransmit"`
  52. TCPRenoFailures uint64 `json:"tcp_reno_failures"`
  53. TCPSackFailures uint64 `json:"tcp_sack_failures"`
  54. TCPLossFailures uint64 `json:"tcp_loss_failures"`
  55. TCPFastRetrans uint64 `json:"tcp_fast_retrans"`
  56. TCPForwardRetrans uint64 `json:"tcp_forward_retrans"`
  57. TCPSlowStartRetrans uint64 `json:"tcp_slow_start_retrans"`
  58. TCPTimeouts uint64 `json:"tcp_timeouts"`
  59. TCPLossProbes uint64 `json:"tcp_loss_probes"`
  60. TCPLossProbeRecovery uint64 `json:"tcp_loss_probe_recovery"`
  61. TCPRenoRecoveryFail uint64 `json:"tcp_reno_recovery_fail"`
  62. TCPSackRecoveryFail uint64 `json:"tcp_sack_recovery_fail"`
  63. TCPSchedulerFailed uint64 `json:"tcp_scheduler_failed"`
  64. TCPRcvCollapsed uint64 `json:"tcp_rcv_collapsed"`
  65. TCPDSACKOldSent uint64 `json:"tcp_dsack_old_sent"`
  66. TCPDSACKOfoSent uint64 `json:"tcp_dsack_ofo_sent"`
  67. TCPDSACKRecv uint64 `json:"tcp_dsack_recv"`
  68. TCPDSACKOfoRecv uint64 `json:"tcp_dsack_ofo_recv"`
  69. TCPAbortOnSyn uint64 `json:"tcp_abort_on_syn"`
  70. TCPAbortOnData uint64 `json:"tcp_abort_on_data"`
  71. TCPAbortOnClose uint64 `json:"tcp_abort_on_close"`
  72. TCPAbortOnMemory uint64 `json:"tcp_abort_on_memory"`
  73. TCPAbortOnTimeout uint64 `json:"tcp_abort_on_timeout"`
  74. TCPAbortOnLinger uint64 `json:"tcp_abort_on_linger"`
  75. TCPAbortFailed uint64 `json:"tcp_abort_failed"`
  76. TCPMemoryPressures uint64 `json:"tcp_memory_pressures"`
  77. TCPSACKDiscard uint64 `json:"tcp_sack_discard"`
  78. TCPDSACKIgnoredOld uint64 `json:"tcp_dsack_ignored_old"`
  79. TCPDSACKIgnoredNoUndo uint64 `json:"tcp_dsack_ignored_no_undo"`
  80. TCPSpuriousRTOs uint64 `json:"tcp_spurious_rtos"`
  81. TCPMD5NotFound uint64 `json:"tcp_md5_not_found"`
  82. TCPMD5Unexpected uint64 `json:"tcp_md5_unexpected"`
  83. TCPSackShifted uint64 `json:"tcp_sack_shifted"`
  84. TCPSackMerged uint64 `json:"tcp_sack_merged"`
  85. TCPSackShiftFallback uint64 `json:"tcp_sack_shift_fallback"`
  86. TCPBacklogDrop uint64 `json:"tcp_backlog_drop"`
  87. TCPMinTTLDrop uint64 `json:"tcp_min_ttl_drop"`
  88. TCPDeferAcceptDrop uint64 `json:"tcp_defer_accept_drop"`
  89. IPReversePathFilter uint64 `json:"ip_reverse_path_filter"`
  90. TCPTimeWaitOverflow uint64 `json:"tcp_time_wait_overflow"`
  91. TCPReqQFullDoCookies uint64 `json:"tcp_req_q_full_do_cookies"`
  92. TCPReqQFullDrop uint64 `json:"tcp_req_q_full_drop"`
  93. TCPRetransFail uint64 `json:"tcp_retrans_fail"`
  94. TCPRcvCoalesce uint64 `json:"tcp_rcv_coalesce"`
  95. TCPOFOQueue uint64 `json:"tcp_ofo_drop"`
  96. TCPOFODrop uint64 `json:"tcp_ofo_drop"`
  97. TCPOFOMerge uint64 `json:"tcp_ofo_merge"`
  98. TCPChallengeACK uint64 `json:"tcp_challenge_ack"`
  99. TCPSYNChallenge uint64 `json:"tcp_syn_challenge"`
  100. TCPFastOpenActive uint64 `json:"tcp_fast_open_active"`
  101. TCPFastOpenActiveFail uint64 `json:"tcp_fast_open_active_fail"`
  102. TCPFastOpenPassive uint64 `json:"tcp_fast_open_passive"`
  103. TCPFastOpenPassiveFail uint64 `json:"tcp_fast_open_passive_fail"`
  104. TCPFastOpenListenOverflow uint64 `json:"tcp_fast_open_listen_overflow"`
  105. TCPFastOpenCookieReqd uint64 `json:"tcp_fast_open_cookie_reqd"`
  106. TCPSpuriousRtxHostQueues uint64 `json:"tcp_spurious_rtx_host_queues"`
  107. BusyPollRxPackets uint64 `json:"busy_poll_rx_packets"`
  108. TCPAutoCorking uint64 `json:"tcp_auto_corking"`
  109. TCPFromZeroWindowAdv uint64 `json:"tcp_from_zero_window_adv"`
  110. TCPToZeroWindowAdv uint64 `json:"tcp_to_zero_window_adv"`
  111. TCPWantZeroWindowAdv uint64 `json:"tcp_want_zero_window_adv"`
  112. TCPSynRetrans uint64 `json:"tcp_syn_retrans"`
  113. TCPOrigDataSent uint64 `json:"tcp_orig_data_sent"`
  114. // IpExt
  115. InNoRoutes uint64 `json:"in_no_routes"`
  116. InTruncatedPkts uint64 `json:"in_truncated_pkts"`
  117. InMcastPkts uint64 `json:"in_mcast_pkts"`
  118. OutMcastPkts uint64 `json:"out_mcast_pkts"`
  119. InBcastPkts uint64 `json:"in_bcast_pkts"`
  120. OutBcastPkts uint64 `json:"out_bcast_pkts"`
  121. InOctets uint64 `json:"in_octets"`
  122. OutOctets uint64 `json:"out_octets"`
  123. InMcastOctets uint64 `json:"in_mcast_octets"`
  124. OutMcastOctets uint64 `json:"out_mcast_octets"`
  125. InBcastOctets uint64 `json:"in_bcast_octets"`
  126. OutBcastOctets uint64 `json:"out_bcast_octets"`
  127. InCsumErrors uint64 `json:"in_csum_errors"`
  128. InNoECTPkts uint64 `json:"in_no_ect_pkts"`
  129. InECT1Pkts uint64 `json:"in_ect1_pkts"`
  130. InECT0Pkts uint64 `json:"in_ect0_pkts"`
  131. InCEPkts uint64 `json:"in_ce_pkts"`
  132. }
  133. func ReadNetStat(path string) (*NetStat, error) {
  134. data, err := ioutil.ReadFile(path)
  135. if err != nil {
  136. return nil, err
  137. }
  138. lines := strings.Split(string(data), "\n")
  139. // Maps a netstat metric to its value (i.e. SyncookiesSent --> 0)
  140. statMap := make(map[string]string)
  141. // patterns
  142. // TcpExt: SyncookiesSent SyncookiesRecv SyncookiesFailed... <-- header
  143. // TcpExt: 0 0 1764... <-- values
  144. for i := 1; i < len(lines); i = i + 2 {
  145. headers := strings.Fields(lines[i-1][strings.Index(lines[i-1], ":")+1:])
  146. values := strings.Fields(lines[i][strings.Index(lines[i], ":")+1:])
  147. for j, header := range headers {
  148. statMap[header] = values[j]
  149. }
  150. }
  151. var netstat NetStat = NetStat{}
  152. elem := reflect.ValueOf(&netstat).Elem()
  153. typeOfElem := elem.Type()
  154. for i := 0; i < elem.NumField(); i++ {
  155. if val, ok := statMap[typeOfElem.Field(i).Name]; ok {
  156. parsedVal, _ := strconv.ParseUint(val, 10, 64)
  157. elem.Field(i).SetUint(parsedVal)
  158. }
  159. }
  160. return &netstat, nil
  161. }