Explorar o código

Display errors with slave queries correctly. Added extra tests for slaves

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1299 f882894a-f735-0410-b71e-b25c423dba1c
Ton Voon %!s(int64=20) %!d(string=hai) anos
pai
achega
71ce143ab1
Modificáronse 3 ficheiros con 57 adicións e 16 borrados
  1. 3 1
      NPTest.pm
  2. 13 3
      plugins/check_mysql.c
  3. 41 12
      plugins/t/check_mysql.t

+ 3 - 1
NPTest.pm

@@ -46,6 +46,8 @@ default via the C<use NPTest;> statement.
 
 
 =item getTestParameter( "ENV_VARIABLE", $brief_description, $default )
 =item getTestParameter( "ENV_VARIABLE", $brief_description, $default )
 
 
+$default is optional.
+
 This function allows the test harness
 This function allows the test harness
 developer to interactively request test parameter information from the
 developer to interactively request test parameter information from the
 user. The user can accept the developer's default value or reply "none"
 user. The user can accept the developer's default value or reply "none"
@@ -302,7 +304,7 @@ sub getTestParameter
 {
 {
   my( $param, $envvar, $default, $brief, $scoped );
   my( $param, $envvar, $default, $brief, $scoped );
   my $new_style;
   my $new_style;
-  if (scalar @_ == 3) {
+  if (scalar @_ <= 3) {
 	($param, $brief, $default) = @_;
 	($param, $brief, $default) = @_;
 	$envvar = $param;
 	$envvar = $param;
 	$new_style = 1;
 	$new_style = 1;

+ 13 - 3
plugins/check_mysql.c

@@ -53,6 +53,7 @@ main (int argc, char **argv)
 	/* should be status */
 	/* should be status */
 	
 	
 	char *result = NULL;
 	char *result = NULL;
+	char *error = NULL;
 	char slaveresult[SLAVERESULTSIZE];
 	char slaveresult[SLAVERESULTSIZE];
 
 
 	setlocale (LC_ALL, "");
 	setlocale (LC_ALL, "");
@@ -99,21 +100,30 @@ main (int argc, char **argv)
 	if(check_slave) {
 	if(check_slave) {
 		/* check the slave status */
 		/* check the slave status */
 		if (mysql_query (&mysql, "show slave status") != 0) {
 		if (mysql_query (&mysql, "show slave status") != 0) {
+			error = strdup(mysql_error(&mysql));
 			mysql_close (&mysql);
 			mysql_close (&mysql);
-			die (STATE_CRITICAL, _("slave query error: %s\n"), mysql_error (&mysql));
+			die (STATE_CRITICAL, _("slave query error: %s\n"), error);
 		}
 		}
 
 
 		/* store the result */
 		/* store the result */
 		if ( (res = mysql_store_result (&mysql)) == NULL) {
 		if ( (res = mysql_store_result (&mysql)) == NULL) {
+			error = strdup(mysql_error(&mysql));
 			mysql_close (&mysql);
 			mysql_close (&mysql);
-			die (STATE_CRITICAL, _("slave store_result error: %s\n"), mysql_error (&mysql));
+			die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
+		}
+
+		/* Check there is some data */
+		if (mysql_num_rows(res) == 0) {
+			mysql_close(&mysql);
+			die (STATE_WARNING, "%s\n", _("No slaves defined"));
 		}
 		}
 
 
 		/* fetch the first row */
 		/* fetch the first row */
 		if ( (row = mysql_fetch_row (res)) == NULL) {
 		if ( (row = mysql_fetch_row (res)) == NULL) {
+			error = strdup(mysql_error(&mysql));
 			mysql_free_result (res);
 			mysql_free_result (res);
 			mysql_close (&mysql);
 			mysql_close (&mysql);
-			die (STATE_CRITICAL, _("slave fetch row error: %s\n"), mysql_error (&mysql));
+			die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
 		}
 		}
 
 
 		if (mysql_field_count (&mysql) == 12) {
 		if (mysql_field_count (&mysql) == 12) {

+ 41 - 12
plugins/t/check_mysql.t

@@ -13,20 +13,49 @@ use vars qw($tests);
 
 
 plan skip_all => "check_mysql not compiled" unless (-x "check_mysql");
 plan skip_all => "check_mysql not compiled" unless (-x "check_mysql");
 
 
-plan tests => 3;
-
-my $failureOutput = '/Access denied for user /';
-my $mysqlserver = getTestParameter( "mysql_server", "NP_MYSQL_SERVER", undef,
-		"A MySQL Server");
-my $mysql_login_details = getTestParameter( "mysql_login_details", "MYSQL_LOGIN_DETAILS", undef, 
-		"Command line parameters to specify login access");
+plan tests => 7;
+
+my $bad_login_output = '/Access denied for user /';
+my $mysqlserver = getTestParameter( 
+		"NP_MYSQL_SERVER", 
+		"A MySQL Server with no slaves setup"
+		);
+my $mysql_login_details = getTestParameter( 
+		"MYSQL_LOGIN_DETAILS", 
+		"Command line parameters to specify login access",
+		"-u user -ppw",
+		);
+my $with_slave = getTestParameter( 
+		"NP_MYSQL_WITH_SLAVE", 
+		"MySQL server with slaves setup"
+		);
+my $with_slave_login = getTestParameter( 
+		"NP_MYSQL_WITH_SLAVE_LOGIN", 
+		"Login details for server with slave", 
+		"-uroot -ppw"
+		);
 
 
 my $result;
 my $result;
 
 
-$result = NPTest->testCmd("./check_mysql -H $mysqlserver $mysql_login_details");
-cmp_ok( $result->return_code, '==', 0, "Login okay");
+SKIP: {
+	skip "No mysql server defined", 5 unless $mysqlserver;
+	$result = NPTest->testCmd("./check_mysql -H $mysqlserver $mysql_login_details");
+	cmp_ok( $result->return_code, '==', 0, "Login okay");
+
+	$result = NPTest->testCmd("./check_mysql -H $mysqlserver -u dummy");
+	cmp_ok( $result->return_code, '==', 2, "Login failure");
+	like( $result->output, $bad_login_output, "Expected login failure message");
+
+	$result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details");
+	cmp_ok( $result->return_code, "==", 1, "No slaves defined" );
+	like( $result->output, "/No slaves defined/", "Correct error message");
+}
 
 
-$result = NPTest->testCmd("./check_mysql -H $mysqlserver -u dummy");
-cmp_ok( $result->return_code, '==', 2, "Login expected failure");
-like( $result->output, $failureOutput, "Error string as expected");
+SKIP: {
+	skip "No mysql server with slaves defined", 2 unless $with_slave;
+	$result = NPTest->testCmd("./check_mysql -H $with_slave $with_slave_login");
+	cmp_ok( $result->return_code, '==', 0, "Login okay");
 
 
+	$result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login");
+	cmp_ok( $result->return_code, "==", 0, "Slaves okay" );
+}