from config import config
import MySQLdb

# DEFAULTS
mysql_config = {
	'timeout': 5,
	'min_tables_count': 0,
	'max_tables_count': -1,
}
# /DEFAULTS

# CONFIG
if "mysql" in config:
	mysql_config.update(config["mysql"])
# /CONFIG

def check_query_server(check, data):
	check.addOutput("ScoreEngine: %s Check\n" % (check.getServiceName()))
	check.addOutput("EXPECTED: Sucessful login on the MySQL Database")
	check.addOutput("OUTPUT:\n")

	# Connect to the DB
	check.addOutput("Starting check...")

	try:
		# Connect to the db
		check.addOutput("Connecting to %s:%s with %s (password: %s)" % (data["HOST"], data["PORT"], data["USER"], data["PASS"]))
		db = MySQLdb.connect(host=data["HOST"],
							port=int(data["PORT"]),
							user=data["USER"],
							passwd=data["PASS"],
							db=data["DB_LOOKUP"],
							connect_timeout=mysql_config["timeout"])
		check.addOutput("Connected!")

		cur = db.cursor()

		# Attempt a show tables
		check.addOutput("Attempting to describe all tables...")
		cur.execute("SHOW tables;")

		# Verify tables
		if cur.rowcount < mysql_config["min_tables_count"]:
			check.addOutput("ERROR: The table count returned is incorrect.")
			return

		if mysql_config["max_tables_count"] > 0:
			if cur.rowcount > mysql_config["max_tables_count"]:
				check.addOutput("ERROR: The table count returned is incorrect.")
				return

		# We're done
		check.setPassed()
		check.addOutput("Check successful!")
	except Exception as e:
		check.addOutput("ERROR: %s: %s" % (type(e).__name__, e))

		return
@@ -222,16 +222,1271 @@
 CONFIG%0A%0A
+def check_wordpress(check, data):%0A%09check.addOutput(%22ScoreEngine: %25s Check%5Cn%22 %25 (check.getServiceName()))%0A%09check.addOutput(%22EXPECTED: Login and query against a Wordpress Database%22)%0A%09check.addOutput(%22OUTPUT:%5Cn%22)%0A%0A%09# Connect to the DB%0A%09check.addOutput(%22Starting check...%22)%0A%0A%09try:%0A%09%09# Connect to the db%0A%09%09check.addOutput(%22Connecting to %25s:%25s%22 %25 (data%5B%22HOST%22%5D, data%5B%22PORT%22%5D))%0A%09%09db = MySQLdb.connect(host=data%5B%22HOST%22%5D,%0A%09%09%09%09%09%09%09port=int(data%5B%22PORT%22%5D),%0A%09%09%09%09%09%09%09user=data%5B%22USER%22%5D,%0A%09%09%09%09%09%09%09passwd=data%5B%22PASS%22%5D,%0A%09%09%09%09%09%09%09db=data%5B%22DB_LOOKUP%22%5D,%0A%09%09%09%09%09%09%09connect_timeout=mysql_config%5B%22timeout%22%5D)%0A%09%09check.addOutput(%22Connected!%22)%0A%0A%09%09cur = db.cursor()%0A%0A%09%09# Select the wordpress database%0A%09%09check.addOutput(%22Querying for some data from the database...%22)%0A%09%09cur.execute(%22SELECT option_value FROM wp_options WHERE option_name = 'blogname';%22)%0A%0A%09%09# Verify%0A%09%09check.addOutput(%22Verifying the data...%22)%0A%09%09if cur.rowcount != 1:%0A%09%09%09check.addOutput(%22ERROR: Invalid data returned.%22)%0A%09%09%09return%0A%0A%09%09data = cur.fetchone()%0A%09%09if data%5B0%5D != data%5B%22EXPECTED_BLOG_NAME%22%5D:%0A%09%09%09check.addOutput(%22ERROR: Invalid data returned.%22)%0A%09%09%09return%0A%0A%09%09# We're done%0A%09%09check.setPassed()%0A%09%09check.addOutput(%22Check successful!%22)%0A%09except Exception as e:%0A%09%09check.addOutput(%22ERROR: %25s: %25s%22 %25 (type(e).__name__, e))%0A%0A%09%09return%0A%0A%0A
 def chec
@@ -1506,32 +1506,32 @@
 r(check, data):%0A
-
 %09check.addOutput
@@ -1764,32 +1764,32 @@
 nnect to the db%0A
+
 %09%09check.addOutpu
@@ -1814,31 +1814,8 @@
 s:%25s
- with %25s (password: %25s)
 %22 %25 

Update MySQL checks