Merge pull request #2046 from RincewindsHat/fix/format_string
[monitoring-plugins.git] / plugins / urlize.c
blob1aa4e4254c7f705cbbc6fe96484cd6285be99f16
1 /*****************************************************************************
3 * Monitoring urlize plugin
5 * License: GPL
6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
8 * Description:
10 * This file contains the urlize plugin
12 * This plugin wraps the text output of another command (plugin) in HTML <A>
13 * tags. This plugin returns the status of the invoked plugin.
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.
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.
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/>.
30 *****************************************************************************/
32 const char *progname = "urlize";
33 const char *copyright = "2000-2024";
34 const char *email = "devel@monitoring-plugins.org";
36 #include "common.h"
37 #include "utils.h"
38 #include "popen.h"
40 #define PERF_CHARACTER "|"
41 #define NEWLINE_CHARACTER '\n'
43 void print_help(void);
44 void print_usage(void);
46 int main(int argc, char **argv) {
47 int found = 0, result = STATE_UNKNOWN;
48 char *url = NULL;
49 char *cmd;
50 char *buf;
51 char *nstr;
52 char tstr[MAX_INPUT_BUFFER];
54 int c;
55 int option = 0;
56 static struct option longopts[] = {
57 {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"url", required_argument, 0, 'u'}, {0, 0, 0, 0}};
59 setlocale(LC_ALL, "");
60 bindtextdomain(PACKAGE, LOCALEDIR);
61 textdomain(PACKAGE);
63 /* Need at least 2 args */
64 if (argc < 3) {
65 print_help();
66 exit(STATE_UNKNOWN);
69 while (1) {
70 c = getopt_long(argc, argv, "+hVu:", longopts, &option);
72 if (c == -1 || c == EOF)
73 break;
75 switch (c) {
76 case 'h': /* help */
77 print_help();
78 exit(EXIT_SUCCESS);
79 break;
80 case 'V': /* version */
81 print_revision(progname, NP_VERSION);
82 exit(EXIT_SUCCESS);
83 break;
84 case 'u':
85 url = strdup(argv[optind]);
86 break;
87 case '?':
88 default:
89 usage5();
93 if (url == NULL)
94 url = strdup(argv[optind++]);
96 cmd = strdup(argv[optind++]);
97 for (c = optind; c < argc; c++) {
98 xasprintf(&cmd, "%s %s", cmd, argv[c]);
101 child_process = spopen(cmd);
102 if (child_process == NULL) {
103 printf(_("Could not open pipe: %s\n"), cmd);
104 exit(STATE_UNKNOWN);
107 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r");
108 if (child_stderr == NULL) {
109 printf(_("Could not open stderr for %s\n"), cmd);
112 bzero(tstr, sizeof(tstr));
113 buf = malloc(MAX_INPUT_BUFFER);
114 printf("<A href=\"%s\">", argv[1]);
115 while (fgets(buf, MAX_INPUT_BUFFER - 1, child_process)) {
116 found++;
117 /* Collect the string in temp str so we can tokenize */
118 strcat(tstr, buf);
121 if (!found)
122 die(STATE_UNKNOWN, _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"), argv[0], cmd);
124 /* chop the newline character */
125 if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
126 *nstr = '\0';
128 /* tokenize the string for Perfdata if there is some */
129 nstr = strtok(tstr, PERF_CHARACTER);
130 printf("%s", nstr);
131 printf("</A>");
132 nstr = strtok(NULL, PERF_CHARACTER);
133 if (nstr != NULL)
134 printf(" | %s", nstr);
136 /* close the pipe */
137 result = spclose(child_process);
139 /* WARNING if output found on stderr */
140 if (fgets(buf, MAX_INPUT_BUFFER - 1, child_stderr))
141 result = max_state(result, STATE_WARNING);
143 /* close stderr */
144 (void)fclose(child_stderr);
146 return result;
149 void print_help(void) {
150 print_revision(progname, NP_VERSION);
152 printf("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
153 printf(COPYRIGHT, copyright, email);
155 printf("%s\n", _("This plugin wraps the text output of another command (plugin) in HTML <A>"));
156 printf("%s\n", _("tags, thus displaying the child plugin's output as a clickable link in compatible"));
157 printf("%s\n", _("monitoring status screen. This plugin returns the status of the invoked plugin."));
159 printf("\n\n");
161 print_usage();
163 printf(UT_HELP_VRSN);
165 printf("\n");
166 printf("%s\n", _("Examples:"));
167 printf("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected"));
168 printf("%s\n\n", _("data to the plugin. For example, in:"));
169 printf(" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'"));
170 printf(" %s\n", _("the shell will remove the single quotes and urlize will see:"));
171 printf(" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r two words"));
172 printf(" %s\n\n", _("You probably want:"));
173 printf(" %s\n", _("urlize http://example.com/ \"check_http -H example.com -r 'two words'\""));
175 printf(UT_SUPPORT);
178 void print_usage(void) {
179 printf("%s\n", _("Usage:"));
180 printf("%s <url> <plugin> <arg1> ... <argN>\n", progname);