Thursday, May 26, 2016

Example perl script parse hylafax errors

In this article I share a script errors parse in the log Hylafax.
This script can be useful to those who are move faxes to Asterisk/Elastix. The script will be sent by e-mail failure fax, missed calls for monitoring.

download hylafax_parse_error.pl

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
#!/usr/bin/perl -w

use warnings;
use MIME::Entity;

my $file = "";
my $tr = 0;
my $CID = "";
my $FaxModem = "";
my $FaxErr = "";
my $PATH_TO_SENDMAIL = "/usr/lib/sendmail";
my @arr_for_tmp = "";
my $cfile_name = "";
my $var = "";
my $count_filename = '/home/Scripts/error_get_count';

open(my $fh, '<', $count_filenameor die "cant open count file";
{
  local $/;
  $content = <$fh>;
}

@cfiles = </var/spool/hylafax/log/c*>;
foreach $cfile (@cfiles) {
  @arr_for_tmp=split /\//,$cfile;
  $cfile_name=$arr_for_tmp[(scalar(@arr_for_tmp))-1];
  $cfile_name =~ s/c//;
  if ($cfile_name > $content) {
   check_processing_jobs($cfile);
  }
}

close $fh;
open($fh, '>', $count_filenameor die "cant open count file";
close $fh;

sub check_processing_jobs
{
 $tr = 0
 $file = $_[0];
 open my $info, $file or die "Could not open $file: $!";

 whilemy $line = <$info>)  { 
if ($line =~ /CallID/) {
 $CID = $line;     
}
if ($line =~ /FAX CONNECTION  DEVICE/) {
 $FaxModem = $line;     
}
if ($line =~ /error|invalid/) {
 $FaxErr = $line;
 $tr = 1;     
}
}

if ($tr == 1) {
$CID =~ m/^"\d+"\s$/gi;

if ($FaxModem =~ "ttyIAX2") {
   $to_email = "fax_user2\@blogspot.com";
   $data_email = "fax for user2 123456789 ";
}

if ($FaxModem =~ "ttyIAX1") {
   $to_email = "fax_user1\@blogspot.com";
   $data_email = "fax for user1 987654321 ";
}

    $top = MIME::Entity->build(Type    => "multipart/mixed; charset=UTF-8",
       Charset    => "UTF-8",
       From    => "fax\@blogspot.com",
       To      => $to_email,
       Subject => 'fax');
    local $data = "Error " . $data_email . "\n\t" . $CID . "\n\t" . $FaxModem . "\n\t" . $FaxErr;
    $data =~ s/\n+/\n/gi;
    $top->attach(Data => $data);
    open MAIL, "| " . $PATH_TO_SENDMAIL . " -t -oi -oem" or die "open: $!";
    $top->print(\*MAIL);
    close MAIL;
}

close $info;
}