Changeset 416


Ignore:
Timestamp:
May 18, 2010, 6:08:12 PM (11 years ago)
Author:
Kris Deugau
Message:

/trunk

Update mailNotify to use new table notify to decide who to spam
with notices on various events. See #2.

  • existing notification calls updated. Still need to move calls into IPDB.pm's subs
  • IPDB.pm and MyIPDB.pm updated with new semiglobal vars for org name, SMTP host, domain. Will add others as needed so MyIPDB.pm can be moved to /etc/ipdb/ (see #17).

Internal allocation types will need to be rearranged somewhat to
make full use of the flexibility possible. See #20.

Location:
trunk/cgi-bin
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/IPDB.pm

    r404 r416  
    2020use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
    2121
    22 $VERSION        = 2.0;
     22$VERSION        = 2.6;
    2323@ISA            = qw(Exporter);
    2424@EXPORT_OK    = qw(
     
    5252our %bigfree;
    5353our %IPDBacl;
     54
     55our $orgname = 'Example Corp';
     56our $smtphost = 'smtp.example.com';
     57our $domain = 'example.com';
    5458
    5559# Let's initialize the globals.
     
    806810## IPDB::mailNotify()
    807811# Sends notification mail to recipients regarding an IPDB operation
    808 sub mailNotify ($$$) {
    809   my ($recip,$subj,$message) = @_;
    810   my $mailer = Net::SMTP->new("smtp.example.com", Hello => "ipdb.example.com");
    811 
    812   $mailer->mail('ipdb@example.com');
    813   $mailer->to($recip);
    814   $mailer->data("From: \"IP Database\" <ipdb\@example.com>\n",
     812sub mailNotify {
     813  my $dbh = shift;
     814  my ($action,$subj,$message) = @_;
     815
     816# split action into parts for fiddlement.  nb: there are almost certainly better ways to do this.
     817  my @actionbits = ($action =~ /^(.)(.)(.)$/);
     818
     819  # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
     820  # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
     821  # and "all events with this action"
     822  my @actionsets = ($action);
     823##fixme: ick, eww.  really gotta find a better way to handle this...
     824  push @actionsets, ($actionbits[0].'.'.$actionbits[2],
     825        $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
     826
     827  my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
     828
     829  # get recip list from db
     830  my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
     831
     832  my @reciplist;
     833  foreach (@actionsets) {
     834    $sth->execute($action);
     835##fixme - need to handle db errors
     836    my ($recipsub) = $sth->fetchrow_array;
     837    next if !$recipsub;
     838    foreach (split(/,/, $recipsub)) {
     839      push @reciplist, $_ if !grep {/^$_$/} @reciplist;
     840    }
     841  }
     842
     843  foreach my $recip (@reciplist) {
     844    $mailer->mail("ipdb\@$domain");
     845    $mailer->to($recip);
     846    $mailer->data("From: \"IP Database\" <ipdb\@$domain>\n",
    815847        "To: $recip\n",
    816848        "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
    817849        "Subject: {IPDB} $subj\n",
    818850        "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
    819         "Organization: Example Corp\n",
     851        "Organization: $orgname\n",
    820852        "\n$message\n");
     853  }
    821854  $mailer->quit;
    822855}
  • trunk/cgi-bin/MyIPDB.pm

    r320 r416  
    1919} # end connectDB_My()
    2020
     21# Set some globals declared in IPDB.pm.  Most of these only affect mailNotify().
     22# Note that while you *can* leave these at defaults, it's probably a Really Bad Idea.
     23#$IPDB::orgname = "Bob's Big Bonaza";
     24#$IPDB::smtphost = '127.0.0.1';
     25#$IPDB::domain = 'bob.com';
     26
    2127# Keep Perl from complaining.
    22281;
  • trunk/cgi-bin/admin.cgi

    r415 r416  
    250250        syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
    251251                "'$webvar{alloctype}'";
    252         # Notify tech@example.com
    253         mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
     252        mailNotify($ip_dbh, "$disp_alloctypes{$webvar{alloctype}} allocation",
    254253          "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer".
    255254          " $webvar{custid}\n".
  • trunk/cgi-bin/ipdb.psql

    r409 r416  
    226226    node_ip inet
    227227);
     228
     229-- Email notifications on <action>
     230CREATE TABLE notify (
     231    action varchar(5) NOT NULL PRIMARY KEY,
     232    reciplist varchar(500)
     233);
  • trunk/cgi-bin/main.cgi

    r415 r416  
    890890                qq(&ipdb=1&ip=$msg">Add this IP to RADIUS user table</a></div>)
    891891        : "</div>");
    892       # Notify tech@example.com
    893 #      mailNotify('tech@example.com',"ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
    894 #       "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
    895 #       "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
     892      mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
     893        "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
     894        "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
    896895    } else {
    897896      my $netblock = new NetAddr::IP $webvar{fullcidr};
     
    905904                qq(">Add this netblock to RADIUS user table</a></div>)
    906905        : "</div>");
    907 #      mailNotify('nocmgr@example.com',"ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
    908 #       "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
    909 #       "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
     906      mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
     907        "$disp_alloctypes{$webvar{alloctype}} $webvar{fullcidr} allocated to customer $webvar{custid}\n".
     908        "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
    910909    }
    911910    syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
     
    12241223  # If we get here, the operation succeeded.
    12251224  syslog "notice", "$authuser updated $webvar{block}";
    1226 #mailNotify('nocmgr@example.com',"SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
    1227 #       "$webvar{block} had SWIP status changed to \"Yes\" by $authuser");
     1225##fixme:  need to wedge something in to allow "update:field" notifications
     1226## hmm.  how to tell what changed?  O_o
     1227mailNotify($ip_dbh, 'ua', "SWIPed: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
     1228        "$webvar{block} had SWIP status changed to \"Yes\" by $authuser") if $webvar{swip} eq 'on';
    12281229  open (HTML, "../updated.html")
    12291230        or croak "Could not open updated.html :$!";
     
    14011402    syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}".
    14021403        " $custid, $city, desc='$description'";
    1403     # Notify tech@ when a block/IP is deallocated
    1404 #    mailNotify('tech@example.com',"REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
    1405 #       "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
    1406 #       "CustID: $custid\nCity: $city\nDescription: $description\n");
    1407 #    mailNotify('nocmgr@example.com',"REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
    1408 #       "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
    1409 #       "CustID: $custid\nCity: $city\nDescription: $description\n");
     1404    mailNotify($ip_dbh, 'da', "REMOVED: $disp_alloctypes{$webvar{alloctype}} $webvar{block}",
     1405        "$disp_alloctypes{$webvar{alloctype}} $webvar{block} deallocated by $authuser\n".
     1406        "CustID: $custid\nCity: $city\nDescription: $description\n");
    14101407  } else {
    14111408    if ($webvar{alloctype} =~ /^.i$/) {
  • trunk/cgi-bin/newcity.cgi

    r415 r416  
    4848if ($sth->err) {
    4949  print "Error adding city to database: ".$sth->errstr;
    50 #  mailNotify('ipdbadmin@example.com',"IPDB city add failure",
    51 #       "$authuser could not add city '$webvar{city}' to database: ".$sth->errstr);
     50  mailNotify($dbh, 'f:nci', "IPDB city add failure",
     51        "$authuser could not add city '$webvar{city}' to database: ".$sth->errstr);
    5252  syslog "err", "$authuser could not add city '$webvar{city}' to database: ".$sth->errstr;
    5353} else {
  • trunk/cgi-bin/newnode.cgi

    r397 r416  
    4545if ($sth->err) {
    4646  print "Error adding node to database: ".$sth->errstr;
    47   mailNotify('noc@example.com',"IPDB node add failure",
     47  mailNotify($dbh, 'f:nno', "IPDB node add failure",
    4848        "$authuser could not add node '$webvar{nodename}','$webvar{type}' to database: ".$sth->errstr);
    4949  syslog "err", "$authuser could not add node '$webvar{nodename}','$webvar{type}' to database: ".$sth->errstr;
Note: See TracChangeset for help on using the changeset viewer.