æ¦è¦
netstat
ãss
ã³ãã³ãã«ããããããã¹ãã¨ä»ã®ãã¹ãã¨ã®ã³ãã¯ã·ã§ã³ãä¸è¦§è¡¨ç¤ºã§ãããããããWebã·ã¹ãã ã®å ´åãã¯ã©ã¤ã¢ã³ãã並è¡æ¥ç¶ããããã åä¸ãã¹ãããè¤æ°ã®ãã¼ããä»ãã¦ã³ãã¯ã·ã§ã³ã確ç«ãã¦ããã±ã¼ã¹ãå¤ããã³ãã¯ã·ã§ã³æ°ã大ããå ´åã¯ã1ä¸ä»¥ä¸ã®ã³ãã¯ã·ã§ã³ã表示ããããã¹ãéã®ã³ãã¯ã·ã§ã³ç¶æ³ã人éã®ç®ã§æ¦è¦³ãããã¨ãé£ããã£ãã
ããã§ãåä¸ãã¹ãã¨ã®ã³ãã¯ã·ã§ã³ãéç´è¡¨ç¤ºããã³ãã¯ã·ã§ã³ç¶æ³ãæ¦è¦³ãã ãlstfã (ãããããã¦ãã¼ããµã)ã³ãã³ããã¤ãã£ãã
lstfã®ç¹å¾´ã¯ä»¥ä¸ã®éãã
- ã³ãã³ãå®è¡ãã¹ããèµ·ç¹ã«ãactive openã³ãã¯ã·ã§ã³ãpassive openã³ãã¯ã·ã§ã³ãå¤å®ãããã¤ã¾ããæ¥ç¶ãããå´ããããå´ããå¤å®ããã
- åãã¹ãããã¼ãã¨ã«ã³ãã¯ã·ã§ã³æ°ã表示ãã
- Goã§å®è£ ããã¦ãããã¼ã¿ããªãã£ãi386ãã¤ããªã§ããã°ãCentOS5ã§ãåä½ããã*1
- JSONãµãã¼ã
å®è¡çµæ
ã³ãã³ãå®è¡çµæãã¿ã¦ã¿ããã
$ lstf -n Local Address:Port <--> Peer Address:Port Connections 10.0.1.9:many --> 10.0.1.10:3306 22 10.0.1.9:many --> 10.0.1.11:3306 14 10.0.2.10:22 <-- 192.168.10.10:many 1 10.0.1.9:80 <-- 10.0.2.13:many 120 10.0.1.9:80 <-- 10.0.2.14:many 202
ãã¼ã«ã«ãã¹ã(10.0.1.9)ã¯Webãµã¼ãã§ãã¼ã80çªã§å¾
ã¡åãã¦ããã10.0.2.13ã¨10.0.2.14ããHTTPãªã¯ã¨ã¹ããåãä»ãã¦ãããã¨ãããããéã«ãã¼ã«ã«ãã¹ããã10.0.1.10ã¨10.0.1.11ã®MySQLãµã¼ãã®ãã¼ã3306çªã¸æ¥ç¶ãã¦ãããã¨ããããããã¹ãå士ã®æ¥ç¶ç¶æ³ãç¥ãããã ãã§ããã°ãã¯ã©ã¤ã¢ã³ãã½ã±ãããå©ç¨ãããã¼ãçªå·ã表示ããæå³ã¯ãªããããmany
ã¨ãã¦éç´ãã¦ããã
--json
ãªãã·ã§ã³ã§JSON表示ãã§ãããããä»ã®ãã¼ã«ã¨é£æºããå©ç¨ã§ããã
$ lstf -n --json | jq -r -M '.' [ { "direction": "active", "local": { "Addr": "localhost", "Port": "many" }, "peer": { "addr": "10.0.100.1", "port": "3306" }, "connections": 20 }, { "direction": "passive", "local": { "addr": "localhost", "port": "80" }, "peer": { "addr": "10.0.200.1", "port": "many" }, "connections": 27 }, ... ]
å®è£
/proc/net/tcp
ããã¼ã¹ããé¨åã¯ãGitHub - shirou/gopsutil: psutil for golang ã使ããã¦ããã£ã¦ãã
active openãpassive openãã®å¤å®ã¯ç°¡åã§ãã³ãã³ãå®è¡ãã¹ãã®LISTENãã¼ãã«å¯¾ããã³ãã¯ã·ã§ã³ãpassive openããã以å¤ãactive openã¨ãã¦ããã *2
ãã¾ã
Linuxã®netfilterã«ã¯ãconntrackã¨ããL4ã®ã³ãã¯ã·ã§ã³ããã¼ã«é¢ãããã±ããã追跡ããæ©æ§ãããã(/proc/net/ipconntrack
, /proc/net/nfconntrack
ããdumpçµæãèªãã) ãããå©ç¨ãã¦ãåãããã«ã³ãã¯ã·ã§ã³ããã¼ãéç´è¡¨ç¤ºãããã¼ã«ãä½ã£ã¦ã¿ããgithub.com
inboundã¨outboundã¨ããããã«ã¤ãã¦ãåãã±ããã®ãµã¤ãºãåè¨ããconntrack tableã«åå¨ããããã¼ãã¨ã®ãã©ãã£ãã¯éã¨ãã±ããæ°ã表示ã§ããã¨ããã¡ãªããã¯ããã ããããã³ãã¯ã·ã§ã³æ°ã大ãããã¹ãã§ã¯conntrack tableããµããé¿ããããã«ãconntrackãç¡å¹ã«ãã¦ãããããã©ã®ãã¹ãã§ã使ããããã§ã¯ãªãã£ãã
distributed black-box tracingãããããã®é¨åã¥ãããæè¿ãã£ã¦ãã
追è¨
Linuxãµã¼ãä¸ã§ãã¹ãéã³ãã¯ã·ã§ã³ãéç´è¡¨ç¤ºãããã¼ã« lstf ãã¤ãã£ã - ããããã¡ã¢b.hatena.ne.jp
- [golang]
ã¨ã¦ãè¯ããããã³ãã¯ã·ã§ã³æ°ãå¤ãå ´å㯠ss ã®ããã« netlink çµç±ã®ã»ããè¯ãã¨æããã©ããªãã ã
2018/03/25 16:57
ã³ãã³ãå®è¡é度ã®è¦³ç¹ã§è¨ãã°ã/proc/net/tcp
ããã¼ã¹ãããããnetlinkã®ã»ããæå©ãªããã§ãã*3
GitHub - vishvananda/netlink: Simple netlink library for go. ã使ãã°ãããã¨ç°¡åã«netlink対å¿ã§ãããã§ãããã¬ã¬ã·ã¼OSã«å¯¾å¿ããªããã°ãããªãäºæ
*4ã§ãã¼ã¿ããªãã£ã®ç¢ºä¿ãåªå
ãããã¡ã¤ã«ããã¼ã¹ããå®è£
ãé¸ãã§ãã¾ãã
*1:CentOS5ã§ãå¤ããã¼ã¸ã§ã³ã§ã¯åä½ããªã
*2:https://github.com/yuuki/lstf/blob/d65651e9c3d1956aa9db88812acb6199a2473235/tcpflow/tcpflow.go#L130-L142
*3:åè:netstatコマンドを高速化する - Qiita posix_fadvise(2)ã§POSIX_FADV_SEQUENTIALã使ãã°ããã¡ããread(2)ã®åæ°æ¸ãããªãããªã¨æã£ããã©ããããè¾¼ã¿ã§ãã¼ã¸ãµã¤ãºåºå®ã¨ãããã¨ãããããªãã
*4:åå調ã¹ã«ããã¨netlinkããã±ã¼ã¸ã¯CentOS5ã§ãµãã¼ãããã¦ããªãã·ã¹ãã ã³ã¼ã«ãªãã·ã§ã³ã使ã£ã¦ãããGoå¦çç³»ãã®ãã®ãCentOS5ããµãã¼ããã¦ããªããããä»æ¹ããªããcgoä¾åã¯æé¤ããã¦ããæ§åãEliminate cgo from netlink. by hugelgupf · Pull Request #308 · vishvananda/netlink · GitHub