Skip to content

Instantly share code, notes, and snippets.

@tleyden
Last active August 29, 2015 14:23
Show Gist options
  • Save tleyden/d928a8167722653e14e4 to your computer and use it in GitHub Desktop.
Save tleyden/d928a8167722653e14e4 to your computer and use it in GitHub Desktop.
sg-collectinfo

Collecting

  1. Clone this gist
  2. chmod *.sh
  3. ./collect.sh

Sending

  1. zip up the output directory
  2. Send/upload zipfile
  3. delete the output directory
mkdir -p output
while true; do
# Collect netstat output
# This must be run as sudo, because otherwise it will give errors
# for the "netstat -p" about not being root. (even though running
# netstat -p on the command line works fine)
sudo python netstat.py >> output/netstat.txt
# Collect top output
./top.sh >> output/top.txt
# Collect expvars output
curl http://localhost:4985/_expvar >> output/expvars.txt
sleep 60
done
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()
# - Call top
# - Grep the line with sync_gateway
# - Parse out these fields:
# VIRT
# RES
# %CPU
# - Output JSON: {"VSS":1976804 "RSS":1.291g "CPU":0.0}
top -n 1 -b | grep -i sync_gateway | awk '{print "{\"VSS\":" $5 " \"RSS\":" $6 " \"CPU\":" $9 "}"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment