def run(self): logging.basicConfig(filename=self.log) logging.info('Sniper starting') while True: time.sleep(1) # fetch logging.info('FETCHING') proc_info = '' ret = commands.getstatusoutput( 'mysql -u%s -p%s -s -e '\ '"SHOW ENGINE INNODB STATUS\G"' % (self.user, self.password)) if ret[0] == 0: proc_info = ret[1] else: logging.error( 'sniper.py - Failed to fetch DB status: %s' % ret[1]) # Parse parser = MySqlParser(StringIO.StringIO(proc_info)) result = {} data = parser.initial_parse() if _TRANSACTIONS in data: result[_TRANSACTIONS] = parser.parse_transactions(data[_TRANSACTIONS]) # Kill if _TRANSACTIONS in result: data = result[_TRANSACTIONS] self.kill(data) else: logging.info('sniper.py found no data from the parser')
class TestMySqlParser(unittest.TestCase): def setUp(self): self.parser = MySqlParser(StringIO.StringIO(_TEST_BUFFER)) def test_initial_parse(self): data = self.parser.initial_parse() self.assertIn("BACKGROUND THREAD", data.keys()) self.assertIn("SEMAPHORES", data.keys()) self.assertIn("TRANSACTIONS", data.keys()) self.assertIn("FILE I/O", data.keys()) self.assertIn("INSERT BUFFER AND ADAPTIVE HASH INDEX", data.keys()) self.assertIn("LOG", data.keys()) self.assertIn("BUFFER POOL AND MEMORY", data.keys()) self.assertIn("ROW OPERATIONS", data.keys()) def test_parse_transactions(self): data = self.parser.parse_transactions(_TEST_TRANSACTIONS) self.assertEqual(_EXPECTED_TRANSACTIONS, data)
def setUp(self): self.parser = MySqlParser(StringIO.StringIO(_TEST_BUFFER))