aboutsummaryrefslogtreecommitdiff
path: root/plugins/urlize.c
blob: 12fb3ec9e09eff9e98e0dd28ac2e2ce01673b26d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/******************************************************************************
 *
 * urlize.c
 *
 * Program: plugin wrapper for Nagios
 * License: GPL
 * Copyright (c) 2000 Karl DeBisschop (kdebiss@alum.mit.edu)
 *
 * Last Modified: $Date$
 * 2000-06-01 Karl DeBisschop <karl@debisschop.net>
 *  Written based of concept in urlize.pl
 *
 * Usage: urlize <url> <plugin> <arg1> ... <argN>
 *
 * Description:
 *
 * This plugin wraps the text output of another command (plugin) in HTML
 * <A> tags, thus displaying the plugin output in as a clickable link in
 * the Nagios status screen.  The return status is the same as the plugin
 * invoked by urlize
 *
 * 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.
 *
 *****************************************************************************/

const char *progname = "urlize";
const char *revision = "$Revision$";
const char *copyright = "2000-2003";
const char *email = "nagiosplug-devel@lists.sourceforge.net";

#include "common.h"
#include "utils.h"
#include "popen.h"

void
print_usage (void)
{
	printf (_("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n"), progname);
}

void
print_help (void)
{
	print_revision (progname, revision);

	printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
	printf (_(COPYRIGHT), copyright, email);

	printf (_("\n\
This plugin wraps the text output of another command (plugin) in HTML\n\
<A> tags, thus displaying the plugin output in as a clickable link in\n\
the Nagios status screen.  The return status is the same as the invoked\n\
plugin.\n\n"));

	print_usage ();

	printf (_("\n\
Pay close attention to quoting to ensure that the shell passes the expected\n\
data to the plugin. For example, in:\n\
\n\
    urlize http://example.com/ check_http -H example.com -r 'two words'\n\
\n\
the shell will remove the single quotes and urlize will see:\n\
\n\
    urlize http://example.com/ check_http -H example.com -r two words\n\
\n\
You probably want:\n\
\n\
    urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
	exit (STATE_OK);
}

int
main (int argc, char **argv)
{
	int i = 0, found = 0, result = STATE_UNKNOWN;
	char *cmd = NULL;
	char input_buffer[MAX_INPUT_BUFFER];

	if (argc < 2) {
		print_usage ();
		exit (STATE_UNKNOWN);
	}

	if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
		print_help ();
		exit (STATE_OK);
	}

	if (!strcmp (argv[1], "-V") || !strcmp (argv[1], "--version")) {
		print_revision (progname, revision);
		exit (STATE_OK);
	}

	if (argc < 2) {
		print_usage ();
		exit (STATE_UNKNOWN);
	}

	asprintf (&cmd, "%s", argv[2]);
	for (i = 3; i < argc; i++) {
		asprintf (&cmd, "%s %s", cmd, argv[i]);
	}

	child_process = spopen (cmd);
	if (child_process == NULL) {
		printf (_("Could not open pipe: %s\n"), cmd);
		exit (STATE_UNKNOWN);
	}

	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
	if (child_stderr == NULL) {
		printf (_("Could not open stderr for %s\n"), cmd);
	}

	printf ("<A href=\"%s\">", argv[1]);
	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
		found++;
		if (index (input_buffer, '\n')) {
			input_buffer[strcspn (input_buffer, "\n")] = 0;
			printf ("%s", input_buffer);
		}
		else {
			printf ("%s", input_buffer);
		}
	}

	if (!found) {
		printf (_("%s problem - No data recieved from host\nCMD: %s</A>\n"), argv[0],
						cmd);
		exit (STATE_UNKNOWN);
	}

	/* 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);

	printf ("</A>\n");
	return result;
}