ASNMTAP

 view release on metacpan or  search on metacpan

lib/ASNMTAP/Asnmtap/Plugins/Mail.pm  view on Meta::CPAN

  $mailcfg {retries} = $self->{_SMTP}->{retries};
  $mailcfg {delay}   = $self->{_SMTP}->{delay};
  $mailcfg {mime}    = $self->{_SMTP}->{mime};
  $mailcfg {tz}      = $self->{_SMTP}->{tx} if ( defined $self->{_SMTP}->{tx} );
  $mailcfg {debug}   = $$asnmtapInherited->getOptionsValue ('debug');

  my $message;

  if ( $self->{_mailType} ) {
    use Time::Local;
    my ($localYear, $localMonth, $currentYear, $currentMonth, $currentDay, $currentHour, $currentMin, $currentSec) = ((localtime)[5], (localtime)[4], ((localtime)[5] + 1900), ((localtime)[4] + 1), (localtime)[3,2,1,0]);
    my $mailEpochtime = timelocal($currentSec, $currentMin, $currentHour, $currentDay, $localMonth, $localYear);
    $message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE FingerprintEmail SYSTEM \"dtd/FingerprintEmail-1.0.dtd\"><FingerprintEmail><Schema Value=\"1.0\"/><Fingerprint From=\"". $self->{_mail}->{from} ."\" To=\"". $self->{_mail}->{to} ."\"...
  } else {
    use ASNMTAP::Time qw(&get_datetimeSignal);
    $message = $self->{_subject_} ."\n". $self->{_branding_} ."\n". $self->{_timestamp_} .' '. get_datetimeSignal() ."\n". $self->{_status_} ."\n";
  }

  $message .= $self->{_mail}->{body} ."\n";
  my %mail = ( To => $self->{_mail}->{to}, From => $self->{_mail}->{from}, Subject => $self->{_subject_}, Message => $message );

  my $returnCode = (sendmail %mail) ? $ERRORS{OK} : $ERRORS{CRITICAL};
  $$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => ( defined $parms{perfdataLabel} ? $parms{perfdataLabel} : 'email send' ) . ( $returnCode ? ' failed' : '' ) }, $TYPE{APPEND} );

  if ( defined $parms{perfdataLabel} ) {
    my $responseTime = $$asnmtapInherited->setEndTime_and_getResponsTime ( $$asnmtapInherited->pluginValue ('endTime') );
    $$asnmtapInherited->appendPerformanceData ( "'". $parms{perfdataLabel} ."'=". $responseTime .'ms;;;;' );
  }

  print "\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log, "\n" if ( $$asnmtapInherited->getOptionsValue ('debug') );
  return ( $returnCode );
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub receiving_fingerprint_mails {
  my $self = shift; &_checkAccObjRef ( $self ); 

  my $asnmtapInherited = $self->{_asnmtapInherited};
  return ( $$asnmtapInherited->pluginValue ('stateValue') ) unless ( exists $self->{_subject_} );

  my %defaults = ( custom           => undef,
                   customArguments  => undef,
                   checkFingerprint => 1,
                   receivedState    => 0,
                   outOfDate        => undef,
                   perfdataLabel    => undef
                 );
			 
  my %parms = (%defaults, @_);

  if ( $self->{_mailType} ) {
    unless ( defined $parms{outOfDate} ) {
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing MAIL receiving_fingerprint_mails parameter {outOfDate => ...}' }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }
  }

  use ASNMTAP::Asnmtap::Plugins::XML qw(&extract_XML);

  $self->[ $self->[0]{defaultArguments} = @{$self} ] = { date => undef, day => undef, month => undef, year => undef, time => undef, hour => undef, min => undef, sec => undef, numberOfMatches => undef, result => undef };

  unless ($parms{checkFingerprint} =~ /^[01]$/ ) {
    $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Parameter checkFingerprint must be 0 or 1' }, $TYPE{APPEND} );
    return ( $ERRORS{UNKNOWN} );
  }

  unless ($parms{receivedState} =~ /^[01]$/ ) {
    $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Parameter receivedState must be 0 or 1' }, $TYPE{APPEND} );
    return ( $ERRORS{UNKNOWN} );
  }

  my ($numberOfMails, $email);

  if ( defined $self->{_IMAP4}->{imap4} ) {
    unless ( defined $self->{_IMAP4}->{username} ) {
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail IMAP4 parameter username' }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }

    unless ( defined $self->{_IMAP4}->{password} ) {
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail IMAP4 parameter password' }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }

    use Net::IMAP::Simple;
    $email = Net::IMAP::Simple->new ( $self->{_IMAP4}->{imap4}, port => $self->{_IMAP4}->{port}, timeout => $self->{_IMAP4}->{timeout} );
 
    unless ( defined $email ) {
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Cannot connect to IMAP4 server: $self->{_IMAP4}->{imap4}, $Net::IMAP::Simple::errstr" }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }
 
    unless ( $email->login( $self->{_IMAP4}->{username}, $self->{_IMAP4}->{password} ) ){
      my $errstr = $email->errstr; $errstr =~ s/[\n\r]/ /g; $errstr =~ s/ +$//g; 
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Cannot login to IMAP4 server: $self->{_IMAP4}->{imap4}, $errstr" }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }

    $numberOfMails = $email->select ( 'INBOX' );

    unless ( defined $numberOfMails ) {
      my $errstr = $email->errstr; $errstr =~ s/[\n\r]/ /g; $errstr =~ s/ +$//g; 
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Cannot select my INBOX on IMAP4 server: $self->{_IMAP4}->{imap4}, $errstr" }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }
  } elsif ( defined $self->{_POP3}->{pop3} ) {
    unless ( defined $self->{_POP3}->{username} ) {
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail POP3 parameter username' }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }

    unless ( defined $self->{_POP3}->{password} ) {
      $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail POP3 parameter password' }, $TYPE{APPEND} );
      return ( $ERRORS{UNKNOWN} );
    }

    use Mail::POP3Client;
    $email = Mail::POP3Client->new ( HOST => $self->{_POP3}->{pop3}, PORT => $self->{_POP3}->{port}, USER => $self->{_POP3}->{username}, PASSWORD => $self->{_POP3}->{password}, TIMEOUT => $self->{_POP3}->{timeout} ); # , DEBUG => ( $self->{_POP3}->{d...

    $numberOfMails = $email->Count();

lib/ASNMTAP/Asnmtap/Plugins/Mail.pm  view on Meta::CPAN

        }

        unless ( $fromNotFound or $toNotFound or $subjectNotFound ) {
          print "\n", ref ($self), "::receiving_fingerprint_mails(): BODY\n" if ($debug);

          if ( defined $self->{_IMAP4}->{imap4} ) {
            use Email::Simple;
            my $mail = Email::Simple->new( join ( '', @{ $email->get ( $msgnum ) } ) );
            $msgbuffer = $mail->body;
          } elsif ( defined $self->{_POP3}->{pop3} ) {
            $msgbuffer = $email->Body ( $msgnum );
          }

          use MIME::Decoder;

          unless ( supported MIME::Decoder $head->mime_encoding ) {
            print "MIME .... : '". $head->mime_encoding ."' encoding is not supported!\n" if ($debug );
            $returnCode = $ERRORS{UNKNOWN};
            $$asnmtapInherited->pluginValues ( { stateValue => $returnCode, error => "MIME-Encoding: '". $head->mime_encoding ."' is not supported!" }, $TYPE{APPEND} );
            next;
          }

        # if ( $head->mime_encoding eq 'quoted-printable' or $head->mime_encoding eq '7bit' or $head->mime_encoding eq '8bit' ) { 
          if ( $head->mime_encoding eq '7bit' ) { 
             $msgbuffer = MIME::QuotedPrint::decode($msgbuffer); 
          } else { 
            use IO::String; 
            my $ioIN  = IO::String->new($msgbuffer); 
            my $ioOUT = IO::String->new($msgbuffer); 

            my $decoder = new MIME::Decoder $head->mime_encoding; 
            $decoder->decode($ioIN, $ioOUT); 

            $ioIN->close; 
            $ioOUT->close; 
          }

          if ( $parms{checkFingerprint} ) {
            foreach my $msgline ( split (/[\n\r]/, $msgbuffer) ) {
              next unless ( $msgline );
              last unless ( $self->{_mailType} ? $xmlNotFound : $fingerprintFound );

              if ( $self->{_mailType} ) {
                if ( $msgline =~ /\Q$fingerprintXML\E/ ) {
			      $xmlNotFound = 0; $fingerprintFound--;
  	 	          print "XML ..... : $msgline\n  (match) : $msgline\n" if ( $debug );
                  my ( $returnCode, $xml ) = extract_XML ( asnmtapInherited => $self->{_asnmtapInherited}, resultXML => $msgline, headerXML => HEADER, footerXML => FOOTER, validateDTD => 0, filenameDTD => SYSTEM );

                  unless ( $returnCode ) {
                    if ( $xml->{Fingerprint}{From} =~ /^$self->{_mail}->{from}/ and $xml->{Fingerprint}{To} =~ /^$self->{_mail}->{to}/ and $xml->{Fingerprint}{Destination} eq 'ASNMTAP' and $xml->{Fingerprint}{Plugin} eq $$asnmtapInherited->{_programN...
                      use Date::Calc qw(check_date);

                      $self->{defaultArguments}->{date}  = 0;
                      $self->{defaultArguments}->{year}  = 0;
                      $self->{defaultArguments}->{month} = 0;
                      $self->{defaultArguments}->{day}   = 0;

                      $self->{defaultArguments}->{time}  = 0;
                      $self->{defaultArguments}->{hour}  = 0;
                      $self->{defaultArguments}->{min}   = 0;
                      $self->{defaultArguments}->{sec}   = 0;

                      my $currentTimeslot = timelocal ( (localtime)[0,1,2,3,4,5] );
                      my ($checkEpochtime, $checkDate, $checkTime) = ($xml->{Fingerprint}{Epochtime}, $xml->{Fingerprint}{Date}, $xml->{Fingerprint}{Time});
                      my ($checkYear, $checkMonth, $checkDay) = split (/\/|-/, $checkDate);
                      my ($checkHour, $checkMin, $checkSec) = split (/:/, $checkTime);
                      my $xmlEpochtime = timelocal ( $checkSec, $checkMin, $checkHour, $checkDay, ($checkMonth-1), ($checkYear-1900) );
                      print "$checkEpochtime, $xmlEpochtime ($checkDate, $checkTime), $currentTimeslot - $checkEpochtime = ". ($currentTimeslot - $checkEpochtime) ." > ". $parms{outOfDate} ."\n" if ( $debug );

                      unless ( check_date ( $checkYear, $checkMonth, $checkDay) or check_time($checkHour, $checkMin, $checkSec ) ) {
                        $returnCode = $ERRORS{CRITICAL};
                        $$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => "Date or Time into Fingerprint XML are wrong: $checkDate $checkTime" }, $TYPE{APPEND} );
                      } elsif ( $checkEpochtime != $xmlEpochtime ) {
                        $returnCode = $ERRORS{CRITICAL};
                        $$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => "Epochtime difference from Date and Time into Fingerprint XML are wrong: $checkEpochtime != $xmlEpochtime ($checkDate $checkTime)" }, $TYPE{APPEND} );
                      } elsif ( $currentTimeslot - $checkEpochtime > $parms{outOfDate} * 2 ) {
                        $returnCode = $ERRORS{CRITICAL};
                        $$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => "Result into Fingerprint XML are out of date: $checkDate $checkTime" }, $TYPE{APPEND} );
                      } elsif ( $currentTimeslot - $checkEpochtime > $parms{outOfDate} ) {
                        $returnCode = $ERRORS{WARNING};
                        $$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => "Result into Fingerprint XML are out of date: $checkDate $checkTime" }, $TYPE{APPEND} );
                      } else {
 				        ($self->{defaultArguments}->{date}, $self->{defaultArguments}->{time}) = ($checkDate, $checkTime);
                        ($self->{defaultArguments}->{day}, $self->{defaultArguments}->{month}, $self->{defaultArguments}->{year}) = split(/[\/|-]/, $checkDate);
                        ($self->{defaultArguments}->{hour}, $self->{defaultArguments}->{min}, $self->{defaultArguments}->{sec}) = split(/:/, $checkTime);
                      }

                      $statusNotFound = ( $xml->{Fingerprint}{Status} ne $self->{_mail}->{status} );
                      my $label = $statusNotFound ? '    (?)' : '(match)';
                      print "  $label : $self->{_text}->{status} < $self->{_mail}->{status} >\n" if ( $debug );
                      unless ( $statusNotFound ) { $fingerprintFound--; last; }
                    } else {
                      if ( $debug ) {
                        my $label = ( $xml->{Fingerprint}{From} =~ /^$self->{_mail}->{from}/ ? '(match)' : '    (?)' );
                        print "  $label : $self->{_text}->{status} < $self->{_mail}->{status} >\n";
                        $label = ( $xml->{Fingerprint}{From} =~ /^$self->{_mail}->{from}/ ? '(match)' : '    (?)' );
                        print "  $label : From ". $xml->{Fingerprint}{From} ."\n";
                        $label = ( $xml->{Fingerprint}{To} =~ /^$self->{_mail}->{to}/ ? '(match)' : '    (?)' );
                        print "  $label : To ". $xml->{Fingerprint}{To} ."\n";
                        $label = ( $xml->{Fingerprint}{Destination} eq 'ASNMTAP' ? '(match)' : '    (?)' );
                        print "  $label : Destination ". $xml->{Fingerprint}{Destination} ."\n";
                        $label = ( $xml->{Fingerprint}{Plugin} eq $$asnmtapInherited->{_programName} ? '(match)' : '    (?)' );
                        print "  $label : Plugin ". $xml->{Fingerprint}{Plugin} ."\n";
                        $label = ( $xml->{Fingerprint}{Description} eq $$asnmtapInherited->{_programDescription} ? '(match)' : '    (?)' );
                        print "  $label : Description ". $xml->{Fingerprint}{Description} ."\n";
                        $label = ( $xml->{Fingerprint}{Environment} =~ /^$self->{_environment_}/i ? '(match)' : '    (?)' );
                        print "  $label : Environment ". $xml->{Fingerprint}{Environment} ."\n";
                      }

                      last;
                    }
                  }
          
                  next;
                }
              } else {
       		    if ( $messageNotFound ) {
                  if ($msgline =~ /^$self->{_subject_}/) {
      	 	        print "Header .. : $msgline\n  (match) : $msgline\n" if ($debug);
      			    $messageNotFound = 0; $fingerprintFound--; next;
    		      }
                }

    		    if ( $brandingNotFound ) {
                  if ( $msgline =~ /$$asnmtapInherited->{_programName}/ ) {
                    if ( $debug ) {
                      my $msglineDebug = $msgline; $msglineDebug =~ s/</< /g; $msglineDebug =~ s/>/ >/g;
       	              print "Branding  : $msglineDebug\n";
                    }

                    $brandingNotFound = ( $msgline !~ /^$self->{_branding_}/ );
                    my $label = $brandingNotFound ? '    (?)' : '(match)';

                    if ( $debug ) {
                      my $msglineDebug = $msgline; $msglineDebug =~ s/</< /g; $msglineDebug =~ s/>/ >/g;
		              print "  $label : $msglineDebug\n";
                    }

                    unless ( $brandingNotFound ) { $fingerprintFound--; next; }
		          }
                }

	    	    if ( $timestampNotFound ) {
		          if ( $msgline =~ /^$self->{_timestamp_}/ ) {
                    print "Timestamp : $msgline\n" if ( $debug );
                    (undef, $msgline) = split(/:\s+/, $msgline, 2);
                    ($self->{defaultArguments}->{date}, $self->{defaultArguments}->{time}) = split(/\s+/, $msgline);
                    ($self->{defaultArguments}->{year}, $self->{defaultArguments}->{month}, $self->{defaultArguments}->{day}) = split(/[\/-]/, $self->{defaultArguments}->{date});
                    ($self->{defaultArguments}->{hour}, $self->{defaultArguments}->{min}, $self->{defaultArguments}->{sec}) = split(/:/, $self->{defaultArguments}->{time});
                    print '  (match) : ', $self->{_timestamp_}, ' ', $self->{defaultArguments}->{year}, '/', $self->{defaultArguments}->{month}, '/', $self->{defaultArguments}->{day}, ' ', $self->{defaultArguments}->{hour}, ':', $self->{defaultArgume...
                    $timestampNotFound = 0; $fingerprintFound--; next;
                  }
                }

	    	    if ( $fingerprintFound == 1 and $statusNotFound ) {
      		      if ( $msgline =~ /^$self->{_text}->{status}/ ) {
                    if ( $debug ) {
                      my $msglineDebug = $msgline; $msglineDebug =~ s/</< /g; $msglineDebug =~ s/>/ >/g;
                      print "Status .. : $msglineDebug\n";
                    }

                    $statusNotFound = ( $msgline !~ /^$self->{_text}->{status}\s*<$self->{_mail}->{status}>/ );
                    my $label = $statusNotFound ? '    (?)' : '(match)';
                    print "  $label : $self->{_text}->{status} < $self->{_mail}->{status} >\n" if ($debug);
                    unless ( $statusNotFound ) { $fingerprintFound--; last; }
                  }
                }
              }

              unless ( $fingerprintFound ) {
                $self->{defaultArguments}->{result} .= "$msgline\n";
                print "- - - - - : $msgline\n" if ($debug);
                last;
              }

	           print ". . . . . : $msgline\n" if ($debug >= 2);
            }
          }
        }

        if ( $fingerprintFound == 0 ) {
          if ( $parms{checkFingerprint} ) {
            my $regexp = ( $self->{_mailType} ? "\Q$fingerprintXML\E" .'.+'. "\Q<\/FingerprintEmail>\E" : $self->{_text}->{status} .'\s+<'. $self->{_mail}->{status} .'>' );
            $msgbuffer =~ /$regexp/;
            $self->{defaultArguments}->{result} = ( $' ) ? $' : '';
          } else {
            $self->{defaultArguments}->{result} = $msgbuffer;
          }

          print "\n", ref ($self), "::receiving_fingerprint_mails(): BODY MESSAGE\n". $self->{defaultArguments}->{result}. "\n" if ($debug >= 2);

          if ( defined $parms{custom} ) {
            $returnCode = ( defined $parms{customArguments} ) ? $parms{custom}->($self, $self->{_asnmtapInherited}, $email, $msgnum, $parms{customArguments}) : $parms{custom}->($self, $self->{_asnmtapInherited}, $email, $msgnum);
          } else {
            $self->{defaultArguments}->{numberOfMatches}++;

            unless ( $debug or $$asnmtapInherited->getOptionsValue ('onDemand') ) {
              if ( defined $self->{_IMAP4}->{imap4} ) {
                $email->delete ( $msgnum );
              } elsif ( defined $self->{_POP3}->{pop3} ) {
                $email->Delete( $msgnum );
              }
            }
          }
        }

        $self->{defaultArguments}->{date}  = undef;
        $self->{defaultArguments}->{day}   = undef;
        $self->{defaultArguments}->{month} = undef;
        $self->{defaultArguments}->{year}  = undef;
        $self->{defaultArguments}->{time}  = undef;
        $self->{defaultArguments}->{hour}  = undef;
        $self->{defaultArguments}->{min}   = undef;
        $self->{defaultArguments}->{sec}   = undef;

        undef $head;
        $entity->purge;
        undef $entity;
      }

      undef $parser;
    }

    if ( defined $self->{defaultArguments}->{numberOfMatches} and $self->{defaultArguments}->{numberOfMatches} ) {
      $$asnmtapInherited->pluginValues ( { alert => $self->{defaultArguments}->{numberOfMatches} .' '. ( defined $parms{perfdataLabel} ? $parms{perfdataLabel} : 'email(s) received' ) }, $TYPE{APPEND} );
    } else {
      $returnCode = $parms{receivedState} ? $ERRORS{OK} : $ERRORS{CRITICAL};
      $$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => 'No '. ( defined $parms{perfdataLabel} ? $parms{perfdataLabel} : 'email(s) received' ) }, $TYPE{APPEND} );
    }

    if ( defined $self->{_IMAP4}->{imap4} ) {
      $email->quit;
    } elsif ( defined $self->{_POP3}->{pop3} ) {
      $email->Close;
    }

    $$asnmtapInherited->appendPerformanceData ( "'". $parms{perfdataLabel} ."'=". $self->{defaultArguments}->{numberOfMatches} .';;;;' ) if ( defined $parms{perfdataLabel} );
    return ( $returnCode, $self->{defaultArguments}->{numberOfMatches} );
  }
}

# Destructor  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub DESTROY { print (ref ($_[0]), "::DESTROY: ()\n") if ( exists $$_[0]->{_asnmtapInherited} and $$_[0]->{_asnmtapInherited}->{_debug} ); }

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

1;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

__END__

=head1 NAME

ASNMTAP::Asnmtap::Plugins::Mail is a Perl module that provides Mail functions used by ASNMTAP-based plugins.

=head1 SEE ALSO

ASNMTAP::Asnmtap, ASNMTAP::Asnmtap::Plugins

=head1 AUTHOR

Alex Peeters [alex.peeters@citap.be]

=head1 COPYRIGHT NOTICE

(c) Copyright 2000-2011 by Alex Peeters [alex.peeters@citap.be],
                        All Rights Reserved.

ASNMTAP is based on 'Process System daemons v1.60.17-01', Alex Peeters [alex.peeters@citap.be]

 Purpose: CronTab (CT, sysdCT),
          Disk Filesystem monitoring (DF, sysdDF),



( run in 2.036 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )