Created
September 24, 2014 21:50
-
-
Save jiminoc/ad1f45b4979a19458144 to your computer and use it in GitHub Desktop.
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
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