Code

Fix translations when extra-opts aren't enabled
[nagiosplug.git] / plugins / check_mysql_query.c
index 404ca79fc875d06696c02c766d8c31f639b8b89f..40adf09279fca709006a1ddd9da9548e27a01df8 100644 (file)
@@ -1,25 +1,41 @@
-/******************************************************************************
-*
-* CHECK_MYSQL_QUERY.C
-*
-* Program: Mysql plugin for Nagios
+/*****************************************************************************
+* 
+* Nagios check_mysql_query plugin
+* 
 * License: GPL
-* Copyright (c) 2006 Nagios Plugins Team, after Didi Rieder (check_mysql)
+* Copyright (c) 2006-2009 Nagios Plugins Development Team
+* Original code from check_mysql, copyright 1999 Didi Rieder
 * 
-* $Id$
-*
 * Description:
-*  This plugin is for running arbitrary SQL and checking the results
-*
-******************************************************************************/
+* 
+* This file contains the check_mysql_query plugin
+* 
+* This plugin is for running arbitrary SQL and checking the results
+* 
+* 
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+* 
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+* 
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
+* 
+*****************************************************************************/
 
 const char *progname = "check_mysql_query";
-const char *revision = "$Revision$";
-const char *copyright = "2006";
+const char *copyright = "1999-2007";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
 #include "utils.h"
+#include "utils_base.h"
 #include "netutils.h"
 
 #include <mysql.h>
@@ -27,6 +43,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 char *db_user = NULL;
 char *db_host = NULL;
+char *db_socket = NULL;
 char *db_pass = NULL;
 char *db = NULL;
 unsigned int db_port = MYSQL_PORT;
@@ -57,6 +74,9 @@ main (int argc, char **argv)
        bindtextdomain (PACKAGE, LOCALEDIR);
        textdomain (PACKAGE);
 
+       /* Parse extra opts if any */
+       argv=np_extra_opts (&argc, argv, progname);
+
        if (process_arguments (argc, argv) == ERROR)
                usage4 (_("Could not parse arguments"));
 
@@ -66,7 +86,7 @@ main (int argc, char **argv)
        mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
 
        /* establish a connection to the server and error checking */
-       if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
+       if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
                if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
                        die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
                else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
@@ -150,6 +170,7 @@ process_arguments (int argc, char **argv)
        int option = 0;
        static struct option longopts[] = {
                {"hostname", required_argument, 0, 'H'},
+               {"socket", required_argument, 0, 's'},
                {"database", required_argument, 0, 'd'},
                {"username", required_argument, 0, 'u'},
                {"password", required_argument, 0, 'p'},
@@ -167,7 +188,7 @@ process_arguments (int argc, char **argv)
                return ERROR;
 
        while (1) {
-               c = getopt_long (argc, argv, "hvVSP:p:u:d:H:q:w:c:", longopts, &option);
+               c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:", longopts, &option);
 
                if (c == -1 || c == EOF)
                        break;
@@ -181,14 +202,17 @@ process_arguments (int argc, char **argv)
                                usage2 (_("Invalid hostname/address"), optarg);
                        }
                        break;
-               case 'd':                                                                       /* hostname */
+               case 's':                                                                       /* socket */
+                       db_socket = optarg;
+                       break;
+               case 'd':                                                                       /* database */
                        db = optarg;
                        break;
                case 'u':                                                                       /* username */
                        db_user = optarg;
                        break;
                case 'p':                                                                       /* authentication information: password */
-                       asprintf(&db_pass, "%s", optarg);
+                       db_pass = strdup(optarg);
 
                        /* Delete the password from process list */
                        while (*optarg != '\0') {
@@ -203,7 +227,7 @@ process_arguments (int argc, char **argv)
                        verbose++;
                        break;
                case 'V':                                                                       /* version */
-                       print_revision (progname, revision);
+                       print_revision (progname, NP_VERSION);
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
@@ -218,7 +242,7 @@ process_arguments (int argc, char **argv)
                        critical = optarg;
                        break;
                case '?':                                                                       /* help */
-                       usage2 (_("Unknown argument"), optarg);
+                       usage5 ();
                }
        }
 
@@ -242,9 +266,6 @@ validate_arguments (void)
        if (db_host == NULL)
                db_host = strdup("");
 
-       if (db_pass == NULL)
-               db_pass == strdup("");
-
        if (db == NULL)
                db = strdup("");
 
@@ -258,7 +279,7 @@ print_help (void)
        char *myport;
        asprintf (&myport, "%d", MYSQL_PORT);
 
-       print_revision (progname, revision);
+       print_revision (progname, NP_VERSION);
 
        printf (_(COPYRIGHT), copyright, email);
 
@@ -268,11 +289,14 @@ print_help (void)
 
        print_usage ();
 
-       printf (_(UT_HELP_VRSN));
+       printf (UT_HELP_VRSN);
+       printf (UT_EXTRA_OPTS);
        printf (" -q, --query=STRING\n");
        printf ("    %s\n", _("SQL query to run. Only first column in first row will be read"));
-       printf (_(UT_WARN_CRIT_RANGE));
-       printf (_(UT_HOST_PORT), 'P', myport);
+       printf (UT_WARN_CRIT_RANGE);
+       printf (UT_HOST_PORT, 'P', myport);
+       printf (" %s\n", "-s, --socket=STRING");
+       printf ("    %s\n", _("Use the specified socket (has no effect if -H is used)"));
        printf (" -d, --database=STRING\n");
        printf ("    %s\n", _("Database to check"));
        printf (" -u, --username=STRING\n");
@@ -280,13 +304,22 @@ print_help (void)
        printf (" -p, --password=STRING\n");
        printf ("    %s\n", _("Password to login with"));
        printf ("    ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
+       printf ("    %s\n", _("Your clear-text password could be visible as a process table entry"));
 
        printf ("\n");
+       printf (" %s\n", _("A query is required. The result from the query should be numeric."));
+       printf (" %s\n", _("For extra security, create a user with minimal access."));
 
-       printf ("%s\n", _("A query is required. The result from the query should be numeric."));
-       printf ("%s\n", _("For extra security, create a user with minimal access."));
+#ifdef NP_EXTRA_OPTS
+       printf ("\n");
+       printf ("%s\n", _("Notes:"));
+       printf (UT_EXTRA_OPTS_NOTES);
+       printf ("\n");
+       printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
+       printf (" %s\n", _("overriding any my.cnf settings."));
+#endif
 
-       printf (_(UT_SUPPORT));
+       printf (UT_SUPPORT);
 }
 
 
@@ -294,6 +327,6 @@ void
 print_usage (void)
 {
   printf (_("Usage:"));
-       printf ("%s -q SQL_query [-w warn] [-c crit]\n",progname);
-  printf ("[-d database] [-H host] [-P port] [-u user] [-p password]\n");
+  printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname);
+  printf ("       [-d database] [-u user] [-p password]\n");
 }