Back to index

nagios-plugins  1.4.16
urlize.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios urlize plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2000-2007 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the urlize plugin
00011 * 
00012 * This plugin wraps the text output of another command (plugin) in HTML <A>
00013 * tags, thus displaying the child plugin's output as a clickable link in the
00014 * Nagios status screen.  This plugin returns the status of the invoked plugin.
00015 * 
00016 * 
00017 * This program is free software: you can redistribute it and/or modify
00018 * it under the terms of the GNU General Public License as published by
00019 * the Free Software Foundation, either version 3 of the License, or
00020 * (at your option) any later version.
00021 * 
00022 * This program is distributed in the hope that it will be useful,
00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 * GNU General Public License for more details.
00026 * 
00027 * You should have received a copy of the GNU General Public License
00028 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00029 * 
00030 * 
00031 *****************************************************************************/
00032 
00033 const char *progname = "urlize";
00034 const char *copyright = "2000-2006";
00035 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00036 
00037 #include "common.h"
00038 #include "utils.h"
00039 #include "popen.h"
00040 
00041 #define PERF_CHARACTER "|"
00042 #define NEWLINE_CHARACTER '\n'
00043 
00044 void print_help (void);
00045 void print_usage (void);
00046 
00047 int
00048 main (int argc, char **argv)
00049 {
00050        int found = 0, result = STATE_UNKNOWN;
00051        char *url = NULL;
00052        char *cmd;
00053        char *buf;
00054        char *nstr;
00055        char tstr[MAX_INPUT_BUFFER];
00056 
00057        int c;
00058        int option = 0;
00059        static struct option longopts[] = {
00060               {"help", no_argument, 0, 'h'},
00061               {"version", no_argument, 0, 'V'},
00062               {"url", required_argument, 0, 'u'},
00063               {0, 0, 0, 0}
00064        };
00065 
00066        setlocale (LC_ALL, "");
00067        bindtextdomain (PACKAGE, LOCALEDIR);
00068        textdomain (PACKAGE);
00069 
00070        /* Need at least 2 args */
00071        if (argc < 3) {
00072               print_help();
00073               exit (STATE_UNKNOWN);
00074        }
00075 
00076        while (1) {
00077               c = getopt_long (argc, argv, "+hVu:", longopts, &option);
00078 
00079               if (c == -1 || c == EOF)
00080                      break;
00081 
00082               switch (c) {
00083               case 'h':     /* help */
00084                      print_help ();
00085                      exit (EXIT_SUCCESS);
00086                      break;
00087               case 'V':     /* version */
00088                      print_revision (progname, NP_VERSION);
00089                      exit (EXIT_SUCCESS);
00090                      break;
00091               case 'u':
00092                      url = strdup (argv[optind]);
00093                      break;
00094               case '?':
00095               default:
00096                      usage5 ();
00097               }
00098        }
00099 
00100        if (url == NULL)
00101               url = strdup (argv[optind++]);
00102 
00103        cmd = strdup (argv[optind++]);
00104        for (c = optind; c < argc; c++) {
00105               asprintf (&cmd, "%s %s", cmd, argv[c]);
00106        }
00107 
00108        child_process = spopen (cmd);
00109        if (child_process == NULL) {
00110               printf (_("Could not open pipe: %s\n"), cmd);
00111               exit (STATE_UNKNOWN);
00112        }
00113 
00114        child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
00115        if (child_stderr == NULL) {
00116               printf (_("Could not open stderr for %s\n"), cmd);
00117        }
00118 
00119        bzero(tstr, sizeof(tstr));
00120        buf = malloc(MAX_INPUT_BUFFER);
00121        printf ("<A href=\"%s\">", argv[1]);
00122        while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
00123               found++;
00124               /* Collect the string in temp str so we can tokenize */
00125               strcat(tstr, buf);
00126        }
00127 
00128        if (!found)
00129               die (STATE_UNKNOWN,
00130                    _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
00131                    argv[0], cmd);
00132 
00133 
00134        /* chop the newline character */
00135        if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
00136               *nstr = '\0';
00137 
00138        /* tokenize the string for Perfdata if there is some */
00139        nstr = strtok(tstr, PERF_CHARACTER);
00140        printf ("%s", nstr);
00141        printf ("</A>");
00142        nstr = strtok(NULL, PERF_CHARACTER);
00143        if (nstr != NULL)
00144               printf (" | %s", nstr);
00145 
00146        /* close the pipe */
00147        result = spclose (child_process);
00148 
00149        /* WARNING if output found on stderr */
00150        if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
00151               result = max_state (result, STATE_WARNING);
00152 
00153        /* close stderr */
00154        (void) fclose (child_stderr);
00155 
00156        return result;
00157 }
00158 
00159 
00160 
00161 void
00162 print_help (void)
00163 {
00164        print_revision (progname, NP_VERSION);
00165 
00166        printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
00167        printf (COPYRIGHT, copyright, email);
00168 
00169        printf ("%s\n", _("This plugin wraps the text output of another command (plugin)"));
00170   printf ("%s\n", _("in HTML <A> tags, thus displaying the child plugin's output as a clickable link in"));
00171   printf ("%s\n", _("the Nagios status screen.  This plugin returns the status of the invoked plugin."));
00172 
00173   printf ("\n\n");
00174 
00175        print_usage ();
00176 
00177   printf (UT_HELP_VRSN);
00178 
00179   printf ("\n");
00180   printf ("%s\n", _("Examples:"));
00181        printf ("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected"));
00182   printf ("%s\n\n", _("data to the plugin. For example, in:"));
00183   printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'"));
00184   printf ("    %s\n", _("the shell will remove the single quotes and urlize will see:"));
00185   printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r two words"));
00186   printf ("    %s\n\n", _("You probably want:"));
00187   printf (" %s\n", _("urlize http://example.com/ \"check_http -H example.com -r 'two words'\""));
00188 
00189        printf (UT_SUPPORT);
00190 }
00191 
00192 
00193 
00194 void
00195 print_usage (void)
00196 {
00197   printf ("%s\n", _("Usage:"));
00198        printf ("%s <url> <plugin> <arg1> ... <argN>\n", progname);
00199 }