| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /******************************************************************************
- *
- * Program: Inverting plugin wrapper for Nagios
- * License: GPL
- *
- * License Information:
- *
- * 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 2 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, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
- *
- *****************************************************************************/
- const char *progname = "negate";
- #define REVISION "$Revision$"
- #define COPYRIGHT "2002"
- #define AUTHOR "Karl DeBisschop"
- #define EMAIL "kdebisschop@users.sourceforge.net"
- #define SUMMARY "Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n"
- #define OPTIONS "\
- [-t timeout] <definition of wrapped plugin>"
- #define LONGOPTIONS "\
- -t, --timeout=INTEGER\n\
- Terminate test if timeout limit is exceeded (default: %d)\n\
- [keep this less than the plugin timeout to retain CRITICAL status]\n"
- #define EXAMPLES "\
- negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
- Run check_ping and invert result. Must use full path to plugin\n\
- negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
- Use single quotes if you need to retain spaces\n"
- #define DESCRIPTION "\
- This plugin is a wrapper to take the output of another plugin and invert it.\n\
- If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
- If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
- Otherwise, the output state of the wrapped plugin is unchanged.\n"
- #define DEFAULT_TIMEOUT 9
- #include "common.h"
- #include "utils.h"
- #include "popen.h"
- char *command_line;
- int process_arguments (int, char **);
- int validate_arguments (void);
- void print_usage (void);
- void print_help (void);
- /******************************************************************************
- The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
- tags in the comments. With in the tags, the XML is assembled sequentially.
- You can define entities in tags. You also have all the #defines available as
- entities.
- Please note that all tags must be lowercase to use the DocBook XML DTD.
- @@-<article>
- <sect1>
- <title>Quick Reference</title>
- <!-- The refentry forms a manpage -->
- <refentry>
- <refmeta>
- <manvolnum>5<manvolnum>
- </refmeta>
- <refnamdiv>
- <refname>&progname;</refname>
- <refpurpose>&SUMMARY;</refpurpose>
- </refnamdiv>
- </refentry>
- </sect1>
- <sect1>
- <title>FAQ</title>
- </sect1>
- <sect1>
- <title>Theory, Installation, and Operation</title>
- <sect2>
- <title>General Description</title>
- <para>
- &DESCRIPTION;
- </para>
- </sect2>
- <sect2>
- <title>Future Enhancements</title>
- <para>ToDo List</para>
- <itemizedlist>
- <listitem>Add option to do regex substitution in output text</listitem>
- </itemizedlist>
- </sect2>
- <sect2>
- <title>Functions</title>
- -@@
- ******************************************************************************/
- int
- main (int argc, char **argv)
- {
- int found = 0, result = STATE_UNKNOWN;
- char input_buffer[MAX_INPUT_BUFFER];
- if (process_arguments (argc, argv) == ERROR)
- usage ("Could not parse arguments\n");
- /* Set signal handling and alarm */
- if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
- terminate (STATE_UNKNOWN, "Cannot catch SIGALRM");
- (void) alarm ((unsigned) timeout_interval);
- child_process = spopen (command_line);
- if (child_process == NULL)
- terminate (STATE_UNKNOWN, "Could not open pipe: %s\n", command_line);
- child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
- if (child_stderr == NULL) {
- printf ("Could not open stderr for %s\n", command_line);
- }
- while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
- found++;
- if (strchr (input_buffer, '\n')) {
- input_buffer[strcspn (input_buffer, "\n")] = 0;
- printf ("%s\n", input_buffer);
- }
- else {
- printf ("%s\n", input_buffer);
- }
- }
- if (!found)
- terminate (STATE_UNKNOWN,\
- "%s problem - No data recieved from host\nCMD: %s\n",\
- argv[0], command_line);
- /* close the pipe */
- result = spclose (child_process);
- /* WARNING if output found on stderr */
- if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
- result = max_state (result, STATE_WARNING);
- /* close stderr */
- (void) fclose (child_stderr);
- if (result == STATE_OK)
- exit (STATE_CRITICAL);
- else if (result == STATE_CRITICAL)
- exit (EXIT_SUCCESS);
- else
- exit (result);
- }
- void
- print_help (void)
- {
- print_revision (progname, REVISION);
- printf
- ("Copyright (c) %s %s <%s>\n\n%s\n",
- COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
- print_usage ();
- printf
- ("\nOptions:\n" LONGOPTIONS "\n" "Examples:\n" EXAMPLES "\n"
- DESCRIPTION "\n", DEFAULT_TIMEOUT);
- support ();
- }
- void
- print_usage (void)
- {
- printf ("Usage:\n" " %s %s\n"
- " %s (-h | --help) for detailed help\n"
- " %s (-V | --version) for version information\n",
- progname, OPTIONS, progname, progname);
- }
- /******************************************************************************
- @@-
- <sect3>
- <title>process_arguments</title>
- <para>This function parses the command line into the needed
- variables.</para>
- <para>Aside from the standard 'help' and 'version' options, there
- is a only a 'timeout' option.No validation is currently done.</para>
- </sect3>
- -@@
- ******************************************************************************/
- /* process command-line arguments */
- int
- process_arguments (int argc, char **argv)
- {
- int c;
- int option_index = 0;
- static struct option long_options[] = {
- {"help", no_argument, 0, 'h'},
- {"version", no_argument, 0, 'V'},
- {"timeout", required_argument, 0, 't'},
- {0, 0, 0, 0}
- };
- while (1) {
- c = getopt_long (argc, argv, "+hVt:",
- long_options, &option_index);
- if (c == -1 || c == EOF)
- break;
- switch (c) {
- case '?': /* help */
- usage3 ("Unknown argument", optopt);
- case 'h': /* help */
- print_help ();
- exit (EXIT_SUCCESS);
- case 'V': /* version */
- print_revision (progname, REVISION);
- exit (EXIT_SUCCESS);
- case 't': /* timeout period */
- if (!is_integer (optarg))
- usage2 ("Timeout Interval must be an integer", optarg);
- timeout_interval = atoi (optarg);
- break;
- }
- }
- asprintf (&command_line, "%s", argv[optind]);
- for (c = optind+1; c < argc; c++) {
- asprintf (&command_line, "%s %s", command_line, argv[c]);
- }
- return validate_arguments ();
- }
- /******************************************************************************
- @@-
- <sect3>
- <title>validate_arguments</title>
- <para>No validation is currently done.</para>
- </sect3>
- -@@
- ******************************************************************************/
- int
- validate_arguments ()
- {
- if (command_line == NULL)
- return ERROR;
- return STATE_OK;
- }
- /******************************************************************************
- @@-
- </sect2>
- </sect1>
- </article>
- -@@
- ******************************************************************************/
|