1 /*****************************************************************************
3 * Monitoring urlize plugin
6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
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";
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
;
52 char tstr
[MAX_INPUT_BUFFER
];
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
);
63 /* Need at least 2 args */
70 c
= getopt_long(argc
, argv
, "+hVu:", longopts
, &option
);
72 if (c
== -1 || c
== EOF
)
80 case 'V': /* version */
81 print_revision(progname
, NP_VERSION
);
85 url
= strdup(argv
[optind
]);
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
);
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
)) {
117 /* Collect the string in temp str so we can tokenize */
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
)
128 /* tokenize the string for Perfdata if there is some */
129 nstr
= strtok(tstr
, PERF_CHARACTER
);
132 nstr
= strtok(NULL
, PERF_CHARACTER
);
134 printf(" | %s", nstr
);
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
);
144 (void)fclose(child_stderr
);
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."));
163 printf(UT_HELP_VRSN
);
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'\""));
178 void print_usage(void) {
179 printf("%s\n", _("Usage:"));
180 printf("%s <url> <plugin> <arg1> ... <argN>\n", progname
);