mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame^] | 2 | import os, sys, signal, time, subprocess, logging |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 3 | from optparse import OptionParser |
showard | 701f626 | 2009-04-16 03:10:11 +0000 | [diff] [blame] | 4 | import common |
| 5 | from autotest_lib.client.common_lib import error, global_config, utils |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame^] | 6 | from autotest_lib.client.common_lib import logging_manager |
| 7 | from autotest_lib.scheduler import babysitter_logging_config |
| 8 | from autotest_lib.scheduler import scheduler_logging_config |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 9 | |
| 10 | PAUSE_LENGTH = 60 |
| 11 | STALL_TIMEOUT = 2*60*60 |
| 12 | |
| 13 | parser = OptionParser() |
| 14 | parser.add_option("-r", action="store_true", dest="recover") |
| 15 | (options, args) = parser.parse_args() |
| 16 | |
| 17 | autodir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 18 | results_dir = os.path.join(autodir, 'results') |
| 19 | monitor_db = os.path.join(autodir, 'scheduler/monitor_db.py') |
| 20 | recover = (options.recover == True) |
| 21 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 22 | # load logging settings |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame^] | 23 | logging_manager.configure_logging( |
| 24 | babysitter_logging_config.BabysitterLoggingConfig()) |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 25 | |
| 26 | if len(args) != 0: |
| 27 | print "Usage: %s [options]" % __file__ |
| 28 | print " -r Run recovery mode. (Note: recovery is implicit after" |
| 29 | print " any crash!)" |
| 30 | print |
| 31 | sys.exit(1) |
| 32 | |
| 33 | |
showard | 701f626 | 2009-04-16 03:10:11 +0000 | [diff] [blame] | 34 | def run_banner_output(cmd): |
| 35 | """Returns ------ CMD ------\nCMD_OUTPUT in a string""" |
| 36 | banner_output = '%s\n%%s\n\n' % cmd.center(60, '-') |
| 37 | command_output = '' |
| 38 | try: |
| 39 | cmd_out = utils.run(cmd, ignore_status=True, timeout=30) |
| 40 | command_output = cmd_out.stdout + cmd_out.stderr |
| 41 | except error.CmdError: |
| 42 | command_output = 'Timed out' |
| 43 | |
| 44 | return banner_output % command_output |
| 45 | |
| 46 | |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 47 | def kill_all_monitors(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 48 | logging.info("Killing all monitor_dbs") |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 49 | # try shutdown first |
| 50 | status = os.system("killall -2 monitor_db.py") |
| 51 | if status == 0: # were any killed? |
| 52 | # give them some time to shutdown |
| 53 | time.sleep(30) |
| 54 | # kill any that are left |
| 55 | os.system("killall monitor_db.py") |
| 56 | |
| 57 | |
| 58 | def handle_sigterm(signum, frame): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 59 | logging.info('Caught SIGTERM') |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 60 | kill_all_monitors() |
| 61 | sys.exit(1) |
| 62 | |
| 63 | signal.signal(signal.SIGTERM, handle_sigterm) |
| 64 | |
| 65 | |
| 66 | class MonitorProc: |
| 67 | def __init__(self, do_recovery=False): |
| 68 | args = [monitor_db] |
| 69 | if do_recovery: |
| 70 | args.append("--recover-hosts") |
| 71 | args.append(results_dir) |
| 72 | |
| 73 | kill_all_monitors() |
mbligh | c9895aa | 2009-04-01 18:36:58 +0000 | [diff] [blame] | 74 | environ = os.environ |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame^] | 75 | scheduler_config = scheduler_logging_config.SchedulerLoggingConfig |
| 76 | log_name = scheduler_config.get_log_name() |
showard | 50e463b | 2009-04-07 18:13:45 +0000 | [diff] [blame] | 77 | os.environ['AUTOTEST_SCHEDULER_LOG_NAME'] = log_name |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame^] | 78 | scheduler_log_dir = scheduler_config.get_server_log_dir() |
| 79 | self.log_path = os.path.join(scheduler_log_dir, log_name) |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 80 | |
mbligh | c9895aa | 2009-04-01 18:36:58 +0000 | [diff] [blame] | 81 | self.log_size = 0 |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 82 | self.last_log_change = time.time() |
| 83 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 84 | logging.info("STARTING monitor_db with log file %s" % self.log_path) |
showard | 50e463b | 2009-04-07 18:13:45 +0000 | [diff] [blame] | 85 | devnull = open(os.devnull, 'w') |
| 86 | self.proc = subprocess.Popen(args, stdout=devnull) |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def is_running(self): |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 90 | if self.proc.poll() is not None: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 91 | logging.info("monitor_db DIED") |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 92 | return False |
| 93 | |
| 94 | old_size = self.log_size |
| 95 | new_size = os.path.getsize(self.log_path) |
| 96 | if old_size != new_size: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 97 | logging.info("Log was touched") |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 98 | self.log_size = new_size |
| 99 | self.last_log_change = time.time() |
| 100 | elif self.last_log_change + STALL_TIMEOUT < time.time(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 101 | logging.info("monitor_db STALLED") |
showard | 701f626 | 2009-04-16 03:10:11 +0000 | [diff] [blame] | 102 | self.collect_stalled_info() |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 103 | return False |
| 104 | |
| 105 | return True |
| 106 | |
| 107 | |
showard | 701f626 | 2009-04-16 03:10:11 +0000 | [diff] [blame] | 108 | def collect_stalled_info(self): |
| 109 | INFO_TO_COLLECT = ['uptime', |
| 110 | 'ps auxwww', |
| 111 | 'iostat -k -x 2 4', |
| 112 | ] |
| 113 | db_cmd = '/usr/bin/mysqladmin --verbose processlist -u%s -p%s' |
| 114 | config = global_config.global_config |
| 115 | try: |
| 116 | user = config.get_config_value("BACKUP", "user") |
| 117 | password = config.get_config_value("BACKUP", "password") |
| 118 | db_cmd %= (user, password) |
| 119 | INFO_TO_COLLECT.append(db_cmd) |
| 120 | except global_config.ConfigError: |
| 121 | pass |
| 122 | stall_log_path = self.log_path + '.stall_info' |
| 123 | log = open(stall_log_path, "w") |
| 124 | for cmd in INFO_TO_COLLECT: |
| 125 | log.write(run_banner_output(cmd)) |
| 126 | |
| 127 | log.close() |
| 128 | |
| 129 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 130 | logging.info("initializing") |
mbligh | 6adf837 | 2009-01-30 00:51:18 +0000 | [diff] [blame] | 131 | |
| 132 | if os.getuid() == 0: |
showard | 701f626 | 2009-04-16 03:10:11 +0000 | [diff] [blame] | 133 | logging.critical("running as root, aborting!") |
mbligh | 6adf837 | 2009-01-30 00:51:18 +0000 | [diff] [blame] | 134 | sys.exit(1) |
| 135 | |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 136 | utils.write_pid("monitor_db_babysitter") |
| 137 | |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 138 | while True: |
| 139 | proc = MonitorProc(do_recovery=recover) |
| 140 | time.sleep(PAUSE_LENGTH) |
| 141 | while proc.is_running(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 142 | logging.info("Tick") |
mbligh | c0e24fb | 2008-10-02 20:17:37 +0000 | [diff] [blame] | 143 | time.sleep(PAUSE_LENGTH) |
| 144 | recover = False |