summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_mysql.c16
-rw-r--r--plugins/t/check_mysql.t53
2 files changed, 54 insertions, 15 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 3194ece..45f86a9 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -53,6 +53,7 @@ main (int argc, char **argv)
53 /* should be status */ 53 /* should be status */
54 54
55 char *result = NULL; 55 char *result = NULL;
56 char *error = NULL;
56 char slaveresult[SLAVERESULTSIZE]; 57 char slaveresult[SLAVERESULTSIZE];
57 58
58 setlocale (LC_ALL, ""); 59 setlocale (LC_ALL, "");
@@ -99,21 +100,30 @@ main (int argc, char **argv)
99 if(check_slave) { 100 if(check_slave) {
100 /* check the slave status */ 101 /* check the slave status */
101 if (mysql_query (&mysql, "show slave status") != 0) { 102 if (mysql_query (&mysql, "show slave status") != 0) {
103 error = strdup(mysql_error(&mysql));
102 mysql_close (&mysql); 104 mysql_close (&mysql);
103 die (STATE_CRITICAL, _("slave query error: %s\n"), mysql_error (&mysql)); 105 die (STATE_CRITICAL, _("slave query error: %s\n"), error);
104 } 106 }
105 107
106 /* store the result */ 108 /* store the result */
107 if ( (res = mysql_store_result (&mysql)) == NULL) { 109 if ( (res = mysql_store_result (&mysql)) == NULL) {
110 error = strdup(mysql_error(&mysql));
108 mysql_close (&mysql); 111 mysql_close (&mysql);
109 die (STATE_CRITICAL, _("slave store_result error: %s\n"), mysql_error (&mysql)); 112 die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
113 }
114
115 /* Check there is some data */
116 if (mysql_num_rows(res) == 0) {
117 mysql_close(&mysql);
118 die (STATE_WARNING, "%s\n", _("No slaves defined"));
110 } 119 }
111 120
112 /* fetch the first row */ 121 /* fetch the first row */
113 if ( (row = mysql_fetch_row (res)) == NULL) { 122 if ( (row = mysql_fetch_row (res)) == NULL) {
123 error = strdup(mysql_error(&mysql));
114 mysql_free_result (res); 124 mysql_free_result (res);
115 mysql_close (&mysql); 125 mysql_close (&mysql);
116 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), mysql_error (&mysql)); 126 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
117 } 127 }
118 128
119 if (mysql_field_count (&mysql) == 12) { 129 if (mysql_field_count (&mysql) == 12) {
diff --git a/plugins/t/check_mysql.t b/plugins/t/check_mysql.t
index b29c5c6..764db72 100644
--- a/plugins/t/check_mysql.t
+++ b/plugins/t/check_mysql.t
@@ -13,20 +13,49 @@ use vars qw($tests);
13 13
14plan skip_all => "check_mysql not compiled" unless (-x "check_mysql"); 14plan skip_all => "check_mysql not compiled" unless (-x "check_mysql");
15 15
16plan tests => 3; 16plan tests => 7;
17 17
18my $failureOutput = '/Access denied for user /'; 18my $bad_login_output = '/Access denied for user /';
19my $mysqlserver = getTestParameter( "mysql_server", "NP_MYSQL_SERVER", undef, 19my $mysqlserver = getTestParameter(
20 "A MySQL Server"); 20 "NP_MYSQL_SERVER",
21my $mysql_login_details = getTestParameter( "mysql_login_details", "MYSQL_LOGIN_DETAILS", undef, 21 "A MySQL Server with no slaves setup"
22 "Command line parameters to specify login access"); 22 );
23my $mysql_login_details = getTestParameter(
24 "MYSQL_LOGIN_DETAILS",
25 "Command line parameters to specify login access",
26 "-u user -ppw",
27 );
28my $with_slave = getTestParameter(
29 "NP_MYSQL_WITH_SLAVE",
30 "MySQL server with slaves setup"
31 );
32my $with_slave_login = getTestParameter(
33 "NP_MYSQL_WITH_SLAVE_LOGIN",
34 "Login details for server with slave",
35 "-uroot -ppw"
36 );
23 37
24my $result; 38my $result;
25 39
26$result = NPTest->testCmd("./check_mysql -H $mysqlserver $mysql_login_details"); 40SKIP: {
27cmp_ok( $result->return_code, '==', 0, "Login okay"); 41 skip "No mysql server defined", 5 unless $mysqlserver;
42 $result = NPTest->testCmd("./check_mysql -H $mysqlserver $mysql_login_details");
43 cmp_ok( $result->return_code, '==', 0, "Login okay");
44
45 $result = NPTest->testCmd("./check_mysql -H $mysqlserver -u dummy");
46 cmp_ok( $result->return_code, '==', 2, "Login failure");
47 like( $result->output, $bad_login_output, "Expected login failure message");
48
49 $result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details");
50 cmp_ok( $result->return_code, "==", 1, "No slaves defined" );
51 like( $result->output, "/No slaves defined/", "Correct error message");
52}
28 53
29$result = NPTest->testCmd("./check_mysql -H $mysqlserver -u dummy"); 54SKIP: {
30cmp_ok( $result->return_code, '==', 2, "Login expected failure"); 55 skip "No mysql server with slaves defined", 2 unless $with_slave;
31like( $result->output, $failureOutput, "Error string as expected"); 56 $result = NPTest->testCmd("./check_mysql -H $with_slave $with_slave_login");
57 cmp_ok( $result->return_code, '==', 0, "Login okay");
32 58
59 $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login");
60 cmp_ok( $result->return_code, "==", 0, "Slaves okay" );
61}