remove_perfdata.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*****************************************************************************
  2. *
  3. * Nagios remove perfdata plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2002-2017 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the remove_perfdata plugin
  11. *
  12. * Removes perfdata from a specified plugin's output. Optionally,
  13. * you can choose to remove any long output as well
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "remove_perfdata";
  32. const char *copyright = "2002-2017";
  33. const char *email = "devel@nagios-plugins.org";
  34. /* timeout should be handled in the plugin being called */
  35. #define DEFAULT_TIMEOUT 300
  36. #include "common.h"
  37. #include "utils.h"
  38. #include "utils_cmd.h"
  39. #include <ctype.h>
  40. static const char **process_arguments(int, char **);
  41. void validate_arguments(char **);
  42. void print_help(void);
  43. void print_usage(void);
  44. int remove_perfdata = 1;
  45. int remove_long_output = 0;
  46. int
  47. main(int argc, char **argv)
  48. {
  49. int result = STATE_UNKNOWN;
  50. int c = 0;
  51. int i = 0;
  52. int j = 0;
  53. char *buf;
  54. char *sub;
  55. char **command_line;
  56. output chld_out;
  57. output chld_err;
  58. setlocale(LC_ALL, "");
  59. bindtextdomain(PACKAGE, LOCALEDIR);
  60. textdomain(PACKAGE);
  61. command_line = (char **) process_arguments(argc, argv);
  62. /* Set signal handling and alarm */
  63. if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  64. die(STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  65. }
  66. (void) alarm((unsigned) DEFAULT_TIMEOUT);
  67. /* catch when the command is quoted */
  68. if (command_line[1] == NULL) {
  69. result = cmd_run(command_line[0], &chld_out, &chld_err, 0);
  70. } else {
  71. result = cmd_run_array(command_line, &chld_out, &chld_err, 0);
  72. }
  73. if (chld_err.lines > 0) {
  74. printf("%s:\n", _("Error output from command"));
  75. for (i = 0; i < chld_err.lines; i++) {
  76. printf("%s\n", chld_err.line[i]);
  77. }
  78. exit(STATE_WARNING);
  79. }
  80. /* Return UNKNOWN or worse if no output is returned */
  81. if (chld_out.lines == 0) {
  82. die(max_state_alt(result, STATE_UNKNOWN), _("No data returned from command\n"));
  83. }
  84. for (i = 0; i < chld_out.lines; i++) {
  85. /* if we're on the first line, remove the perfdata */
  86. if (remove_perfdata && i == 0) {
  87. int in_quotes = 0;
  88. for (j = 0; j < (int) strlen(chld_out.line[i]); j++) {
  89. c = chld_out.line[i][j];
  90. if (c == '"') {
  91. if (in_quotes) {
  92. in_quotes = 0;
  93. }
  94. else {
  95. in_quotes = 1;
  96. }
  97. }
  98. /* when we reach an unquoted |, stop printing */
  99. if (!in_quotes && c == '|')
  100. break;
  101. printf("%c", c);
  102. }
  103. /* and print a newline if we skipped past it */
  104. if (c != '\n') {
  105. printf("\n");
  106. }
  107. }
  108. /* if we don't want long output, don't print it */
  109. else if (remove_long_output && i > 0) {
  110. break;
  111. }
  112. /* for everything else, there's mastercard - or printing the full line to the screen */
  113. else {
  114. printf("%s\n", chld_out.line[i]);
  115. }
  116. }
  117. exit(result);
  118. }
  119. /* process command-line arguments */
  120. static const char **
  121. process_arguments(int argc, char **argv)
  122. {
  123. int c = 0;
  124. int option = 0;
  125. static struct option longopts[] = {
  126. {"help", no_argument, 0, 'h'},
  127. {"version", no_argument, 0, 'V'},
  128. {"remove-long-output", no_argument, 0, 'l'},
  129. {"dont-remove-perfdata", required_argument, 0, 'd'},
  130. {0, 0, 0, 0}
  131. };
  132. while (1) {
  133. c = getopt_long(argc, argv, "+hVld", longopts, &option);
  134. if (c == -1 || c == EOF)
  135. break;
  136. switch (c) {
  137. /* help */
  138. case '?':
  139. usage5();
  140. break;
  141. /* help */
  142. case 'h':
  143. print_help();
  144. exit(EXIT_SUCCESS);
  145. break;
  146. /* version */
  147. case 'V':
  148. print_revision(progname, NP_VERSION);
  149. exit(EXIT_SUCCESS);
  150. /* remove long output */
  151. case 'l':
  152. remove_long_output = 1;
  153. break;
  154. /* don't remove perfdata */
  155. case 'd':
  156. remove_perfdata = 0;
  157. break;
  158. }
  159. }
  160. validate_arguments(&argv[optind]);
  161. return (const char **) &argv[optind];
  162. }
  163. void
  164. validate_arguments(char **command_line)
  165. {
  166. if (command_line[0] == NULL)
  167. usage4(_("Could not parse arguments"));
  168. if ( strncmp(command_line[0], "/", 1) != 0
  169. && strncmp(command_line[0], "./", 2) != 0)
  170. usage4(_("Require path to command"));
  171. }
  172. void
  173. print_help(void)
  174. {
  175. print_revision(progname, NP_VERSION);
  176. printf(COPYRIGHT, copyright, email);
  177. printf("%s\n", _("Removes perfdata from plugin output."));
  178. printf("%s\n", _("Additional switches can be used to remove long output as well."));
  179. printf("\n\n");
  180. print_usage();
  181. printf(UT_HELP_VRSN);
  182. printf(" -l, --remove-long-output\n");
  183. printf(" %s\n", _("Remove long output from specified plugin's output."));
  184. printf(" -d, --dont-remove-perfdata\n");
  185. printf(" %s\n", _("Don't remove perfdata from the specified plugin's output.\n"));
  186. printf("\n");
  187. printf("%s\n", _("Examples:"));
  188. printf("\n");
  189. printf("%s\n", "remove_perfdata /usr/local/nagios/libexec/check_ping -H host");
  190. printf(" %s\n", _("Run check_ping and remove performance data. (Must use full path to plugin.)"));
  191. printf("\n");
  192. printf("%s\n", _("Notes:"));
  193. printf(" %s\n", _("This plugin is a wrapper to take the output of another plugin and alter it."));
  194. printf(" %s\n", _("The full path of the plugin must be provided."));
  195. printf("\n");
  196. printf(UT_SUPPORT);
  197. }
  198. void
  199. print_usage(void)
  200. {
  201. printf("%s\n", _("Usage:"));
  202. printf("%s [-hV] [-l] [-d] <definition of wrapped plugin>\n", progname);
  203. }