Projet

Général

Profil

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

root / plugins / network / fms_apps @ 17f78427

Historique | Voir | Annoter | Télécharger (9,41 ko)

1
#!/usr/bin/perl -w
2
#
3
# Plugin to monitor active connections in applications of Flash Media Server.
4
#
5
# Parameters:
6
#
7
#       config   (required)
8
#       autoconf (optional - only used by munin-config)
9
#
10
# Requirements:
11
#
12
#       libwww-perl (LWP) Perl library
13
#	      Proc::ProcessTable Perl module
14
#
15
# Tested with:
16
#       Debian Etch
17
#       Macromedia Flash Media Server 2.0.3 r68
18
#       Adobe Flash Media Server 3.0.1 r123
19
#       Adobe Flash Media Server 3.5.0 r405
20
#
21
# $Log$
22
# Revision 1.0  2009/01/25 05:07:23  muzso
23
# Initial release.
24
#
25
# Usage
26
# -----
27
#
28
#   1. You will need the following Perl modules for this plugin to work:
29
#
30
#        LWP - The World-Wide Web library for Perl
31
#        Used modules: HTTP::Request, LWP::Simple, LWP::UserAgent
32
#        http://search.cpan.org/perldoc?LWP
33
#
34
#        Proc::ProcessTable - Perl extension to access the unix process table
35
#        http://search.cpan.org/perldoc?Proc::ProcessTable
36
#
37
#      On a Debian/Ubuntu system:
38
#        apt-get install libwww-perl libproc-process-perl
39
#
40
#      In Ubuntu and recent Debian (Lenny and later) versions the latter is
41
#      renamed to libproc-processtable-perl, but for now (at the time of
42
#      writing) libproc-process-perl still exists as a dummy package to provide
43
#      backward compatibility.
44
#
45
#   2. You've to publish the "getApps" and "getAppStats" commands of the
46
#      Flash Media Administration Server for use through the HTTP admin API.
47
#      This means that you've to set "USERS.HTTPCOMMAND_ALLOW" to "true"
48
#      in ${FMS_DIR}/conf/fms.ini and in ${FMS_DIR}/conf/Users.xml set the
49
#      value of the node "Root/AdminServer/HTTPCommands/Allow" to contain
50
#      "getApps" and "getAppStats" too (by default it allows only "ping").
51
#
52
#   3. If you want to monitor an FMS running locally (ie. on the same server
53
#      where this plugin is deployed), then the plugin will autoconfigure
54
#      itself by fetching parameters from the ${FMS_DIR}/conf/fms.ini config
55
#      file of your FMS installation (autoconfigure will only work if the Flash
56
#      Media Administration Server is running since the plugin fetches the
57
#      location of the FMS directory through the current working directory of
58
#      the "fmsadmin" process). For this you have to run the plugin with root
59
#      privileges. To do so, specify in the Munin node plugin config file to
60
#      use "root" user for the plugin.
61
#
62
#      On Debian/Ubuntu this file can be found at
63
#        /etc/munin/plugin-conf.d/munin-node
64
#
65
#      The entry for the plugin should look like this:
66
#        [fms_apps]
67
#        user = root
68
#
69
#      If autoconfiguration does not work or you want to specify the parameters
70
#      yourself (eg. you want to monitor an FMS that is on a different host),
71
#      then add a plugin entry to the Munin node plugin config file like this:
72
#        [fms_apps]
73
#        env.fms_admin_host = fms.example.com
74
#        env.fms_admin_port = 1111
75
#        env.fms_admin_username = admin
76
#        env.fms_admin_password = the_admin_password
77
#
78
#     For a local FMS the fms_admin_host can be set to "localhost".
79
#
80
#     If you're using the env.fms_admin_* variables for configuration, then
81
#     the plugin does not require root privileges.
82
#
83
#     However if doing so, it is strongly advised that you restrict access to
84
#     the Munin node plugin config file. It should be readable only by the
85
#     Munin node daemon since your FMS admin password should not be accessible
86
#     by any other user.
87
#     In case of Debian/Ubuntu the /etc/munin/plugin-conf.d/munin-node file
88
#     is owned by root and the munin-node process runs with root privileges,
89
#     so the correct permission is to have the file readable only by root.
90
#
91
#
92
#
93
# Note: in case something "bad" happens (eg. plugin is run as non-root and
94
#       autoconfiguration is used) the plugin writes some informative message
95
#       to stderr to make debugging easier. This should not affect the normal
96
#       operation of munin-node since only the stdout of the plugins is used
97
#       and that's always kept as munin-node expects.
98
#
99
#
100
#
101
# Magic markers (optional - used by munin-config and installation scripts):
102
#
103
#%# family=auto
104
#%# capabilities=autoconf
105

    
106

    
107

    
108
use strict;
109
use LWP::UserAgent;
110
use LWP::Simple;
111
use Proc::ProcessTable;
112

    
113
my ($host, $port, $username, $password);
114

    
115
sub name_filter { my ($n) = @_; $n =~ s/[^a-zA-Z0-9_]/_/g; $n }
116

    
117
sub get_apps {
118
	my @applist;
119
	my $ua = LWP::UserAgent->new(timeout => 30);
120
	my $url = sprintf("http://%s:%d/admin/getApps?auser=%s\&apswd=%s", $host, $port, $username, $password);
121

    
122
	my $response = $ua->request(HTTP::Request->new('GET', $url));
123
	if ( $response->content =~ /<data>[^<]*(<.*>)[^>]*<\/data>/is ) {
124
		my $apps = $1;
125
		while ( $apps =~ /<_[0-9]+> *([^<]*) *<\/_[0-9]+>/gi ) {
126
			my $appname = $1;
127
			push(@applist, $appname);
128
		}
129
	}
130
	return @applist;
131
}
132

    
133
if ( defined($ENV{fms_admin_host}) and length($ENV{fms_admin_host}) > 0 ) {
134
	$host = $ENV{fms_admin_host};
135
}
136
if ( defined($ENV{fms_admin_port}) and length($ENV{fms_admin_port}) > 0 ) {
137
	$port = $ENV{fms_admin_port};
138
}
139
if ( defined($ENV{fms_admin_username}) and length($ENV{fms_admin_username}) > 0 ) {
140
	$username = $ENV{fms_admin_username};
141
}
142
if ( defined($ENV{fms_admin_password}) and length($ENV{fms_admin_password}) > 0 ) {
143
	$password = $ENV{fms_admin_password};
144
}
145

    
146
if ( !( defined($host) and defined($port) and defined($username) and defined($password) ) ) {
147
	# Autoconfiguration:
148
	# 1. Find the "fmsadmin" process and assume that FMS is installed in the
149
	#    current working directory of the process.
150
	# 2. Look for the FMS config file in ${FMS_DIR}/conf/fms.ini.
151
	# 3. Fetch host, port, admin username and password values from the
152
	#    config file.
153

    
154
	# check that plugin is running with root privileges
155
	if ( $> == 0 ) {
156
		my $ProcTable = new Proc::ProcessTable;
157
		PROC_LOOP: foreach my $proc (@{$ProcTable->table}) {
158
			# match any filename starting with "fmsadmin"
159
			# (this way the plugin might work on platforms other
160
			#  than linux too)
161
			if ( defined($proc->{fname}) and $proc->{fname} =~ /^fmsadmin/i and defined($proc->{cwd}) ) {
162
				my $fms_config = $proc->{cwd} . "/conf/fms.ini";
163
				if ( open(my $fp, '<', $fms_config) ) {
164
					while (my $line = <$fp>) {
165
						chomp($line);
166
						if ( $line =~ /^ *SERVER\.ADMIN_USERNAME *= *([^ ].*) *$/ ) {
167
							$username = $1;
168
						} elsif ( $line =~ /^ *SERVER\.ADMIN_PASSWORD *= *([^ ].*) *$/ ) {
169
							$password = $1;
170
						} elsif ( $line =~ /^ *SERVER\.ADMINSERVER_HOSTPORT *= *([^ ]*)/ ) {
171
							my @data = split(":", $1);
172
							if ( $#data > 0 ) {
173
								if ( defined($data[0]) and length($data[0]) > 0 ) {
174
									$host = $data[0];
175
								} else {
176
									$host = "localhost";
177
								}
178
								$port = $data[1];
179
							}
180
						}
181
						# exit the loop if we've got all parameters
182
						last PROC_LOOP if defined($host) and defined($port) and defined($username) and defined($password);
183
					}
184
				} else {
185
					print(STDERR "Can't open FMS config file (" . $fms_config . "): $!\n");
186
				}
187
				# exit the loop since we've already found the process
188
				# that we were looking for, we just failed to find
189
				# all required parameters in fms.ini (or failed to
190
				# find fms.ini at all in (cwd of fmsadmin)/conf
191
				last PROC_LOOP;
192
			}
193
		}
194
	} else {
195
		print(STDERR "Plugin must be run with root privileges for autoconfiguration to work!\n");
196
	}
197
}
198

    
199
if ( defined($ARGV[0]) ) {
200
	if ( $ARGV[0] eq "autoconf" ) {
201
		if ( defined($host) and defined($port) and defined($username) and defined($password) ) {
202
			print("yes\n");
203
			exit 0;
204
		} else {
205
			print("no\n");
206
			exit 1;
207
		}
208
	} elsif ( $ARGV[0] eq "config" ) {
209
		print <<'END_GRAPH_CONFIG';
210
graph_title Flash Media Server application connections
211
graph_args -l 0 --base 1000
212
graph_vlabel active connections
213
graph_category network
214
graph_period second
215
graph_info This graph shows the number of active connections for each application on the Flash Media Server.
216
END_GRAPH_CONFIG
217
		if ( defined($host) and defined($port) and defined($username) and defined($password) ) {
218
			my @apps = get_apps();
219
			if ( $#apps >= 0 ) {
220
				foreach my $app (@apps) {
221
					my $symbol = name_filter($app);
222
					print <<"END_APP_CONFIG";
223
fms_app_$symbol.label $app
224
fms_app_$symbol.type GAUGE
225
fms_app_$symbol.min 0
226
END_APP_CONFIG
227
				}
228
				exit 0;
229
			} else {
230
				print(STDERR "Failed to get list of applications from the Flash Media Administration Server!\n");
231
				exit 1;
232
			}
233
		} else {
234
			print(STDERR "Failed to get all parameters needed for the connection to the Flash Media Administration Server!\n");
235
			exit 1;
236
		}
237
	}
238
}
239

    
240
if ( defined($host) and defined($port) and defined($username) and defined($password) ) {
241
	my @apps = get_apps();
242
	if ( $#apps >= 0 ) {
243
		my $ua = LWP::UserAgent->new(timeout => 30);
244
		foreach my $app (@apps) {
245
			my $symbol = name_filter($app);
246
			my $url = sprintf("http://%s:%d/admin/getAppStats?auser=%s\&apswd=%s\&app=%s", $host, $port, $username, $password, $app);
247
			my $response = $ua->request(HTTP::Request->new('GET', $url));
248
			if ( $response->content =~ /<data>.*<connected>[^0-9]*([0-9]+)[^0-9]*<\/connected>.*<\/data>/is ) {
249
				print("fms_app_$symbol.value $1\n");
250
			} else {
251
				print(STDERR "Failed to get number of active streams for the \"$app\" application from the Flash Media Administration Server!\n");
252
				print("fms_app_$symbol.value U\n");
253
			}
254
		}
255
		exit 0;
256
	} else {
257
		print(STDERR "Failed to get list of applications from the Flash Media Administration Server!\n");
258
		exit 1;
259
	}
260
} else {
261
	print(STDERR "Failed to get all parameters needed for the connection to the Flash Media Administration Server!\n");
262
	exit 2;
263
}
264