]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Statistics.pm
PMG/API2/Statistics.pm: improve spamscore api - include percentage
[pmg-api.git] / PMG / API2 / Statistics.pm
1 package PMG::API2::Statistics;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6
7 use PVE::Tools;
8 use PVE::SafeSyslog;
9 use PVE::INotify;
10 use PVE::Exception qw(raise_param_exc);
11 use PVE::RESTHandler;
12 use PMG::RESTEnvironment;
13 use PVE::JSONSchema qw(get_standard_option);
14
15 use PMG::Utils;
16 use PMG::RuleDB;
17 use PMG::Statistic;
18
19 use base qw(PVE::RESTHandler);
20
21 __PACKAGE__->register_method ({
22 name => 'index',
23 path => '',
24 method => 'GET',
25 description => "Directory index.",
26 permissions => { check => [ 'admin', 'qmanager', 'audit'] },
27 parameters => {
28 additionalProperties => 0,
29 properties => {},
30 },
31 returns => {
32 type => 'array',
33 items => {
34 type => "object",
35 properties => {},
36 },
37 links => [ { rel => 'child', href => "{name}" } ],
38 },
39 code => sub {
40 my ($param) = @_;
41
42 return [
43 { name => "mail" },
44 { name => "spamscores" },
45 { name => "virus" },
46 ];
47 }});
48
49 __PACKAGE__->register_method ({
50 name => 'mail',
51 path => 'mail',
52 method => 'GET',
53 description => "General Mail Statistics.",
54 permissions => { check => [ 'admin', 'qmanager', 'audit'] },
55 parameters => {
56 additionalProperties => 0,
57 properties => {
58 starttime => get_standard_option('pmg-starttime'),
59 endtime => get_standard_option('pmg-endtime'),
60 },
61 },
62 returns => {
63 type => "object",
64 properties => {
65 avptime => {
66 description => "Average mail processing time in seconds.",
67 type => 'number',
68 },
69 bounces_in => {
70 description => "Incoming bounce mail count (sender = <>).",
71 type => 'number',
72 },
73 bounces_out => {
74 description => "Outgoing bounce mail count (sender = <>).",
75 type => 'number',
76 },
77 count => {
78 description => "Overall mail count (in and out).",
79 type => 'number',
80 },
81 count_in => {
82 description => "Incoming mail count.",
83 type => 'number',
84 },
85 count_out => {
86 description => "Outgoing mail count.",
87 type => 'number',
88 },
89 glcount => {
90 description => "Number of greylisted mails.",
91 type => 'number',
92 },
93 junk_in => {
94 description => "Incoming junk mail count (viruscount_in + spamcount_in + glcount + spfcount).",
95 type => 'number',
96 },
97 junk_out => {
98 description => "Outgoing junk mail count (viruscount_out + spamcount_out).",
99 type => 'number',
100 },
101 spamcount_in => {
102 description => "Incoming spam mails.",
103 type => 'number',
104 },
105 spamcount_out => {
106 description => "Outgoing spam mails.",
107 type => 'number',
108 },
109 spfcount => {
110 description => "Mails rejected by SPF.",
111 type => 'number',
112 },
113 traffic_in => {
114 description => "Incoming mail traffic (bytes).",
115 type => 'number',
116 },
117 traffic_out => {
118 description => "Outgoing mail traffic (bytes).",
119 type => 'number',
120 },
121 viruscount_in => {
122 description => "Number of incoming virus mails.",
123 type => 'number',
124 },
125 viruscount_out => {
126 description => "Number of outgoing virus mails.",
127 type => 'number',
128 },
129 },
130 },
131 code => sub {
132 my ($param) = @_;
133
134 my $restenv = PMG::RESTEnvironment->get();
135 my $cinfo = $restenv->{cinfo};
136
137 my $start = $param->{starttime} // (time - 86400);
138 my $end = $param->{endtime} // ($start + 86400);
139
140 my $stat = PMG::Statistic->new($start, $end);
141 my $rdb = PMG::RuleDB->new();
142
143 my $res = $stat->total_mail_stat($rdb);
144
145 return $res;
146 }});
147
148 __PACKAGE__->register_method ({
149 name => 'virus',
150 path => 'virus',
151 method => 'GET',
152 description => "Get Statistics about detected Viruses.",
153 permissions => { check => [ 'admin', 'qmanager', 'audit'] },
154 parameters => {
155 additionalProperties => 0,
156 properties => {
157 starttime => get_standard_option('pmg-starttime'),
158 endtime => get_standard_option('pmg-endtime'),
159 },
160 },
161 returns => {
162 type => 'array',
163 items => {
164 type => "object",
165 properties => {
166 name => {
167 description => 'Virus name.',
168 type => 'string',
169 },
170 count => {
171 description => 'Detection count.',
172 type => 'integer',
173 },
174 },
175 }
176 },
177 code => sub {
178 my ($param) = @_;
179
180 my $restenv = PMG::RESTEnvironment->get();
181 my $cinfo = $restenv->{cinfo};
182
183 my $start = $param->{starttime} // (time - 86400);
184 my $end = $param->{endtime} // ($start + 86400);
185
186 my $stat = PMG::Statistic->new($start, $end);
187 my $rdb = PMG::RuleDB->new();
188
189 my $res = $stat->total_virus_stat($rdb);
190
191 return $res;
192 }});
193
194 __PACKAGE__->register_method ({
195 name => 'spamscores',
196 path => 'spamscores',
197 method => 'GET',
198 description => "Get the count of spam mails grouped by spam score. " .
199 "Count for score 10 includes mails with spam score > 10.",
200 permissions => { check => [ 'admin', 'qmanager', 'audit'] },
201 parameters => {
202 additionalProperties => 0,
203 properties => {
204 starttime => get_standard_option('pmg-starttime'),
205 endtime => get_standard_option('pmg-endtime'),
206 },
207 },
208 returns => {
209 type => 'array',
210 items => {
211 type => "object",
212 properties => {
213 level => {
214 description => 'Spam level.',
215 type => 'string',
216 },
217 count => {
218 description => 'Detection count.',
219 type => 'integer',
220 },
221 ratio => {
222 description => 'Portion of overall mail count.',
223 type => 'number',
224 },
225 },
226 }
227 },
228 code => sub {
229 my ($param) = @_;
230
231 my $restenv = PMG::RESTEnvironment->get();
232 my $cinfo = $restenv->{cinfo};
233
234 my $start = $param->{starttime} // (time - 86400);
235 my $end = $param->{endtime} // ($start + 86400);
236
237 my $stat = PMG::Statistic->new($start, $end);
238 my $rdb = PMG::RuleDB->new();
239
240 my $totalstat = $stat->total_mail_stat ($rdb);
241 my $spamstat = $stat->total_spam_stat($rdb);
242
243 my $res = [];
244
245 my $count_in = $totalstat->{count_in};
246 my $rest = $totalstat->{spamcount_in};
247
248 my $levelcount = {};
249 foreach my $ref (@$spamstat) {
250 my $level = $ref->{spamlevel} // 0;
251 next if $level >= 10 || $level < 1;
252 $rest -= $ref->{count} if $level >= 3;
253 $levelcount->{$level} = $ref->{count};
254 }
255
256 $levelcount->{0} = $totalstat->{count_in} - $totalstat->{spamcount_in};
257 $levelcount->{10} = $rest if $rest;
258
259 for (my $i = 0; $i <= 10; $i++) {
260 my $count = $levelcount->{$i} // 0;
261 my $ratio = $count_in ? $count/$count_in : 0;
262 push @$res, { level => $i, count => $count, ratio => $ratio };
263 }
264
265 return $res;
266 }});
267
268 1;