Переглянути джерело

Updated check_mysql.c to take client options file and group parameters

Jonathan Milby 13 роки тому
батько
коміт
5ed7194c7e
1 змінених файлів з 35 додано та 4 видалено
  1. 35 4
      plugins/check_mysql.c

+ 35 - 4
plugins/check_mysql.c

@@ -49,6 +49,8 @@ char *db_host = NULL;
 char *db_socket = NULL;
 char *db_socket = NULL;
 char *db_pass = NULL;
 char *db_pass = NULL;
 char *db = NULL;
 char *db = NULL;
+char *opt_file = NULL;
+char *opt_group = NULL;
 unsigned int db_port = MYSQL_PORT;
 unsigned int db_port = MYSQL_PORT;
 int check_slave = 0, warn_sec = 0, crit_sec = 0;
 int check_slave = 0, warn_sec = 0, crit_sec = 0;
 int verbose = 0;
 int verbose = 0;
@@ -86,8 +88,14 @@ main (int argc, char **argv)
 
 
 	/* initialize mysql  */
 	/* initialize mysql  */
 	mysql_init (&mysql);
 	mysql_init (&mysql);
+	
+	if (opt_file != NULL)
+		mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
 
 
-	mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
+	if (opt_group != NULL)
+		mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
+	else
+		mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
 
 
 	/* establish a connection to the server and error checking */
 	/* establish a connection to the server and error checking */
 	if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
 	if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
@@ -248,6 +256,8 @@ process_arguments (int argc, char **argv)
 		{"database", required_argument, 0, 'd'},
 		{"database", required_argument, 0, 'd'},
 		{"username", required_argument, 0, 'u'},
 		{"username", required_argument, 0, 'u'},
 		{"password", required_argument, 0, 'p'},
 		{"password", required_argument, 0, 'p'},
+		{"file", required_argument, 0, 'f'},
+		{"group", required_argument, 0, 'g'},
 		{"port", required_argument, 0, 'P'},
 		{"port", required_argument, 0, 'P'},
 		{"critical", required_argument, 0, 'c'},
 		{"critical", required_argument, 0, 'c'},
 		{"warning", required_argument, 0, 'w'},
 		{"warning", required_argument, 0, 'w'},
@@ -262,7 +272,7 @@ process_arguments (int argc, char **argv)
 		return ERROR;
 		return ERROR;
 
 
 	while (1) {
 	while (1) {
-		c = getopt_long (argc, argv, "hvVSP:p:u:d:H:s:c:w:", longopts, &option);
+		c = getopt_long (argc, argv, "hvVSP:p:u:d:f:g:H:s:c:w:", longopts, &option);
 
 
 		if (c == -1 || c == EOF)
 		if (c == -1 || c == EOF)
 			break;
 			break;
@@ -294,6 +304,12 @@ process_arguments (int argc, char **argv)
 				optarg++;
 				optarg++;
 			}
 			}
 			break;
 			break;
+		case 'f':									/* username */
+			opt_file = optarg;
+			break;
+		case 'g':									/* username */
+			opt_group = optarg;
+			break;
 		case 'P':									/* critical time threshold */
 		case 'P':									/* critical time threshold */
 			db_port = atoi (optarg);
 			db_port = atoi (optarg);
 			break;
 			break;
@@ -335,6 +351,10 @@ process_arguments (int argc, char **argv)
 			}
 			}
 		else if (db_user == NULL)
 		else if (db_user == NULL)
 			db_user = argv[c++];
 			db_user = argv[c++];
+		else if (opt_file == NULL)
+			opt_file = argv[c++];
+		else if (opt_group == NULL)
+			opt_group = argv[c++];
 		else if (db_pass == NULL)
 		else if (db_pass == NULL)
 			db_pass = argv[c++];
 			db_pass = argv[c++];
 		else if (db == NULL)
 		else if (db == NULL)
@@ -355,6 +375,12 @@ validate_arguments (void)
 	if (db_user == NULL)
 	if (db_user == NULL)
 		db_user = strdup("");
 		db_user = strdup("");
 
 
+	if (opt_file == NULL)
+		opt_file = strdup("");
+
+	if (opt_group == NULL)
+		opt_group = strdup("");
+
 	if (db_host == NULL)
 	if (db_host == NULL)
 		db_host = strdup("");
 		db_host = strdup("");
 
 
@@ -369,7 +395,7 @@ void
 print_help (void)
 print_help (void)
 {
 {
 	char *myport;
 	char *myport;
-	xasprintf (&myport, "%d", MYSQL_PORT);
+	asprintf (&myport, "%d", MYSQL_PORT);
 
 
 	print_revision (progname, NP_VERSION);
 	print_revision (progname, NP_VERSION);
 
 
@@ -390,6 +416,10 @@ print_help (void)
 
 
   printf (" %s\n", "-d, --database=STRING");
   printf (" %s\n", "-d, --database=STRING");
   printf ("    %s\n", _("Check database with indicated name"));
   printf ("    %s\n", _("Check database with indicated name"));
+  printf (" %s\n", "-f, --file=STRING");
+  printf ("    %s\n", _("Read from the specified client options file"));
+  printf (" %s\n", "-g, --group=STRING");
+  printf ("    %s\n", _("Use a client options group"));
   printf (" %s\n", "-u, --username=STRING");
   printf (" %s\n", "-u, --username=STRING");
   printf ("    %s\n", _("Connect using the indicated username"));
   printf ("    %s\n", _("Connect using the indicated username"));
   printf (" %s\n", "-p, --password=STRING");
   printf (" %s\n", "-p, --password=STRING");
@@ -424,5 +454,6 @@ print_usage (void)
 {
 {
 	printf ("%s\n", _("Usage:"));
 	printf ("%s\n", _("Usage:"));
   printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
   printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
-  printf ("       [-u user] [-p password] [-S]\n");
+  printf ("       [-u user] [-p password] [-S] [-f optfile]\n");
+  printf ("       [-g group]\n");
 }
 }