Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use lib '/root/fabricio/lib/';
  4. use EMAILTOTAL;
  5. use Net::SMTP;
  6. use CGI ':standard';
  7. use Net::SSH qw(sshopen2);
  8. use Net::SCP;
  9. use GD::Graph::bars;
  10. use threads;
  11. use threads::shared;
  12. use Data::Dumper;
  13. use DBI;
  14.  
  15. # Global Variables
  16. my $whitelist = "/etc/spam/whitelist.conf";
  17. my @senderstecla = qw(equipe.mail@tecla.com.br fabricio.fonseca@alog.com.br davidson.flores@alog.com.br);
  18. my @sendersalog = qw{equipe.email@alog.com.br suporterj@alog.com.br};
  19. my @remoterelays = qw{relay01 relay02 relay03};
  20. my %auths : shared;
  21. my %hours : shared;
  22. my %pops : shared;
  23. my %imaps : shared;
  24. my @domains;
  25. my $dbhost = "10.0.6.4";
  26. my $db = "ldap";
  27. my $dbuser = "ldap";
  28. my $dbpass = "babaca";
  29.  
  30.  
  31. # Main program.
  32. # This program part will make some threads to optimize the auths counting.
  33. {
  34. my @thrs;
  35. my @thrs2;
  36. foreach my $count (0..$#remoterelays) {
  37. my $thr = threads->create(\&getlogging, $remoterelays[$count]);
  38. my $thr2 = threads->create(\&dovecotlog, $remoterelays[$count]);
  39. push @thrs, $thr;
  40. push @thrs2, $thr2;
  41. }
  42. foreach (@thrs) {
  43. $_->join;
  44. }
  45. foreach (@thrs2) {
  46. $_->join;
  47. }
  48. };
  49.  
  50. # This program part is gonna remove all blacklist user or domains.
  51. {
  52. # Removing some domains which cannot be block.
  53. open WHITE, $whitelist or warn "Nao foi possivel abrir o arquivo de whitelist: $!\n";
  54. while (<WHITE>) {
  55. chomp;
  56. foreach my $keys (keys %auths) {
  57. if ($keys =~ /\@(.+)/) {
  58. delete $auths{$keys} if $1 eq $_;
  59. }
  60. delete $auths{$keys} if $keys eq $_;
  61. }
  62. }
  63. close WHITE;
  64. };
  65.  
  66. my $authscount = &emailscalc(\%auths);
  67. if ($authscount ne 1) {
  68. my $markdomains = &domainmark($db,$dbhost,$dbuser,$dbpass, $authscount);
  69. &graphcreate(\%hours,\%imaps,\%pops);
  70. &html($markdomains);
  71. } else {
  72. &graphcreate(\%hours,\%imaps,\%pops);
  73. &html(undef);
  74. }
  75.  
  76. #foreach my $domain (keys %{$authscount}) {
  77. # print "\n";
  78. # print "$domain\n";
  79. # foreach my $email (keys %{$authscount->{$domain}}) {
  80. # print "$email => $authscount->{$domain}->{$email} \n";
  81. # }
  82. #}
  83.  
  84.  
  85. # Subroutines.
  86. # This subroutine is gonna get all logging information over the Network.
  87. sub getlogging {
  88. my $rhosts = shift;
  89. my $remoteuser = 'root';
  90. my $remotecmd = 'cat /var/log/mail.log';
  91. chomp ($rhosts);
  92. sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$remotecmd") || die "ssh: $!";
  93. while (<READER>) {
  94. 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) {
  95. chomp;
  96. my $hour = $1;
  97. if (/sasl_username=(.+)\,\s{0,2}sasl_sender=(:?.+)/) {
  98. $auths{$2}++;
  99. $hours{$hour}++;
  100. } else {
  101. $auths{$2}++;
  102. $hours{$hour}++;
  103. }
  104. }
  105. }
  106. close READER;
  107. close WRITER;
  108. }
  109.  
  110. sub dovecotlog {
  111. my $rhosts = shift;
  112. my $remoteuser = 'root';
  113. my $remotecmd = 'cat /var/log/dovecot.log';
  114. chomp ($rhosts);
  115. sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$remotecmd") || die "ssh: $!";
  116. while (<READER>) {
  117. 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/) {
  118. $pops{$rhosts}++;
  119. }
  120. 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/) {
  121. $imaps{$rhosts}++;
  122. }
  123. }
  124. close READER;
  125. close WRITER;
  126. }
  127.  
  128. # This sobrouting is gonna do math all accounts.
  129. sub emailscalc {
  130. my $auths = shift;
  131. my %domains;
  132. if ($auths) {
  133. foreach my $auth (keys %{$auths}) {
  134. if ($auths->{$auth} > 10) {
  135. if ($auth =~ /\@(.+)/i) {
  136. $domains{$1}{$auth} = $auths->{$auth};
  137. }
  138. }
  139. delete $auths->{$auth};
  140. }
  141. if (%domains) {
  142. return \%domains;
  143. } else {
  144. return 1;
  145. }
  146. }
  147. }
  148.  
  149. # This subrouting is gonna mark all domains.
  150. sub domainmark {
  151. my $db = shift;
  152. my $dbhost = shift;
  153. my $dbuser = shift;
  154. my $dbpass = shift;
  155. my $domains = shift;
  156. my %refdomainmarkreturn;
  157. my $dbc = DBI->connect("dbi:Pg:database=$db;host=$dbhost",$dbuser,$dbpass) or die $DBI::errstr;
  158. foreach my $domain (keys %{$domains}) {
  159. if ($domain) {
  160. my $objexec = $dbc->prepare("select painel from dominios where dominio=\'$domain\'");
  161. $objexec->execute;
  162. if (my $dbresu = $objexec->fetchrow()) {
  163. if ($dbresu eq "HSPHERE") {
  164. foreach my $auth (keys %{$domains->{$domain}}) {
  165. $refdomainmarkreturn{'tecla'}{$domain}{$auth} = $domains->{$domain}->{$auth};
  166. }
  167. } else {
  168. foreach my $auth (keys %{$domains->{$domain}}) {
  169. $refdomainmarkreturn{'alog'}{$domain}{$auth} = $domains->{$domain}->{$auth};
  170. }
  171. }
  172. }
  173.  
  174. }
  175. }
  176. return \%refdomainmarkreturn;
  177. $dbc->disconnect;
  178. }
  179.  
  180. sub graphcreate {
  181. my $valores = shift;
  182. my $imaps = shift;
  183. my $pops = shift;
  184. {
  185. my @thrs;
  186. my @thrs2;
  187. my @thrs3;
  188. {
  189. my $thr = threads->create(\&conn,$valores);
  190. my $thr2 = threads->create(\&graphimap,$imaps);
  191. my $thr3 = threads->create(\&graphpop,$pops);
  192. push @thrs, $thr;
  193. push @thrs2, $thr2;
  194. push @thrs3, $thr3;
  195. };
  196. foreach (@thrs) {
  197. $_->join;
  198. }
  199. foreach (@thrs2) {
  200. $_->join;
  201. }
  202. foreach (@thrs3) {
  203. $_->join;
  204. }
  205. };
  206.  
  207. sub conn {
  208. my $pngpath = '/var/www/completo.png';
  209. my $get = shift;
  210. my %connects = %{$get};
  211. my @data;
  212. foreach my $connect (sort { $a <=> $b } keys %connects) {
  213. push @{$data[0]}, $connect;
  214. push @{$data[1]}, $connects{$connect};
  215. }
  216. my $graph = new GD::Graph::bars(600,400);
  217. $graph->set(
  218. y_label => 'Autenticacoes',
  219. x_label => 'Horas',
  220. long_ticks => 1,
  221. y_tick_number => 8,
  222. y_label_skip => 1,
  223. bar_spacing => 3,
  224. shadow_depth => 4,
  225. accent_treshold => 200,
  226. transparent => 0,
  227. x_labels_vertical => 0,
  228. values_vertical => 0,
  229. );
  230. $graph = $graph->plot(\@data) or die $graph->error;
  231. open OUT,'>',$pngpath;
  232. binmode OUT;
  233. print OUT $graph->png;
  234. close OUT;
  235. }
  236. sub graphimap {
  237. my $pngpath = '/var/www/imap.png';
  238. my $imaps = shift;
  239. my @data;
  240. foreach my $imap (sort {$imaps->{$a} <=> $imaps->{$b}} keys %{$imaps}) {
  241. push @{$data[0]}, $imap;
  242. push @{$data[1]}, $imaps->{$imap};
  243. }
  244. my $graph = new GD::Graph::bars(600,400);
  245. $graph->set(
  246. y_label => 'Autenticacoes',
  247. x_label => 'Relays',
  248. long_ticks => 1,
  249. y_tick_number => 8,
  250. y_label_skip => 1,
  251. bar_spacing => 3,
  252. shadow_depth => 4,
  253. accent_treshold => 200,
  254. transparent => 0,
  255. x_labels_vertical => 0,
  256. values_vertical => 0,
  257. );
  258. $graph = $graph->plot(\@data) or die $graph->error;
  259. open OUT,'>',$pngpath;
  260. print OUT $graph->png;
  261. close OUT;
  262. }
  263. sub graphpop {
  264. my $pngpath = '/var/www/pop.png';
  265. my $pops = shift;
  266. my @data;
  267. foreach my $pop (sort {$pops->{$a} <=> $pops->{$b}} keys %{$pops}) {
  268. push @{$data[0]}, $pop;
  269. push @{$data[1]}, $pops->{$pop};
  270. }
  271. my $graph = new GD::Graph::bars(600,400);
  272. $graph->set(
  273. y_label => 'Autenticacoes',
  274. x_label => 'Horas',
  275. long_ticks => 1,
  276. y_tick_number => 8,
  277. y_label_skip => 1,
  278. bar_spacing => 3,
  279. shadow_depth => 4,
  280. accent_treshold => 200,
  281. transparent => 0,
  282. x_labels_vertical => 0,
  283. values_vertical => 0,
  284. );
  285. $graph = $graph->plot(\@data) or die $graph->error;
  286. open OUT,'>',$pngpath;
  287. print OUT $graph->png;
  288. close OUT;
  289. }
  290. }
  291.  
  292. sub html {
  293. my $unidades = shift;
  294. my %copy;
  295. if ($unidades) {
  296. foreach my $unidade ( keys %{$unidades}) {
  297. foreach my $domain (keys %{$unidades->{$unidade}}) {
  298. foreach my $email (keys %{$unidades->{$unidade}->{$domain}}) {
  299. $copy{$email}= $unidades->{$unidade}->{$domain}->{$email};
  300. }
  301. }
  302. }
  303. }
  304. $unidades = undef;
  305. print header;
  306. print start_html(-title=>'Relatorio WEB',-style=>{-src=>['/css/spam.css'],-media => 'all'});
  307. print '<table class="emails" cellspacing="0">';
  308. if (%copy) {
  309. print '<tr>';
  310. print '<td class="titulo">Quantidade de Autenticações</td>';
  311. print '<td class="titulo"> Origens </td>';
  312. print '</tr>';
  313. foreach my $email (sort {$copy{$b} <=> $copy{$a}} keys %copy) {
  314. print '<tr>';
  315. print '<td class="emailandnum">' . $copy{$email} . '</td>';
  316. print '<td class="emailandnum">' . $email . '</td>';
  317. print '</tr>';
  318. }
  319. } else {
  320. print '<tr>';
  321. print '<td class="titulo">' . 'Não houve conta que enviou mais de 3000 emails.' . '</td>';
  322. print '</tr>';
  323. }
  324. print '</table>';
  325. print '<table class="graficos" cellspacing="0">';
  326. print '<tr>';
  327. print '<td class="titulo"> Gráficos </td>';
  328. print '<tr>';
  329. print '<tr>';
  330. print '<td class="titulo"> Quantidade de autenticação</td>';
  331. print '</tr>';
  332. print '<tr>';
  333. print '<td><img src="/completo.png"></img></td>';
  334. print '</td>';
  335. print '<tr>';
  336. print '<td class="titulo">Autenticacoes IMAP</td>';
  337. print '</tr>';
  338. print '<tr>';
  339. print '<td><img src="/imap.png"></img></td>';
  340. print '</td>';
  341. print '<tr>';
  342. print '<td class="titulo">Autenticacoes POP</td>';
  343. print '</tr>';
  344. print '<tr>';
  345. print '<td><img src="/pop.png"></img></td>';
  346. print '</td>';
  347. print ' </table>';
  348. print end_html;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement