#!/sw/bin/perl -w ####################################################################################### # # Spong-Bot Version 2.0 # $Date: 2001/12/01 14:52:32 $ # # This bot always notifies you about the status of your systems. You need an # installed and running spong. Depending on $interval, it sends color changes to # the channels its on. You can also request reports by typing !spong. # # To run the bot, you need Net::IRC and Proc::Daemon. Further, you have to # change the configuration variables. When you're finished with that, just run # the bot with "./spongbot.pl". # # You may use this software under the same terms as Spong # . This mean, either GPL or Perl Artistic # License. # # (c) 2001, Thomas Bader # ####################################################################################### # ------------- # Load modules # ------------- use strict; use Net::IRC; use Proc::Daemon; # ------------- # Configuration # ------------- use vars ('$nick', '$server', '$port', '$username', '$ircname', '@channel'); use vars ('$spongvar', '$interval', '$nsnick', '$nspass', '%stack', '%colors'); $nick = 'spongbot'; # the nickname $server = 'spongbot'; # IRC server to connect $port = 6667; # port of the irc daemon $username = 'spongbot'; # the username of the bot $ircname = 'System Monitoring'; # the "realname" @channel = ('#channel1', '#channel2'); # the channels, the bot is on $spongvar = '/sw/pkgs/spong-2.7.5/var/database'; # from the spong package $interval = 60; # specifies, how often to check $nsnick = 'NickServ'; # Nick of NickServ $nspass = ''; # password for NickServ, leave # empty when there's no NickServ %colors = ( red => "7", # make it color full yellow => "5", purple => "6", green => "3", clear => "1"); # ------------- # Fork to background # ------------- Proc::Daemon::Init; # ------------- # Define our IRC connection # ------------- my $irc = new Net::IRC; my $conn = $irc->newconn(Nick => $nick, Server => $server, Port => $port, Username => $username, Ircname => $ircname); # ------------- # Set signal handlers # ------------- $SIG{TERM}=\&catch_sigterm; $SIG{ALRM}=\&catch_sigalarm; $SIG{CHLD}="IGNORE"; $SIG{HUP}="IGNORE"; alarm($interval); # ------------- # Add handlers # ------------- $conn->add_global_handler('376', \&on_connect); $conn->add_global_handler('disconnect', \&on_disconnect); $conn->add_handler('join', \&on_join); $conn->add_handler('cping', \&on_ping); $conn->add_handler('public', \&on_public); $conn->add_handler('cversion', \&on_cversion); # ------------- # Start the connection # ------------- chdir $spongvar; $irc->start; # ------------- # Subroutines # ------------- ### What to do after the MOTD sub on_connect { my $self = shift; # identify at the NickServ if($main::nspass ne "") { $self->privmsg($main::nsnick, "IDENTIFY $main::nspass"); } # join the channels foreach(@main::channel) { $self->join($_); $self->privmsg($_, "Hi there...."); } } ### What to do when someone joins a chænnel the bot is on sub on_join { my ($self, $event) = @_; my ($channel) = ($event->to)[0]; my ($nick, $mynick) = ($event->nick, $self->nick); $self->privmsg($channel, "ahoi $nick") unless $nick eq $mynick; } ### What to do if we get a channel msg sub on_public { my ($self, $event) = @_; my @to = $event->to; my ($nick, $mynick) = ($event->nick, $self->nick); my ($arg) = ($event->args); if ($arg =~ /^!spong$/i) { &spongcheck($self, $event, $nick, @to); } } ### What to do when someone sends us pings sub on_ping { my ($self, $event) = @_; my $nick = $event->nick; $self->ctcp_reply($nick, join(' ', ($event->args))); } ### What to do when we die sub on_disconnect { my ($self, $event) = @_; $self->connect; } ### Reply to CTCP version sub on_cversion { my ($self, $event) = @_; my ($nick, $mynick) = ($event->nick, $self->nick); my $reply = $mynick . ", running Spong-Bot 2.0"; $self->ctcp_reply($nick, join(' ', ($event->args), $reply)); } ### What to do when we get a SIGTERM sub catch_sigterm { $conn->quit("And all the science, I don't understand... it's just my job five days a " . "week...."); exit 0; } ### What to when we get a SIGALARM sub catch_sigalarm { spongperiodic($conn, @channel); # come back later $SIG{ALRM}=\&catch_sigalarm; alarm ($main::interval); } ### Check the Spong Logs sub spongcheck { my ($self, $event, $nick, @to) = @_; open(IN, "find . -type f | grep services |"); while() { my ($host, $service, $color) = (m#^\./([A-Za-z0-9\.]+)/services/([a-z]+)-([a-z]+)$#); if($color eq "red" || $color eq "yellow" || $color eq "purple") { $self->privmsg([ @to ], "$nick, ".$main::colors{$color}."$host:$service is $color.".$main::colors{clear}); } } $self->privmsg([ @to ], "$nick, End of Report."); } ### periodoc check sub spongperiodic { my ($self, @to) = @_; open(IN, "find . -type f | grep services |"); while() { my ($host, $service, $color) = (m#^\./([A-Za-z0-9\.]+)/services/([a-z]+)-([a-z]+)$#); if($color eq "red" || $color eq "yellow" || $color eq "green" || $color eq "purple") { if(defined($stack{"$host:$service"})) { if($stack{"$host:$service"} ne $color) { $self->privmsg([ @to ], $main::colors{$color}."$host:$service is $color".$main::colors{clear}); $stack{"$host:$service"} = $color; } } $stack{"$host:$service"} = $color; } } }