File: munin-node-simple.in

package info (click to toggle)
munin 1.2.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,940 kB
  • ctags: 98
  • sloc: sh: 4,215; makefile: 452; perl: 135
file content (208 lines) | stat: -rwxr-xr-x 4,235 bytes parent folder | download
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/perl -wT
#
# Copyright (C) 2004 Jimmy Olsen, Audun Ytterdal
#
# 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; version 2 dated June,
# 1991.
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# Simple munin-node. Should be run from inetd or similar.
#
# This should be as small and elegant as possible, to make for easier
# reviewing by sysadmins that want to do simple munin-stuff in high
# security environments.
#
# $Log$
# Revision 1.3  2004/11/16 20:00:42  jimmyo
# License cleanups.
#
# Revision 1.2  2004/01/15 15:20:01  jimmyo
# Making things workable after name change. Upping for test verwion.
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:06  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.3  2003/11/24 10:15:56  jimmyo
# Ready for 0.9.9 release
#
# Revision 1.2  2003/11/19 17:26:30  jimmyo
# Now works with netcat
#
# Revision 1.1  2003/11/19 17:09:46  jimmyo
# New mini-client. Not finished yet (only talks on stdout right now).
#
#

use strict;

$| = 1;

my $clientdir = "@@CONFIDR@@/node.d";
my $conffile  = "@@CONFIDR@@/node-simple.conf";
my $version   = "@@VERSION@@";

# Empty environment
%ENV = ();
$ENV{PATH}  = "/bin:/usr/bin";

my $config  = &read_config($conffile);
my $plugins = &read_plugin_dir($clientdir);

my $hostname;
if (defined $config->{hostname})
{
	$hostname = $config->{hostname};
}
elsif (defined $config->{host_name})
{
	$hostname = $config->{host_name};
}
else
{
	$hostname = `hostname`;
	chomp $hostname;
}

print "# munin node at $hostname\n";

while (<>)
{
	if (/^\s*list(?:\s+\S+|)\s*$/i)
	{ # Print plugin list
		print (join (' ', keys (%{$plugins})), "\n");
	}
	elsif (/^\s*version\s*/i)
	{ # Print version number
		print "# munin node simple on $hostname version: $version\n";
	}
	elsif (/^\s*quit\s*/i or /^\s*\.\s*$/)
	{ # Drop out
		exit (0);
	}
	elsif (/^\s*config\s+([A-Za-z0-9_]+)\s*$/i)
	{
		print &run_file ($plugins->{$1}, "config");
		print ".\n";
	}
	elsif (/^\s*fetch\s+([A-Za-z0-9_]+)\s*$/i)
	{
		print &run_file ($plugins->{$1});
		print ".\n";
	}
	else
	{
		print "# Unknown command. Try list, config, fetch, version or quit\n";
	}
}

exit (0);

sub read_plugin_dir
{
	my $cdir = shift;
	my $ret  = {};
	
    # Open and read the names of the files in the plugin directory
	if (-d $cdir)
	{
		opendir (DIR, $clientdir) or die "Could not open dir \"$cdir\" for reading: $!";
		my @files = grep {!/[^A-Za-z0-9_]/} readdir (DIR); # No funky chars
		closedir (DIR);

		foreach my $file (@files)
		{
			$ret->{$file} = "$cdir/$file";
		}
	}

	return $ret;
}

sub read_config
{
	my $file = shift;
	my $ret  = {};

	if (-f $file)
	{
		if (open (CONF, $file))
		{
			while (<CONF>)
			{
				chomp;
				s/#.*//; # Skip comments
				next unless /\S/; # Skip empty lines

				if (/^\s*([a-zA-Z0-9_]+)\s+(\S+)$/)
				{
					$ret->{$1} = $2;
				}
				else
				{
					warn "Warning: unreadable configuration line in \"$file\": \"$_\".";
				}
			}
			close (CONF);
		}
	}
	return $ret;
}

sub run_file
{
	my $file = shift;
	my $arg  = shift;
	
	if (!defined $file or ! -e $file)
	{
		return "# Plugin does not exist.\n.\n";
	}

	my $pid = open (CHILD, "-|");

	if (!defined $pid) # Something went wrong
	{
		return "# Fork error: $!\n.\n";
	}
	elsif ($pid) # Parent
	{
		my $tmp = join ('', <CHILD>);
		close (CHILD);
		if ($?)
		{
			return "# Error, plugin exited with status $?.\n";
		}
		else
		{
			return $tmp;
		}
	}
	else # Child
	{
		if (defined $arg)
		{
			# print "# Executing: \"$file\" \"$arg\".\n";
			exec $file $file, $arg;
		}
		else
		{
			# print "# Executing: \"$file\".\n";
			exec $file $file;
		}
	}
	# notreached
}