#!/usr/bin/perl -w
#fine file name list and put into an array
use MIME::Lite;
sub findFile($);
sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = '/usr/sbin/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}
sub findFile($)
{
my $dirname = $_[0];
my @matches = ();
local *DIR;
opendir ( DIR, $dirname ) || print "failed to open dir";
while( ($filename = readdir(DIR))){
$entry = "$dirname/$filename";
if (-d $entry )
{
if ($filename =~ m/INBOX/ || $filename =~ m/^\d\d\d\d$/)
{
push @matches, findFile($entry);
}
}
if (-f $entry && $filename =~ m/^msg\d\d\d\d.txt$/)
{
# print"filename ==============","$filename\n";
push @matches, $entry;
}
}
closedir(DIR);
return @matches;
}
@file_list = findFile("/var/lib/asterisk/sounds/voicemail/default");
# print @file_list;
#open an output file
#$out_file = '/tmp/tmp_file'; #define the name of output file
#open(OUT_FILE, ">$out_file");
$output_str = '';
#put head
#print OUT_FILE "Origbox, CallerId, OrigDate, Duration\n";
$output_str = $output_str."Origbox, CallerId, OrigDate, Duration\n";
#loop through file list
foreach $in_file_name (@file_list)
{
#open input file
open(IN_FILE, $in_file_name);
@lines = <IN_FILE>; #read file into an array
#loop through lines
%props = ();
foreach $_ (@lines)
{
if (/^origmailbox=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, origmailbox=>$entry[1]);
}
if (/^callerid=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, callerid=>$entry[1]);
}
if (/^origdate=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, origdate=>$entry[1]);
}
if (/^duration=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, duration=>$entry[1]);
}
}
if (length($props{"origmailbox"}) > 0) {
#print OUT_FILE "$props{origmailbox},$props{callerid},$props{origdate},$props{duration}\n";
$output_str = $output_str."$props{origmailbox},$props{callerid},$props{origdate},$props{duration}\n";
}
#finished job and close input file
close(IN_FILE);
}
#close(OUT_FILE);
my $msg = MIME::Lite->new(
From => 'dev@abc.com',
To => 'tom@abc.com',
Cc => 'jerry@abc.com',
Subject => 'Fonality Voice Mail Box Statistics',
Type => 'multipart/mixed',
);
$msg->attach(
Type => 'TEXT',
Data => "Please see attached file for detail...",
);
$msg->attach(
Type => 'text/csv',
Data => $output_str,
Filename => "fonality.csv",
);
$msg->send;
Tuesday, July 21, 2009
My first perl script
This is a perl script to scan a list of folders with certain name pattern, and retrieve certain information from specific file, then put into a CSV file and send a email to user with attachment.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment