| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 | |
| 7 """Verify that the buildslave host machines are under a disk usage threshold.""" | |
| 8 | |
| 9 | |
| 10 import re | |
| 11 import sys | |
| 12 | |
| 13 from build_step import BuildStep, BuildStepWarning, BuildStepFailure | |
| 14 from py.utils import misc | |
| 15 | |
| 16 sys.path.append(misc.BUILDBOT_PATH) | |
| 17 | |
| 18 from scripts import run_cmd | |
| 19 | |
| 20 | |
| 21 MAX_DISK_USAGE_PERCENT = 90 | |
| 22 | |
| 23 | |
| 24 def get_disk_usage_percent(stdout): | |
| 25 """Parse the disk_usage.py script output and return the disk usage percent. | |
| 26 | |
| 27 Args: | |
| 28 stdout: string; output from the disk_usage script. | |
| 29 Returns: | |
| 30 float; the percentage of disk space used on the machine. | |
| 31 """ | |
| 32 # The disk_usage.py script's output looks like this: | |
| 33 # usage(total=382117335040, used=178414583808, free=184575676416) | |
| 34 total = float(re.findall('total=(\d+)', stdout)[0]) | |
| 35 used = float(re.findall('used=(\d+)', stdout)[0]) | |
| 36 return used / total * 100.0 | |
| 37 | |
| 38 | |
| 39 class CheckSlaveHostsDiskUsage(BuildStep): | |
| 40 def _Run(self): | |
| 41 disk_usage_script = run_cmd.ResolvablePath('third_party', 'disk_usage', | |
| 42 'disk_usage.py') | |
| 43 results = run_cmd.run_on_all_slave_hosts(['python', disk_usage_script]) | |
| 44 failed = [] | |
| 45 over_threshold = False | |
| 46 print 'Maximum allowed disk usage percent: %d\n' % MAX_DISK_USAGE_PERCENT | |
| 47 for host in results.iterkeys(): | |
| 48 print host, | |
| 49 got_result = True | |
| 50 if results[host].returncode != 0: | |
| 51 got_result = False | |
| 52 else: | |
| 53 try: | |
| 54 percent_used = get_disk_usage_percent(results[host].stdout) | |
| 55 print ': %d%%' % percent_used, | |
| 56 if percent_used > MAX_DISK_USAGE_PERCENT: | |
| 57 print ' (over threshold)' | |
| 58 over_threshold = True | |
| 59 else: | |
| 60 print | |
| 61 except (IndexError, ZeroDivisionError): | |
| 62 got_result = False | |
| 63 if not got_result: | |
| 64 failed.append(host) | |
| 65 print ': failed: ', results[host].stderr | |
| 66 | |
| 67 if failed: | |
| 68 print | |
| 69 print 'Failed to get disk usage for the following hosts:' | |
| 70 for failed_host in failed: | |
| 71 print ' ', failed_host | |
| 72 | |
| 73 if over_threshold: | |
| 74 raise BuildStepFailure('Some hosts are over threshold.') | |
| 75 | |
| 76 if failed: | |
| 77 # TODO(borenet): Make sure that we can log in to all hosts, then make this | |
| 78 # an error. | |
| 79 raise BuildStepWarning('Could not log in to some hosts.') | |
| 80 | |
| 81 | |
| 82 if '__main__' == __name__: | |
| 83 sys.exit(BuildStep.RunBuildStep(CheckSlaveHostsDiskUsage)) | |
| OLD | NEW |