Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / shoutcast2_multi @ ee4bc102

Historique | Voir | Annoter | Télécharger (10,6 ko)

1 a2dd3b1b Matt West
#!/usr/bin/perl
2
#
3
=head1 Shoutcast 2.0.x Plugin
4
5
 A Plugin for monitoring a Shoutcast 2.0.x Server (Multigraph)
6
7
=head1 Munin Configuration
8
9
 [shoutcast2_multi]
10
  env.host 127.0.0.1			*default*
11
  env.port 8000					*default*
12
  env.pass changeme				*default*
13
14
=head2 Munin Configuration Explanation
15
16
 host = host we are attempting to connection to, can be ip, or hostname
17
 port = port we need to connect to in order to get to admin.cgi
18
 pass = password to use to authenticate as admin user
19
20
=head1 License
21
22
 GPLv2
23
24
=head1 Magic Markers
25
26
#%# family=auto
27
#%# capabilities=autoconf
28
29
=cut
30
31
use strict;
32
use warnings;
33
use LWP::UserAgent;
34
use XML::Simple;
35
use Munin::Plugin;
36
37
need_multigraph();
38
39
=head1 Variable Declarations
40
41
 This section is mainly for importing / declaring our environment variables.
42
 This is were we will import the data from our plugin-conf.d file so we can
43
 override the default settings which will only work for Shoutcast test configs.
44
45
=cut
46
47
my $host = $ENV{host} || '127.0.0.1';
48
my $port = $ENV{port} || 8000;
49
my $pass = $ENV{pass} || 'changeme';
50
51
# Initialize hashref for storing results information...
52
my $dataRef;
53
54
# Create a hashref for our graph information that we will call up later...
55
my $graphsRef;
56
57
my $ua = LWP::UserAgent->new();
58
$ua->agent('Munin Shoutcast Plugin/0.1');
59
$ua->timeout(5);
60
$ua->credentials($host.':'.$port, 'Shoutcast Server', 'admin'=>$pass);
61
62
=head1 Graphs Declarations
63
64
 The following section of code contains our graph information. This is the data
65
 provided to Munin, so that it may properly draw our graphs based on the values
66
 the plugin returns.
67
68
 While you are free to change colors or labels changing the type, min, or max
69
 could cause unfortunate problems with your graphs.
70
71
=cut
72
73
$graphsRef->{active} = {
74
	config => {
75
		args => '--base 1000 --lower-limit 0',
76
		vlabel => 'Is a DJ Actively Connected?',
77
		category => 'shoutcast2',
78
		title => 'Active States',
79
		info => 'This graph shows us the active state or not, depending on if a DJ is connected',
80
	},
81
	datasrc => [
82
		{ name => 'active', draw => 'AREA', min => '0', max => '1', label => 'On or Off', type => 'GAUGE' },
83
	],
84
};
85
86
$graphsRef->{listeners} = {
87
	config => {
88
		args => '--base 1000 --lower-limit 0',
89
		vlabel => 'Listener Count',
90
		category => 'shoutcast2',
91
		title => 'Listeners',
92
		info => 'This graph shows us the various counts for listener states we are tracking',
93
	},
94
	datasrc => [
95
		{ name => 'maxlisteners', draw => 'STACK', min => '0', label => 'Max Listeners', type => 'GAUGE' },
96
		{ name => 'currlisteners', draw => 'AREA', min => '0', label => 'Current Listeners', type => 'GAUGE' },
97
	],
98
};
99
100
$graphsRef->{sid_active} = {
101
	config => {
102
		args => '--base 1000 --lower-limit 0',
103
		vlabel => 'Is a DJ Actively Connected?',
104
		title => 'Active State',
105
		info => 'This graph shows us the active state or not, depending on if a DJ is connected',
106
	},
107
	datasrc => [
108
		{ name => 'active', draw => 'AREA', min => '0', max => '1', label => 'On or Off', type => 'GAUGE', xmlkey => 'STREAMSTATUS' },
109
	],
110
};
111
112
$graphsRef->{sid_listeners} = {
113
	config => {
114
		args => '--base 1000 --lower-limit 0',
115
		vlabel => 'Listener Count',
116
		title => 'Listeners',
117
		info => 'This graph shows us the various counts for listener states we are tracking',
118
	},
119
	datasrc => [
120
		{ name => 'maxlisteners', draw => 'STACK', min => '0', label => 'Max Listeners', type => 'GAUGE', xmlkey => 'MAXLISTENERS' },
121
		{ name => 'currlisteners', draw => 'AREA', min => '0', label => 'Current Listeners', type => 'GAUGE', xmlkey => 'CURRENTLISTENERS' },
122
		{ name => 'peaklisteners', draw => 'LINE2', min => '0', label => 'Peak Listeners', type => 'GAUGE', xmlkey => 'PEAKLISTENERS' },
123
		{ name => 'uniqlisteners', draw => 'LINE2', min => '0', label => 'Unique Listeners', type => 'GAUGE', xmlkey => 'UNIQUELISTENERS' },
124
	],
125
};
126
127
if (defined($ARGV[0]) && ($ARGV[0] eq 'config')) {
128
	config();
129
	exit;
130
}
131
132
if (defined($ARGV[0]) && ($ARGV[0] eq 'autoconf')) {
133
	check_autoconf();
134
	exit;
135
}
136
137
# I guess we are collecting stats to return, execute main subroutine.
138
main();
139
140
exit;
141
142
sub main {
143
	my ($returnBit,$adminRef) = fetch_admin_data($ua,$host,$port);
144
	if ($returnBit == 0) {
145
		exit;
146
	}
147
	my $streamConfigRef = $adminRef->{STREAMCONFIGS}->{STREAMCONFIG};
148
	my $sidDataRef;
149 8be793e8 Matt West
	if ($adminRef->{STREAMCONFIGS}->{TOTALCONFIGS} == 1) {
150
		my $sid = $streamConfigRef->{id};
151 a2dd3b1b Matt West
		my ($return,$tmpSidRef) = fetch_sid_data($sid);
152
		if ($return == 0) {
153 8be793e8 Matt West
			# Only one stream, and we didn't get a good response.
154
			exit;
155 a2dd3b1b Matt West
		}
156
		$sidDataRef->{$sid} = $tmpSidRef;
157 8be793e8 Matt West
	} else {
158
		foreach my $sid (keys %{$streamConfigRef}) {
159
			my ($return,$tmpSidRef) = fetch_sid_data($sid);
160
			if ($return == 0) {
161
				next;
162
			}
163
			$sidDataRef->{$sid} = $tmpSidRef;
164
		}
165 a2dd3b1b Matt West
	}
166
	print_active_data($sidDataRef);
167
	print_listener_data($adminRef->{STREAMCONFIGS}->{SERVERMAXLISTENERS}, $sidDataRef);
168
	return;
169
}
170
171
sub print_active_data {
172
	my ($sidDataRef) = (@_);
173
	my $globalActive = 0;
174
	foreach my $sid (sort keys %{$sidDataRef}) {
175
		print "multigraph shoutcast2_active.active_sid_$sid\n";
176
		foreach my $dsrc (@{$graphsRef->{sid_active}->{datasrc}}) {
177
			print "$dsrc->{name}.value $sidDataRef->{$sid}->{$dsrc->{xmlkey}}\n";	
178
			if ($sidDataRef->{$sid}->{$dsrc->{xmlkey}} == 1) {
179
				$globalActive = 1;
180
			}
181
		}
182
	}
183
	print "multigraph shoutcast2_active\n";
184
	foreach my $dsrc (@{$graphsRef->{active}->{datasrc}}) {
185
		print "$dsrc->{name}.value $globalActive\n";
186
	}
187
	return;
188
}
189
190
sub print_listener_data {
191
	my ($maxListeners,$sidDataRef) = (@_);
192
	my $globalListeners = 0;
193
	foreach my $sid (sort keys %{$sidDataRef}) {
194
		print "multigraph shoutcast2_listeners.listeners_sid_$sid\n";
195
		foreach my $dsrc (@{$graphsRef->{sid_listeners}->{datasrc}}) {
196
			print "$dsrc->{name}.value $sidDataRef->{$sid}->{$dsrc->{xmlkey}}\n";	
197
			if ($dsrc->{name} eq 'currlisteners') {
198
				$globalListeners += $sidDataRef->{$sid}->{$dsrc->{xmlkey}};
199
			}
200
		}
201
	}
202
	print "multigraph shoutcast2_active\n";
203
	foreach my $dsrc (@{$graphsRef->{listeners}->{datasrc}}) {
204
		if ($dsrc->{name} eq 'maxlisteners') {
205
			print "$dsrc->{name}.value $maxListeners\n";
206
		} else {
207
			print "$dsrc->{name}.value $globalListeners\n";
208
		}
209
	}
210
	return;
211
}
212
213
sub config {
214
	my ($returnBit,$adminRef) = fetch_admin_data($ua,$host,$port);
215
	if ($returnBit == 0) {
216
		# $adminRef returned a string, we'll just print it out.
217
		print "no (error response: $adminRef)\n";
218
		exit;
219
	}
220
	my $streamConfigRef = $adminRef->{STREAMCONFIGS}->{STREAMCONFIG};
221
	my $sidDataRef;
222 8be793e8 Matt West
	if ($adminRef->{STREAMCONFIGS}->{TOTALCONFIGS} == 1) {
223
		my $sid = $streamConfigRef->{id};
224 a2dd3b1b Matt West
		my ($return,$tmpSidRef) = fetch_sid_data($sid);
225
		if ($return == 0) {
226 8be793e8 Matt West
			# Only one stream, and we didn't get a good response.
227
			exit;
228 a2dd3b1b Matt West
		}
229
		$sidDataRef->{$sid} = $tmpSidRef;
230 8be793e8 Matt West
	} else {
231
		foreach my $sid (keys %{$streamConfigRef}) {
232
			my ($return,$tmpSidRef) = fetch_sid_data($sid);
233
			if ($return == 0) {
234
				next;
235
			}
236
			$sidDataRef->{$sid} = $tmpSidRef;
237
		}
238 a2dd3b1b Matt West
	}
239
	print_active_config($sidDataRef);
240
	print_listener_config($sidDataRef);
241
	return;	
242
}
243
244
sub print_active_config {
245
	my ($sidDataRef) = (@_);
246
	foreach my $sid (sort keys %{$sidDataRef}) {
247
		# Print the Config Info First
248
		print "multigraph shoutcast2_active.active\_sid\_$sid\n";
249
		print "graph_title ".$graphsRef->{sid_active}->{config}->{title}." for StreamID: $sid\n";
250
		print "graph_args ".$graphsRef->{sid_active}->{config}->{args}."\n";
251
		print "graph_vlabel ".$graphsRef->{sid_active}->{config}->{vlabel}."\n";
252
		print "graph_category streamid_$sid\n";
253
		print "graph_info ".$graphsRef->{sid_active}->{config}->{info}."\n";
254
		# Print the Data Value Info
255
		foreach my $dsrc (@{$graphsRef->{sid_active}->{datasrc}}) {
256
			while ( my ($key, $value) = each (%{$dsrc})) {
257
				next if ($key eq 'name');
258
				next if ($key eq 'xmlkey');
259
				print "$dsrc->{name}.$key $value\n";
260
			}
261
		}
262
	}
263
	print "multigraph shoutcast2_active\n";
264
	print "graph_title ".$graphsRef->{active}->{config}->{title}."\n";
265
	print "graph_args ".$graphsRef->{active}->{config}->{args}."\n";
266
	print "graph_vlabel ".$graphsRef->{active}->{config}->{vlabel}."\n";
267 8be793e8 Matt West
	print "graph_category ".$graphsRef->{active}->{config}->{category}."\n";
268 a2dd3b1b Matt West
	print "graph_info ".$graphsRef->{active}->{config}->{info}."\n";
269
	# Print the Data Value Info
270
	foreach my $dsrc (@{$graphsRef->{active}->{datasrc}}) {
271
		while ( my ($key, $value) = each (%{$dsrc})) {
272
			next if ($key eq 'name');
273
			print "$dsrc->{name}.$key $value\n";
274
		}
275
	}
276
	return;
277
}
278
279
sub print_listener_config {
280
	my ($sidDataRef) = (@_);
281
	foreach my $sid (sort keys %{$sidDataRef}) {
282
		# Print the Config Info First
283
		print "multigraph shoutcast2_listeners.listeners\_sid\_$sid\n";
284
		print "graph_title ".$graphsRef->{sid_listeners}->{config}->{title}." for StreamID: $sid\n";
285
		print "graph_args ".$graphsRef->{sid_listeners}->{config}->{args}."\n";
286
		print "graph_vlabel ".$graphsRef->{sid_listeners}->{config}->{vlabel}."\n";
287
		print "graph_category streamid_$sid\n";
288
		print "graph_info ".$graphsRef->{sid_listeners}->{config}->{info}."\n";
289
		# Print the Data Value Info
290
		foreach my $dsrc (@{$graphsRef->{sid_listeners}->{datasrc}}) {
291
			while ( my ($key, $value) = each (%{$dsrc})) {
292
				next if ($key eq 'name');
293
				next if ($key eq 'xmlkey');
294
				print "$dsrc->{name}.$key $value\n";
295
			}
296
		}
297
	}
298
	print "multigraph shoutcast2_listeners\n";
299
	print "graph_title ".$graphsRef->{listeners}->{config}->{title}."\n";
300
	print "graph_args ".$graphsRef->{listeners}->{config}->{args}."\n";
301
	print "graph_vlabel ".$graphsRef->{listeners}->{config}->{vlabel}."\n";
302 8be793e8 Matt West
	print "graph_category ".$graphsRef->{listeners}->{config}->{category}."\n";
303 a2dd3b1b Matt West
	print "graph_info ".$graphsRef->{listeners}->{config}->{info}."\n";
304
	# Print the Data Value Info
305
	foreach my $dsrc (@{$graphsRef->{listeners}->{datasrc}}) {
306
		while ( my ($key, $value) = each (%{$dsrc})) {
307
			next if ($key eq 'name');
308
			print "$dsrc->{name}.$key $value\n";
309
		}
310
	}
311
	return;
312
}
313
314
sub check_autoconf {
315
	my ($returnBit,$adminRef) = fetch_admin_data($ua,$host,$port);
316
	if ($returnBit == 0) {
317
		# $adminRef returned a string, we'll just print it out.
318
		print "no (error response: $adminRef)\n";
319
	} else {
320
		print "yes\n";
321
	}
322
	return;
323
}
324
325
sub fetch_sid_data {
326
	my ($sid) = (@_);
327
	my $url = 'http://'.$host.':'.$port.'/stats?sid='.$sid;
328
	my $response = $ua->get($url);
329
	if ($response->is_success) {
330
		my $returnRef = XMLin($response->decoded_content);
331
		return (1, $returnRef);	
332
	} else {
333
		return (0, $response->status_line);
334
	}
335
}
336
337
sub fetch_admin_data {
338
	my $url = 'http://'.$host.':'.$port.'/admin.cgi?sid=1&mode=viewxml&page=6';
339
	my $response = $ua->get($url);
340
	if ($response->is_success) {
341
		my $returnRef = XMLin($response->decoded_content);
342
		if (($returnRef->{STREAMCONFIGS}->{TOTALCONFIGS} > 0) && (defined($returnRef->{STREAMCONFIGS}->{STREAMCONFIG}))) {
343
			return (1, $returnRef);	
344
		} else {
345
			return (0, 'Unable to Detect any Stream Configurations');
346
		}
347
	} else {
348
		return (0, $response->status_line);
349
	}
350
}