-
-
Save DavidGoodwin/61b5f644b868ecd484c5 to your computer and use it in GitHub Desktop.
Revisions
-
DavidGoodwin revised this gist
Dec 15, 2015 . 2 changed files with 76 additions and 40 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,76 @@ #!/usr/bin/python # See https://gist.github.com/wryfi/3902757 # Crude CPU usage thing for container(s). import multiprocessing, os, re, sys, time import getopt cgroup_dir = '/sys/fs/cgroup/lxc/' # some sort of regexp to match container names you want to report on. node_rgx = u'[a-z0-9]{3,}' def main(interval, show_all = False): cpus = multiprocessing.cpu_count() startval = get_values() time.sleep(interval) endval = get_values() for key, value in startval.iteritems(): if key in endval: delta = int(endval[key]) - int(startval[key]) dpns = float(delta / interval) dps = dpns / 1000000000 percent = dps / cpus if show_all == False and percent <= 0.1 : # ignore < 0.1% continue print key + ':' + '{percent:.2%}'.format(percent=percent) def get_values(): values = {} # read the 'parent' cgroup for total figures. if os.path.exists('%s/cpuacct.usage' % cgroup_dir): with open('%s/cpuacct.usage' % cgroup_dir, 'rb') as tobj: values['total'] = tobj.read() else: print "cpuacct.usage doesn't exist for cgroup_dir (%s) - invalid cgroup config?" % cgroup_dir sys.exit(1) # no go through each cgroup(note: node_rgx) , and find usage from it for fd in os.listdir(cgroup_dir): if os.path.isdir('%s/%s' % (cgroup_dir, fd)) and re.match(node_rgx, fd): acctf = '%s/%s/cpuacct.usage' % (cgroup_dir, fd) if os.path.isfile(acctf): with open(acctf, 'rb') as accto: values[fd] = accto.read() return values def usage() : print "Usage: -i <interval> -a (show all, regardless of usage) or -h (show help). " if __name__ == "__main__": interval = 1 show_all = False try : opts, args = getopt.getopt(sys.argv[1:], 'hai:', ['help', 'all', 'interval=']) except getopt.GetoptError as err: print str(err); usage() sys.exit(1) for o, a in opts: if o in ( '-i', '--interval') : interval = int(a) elif o in ( '-a', '--all' ): show_all = True elif o in ( '-h', '--help' ): usage() sys.exit(0) else: assert False, "Invalid option." main(interval, show_all) 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 charactersOriginal file line number Diff line number Diff line change @@ -1,40 +0,0 @@ -
wryfi created this gist
Oct 16, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ #!/usr/bin/python import multiprocessing, os, re, sys, time cgroup_dir = '/sys/fs/cgroup' node_rgx = u'[a-z]*[0-9]{2}' interval = 1 def main(): cpus = multiprocessing.cpu_count() startval = get_values() time.sleep(interval) endval = get_values() for key, value in startval.iteritems(): if key in endval: delta = int(endval[key]) - int(startval[key]) dpns = float(delta / interval) dps = dpns / 1000000000 percent = dps / cpus print key + ':' + '{percent:.2%}'.format(percent=percent) def get_values(): values = {} if os.path.exists('%s/cpuacct.usage' % cgroup_dir): with open('%s/cpuacct.usage' % cgroup_dir, 'rb') as tobj: values['total'] = tobj.read() else: sys.exit(1) for fd in os.listdir(cgroup_dir): if os.path.isdir('%s/%s' % (cgroup_dir, fd)) and re.match(node_rgx, fd): acctf = '%s/%s/cpuacct.usage' % (cgroup_dir, fd) if os.path.isfile(acctf): with open(acctf, 'rb') as accto: values[fd] = accto.read() return values if __name__ == "__main__": main()