Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use lib '/root/fabricio/lib/';
- use EMAILTOTAL;
- use Net::SMTP;
- use CGI ':standard';
- use Net::SSH qw(sshopen2);
- use Net::SCP;
- use GD::Graph::bars;
- use threads;
- use threads::shared;
- use Data::Dumper;
- use DBI;
- # Global Variables
- my $whitelist = "/etc/spam/whitelist.conf";
- my @senderstecla = qw(equipe.mail@tecla.com.br fabricio.fonseca@alog.com.br davidson.flores@alog.com.br);
- my @sendersalog = qw{equipe.email@alog.com.br suporterj@alog.com.br};
- my @remoterelays = qw{relay01 relay02 relay03};
- my %auths : shared;
- my %hours : shared;
- my %pops : shared;
- my %imaps : shared;
- my @domains;
- my $dbhost = "10.0.6.4";
- my $db = "ldap";
- my $dbuser = "ldap";
- my $dbpass = "babaca";
- # Main program.
- # This program part will make some threads to optimize the auths counting.
- {
- my @thrs;
- my @thrs2;
- foreach my $count (0..$#remoterelays) {
- my $thr = threads->create(\&getlogging, $remoterelays[$count]);
- my $thr2 = threads->create(\&dovecotlog, $remoterelays[$count]);
- push @thrs, $thr;
- push @thrs2, $thr2;
- }
- foreach (@thrs) {
- $_->join;
- }
- foreach (@thrs2) {
- $_->join;
- }
- };
- # This program part is gonna remove all blacklist user or domains.
- {
- # Removing some domains which cannot be block.
- open WHITE, $whitelist or warn "Nao foi possivel abrir o arquivo de whitelist: $!\n";
- while (<WHITE>) {
- chomp;
- foreach my $keys (keys %auths) {
- if ($keys =~ /\@(.+)/) {
- delete $auths{$keys} if $1 eq $_;
- }
- delete $auths{$keys} if $keys eq $_;
- }
- }
- close WHITE;
- };
- my $authscount = &emailscalc(\%auths);
- if ($authscount ne 1) {
- my $markdomains = &domainmark($db,$dbhost,$dbuser,$dbpass, $authscount);
- &graphcreate(\%hours,\%imaps,\%pops);
- &html($markdomains);
- } else {
- &graphcreate(\%hours,\%imaps,\%pops);
- &html(undef);
- }
- #foreach my $domain (keys %{$authscount}) {
- # print "\n";
- # print "$domain\n";
- # foreach my $email (keys %{$authscount->{$domain}}) {
- # print "$email => $authscount->{$domain}->{$email} \n";
- # }
- #}
- # Subroutines.
- # This subroutine is gonna get all logging information over the Network.
- sub getlogging {
- my $rhosts = shift;
- my $remoteuser = 'root';
- my $remotecmd = 'cat /var/log/mail.log';
- chomp ($rhosts);
- sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$remotecmd") || die "ssh: $!";
- while (<READER>) {
- if (/^\w{0,3}\s{0,2}\d{0,2}\s{0,2}(\d{0,2})\:\d{0,2}\:\d{0,2}.+\:\s{0,2}\w{0,15}\:\s{0,2}client=.+\,\s{0,2}sasl_method=LOGIN\,\s{0,2}sasl_username=(.+)/i) {
- chomp;
- my $hour = $1;
- if (/sasl_username=(.+)\,\s{0,2}sasl_sender=(:?.+)/) {
- $auths{$2}++;
- $hours{$hour}++;
- } else {
- $auths{$2}++;
- $hours{$hour}++;
- }
- }
- }
- close READER;
- close WRITER;
- }
- sub dovecotlog {
- my $rhosts = shift;
- my $remoteuser = 'root';
- my $remotecmd = 'cat /var/log/dovecot.log';
- chomp ($rhosts);
- sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$remotecmd") || die "ssh: $!";
- while (<READER>) {
- if (/^\w{0,3}\s{0,2}\d{0,2}\s{0,2}\d{0,2}\:\d{0,2}\:\d{0,2}\s.+pop3-login\:\s{0,2}proxy.+\:\s{0,3}started\sproxying/) {
- $pops{$rhosts}++;
- }
- if (/^\w{0,3}\s{0,2}\d{0,2}\s{0,2}\d{0,2}\:\d{0,2}\:\d{0,2}\s.+imap-login\:\s{0,2}proxy.+\:\s{0,3}started\sproxying/) {
- $imaps{$rhosts}++;
- }
- }
- close READER;
- close WRITER;
- }
- # This sobrouting is gonna do math all accounts.
- sub emailscalc {
- my $auths = shift;
- my %domains;
- if ($auths) {
- foreach my $auth (keys %{$auths}) {
- if ($auths->{$auth} > 10) {
- if ($auth =~ /\@(.+)/i) {
- $domains{$1}{$auth} = $auths->{$auth};
- }
- }
- delete $auths->{$auth};
- }
- if (%domains) {
- return \%domains;
- } else {
- return 1;
- }
- }
- }
- # This subrouting is gonna mark all domains.
- sub domainmark {
- my $db = shift;
- my $dbhost = shift;
- my $dbuser = shift;
- my $dbpass = shift;
- my $domains = shift;
- my %refdomainmarkreturn;
- my $dbc = DBI->connect("dbi:Pg:database=$db;host=$dbhost",$dbuser,$dbpass) or die $DBI::errstr;
- foreach my $domain (keys %{$domains}) {
- if ($domain) {
- my $objexec = $dbc->prepare("select painel from dominios where dominio=\'$domain\'");
- $objexec->execute;
- if (my $dbresu = $objexec->fetchrow()) {
- if ($dbresu eq "HSPHERE") {
- foreach my $auth (keys %{$domains->{$domain}}) {
- $refdomainmarkreturn{'tecla'}{$domain}{$auth} = $domains->{$domain}->{$auth};
- }
- } else {
- foreach my $auth (keys %{$domains->{$domain}}) {
- $refdomainmarkreturn{'alog'}{$domain}{$auth} = $domains->{$domain}->{$auth};
- }
- }
- }
- }
- }
- return \%refdomainmarkreturn;
- $dbc->disconnect;
- }
- sub graphcreate {
- my $valores = shift;
- my $imaps = shift;
- my $pops = shift;
- {
- my @thrs;
- my @thrs2;
- my @thrs3;
- {
- my $thr = threads->create(\&conn,$valores);
- my $thr2 = threads->create(\&graphimap,$imaps);
- my $thr3 = threads->create(\&graphpop,$pops);
- push @thrs, $thr;
- push @thrs2, $thr2;
- push @thrs3, $thr3;
- };
- foreach (@thrs) {
- $_->join;
- }
- foreach (@thrs2) {
- $_->join;
- }
- foreach (@thrs3) {
- $_->join;
- }
- };
- sub conn {
- my $pngpath = '/var/www/completo.png';
- my $get = shift;
- my %connects = %{$get};
- my @data;
- foreach my $connect (sort { $a <=> $b } keys %connects) {
- push @{$data[0]}, $connect;
- push @{$data[1]}, $connects{$connect};
- }
- my $graph = new GD::Graph::bars(600,400);
- $graph->set(
- y_label => 'Autenticacoes',
- x_label => 'Horas',
- long_ticks => 1,
- y_tick_number => 8,
- y_label_skip => 1,
- bar_spacing => 3,
- shadow_depth => 4,
- accent_treshold => 200,
- transparent => 0,
- x_labels_vertical => 0,
- values_vertical => 0,
- );
- $graph = $graph->plot(\@data) or die $graph->error;
- open OUT,'>',$pngpath;
- binmode OUT;
- print OUT $graph->png;
- close OUT;
- }
- sub graphimap {
- my $pngpath = '/var/www/imap.png';
- my $imaps = shift;
- my @data;
- foreach my $imap (sort {$imaps->{$a} <=> $imaps->{$b}} keys %{$imaps}) {
- push @{$data[0]}, $imap;
- push @{$data[1]}, $imaps->{$imap};
- }
- my $graph = new GD::Graph::bars(600,400);
- $graph->set(
- y_label => 'Autenticacoes',
- x_label => 'Relays',
- long_ticks => 1,
- y_tick_number => 8,
- y_label_skip => 1,
- bar_spacing => 3,
- shadow_depth => 4,
- accent_treshold => 200,
- transparent => 0,
- x_labels_vertical => 0,
- values_vertical => 0,
- );
- $graph = $graph->plot(\@data) or die $graph->error;
- open OUT,'>',$pngpath;
- print OUT $graph->png;
- close OUT;
- }
- sub graphpop {
- my $pngpath = '/var/www/pop.png';
- my $pops = shift;
- my @data;
- foreach my $pop (sort {$pops->{$a} <=> $pops->{$b}} keys %{$pops}) {
- push @{$data[0]}, $pop;
- push @{$data[1]}, $pops->{$pop};
- }
- my $graph = new GD::Graph::bars(600,400);
- $graph->set(
- y_label => 'Autenticacoes',
- x_label => 'Horas',
- long_ticks => 1,
- y_tick_number => 8,
- y_label_skip => 1,
- bar_spacing => 3,
- shadow_depth => 4,
- accent_treshold => 200,
- transparent => 0,
- x_labels_vertical => 0,
- values_vertical => 0,
- );
- $graph = $graph->plot(\@data) or die $graph->error;
- open OUT,'>',$pngpath;
- print OUT $graph->png;
- close OUT;
- }
- }
- sub html {
- my $unidades = shift;
- my %copy;
- if ($unidades) {
- foreach my $unidade ( keys %{$unidades}) {
- foreach my $domain (keys %{$unidades->{$unidade}}) {
- foreach my $email (keys %{$unidades->{$unidade}->{$domain}}) {
- $copy{$email}= $unidades->{$unidade}->{$domain}->{$email};
- }
- }
- }
- }
- $unidades = undef;
- print header;
- print start_html(-title=>'Relatorio WEB',-style=>{-src=>['/css/spam.css'],-media => 'all'});
- print '<table class="emails" cellspacing="0">';
- if (%copy) {
- print '<tr>';
- print '<td class="titulo">Quantidade de Autenticações</td>';
- print '<td class="titulo"> Origens </td>';
- print '</tr>';
- foreach my $email (sort {$copy{$b} <=> $copy{$a}} keys %copy) {
- print '<tr>';
- print '<td class="emailandnum">' . $copy{$email} . '</td>';
- print '<td class="emailandnum">' . $email . '</td>';
- print '</tr>';
- }
- } else {
- print '<tr>';
- print '<td class="titulo">' . 'Não houve conta que enviou mais de 3000 emails.' . '</td>';
- print '</tr>';
- }
- print '</table>';
- print '<table class="graficos" cellspacing="0">';
- print '<tr>';
- print '<td class="titulo"> Gráficos </td>';
- print '<tr>';
- print '<tr>';
- print '<td class="titulo"> Quantidade de autenticação</td>';
- print '</tr>';
- print '<tr>';
- print '<td><img src="/completo.png"></img></td>';
- print '</td>';
- print '<tr>';
- print '<td class="titulo">Autenticacoes IMAP</td>';
- print '</tr>';
- print '<tr>';
- print '<td><img src="/imap.png"></img></td>';
- print '</td>';
- print '<tr>';
- print '<td class="titulo">Autenticacoes POP</td>';
- print '</tr>';
- print '<tr>';
- print '<td><img src="/pop.png"></img></td>';
- print '</td>';
- print ' </table>';
- print end_html;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement