Ejemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(description="Monitor your MySQL replication status")
    parser.add_argument("--config", help="Path to the replmon config file (default: {0})".format(DEFAULT_CNF_FILE),
                        default=DEFAULT_CNF_FILE)
    args = parser.parse_args()

    parser = ConfigParser.ConfigParser()
    parser.read(args.config)

    mysql_args = dict(parser.items("mysql"))
    mon = Replmon(mysql_args)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler = logging.FileHandler(DEFAULT_LOG_FILE)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    with daemon.DaemonContext(pidfile=lockfile.pidlockfile.PIDLockFile(DEFAULT_PID_FILE),
                              files_preserve=[file_handler.stream]):
        procname.setprocname("replmon")
        # noinspection PyBroadException
        try:
            mon.run()
        except Exception:
            logger.exception("An error occurred")
Ejemplo n.º 2
0
class StatusFileTestCase(unittest.TestCase):
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.status_file = os.path.join(self.test_dir, "replmon.status")

        self.mon = Replmon({})
        self.mon.status_file = self.status_file

    def tearDown(self):
        shutil.rmtree(self.test_dir)

    def assertFileExists(self, path):
        try:
            open(path)
        except IOError:
            self.fail("File does not exist: {0}".format(path))

    def test_create_status_file(self):
        self.mon.touch_status_file()
        self.assertFileExists(self.status_file)

    def test_touch_status_file(self):
        atime = mtime = 1
        open(self.status_file, "w")
        os.utime(self.status_file, (atime, mtime))

        self.mon.touch_status_file()

        stat = os.stat(self.status_file)
        self.assertNotEqual(atime, stat.st_atime)
        self.assertNotEqual(mtime, stat.st_mtime)

        self.assertAlmostEqual(time.time(), stat.st_atime, delta=2)
        self.assertAlmostEqual(time.time(), stat.st_mtime, delta=2)
Ejemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description="Monitor your MySQL replication status")
    parser.add_argument("--config", help="Path to the replmon config file (default: {0})".format(DEFAULT_CNF_FILE),
                        default=DEFAULT_CNF_FILE)
    args = parser.parse_args()

    parser = ConfigParser.ConfigParser()
    parser.read(args.config)

    mysql_args = dict(parser.items("mysql"))
    mon = Replmon(mysql_args, DEFAULT_STATUS_FILE, DEFAULT_CHECK_INTERVAL)

    try:
        user = parser.get("system", "user")
    except  (ConfigParser.NoSectionError, ConfigParser.NoSectionError):
        uid, gid = os.getuid(), os.getgid()
    else:
        user_entry = pwd.getpwnam(user)
        uid, gid = user_entry.pw_uid, user_entry.pw_gid

    # Ensure we can touch the status file
    for dir in map(os.path.dirname, [DEFAULT_PID_FILE, DEFAULT_STATUS_FILE]):
        ensure_writable(dir, uid, gid)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler = logging.FileHandler(DEFAULT_LOG_FILE)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)


    with daemon.DaemonContext(pidfile=lockfile.pidlockfile.PIDLockFile(DEFAULT_PID_FILE),
                              files_preserve=[file_handler.stream], uid=uid, gid=gid):
        procname.setprocname("replmon")
        # noinspection PyBroadException
        try:
            mon.run()
        except Exception:
            logger.exception("An error occurred")
Ejemplo n.º 4
0
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.status_file = os.path.join(self.test_dir, "replmon.status")

        self.mon = Replmon({})
        self.mon.status_file = self.status_file