-
-
Save keyganker/da6f42eac2ba8b724f02003a7fbf7177 to your computer and use it in GitHub Desktop.
systemtap统计TCP连接数量
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/stap | |
global connections | |
global filter_port = 80 | |
// | |
global report | |
global spend_time | |
global active_count | |
global have_spend_time = 0 | |
global histidx = 0 | |
function on_close(saddr, sport, family){ | |
conn_times = connections[saddr, sport] | |
if (conn_times!=0) { | |
ms = gettimeofday_ms() - conn_times | |
spend_time <<< ms | |
have_spend_time = 1 | |
//printf("close %15s %5d ms=%d active_count=%d\n", format_ipaddr(ntohl(saddr), family), sport, ms, active_count) | |
delete connections[saddr, sport] | |
active_count-- | |
} | |
} | |
function on_open(saddr, sport, family){ | |
connections[saddr, sport] = gettimeofday_ms() | |
//printf("accept %15s %5d\n", format_ipaddr(ntohl(saddr), family), sport) | |
active_count++ | |
} | |
probe kernel.function("tcp_set_state"){ | |
sk = $sk | |
new_state = $state | |
TCP_ESTABLISHED = 1 | |
TCP_CLOSE = 7 | |
TCP_CLOSE_WAIT = 8 | |
if (tcpmib_local_port(sk)==filter_port){ | |
if (new_state==TCP_CLOSE || new_state==TCP_CLOSE_WAIT){ | |
on_close(tcpmib_remote_addr(sk), tcpmib_remote_port(sk), __ip_sock_family(sk)) | |
} | |
if (new_state==TCP_ESTABLISHED){ | |
on_open(tcpmib_remote_addr(sk), tcpmib_remote_port(sk), __ip_sock_family(sk)) | |
} | |
} | |
} | |
probe timer.s(1){ | |
histidx = histidx + 1 | |
report[histidx, "active_count"] = active_count | |
} | |
probe begin { | |
printf("start\n") | |
} | |
probe end{ | |
printf("count=%d max=%d min=%d \n", @count(spend_time), @max(spend_time), @min(spend_time)) | |
for(i=0; i!=histidx; i++){ | |
printf("%d\t%d\n", i, report[i, "active_count"]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
原文作者: 写程序的康德
PS. 需要先安装kernel的debuginfo包,否则会报
semantic error: missing x86_64 kernel/module debuginfo