最近のLinuxはデフォルトではnet-toolsのコマンド(ifconfig、netstat等)はインストールされておらず、代わりにiproute2(ip、ss等)の使用を推奨していたりする。多くの先人の方がnet-toolsからiproute2へ移行する人向けにコマンド対応の一覧まとめて展開してくれているので大抵のコマンドは困らないで済む(ありがたや)。
対応するコマンドがない…
だけど、1つだけどんなに調べても対応するコマンドが見つからない。
それは、ネットワーク統計を表示するnetstat -s だ。
(いや、あるよって方がいたら教えてください)
先人の方々がまとめた記事を見ると、”ss -s”を代替コマンドとしている方が多いのだが実際のコマンド結果を見ると欲しいのはソレじゃない感スゴイ。(私だけ?)
netstat -sの表示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
[user@localhost ~]$ netstat -s Ip: 6027 total packets received 0 forwarded 0 incoming packets discarded 5464 incoming packets delivered 2966 requests sent out Icmp: 0 ICMP messages received 0 input ICMP message failed. ICMP input histogram: 0 ICMP messages sent 0 ICMP messages failed ICMP output histogram: Tcp: 26 active connections openings 1 passive connection openings 0 failed connection attempts 0 connection resets received 1 connections established 5432 segments received 3024 segments send out 1 segments retransmited 0 bad segments received. 0 resets sent Udp: 32 packets received 0 packets to unknown port received. 0 packet receive errors 32 packets sent 0 receive buffer errors 0 send buffer errors UdpLite: TcpExt: 24 TCP sockets finished time wait in fast timer 39 delayed acks sent 1 packets directly queued to recvmsg prequeue. 1547 packet headers predicted 1601 acknowledgments not containing data payload received 1243 predicted acknowledgments 1 congestion windows recovered without slow start after partial ack 1 other TCP timeouts TCPRcvCoalesce: 2 TCPSynRetrans: 1 TCPOrigDataSent: 2843 IpExt: InNoRoutes: 1 InBcastPkts: 562 InOctets: 603244 OutOctets: 699661 InBcastOctets: 103309 InNoECTPkts: 6032 |
ss -sの結果
これは、いくらなんでも端折りではないだろうか…
そもそも、ss -s はヘルプをみるとソケットのサマリーと書いてあってネットワークの統計とは言ってない。
無ければ作る
net-toolsをインストールすれば済む話ですが、それは何だか負けた気がするので自分で代わりのコマンド(シェルスクリプト)を作ってみた。
netstat-s.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
[user@localhost ~]$ cat netstat-s.sh #!/bin/sh TMPFILE=`mktemp` INDENT=' ' trap 'rm -f $TMPFILE;exit' 2 function SHAPING_PSTAT () { KINDS=(`cut -d ":" -f1 $1 | awk '!tmp[$0]++'`) for entry in `seq 1 ${#KINDS[@]}` do ENT=(`grep "${KINDS[$entry-1]}:" $1 | sed -e s/"${KINDS[$entry-1]}:"//g | head -n 1`) VAL=(`grep "${KINDS[$entry-1]}:" $1 | sed -e s/"${KINDS[$entry-1]}:"//g | tail -n 1`) printf "${KINDS[$entry-1]}:\n" for index in `seq 1 ${#ENT[@]}` do printf "$INDENT%-30s%-20d\n" ${ENT[$index-1]} ${VAL[$index-1]} done done } cat /proc/net/snmp > $TMPFILE SHAPING_PSTAT $TMPFILE cat /proc/net/netstat > $TMPFILE SHAPING_PSTAT $TMPFILE rm -f $TMPFILE |
表示結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
[user@localhost ~]$ ./netstat-s.sh Ip: Forwarding 2 DefaultTTL 64 InReceives 6339 InHdrErrors 0 InAddrErrors 0 ForwDatagrams 0 InUnknownProtos 0 InDiscards 0 InDelivers 5613 OutRequests 3052 OutDiscards 0 OutNoRoutes 0 ReasmTimeout 0 ReasmReqds 0 ReasmOKs 0 ReasmFails 0 FragOKs 0 FragFails 0 FragCreates 0 Icmp: InMsgs 0 InErrors 0 InCsumErrors 0 InDestUnreachs 0 InTimeExcds 0 InParmProbs 0 InSrcQuenchs 0 InRedirects 0 InEchos 0 InEchoReps 0 InTimestamps 0 InTimestampReps 0 InAddrMasks 0 InAddrMaskReps 0 OutMsgs 0 OutErrors 0 OutDestUnreachs 0 OutTimeExcds 0 OutParmProbs 0 OutSrcQuenchs 0 OutRedirects 0 OutEchos 0 OutEchoReps 0 OutTimestamps 0 OutTimestampReps 0 OutAddrMasks 0 OutAddrMaskReps 0 Tcp: RtoAlgorithm 1 RtoMin 200 RtoMax 120000 MaxConn -1 ActiveOpens 26 PassiveOpens 1 AttemptFails 0 EstabResets 0 CurrEstab 1 InSegs 5581 OutSegs 3112 RetransSegs 1 InErrs 0 OutRsts 0 InCsumErrors 0 Udp: InDatagrams 32 NoPorts 0 InErrors 0 OutDatagrams 32 RcvbufErrors 0 SndbufErrors 0 InCsumErrors 0 UdpLite: InDatagrams 0 NoPorts 0 InErrors 0 OutDatagrams 0 RcvbufErrors 0 SndbufErrors 0 InCsumErrors 0 TcpExt: SyncookiesSent 0 SyncookiesRecv 0 SyncookiesFailed 0 EmbryonicRsts 0 PruneCalled 0 RcvPruned 0 OfoPruned 0 OutOfWindowIcmps 0 LockDroppedIcmps 0 ArpFilter 0 TW 24 TWRecycled 0 TWKilled 0 PAWSPassive 0 PAWSActive 0 PAWSEstab 0 DelayedACKs 43 DelayedACKLocked 1 DelayedACKLost 0 ListenOverflows 0 ListenDrops 0 TCPPrequeued 1 TCPDirectCopyFromBacklog 0 TCPDirectCopyFromPrequeue 0 TCPPrequeueDropped 0 TCPHPHits 1599 TCPHPHitsToUser 0 TCPPureAcks 1638 TCPHPAcks 1288 TCPRenoRecovery 0 TCPSackRecovery 0 TCPSACKReneging 0 TCPFACKReorder 0 TCPSACKReorder 0 TCPRenoReorder 0 TCPTSReorder 0 TCPFullUndo 0 TCPPartialUndo 0 TCPDSACKUndo 0 TCPLossUndo 1 TCPLostRetransmit 0 TCPRenoFailures 0 TCPSackFailures 0 TCPLossFailures 0 TCPFastRetrans 0 TCPForwardRetrans 0 TCPSlowStartRetrans 0 TCPTimeouts 1 TCPLossProbes 0 TCPLossProbeRecovery 0 TCPRenoRecoveryFail 0 TCPSackRecoveryFail 0 TCPSchedulerFailed 0 TCPRcvCollapsed 0 TCPDSACKOldSent 0 TCPDSACKOfoSent 0 TCPDSACKRecv 0 TCPDSACKOfoRecv 0 TCPAbortOnData 0 TCPAbortOnClose 0 TCPAbortOnMemory 0 TCPAbortOnTimeout 0 TCPAbortOnLinger 0 TCPAbortFailed 0 TCPMemoryPressures 0 TCPSACKDiscard 0 TCPDSACKIgnoredOld 0 TCPDSACKIgnoredNoUndo 0 TCPSpuriousRTOs 0 TCPMD5NotFound 0 TCPMD5Unexpected 0 TCPSackShifted 0 TCPSackMerged 0 TCPSackShiftFallback 0 TCPBacklogDrop 0 TCPMinTTLDrop 0 TCPDeferAcceptDrop 0 IPReversePathFilter 0 TCPTimeWaitOverflow 0 TCPReqQFullDoCookies 0 TCPReqQFullDrop 0 TCPRetransFail 0 TCPRcvCoalesce 2 TCPOFOQueue 0 TCPOFODrop 0 TCPOFOMerge 0 TCPChallengeACK 0 TCPSYNChallenge 0 TCPFastOpenActive 0 TCPFastOpenActiveFail 0 TCPFastOpenPassive 0 TCPFastOpenPassiveFail 0 TCPFastOpenListenOverflow 0 TCPFastOpenCookieReqd 0 TCPSpuriousRtxHostQueues 0 BusyPollRxPackets 0 TCPAutoCorking 0 TCPFromZeroWindowAdv 0 TCPToZeroWindowAdv 0 TCPWantZeroWindowAdv 0 TCPSynRetrans 1 TCPOrigDataSent 2925 TCPHystartTrainDetect 0 TCPHystartTrainCwnd 0 TCPHystartDelayDetect 0 TCPHystartDelayCwnd 0 TCPACKSkippedSynRecv 0 TCPACKSkippedPAWS 0 TCPACKSkippedSeq 0 TCPACKSkippedFinWait2 0 TCPACKSkippedTimeWait 0 TCPACKSkippedChallenge 0 IpExt: InNoRoutes 1 InTruncatedPkts 0 InMcastPkts 0 OutMcastPkts 0 InBcastPkts 725 OutBcastPkts 0 InOctets 647580 OutOctets 720269 InMcastOctets 0 OutMcastOctets 0 InBcastOctets 133265 OutBcastOctets 0 InCsumErrors 0 InNoECTPkts 6355 InECT1Pkts 0 InECT0Pkts 0 InCEPkts 0 |
単に/proc/net/snmpと/proc/net/netstatを整形して表示するだけ。
IPv6系は/proc/net/snmp6を直接catで覗けばよい。
[参考]
逆引きシェルスクリプト/スペース区切りの文字列を配列に格納する方法 – Linuxと過ごす
ソートしないで重複行を削除する – Qiita