This is Bavaria, Germany!
Send a simple http-request to get information from a server. Parsing the answer is simple, because the output is XML.
Output:
Content-Type: text/xml; charset=ISO-8859-1
<?xml version='1.0' encoding='iso-8859-1' standalone='yes'?>
<users>
<anon>
<username>edv</username>
<wie_oft_eingeloggt>1</wie_oft_eingeloggt>
</anon>
<anon>
<username>gsgeisleithe</username>
<wie_oft_eingeloggt>1</wie_oft_eingeloggt>
</anon>
</users>
Perlcode:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use XML::Simple;
# XML-Ausgabe welche Leute gerade eingeloggt sind
print CGI::header('text/xml');
my @users = wer_ist_eingeloggt();
print XMLout(
{ users => [ \@users ] },
XMLDecl => "<?xml version='1.0' encoding='iso-8859-1' standalone='yes'?>",
RootName => "users",
KeepRoot => 1,
NoAttr => 1,
);
exit;
=head2 wer_ist_eingeloggt
Aufruf:
my $users = wer_ist_eingeloggt();
=cut
sub wer_ist_eingeloggt {
# Ausgabe sieht so aus:
# www-data@fugu:~$ who -q
# bahnhof gsgeisleithe root root
# # users=4
my $anz;
my $cmd = '/usr/bin/who -q';
for (`$cmd`) {
chomp;
next if m{^#};
for my $cur_user ( split( m{\s+}, $_ ) ) {
$anz->{$cur_user} += 1;
}
}
my @users =
map { { username => $_, wie_oft_eingeloggt => $anz->{$_} } } keys %$anz;
return @users;
}
Output:
Content-Type: text/xml; charset=ISO-8859-1 <?xml version='1.0' encoding='iso-8859-1' standalone='yes'?> <load>0.00</load>
Perlcode:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use XML::Simple;
# XML-Ausgabe to get load of server
print CGI::header('text/xml');
print XMLout(
{ load => get_load() },
XMLDecl => "<?xml version='1.0' encoding='iso-8859-1' standalone='yes'?>",
RootName => "users",
KeepRoot => 1,
NoAttr => 1,
);
exit;
=head2 get_load
Aufruf:
my $load = get_load();
# $load = 0.22
Error:
$load = -1
=cut
sub get_load {
# Output looks like this
# echolon:/etc/nagios/echolon# uptime
# 10:58:47 up 47 days, 22:23, 1 user, load average: 0.25, 0.45, 0.39
my $load_default = '-1';
my $cmd = '/usr/bin/uptime';
for (`$cmd`) {
chomp;
if (m{load average:\s+(.+?),}) {
return $1;
}
else {
return $load_default;
}
}
}