|
import subprocess |
|
import json |
|
|
|
# Collect the number of connection types from |
|
# the netstat output for the sync_gateway process. |
|
# The result will be a json file like: |
|
# |
|
# {"ESTABLISHED": 250, "TIME_WAIT": 100} |
|
# |
|
def get_filtered_netstat(): |
|
output = get_raw_netstat_output() |
|
lines = filter_netstat_header(output) |
|
lines = filter_non_sync_gateway(lines) |
|
stats = generate_stats(lines) |
|
emit_stats_json(stats) |
|
|
|
def filter_netstat_header(output): |
|
# convert from string to array of strings |
|
lines = output.split("\n") |
|
return lines[2:] # slice off the first two lines, return the rest |
|
|
|
def filter_non_sync_gateway(lines): |
|
# tcp 0 0 10.181.112.50:34656 10.157.88.10:11210 ESTABLISHED 1001 121024 6925/sync_gateway |
|
remaining = [] |
|
for line in lines: |
|
if "sync_gateway" in line: |
|
remaining.append(line) |
|
return remaining |
|
|
|
def get_raw_netstat_output(): |
|
# Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name |
|
# tcp 0 0 10.181.112.50:34656 10.157.88.10:11210 ESTABLISHED 1001 121024 6925/sync_gateway |
|
# tcp 0 0 10.181.112.50:38528 10.109.187.75:11210 ESTABLISHED 1001 121039 6925/sync_gateway |
|
return subprocess.check_output(["netstat", "-apeen"]) |
|
|
|
def generate_stats(lines): |
|
|
|
# key: state (eg, ESTABLISHED) |
|
# val: counter (num times this has been seen) |
|
stats = {} |
|
|
|
for line in lines: |
|
# parse the state field |
|
columns = line.split() |
|
state = columns[5] |
|
if not stats.has_key(state): |
|
stats[state] = 1 |
|
else: |
|
current_count = stats[state] |
|
stats[state] = current_count + 1 |
|
|
|
return stats |
|
|
|
def emit_stats_json(stats): |
|
print json.dumps(stats) |
|
|
|
if __name__ == "__main__": |
|
get_filtered_netstat() |