-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpd_stats.pl
More file actions
executable file
·44 lines (42 loc) · 1.1 KB
/
mpd_stats.pl
File metadata and controls
executable file
·44 lines (42 loc) · 1.1 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
my $Interval = defined ($ENV{'COLLECTD_INTERVAL'}) ? (0 + $ENV{'COLLECTD_INTERVAL'}) : 120;
my $Hostname = defined ($ENV{'COLLECTD_HOSTNAME'}) ? $ENV{'COLLECTD_HOSTNAME'} : 'localhost';
$| = 1;
# CONFIG HERE!
my $mpd_host = "localhost";
my $mpd_port = "6600";
while(42) {
mpd_stats();
sleep(${Interval});
}
sub mpd_stats {
my $ans = "";
my $songs = 0;
my $albums = 0;
my $time = time;
my $socket = new IO::Socket::INET(PeerAddr => $mpd_host,
PeerPort => $mpd_port,
Proto => "tcp",
timeout => 5);
printf "Could not create socket: $!\n" unless $socket;
if ( not $socket->getline() =~ /^OK MPD*/ ) {
print"Could not connect: $!\n";
} else {
print $socket "stats\n";
while ( not $ans =~ /^(OK|ACK)/ ) {
$ans = <$socket>;
if ( $ans =~ s/albums: //) {
$albums = $ans;
}
if ( $ans =~ s/songs: //) {
$songs = $ans;
}
}
close($socket);
print "PUTVAL ${Hostname}/mpd/gauge-albums interval=${Interval} ${time}:${albums}";
print "PUTVAL ${Hostname}/mpd/gauge-songs interval=${Interval} ${time}:${songs}";
}
}