Created
January 22, 2018 08:02
-
-
Save fireflyc/cf82f27ebbfe1ff61c4f789b8b1b0d25 to your computer and use it in GitHub Desktop.
systemtap统计TCP连接数量
This file contains hidden or 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