1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
diff -rNu nagios-plugins-1.4/plugins/check_mysql.c nagios-plugins-1.4_mysql41-slave-fix/plugins/check_mysql.c
--- nagios-plugins-1.4/plugins/check_mysql.c Sun Dec 26 00:17:44 2004
+++ nagios-plugins-1.4_mysql41-slave-fix/plugins/check_mysql.c Thu Feb 10 13:27:13 2005
@@ -12,6 +12,11 @@
* Description:
*
* This plugin is for testing a mysql server.
+*
+******************************************************************************
+* Modified by wouter@widexs.nl:
+* - Changed static column numbers in the slave-check, to dynamic,
+* so newer MySQL versions (eg. 4.1.x) are supported as well (09-02-2005)
******************************************************************************/
const char *progname = "check_mysql";
@@ -49,12 +54,19 @@
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
+ MYSQL_FIELD *field;
/* should be status */
char *result = NULL;
char slaveresult[SLAVERESULTSIZE];
+ int local_mysql_server_version = 0;
+ int local_mysql_client_version = 0;
+ int col_slave_err = 0;
+ int col_slave_run = 0;
+ int col_slave_sql_run = 0;
+
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
@@ -107,6 +119,21 @@
die (STATE_CRITICAL, _("slave store_result error: %s\n"), mysql_error (&mysql));
}
+ /* Get client & server version */
+ local_mysql_client_version = mysql_get_client_version();
+ local_mysql_server_version = (local_mysql_client_version >= 40000 ? mysql_get_server_version(&mysql) : local_mysql_get_server_version());
+
+ /* Fetch column names */
+ while((field = mysql_fetch_field(res)))
+ {
+ if(strcmp (field->name, (local_mysql_server_version >= 40000 ? "Slave_IO_Running" : "Slave_Running")) == 0) {
+ col_slave_run = (mysql_field_tell(res) -1);
+ }
+ if(strcmp (field->name, "Slave_SQL_Running") == 0) {
+ col_slave_sql_run = (mysql_field_tell(res) -1);
+ }
+ }
+
/* fetch the first row */
if ( (row = mysql_fetch_row (res)) == NULL) {
mysql_free_result (res);
@@ -114,27 +141,38 @@
die (STATE_CRITICAL, _("slave fetch row error: %s\n"), mysql_error (&mysql));
}
- if (mysql_field_count (&mysql) == 12) {
- /* mysql 3.23.x */
- snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
- if (strcmp (row[6], "Yes") != 0) {
- mysql_free_result (res);
- mysql_close (&mysql);
- die (STATE_CRITICAL, "%s\n", slaveresult);
+ if (local_mysql_server_version >= 40000) {
+ /* mysql 4.x.x */
+ if ((col_slave_run > 0) && (col_slave_sql_run > 0)) {
+ snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[col_slave_run], row[col_slave_sql_run]);
+ if (strcmp (row[col_slave_run], "Yes") != 0 || strcmp (row[col_slave_sql_run], "Yes") != 0) {
+ col_slave_err = 1;
+ }
+ } else {
+ snprintf (slaveresult, SLAVERESULTSIZE, "Required Slave columns not found");
+ col_slave_err = 1;
}
-
} else {
- /* mysql 4.x.x */
- snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[9], row[10]);
- if (strcmp (row[9], "Yes") != 0 || strcmp (row[10], "Yes") != 0) {
- mysql_free_result (res);
- mysql_close (&mysql);
- die (STATE_CRITICAL, "%s\n", slaveresult);
+ /* mysql 3.23.x */
+ if (col_slave_run > 0) {
+ snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[col_slave_run]);
+ if (strcmp (row[col_slave_run], "Yes") != 0) {
+ col_slave_err = 1;
+ }
+ } else {
+ snprintf (slaveresult, SLAVERESULTSIZE, "Required Slave column not found");
+ col_slave_err = 1;
}
}
/* free the result */
mysql_free_result (res);
+
+ /* Check for trouble */
+ if(col_slave_err > 0) {
+ mysql_close (&mysql);
+ die (STATE_CRITICAL, "%s\n", slaveresult);
+ }
}
/* close the connection */
@@ -150,6 +188,16 @@
return STATE_OK;
}
+/* Get version number for server */
+local_mysql_get_server_version(MYSQL *mysql)
+{
+ uint major, minor, version;
+ char *pos= mysql->server_version, *end_pos;
+ major= (uint) strtoul(pos, &end_pos, 10); pos=end_pos+1;
+ minor= (uint) strtoul(pos, &end_pos, 10); pos=end_pos+1;
+ version= (uint) strtoul(pos, &end_pos, 10);
+ return (ulong) major*10000L+(ulong) (minor*100+version);
+}
/* process command-line arguments */
int
|