@ISA = qw(Exporter Spong::Status);
@EXPORT_OK = qw(status event);
-$VERSION = 0.01;
+$VERSION = 0.02;
+
+# Format and send a status update message
sub status {
my( $addr, $host, $cat, $color, $summary, $message, $ttl ) = @_;
- my( $sock, $ok, $ts );
+ my( $ts );
if ( defined $ttl ) { $ts = time() . ":$ttl"; } else { $ts = time(); }
- $sock = IO::Socket::INET->new( PeerAddr => $addr,
- PeerPort => $main::SPONG_UPDATE_PORT,
- Proto => 'tcp',
- Timeout => 30,
- Reuse => 1,
- );
-
- if ( ! defined $sock ) {
- croak "Could not connect with Spong Server: $@";
- }
-
- # Set an alarm on this block in case we run into problem talking to
- # the spong server.
- eval
- {
- local $SIG{'ALRM'} = sub { die; };
- alarm(30);
+ my $msg = "status $host $cat $color $ts $summary\n$message\n";
- $sock->autoflush(1);
- $sock->print("status $host $cat $color $ts $summary\n");
- $sock->print("$message\n");
+ my $errmsg = SendMsg( $addr, $main::SPONG_UPDATE_PORT, $msg );
- undef $sock;
- $ok = 1;
- alarm(0);
- };
+ main::error("Status: $errmsg") if $errmsg;
- warn scalar(localtime) . " can't connect to spong server.\n" if ! $ok;
}
+# Format and send an event message
+
sub event {
my( $addr, $host, $cat, $color, $summary, $message, $ttl ) = @_;
- my( $sock, $ok, $ts );
- $ts = time();
-
- $sock = IO::Socket::INET->new( PeerAddr => $addr,
- PeerPort => $main::SPONG_UPDATE_PORT,
- Proto => 'tcp',
- Timeout => 30,
- Reuse => 1,
- );
-
- if ( ! defined $sock ) {
- croak "Could not connect with Spong Server: $@";
- }
+ my $ts = time();
- # Set an alarm on this block in case we run into problem talking to
- # the spong server.
- eval
- {
- local $SIG{'ALRM'} = sub { die; };
- alarm(30);
+ my $msg = "event $host $cat $color $ts $summary\n$message\n";
- $sock->autoflush(1);
- $sock->print("event $host $cat $color $ts $summary\n");
- $sock->print("$message\n");
+ my $errmsg = SendMsg( $addr, $main::SPONG_UPDATE_PORT, $msg );
- undef $sock;
- $ok = 1;
- alarm(0);
- };
+ main::error("Event: $errmsg") if $errmsg;
- warn scalar(localtime) . " can't connect to spong server.\n" if ! $ok;
}
+# Format and send a paging message
+
sub page {
my( $addr, $host, $cat, $color, $summary, $message, $ttl ) = @_;
- my( $sock, $ok, $ts );
- $ts = time();
-
- $sock = IO::Socket::INET->new( PeerAddr => $addr,
- PeerPort => $main::SPONG_UPDATE_PORT,
- Proto => 'tcp',
- Timeout => 30,
- Reuse => 1,
- );
-
- if ( ! defined $sock ) {
- croak "Could not connect with Spong Server: $@";
- }
-
- # Set an alarm on this block in case we run into problem talking to
- # the spong server.
- eval
- {
- local $SIG{'ALRM'} = sub { die; };
- alarm(30);
+ my( $ts ) = time();
- $sock->autoflush(1);
- $sock->print("page $host $cat $color $ts $summary\n");
- $sock->print("$message\n");
+ my $msg = "page $host $cat $color $ts $summary\n$message\n";
- undef $sock;
- $ok = 1;
- alarm(0);
- };
+ my $errmsg = SendMsg( $addr, $main::SPONG_UPDATE_PORT, $msg );
- warn scalar(localtime) . " can't connect to spong server.\n" if ! $ok;
+ main::error("Page: $errmsg") if $errmsg;
}
+#
+# This routine handles the actual details of sending a message to the
+# spong-server.
+#
+
+sub SendMsg {
+ my( $addr, $port, $msg ) = @_;
+ my($errmsg) = "";
+ my($sock);
+
+
+ foreach my $server (split /\s+/,$addr) {
+
+ # Parse the spong server name specified (host[:port])
+ my($host, $p ) = split /:/,$server;
+ $p = $port if (! $p); # Default to $port if no port
+
+ # Set an alarm on this block in case we run into problem talking to
+ # the spong server.
+ eval
+ {
+ local $SIG{'ALRM'} = sub { die("socket timed out to $server"); };
+ alarm(30);
+
+ $sock = IO::Socket::INET->new( PeerAddr => $host,
+ PeerPort => $p,
+ Proto => 'tcp',
+ Timeout => 30,
+ Reuse => 1,
+ );
+
+ if ( ! defined $sock ) {
+ die "$@";
+ }
+
+ $sock->autoflush(1);
+ $sock->print($msg);
+ $sock->close();
+
+ undef $sock;
+ alarm(0);
+ };
+
+ # If we got an error message, add it to $errmsg
+ if ($@) { $errmsg .= ($errmsg) ? "\n" : "" . $@; }
+
+ }
+
+ # Return any error messages where done
+ return $errmsg;
+}
+