summaryrefslogtreecommitdiffstats
path: root/utils/rspamd_stats.pl
blob: d0957316edfe8924bb3db86b9693834b17bcb0b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env perl

use Data::Dumper;
use Getopt::Long;
use warnings;
use strict;
use Time::Piece;

my @symbols_search;
my $start = "";
my $end = "";
my $reject_score = 30.0;
my $junk_score = 7.5;
my $log_file = "/var/log/rspamd/rspamd.log";
my $dateformat = "%Y-%m-%d %H:%M:%S";

GetOptions(
  "reject-score=f" => \$reject_score,
  "junk-score=f" => \$junk_score,
  "start=s" => \$start,
  "end=s" => \$end,
  "symbol=s@" => \@symbols_search,
  "log=s" => \$log_file,
  "dateformat=s" => \$dateformat);

# Global vars
my $total = 0;
my $total_spam = 0;
my $total_junk = 0;
my $junk_symbols = 0;
my $spam_symbols = 0;
my $ham_symbols = 0;
my $ham_spam_change = 0;
my $ham_junk_change = 0;
my $diff_alpha = 0.1;
my %sym_res;

my $st = 0;
my $ed = 0;

if ($start ne "") {
  $st = Time::Piece->strptime($start, $dateformat);
}

if ($end ne "") {
  $ed = Time::Piece->strptime($end, $dateformat);
}

my $rspamd_log;

if ($log_file eq '-') {
  $rspamd_log = \*STDIN;
}
else {
  open($rspamd_log, '<', $log_file) or die "cannot open $log_file";
}

while(<$rspamd_log>) {
  if (/^.*rspamd_task_write_log.*$/) {
    my @elts = split /\s+/;
    my $ts = $elts[0] . ' ' . $elts[1];

    if ($st or $ed) {
      my $dt = Time::Piece->strptime($ts, $dateformat) or die "cannot parse $ts";

      if ($dt) {
        if ($st != 0 && $dt < $st) {
          next;
        }
        if ($ed != 0 && $dt > $ed) {
          next;
        }
      }
    }

    if ($_ !~ /\[(-?\d+(?:\.\d+)?)\/(-?\d+(?:\.\d+)?)\]\s+\[([^\]]+)\]/) {
      #print "BAD\n";
      next;
    }

    $total ++;
    my $score = $1 * 1.0;

    if ($score >= $reject_score) {
      $total_spam ++;
    }
    elsif ($score >= $junk_score) {
      $total_junk ++;
    }

    # Symbols
    my @symbols = split /,/, $3;

    foreach my $s (@symbols_search) {
      my @selected = grep /$s/, @symbols;

      if (scalar(@selected) > 0) {
        foreach my $sym (@selected) {
          $sym =~ /^([^\(]+)(?:\(([^\)]+)\))?.*$/;
          my $sym_name = $1;
          my $sym_score = 0;
          if ($2) {
            my $sym_score = $2 * 1.0;

            if (abs($sym_score) < $diff_alpha) {
              next;
            }
          }

          if (!$sym_res{$sym_name}) {
            $sym_res{$sym_name} = {
              hits => 0,
              spam_hits => 0,
              junk_hits => 0,
              spam_change => 0,
              junk_change => 0,
              weight => 0,
            };
          }

          my $r = $sym_res{$sym_name};

          $r->{hits} ++;
          $r->{weight} += $sym_score;
          my $is_spam = 0;
          my $is_junk = 0;

          if ($score >= $reject_score) {
            $is_spam = 1;
            $r->{spam_hits} ++;
          }
          elsif ($score >= $junk_score) {
            $is_junk = 1;
            $r->{junk_hits} ++;
          }

          if ($sym_score != 0) {
            my $score_without = $score - $sym_score;

            if ($sym_score > 0) {
              if ($is_spam && $score_without < $reject_score) {
                $r->{spam_change} ++;
              }
              if ($is_junk && $score_without < $junk_score) {
                $r->{junk_change} ++;
              }
            }
            else {
              if (!$is_spam && $score_without >= $reject_score) {
                $r->{spam_change} ++;
              }
              if (!$is_junk && $score_without >= $junk_score) {
                $r->{junk_change} ++;
              }
            }
          }
        }
      }
    }
  }
}

my $total_ham = $total - ($total_spam + $total_junk);

if ($total > 0) {
  while (my ($s, $r) = each(%sym_res)) {
    if ($r->{hits} > 0) {
      my $th = $r->{hits};
      my $sh = $r->{spam_hits};
      my $jh = $r->{junk_hits};
      my $hh = $r->{hits} - $sh - $jh;
      my $htp = $hh * 100.0 / $total_ham if $total_ham != 0;
      my $stp = $sh * 100.0 / $total_spam if $total_spam != 0;
      my $jtp = $jh * 100.0 / $total_junk if $total_junk != 0;
      printf "Symbol: %s (weight %.3f) (%d hits, %.3f%%)\nHam hits: %d (%.3f%%), total ham: %d (ham with $s: %.3f%%)\nSpam hits: %d (%.3f%%), total spam: %d (spam with $s: %.3f%%)\nJunk hits: %d (%.3f%%), total junk: %d (junk with $s: %.3f%%)\n",
          $s, $r->{weight} / $r->{hits}, $th, ($th / $total * 100.0),
          $hh, ($hh / $th * 100.0), $total_ham, ($htp or 0),
          $sh, ($sh / $th * 100.0), $total_spam, ($stp or 0),
          $jh, ($jh / $th * 100.0), $total_junk, ($jtp or 0);
      my $schp = $r->{spam_change} / $total_spam * 100.0 if $total_spam;
      my $jchp = $r->{junk_change} / $total_junk * 100.0 if $total_junk;

      if ($r->{weight} != 0) {
        if ($r->{weight} > 0) {
          printf "Spam changes (ham/junk -> spam): %d (%.3f%%), total percentage (changes / spam hits): %.3f%%\nJunk changes (ham -> junk): %d (%.3f%%), total percentage (changes / junk hits): %.3f%%\n",
            $r->{spam_change}, ($r->{spam_change} / $th * 100.0), ($schp or 0),
            $r->{junk_change}, ($r->{junk_change} / $th * 100.0), ($jchp or 0);
        }
        else {
          printf "Spam changes (spam -> junk/ham): %d (%.3f%%), total percentage (changes / spam hits): %.3f%%\nJunk changes (junk -> ham): %d (%.3f%%), total percentage (changes / junk hits): %.3f%%\n",
            $r->{spam_change}, ($r->{spam_change} / $th * 100.0), ($schp or 0),
            $r->{junk_change}, ($r->{junk_change} / $th * 100.0), ($jchp or 0);
        }
      }
    }
    else {
      print "Symbol $s has not been met\n";
    }

    print '*' x 20 . "\n";
  }
}