Linux下,如何監控某個進程到底向哪一個地址發起了網絡調用

Linux下,如何監控某個進程到底向哪一個地址發起了網絡調用

有時候,有些應用,好比idea,你發起某個操做時,其底層會去請求網絡,獲取一些數據。java

可是不知道,請求了什麼地址。舉個例子,在idea中,maven管理的java工程,你在reimport的時候,確定會去下載依賴,可是,有時候感受配置的私服沒生效,好像,請求仍是去maven中央倉庫下載。linux

怎麼肯定是否是真的去了maven中央倉庫下載呢?有證據嗎?spring

此時有什麼好辦法呢?若是不用點什麼工具,還真沒什麼好辦法。shell

再好比,我這裏有一個linux上運行的java程序,我調用某個接口的時候,其會請求某個服務,可是如今,日誌裏沒記錄調用日誌。那怎麼知道是代碼沒走到,仍是調用了呢,若是調用了,真實請求的地址是哪裏呢?網絡

netstat:比較簡陋的方式

通常,一個程序剛調用完遠程服務時,其socket的狀態爲time_wait,此時,和請求以前比,看看發生了什麼變化。多線程

netstat -antp |grep pid

好比,請求前,我java應用的網絡鏈接以下:socket

請求後,網絡鏈接以下:maven

pid和進程id的關係

通常進程id,假設爲10095,由於這是個java進程,是多線程的,咱們使用查看其內部的線程:ide

top -H -p 10095
PID   USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND   
19823 root      20   0 9999.0m   1.5g  14632 S  1.7  9.5  67:25.37 java 
10215 root      20   0 9999.0m   1.5g  14632 S  0.3  9.5   1:21.68 java    
10095 root      20   0 9999.0m   1.5g  14632 S  0.0  9.5   0:00.00 java

發現其中,真就有一個線程id是10095。函數

這裏的規律是,假設進程id爲A,那麼線程中,也有一個id相同的線程。

strace使用說明

概述

該命令,主要監控一個進程在運行過程當中,發起了哪些系統調用;若是收到其餘進程發送的signal,也能夠監控。

功能很是強大,由於不管是網絡發送,監聽,請求,都是調用操做系統的函數,好比listen、connect等;所以,能夠監控網絡調用,能夠將方法調用時的入參、出參、花費時間等,都打印出來,或者打印到文件。

同時,能夠監控文件,好比:

open("/dev/null", O_RDONLY) = 3

這個就表示,調用open函數,參數爲/dev/null,O_RDONLY,返回值爲3.

查看進程的文件相關的系統調用

strace -t -p 10095 -q -f -s 10000 -e trace=file

-t: 打印調用時間

-p : 指定pid

-q : 壓制attach、detach的消息,否則的話,attach到進程時,會打印一句提示;結束的時候,也會打印一堆提示。

-f: 不僅是跟蹤當前進程id對應的線程id,還要跟蹤該pid內的所有線程,對於java這類多線程程序,尤爲有用

-s strsize

Specify the maximum string size to print (the default is 32). Note that filenames are not considered strings and are always printed in full.

該參數的做用不便描述,能夠看後面的例子。

-e trace=file

監控文件相關的系統調用

監控線程相關操做

-e trace=process

Trace all system calls which involve process management. This is useful for watching the fork, wait, and exec steps of a process.

監控網絡請求

strace -p 10095 -q -f -e trace=network

效果以下:

若是要看到具體信息:

strace -p 10095 -q -f  -s 10000 -e trace=network

如今就能夠看到了。

得到比平時,更清晰的信息:

[root@localhost shell]# strace -e network telnet 10.15.4.46 8080
Trying 10.15.4.46...
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
setsockopt(3, SOL_IP, IP_TOS, [16], 4)  = 0
connect(3, {sa_family=AF_INET, sin_port=htons(8080), sin_addr=inet_addr("10.15.4.46")}, 16) = -1 ECONNREFUSED (Connection refused)
telnet: connect to address 10.15.4.46: Connection refused
+++ exited with 1 +++

查看文件描述符相關調用

-T:輸出每一個調用花費的時間

strace -p 10095 -q -f  -s 10000 -e trace=desc -T

能夠看到,是epoll這類文件描述符相關調用。

將結果寫到文件

-o filename

Write the trace output to the file filename rather than to stderr. Use filename.pid if -ff is used. If the argument begins with '|' or with '!' then the rest of the argument is treated as a command and all output is piped to it. This is convenient for piping the debugging output to a program without affecting the redirections of executed programs.

strace -p 10095 -q -f  -s 10000 -e trace=desc -v -o abc.txt

每一個線程,生成一個文件

-ff

If the -o filename option is in effect, each processes trace is written to filename.pid where pid is the numeric process id of each process. This is incompatible with -c, since no per-process counts are kept.

strace -p 10095 -q -f  -s 10000 -e trace=desc -ff -v -o abc.txt

效果以下:

-rw-r--r--.  1 root root    0 Jun 16 14:06 abc.txt.23309
-rw-r--r--.  1 root root    0 Jun 16 14:06 abc.txt.23379
-rw-r--r--.  1 root root    0 Jun 16 14:06 abc.txt.23380
-rw-r--r--.  1 root root    0 Jun 16 14:06 abc.txt.23381
-rw-r--r--.  1 root root    0 Jun 16 14:06 abc.txt.23745
-rw-r--r--.  1 root root    0 Jun 16 14:06 abc.txt.9479
-rw-r--r--.  1 root root    0 Jun 16 14:06 abc.txt.9480

統計一段時間內的系統調用

[root@localhost CAD_OneKeyDeploy]# strace -p 10095 -q -f  -s 10000 -e trace=desc -c
^C% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 99.99    0.662317       22077        30           epoll_wait
  0.01    0.000063          15         4           poll
  0.00    0.000023          11         2           ioctl
------ ----------- ----------- --------- --------- ----------------
100.00    0.662403                    36           total
[root@localhost CAD_OneKeyDeploy]# strace -p 10095 -q -f  -s 10000 -e trace=network -c
^C% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 73.33    0.000044          22         2           sendto
 26.67    0.000016           8         2           recvfrom
------ ----------- ----------- --------- --------- ----------------
100.00    0.000060                     4           total

用strace啓動命令

strace java -Dspring.profiles.active=peer1 -jar /home/ceiec/jars/xxx.jar
只打印咱們關注的調用:open
strace -e trace=open java -Dspring.profiles.active=peer1 -jar /home/ceiec/jars/xxx.jar

參考

https://blog.51cto.com/liuleis/1784305

相關文章
相關標籤/搜索