Skip to content

Instantly share code, notes, and snippets.

@jiminoc
Created September 24, 2014 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiminoc/ad1f45b4979a19458144 to your computer and use it in GitHub Desktop.
Save jiminoc/ad1f45b4979a19458144 to your computer and use it in GitHub Desktop.
from fabric.api import task,runs_once,execute,sudo
from fabric.colors import green
# Example of how to combine the output from multiple hosts
# This is a toy example, just to show you how you can loop and combine the results together
@task
def get_stats():
return int(sudo("lsof -i -sTCP:ESTABLISHED | wc -l"))
@task
@runs_once
def stats():
''' returns the number of ESTABLISHED connections on a remote server '''
# this runs get_stats on each host you pass in via -H
results = execute(get_stats)
# combine all the results
total = sum(results.values())
i = 0
for host, cnt in results.items():
pct = round((float(cnt) / float(max(1, total))) * 100, 2)
print '{:10d} {:15s} cnt: {:10.0f} pct: {:7.2f}%'.format(i, host, cnt, pct)
i += 1
print "\n\nTOTAL COUNT: [%s]\n%s" % (total, "-" * 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment