radicale 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # This file is related to Radicale - CalDAV and CardDAV server
  2. # for logwatch (script)
  3. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  4. #
  5. # Detail levels
  6. # >= 5: Logins
  7. # >= 10: ResponseTimes
  8. $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
  9. my %ResponseTimes;
  10. my %ResponseSizes;
  11. my %Responses;
  12. my %Requests;
  13. my %Logins;
  14. my %Loglevel;
  15. my %OtherEvents;
  16. my $sum;
  17. my $length;
  18. sub ResponseTimesMinMaxSum($$) {
  19. my $req = $_[0];
  20. my $time = $_[1];
  21. $ResponseTimes{$req}->{'cnt'}++;
  22. if (! defined $ResponseTimes{$req}->{'min'}) {
  23. $ResponseTimes{$req}->{'min'} = $time;
  24. } elsif ($ResponseTimes->{$req}->{'min'} > $time) {
  25. $ResponseTimes{$req}->{'min'} = $time;
  26. }
  27. if (! defined $ResponseTimes{$req}->{'max'}) {
  28. $ResponseTimes{$req}{'max'} = $time;
  29. } elsif ($ResponseTimes{$req}->{'max'} < $time) {
  30. $ResponseTimes{$req}{'max'} = $time;
  31. }
  32. $ResponseTimes{$req}->{'sum'} += $time;
  33. }
  34. sub ResponseSizesMinMaxSum($$$) {
  35. my $req = $_[0];
  36. my $type = $_[1];
  37. my $size = $_[2];
  38. $ResponseSizes{$type}->{$req}->{'cnt'}++;
  39. if (! defined $ResponseSizes{$type}->{$req}->{'min'}) {
  40. $ResponseSizes{$type}->{$req}->{'min'} = $size;
  41. } elsif ($ResponseSizes{$type}->{$req}->{'min'} > $size) {
  42. $ResponseSizes{$type}->{$req}->{'min'} = $size;
  43. }
  44. if (! defined $ResponseSizes{$type}->{$req}->{'max'}) {
  45. $ResponseSizes{$type}->{$req}{'max'} = $size;
  46. } elsif ($ResponseSizes{$type}->{$req}->{'max'} < $size) {
  47. $ResponseSizes{$type}->{$req}{'max'} = $size;
  48. }
  49. $ResponseSizes{$type}->{$req}->{'sum'} += $size;
  50. }
  51. sub Sum($) {
  52. my $phash = $_[0];
  53. my $sum = 0;
  54. foreach my $entry (keys %$phash) {
  55. $sum += $phash->{$entry};
  56. }
  57. return $sum;
  58. }
  59. sub MaxLength($) {
  60. my $phash = $_[0];
  61. my $length = 0;
  62. foreach my $entry (keys %$phash) {
  63. $length = length($entry) if (length($entry) > $length);
  64. }
  65. return $length;
  66. }
  67. while (defined($ThisLine = <STDIN>)) {
  68. # count loglevel
  69. if ( $ThisLine =~ /\[(DEBUG|INFO|WARNING|ERROR|CRITICAL)\] /o ) {
  70. $Loglevel{$1}++
  71. }
  72. # parse log for events
  73. if ( $ThisLine =~ /Radicale server ready/o ) {
  74. $OtherEvents{"Radicale server started"}++;
  75. }
  76. elsif ( $ThisLine =~ /Stopping Radicale/o ) {
  77. $OtherEvents{"Radicale server stopped"}++;
  78. }
  79. elsif ( $ThisLine =~ / (\S+) response status/o ) {
  80. my $req = $1;
  81. if ( $ThisLine =~ / \S+ response status for .* with depth '(\d)' in ([0-9.]+) seconds: (\d+)/o ) {
  82. $req .= ":D=" . $1 . ":R=" . $3;
  83. ResponseTimesMinMaxSum($req, $2) if ($Detail >= 10);
  84. } elsif ( $ThisLine =~ / \S+ response status for .* in ([0-9.]+) seconds: (\d+)/o ) {
  85. $req .= ":R=" . $2;
  86. ResponseTimesMinMaxSum($req, $1) if ($Detail >= 10);
  87. } elsif ( $ThisLine =~ / \S+ response status for .* with depth '(\d)' in ([0-9.]+) seconds (\S+) (\d+) bytes: (\d+)/o ) {
  88. $req .= ":D=" . $1 . ":R=" . $5;
  89. ResponseTimesMinMaxSum($req, $2) if ($Detail >= 10);
  90. ResponseSizesMinMaxSum($req, $3, $4) if ($Detail >= 10);
  91. } elsif ( $ThisLine =~ / \S+ response status for .* in ([0-9.]+) seconds (\S+) (\d+) bytes: (\d+)/o ) {
  92. $req .= ":R=" . $4;
  93. ResponseTimesMinMaxSum($req, $1) if ($Detail >= 10);
  94. ResponseSizesMinMaxSum($req, $2, $3) if ($Detail >= 10);
  95. }
  96. $Responses{$req}++;
  97. }
  98. elsif ( $ThisLine =~ / (\S+) request for/o ) {
  99. my $req = $1;
  100. if ( $ThisLine =~ / \S+ request for .* with depth '(\d)' received/o ) {
  101. $req .= ":D=" . $1;
  102. }
  103. $Requests{$req}++;
  104. }
  105. elsif ( $ThisLine =~ / (Successful login): '([^']+)'/o ) {
  106. $Logins{$2}++ if ($Detail >= 5);
  107. $OtherEvents{$1}++;
  108. }
  109. elsif ( $ThisLine =~ / (Failed login attempt) /o ) {
  110. $OtherEvents{$1}++;
  111. }
  112. elsif ( $ThisLine =~ / (Profiling data per request method \S+) /o ) {
  113. my $info = $1;
  114. if ( $ThisLine =~ /(no request seen so far)/o ) {
  115. $OtherEvents{$info . " - " . $1}++;
  116. } else {
  117. $OtherEvents{$info}++;
  118. };
  119. }
  120. elsif ( $ThisLine =~ / (Profiling data per request \S+) /o ) {
  121. my $info = $1;
  122. if ( $ThisLine =~ /(suppressed because duration below minimum|suppressed because of no data)/o ) {
  123. $OtherEvents{$info . " - " . $1}++;
  124. } else {
  125. $OtherEvents{$info}++;
  126. };
  127. }
  128. elsif ( $ThisLine =~ /\[(DEBUG|INFO)\] /o ) {
  129. # skip if DEBUG+INFO
  130. }
  131. else {
  132. # Report any unmatched entries...
  133. if ($ThisLine =~ /^({\'| )/o) {
  134. # skip profiling or raw header data
  135. next;
  136. };
  137. if ($ThisLine =~ /^$/o) {
  138. # skip empty line
  139. next;
  140. };
  141. $ThisLine =~ s/^\[\d+(\/Thread-\d+)?\] //; # remove process/Thread ID
  142. chomp($ThisLine);
  143. $OtherList{$ThisLine}++;
  144. }
  145. }
  146. if ($Started) {
  147. print "\nStatistics:\n";
  148. print " Radicale started: $Started Time(s)\n";
  149. }
  150. if (keys %Loglevel) {
  151. $sum = Sum(\%Loglevel);
  152. print "\n**Loglevel counters**\n";
  153. printf "%-18s | %7s | %5s |\n", "Loglevel", "cnt", "ratio";
  154. print "-" x38 . "\n";
  155. foreach my $level (sort keys %Loglevel) {
  156. printf "%-18s | %7d | %3d%% |\n", $level, $Loglevel{$level}, int(($Loglevel{$level} * 100) / $sum);
  157. }
  158. print "-" x38 . "\n";
  159. printf "%-18s | %7d | %3d%% |\n", "", $sum, 100;
  160. }
  161. if (keys %Requests) {
  162. $sum = Sum(\%Requests);
  163. print "\n**Request counters (D=<depth>)**\n";
  164. printf "%-18s | %7s | %5s |\n", "Request", "cnt", "ratio";
  165. print "-" x38 . "\n";
  166. foreach my $req (sort keys %Requests) {
  167. printf "%-18s | %7d | %3d%% |\n", $req, $Requests{$req}, int(($Requests{$req} * 100) / $sum);
  168. }
  169. print "-" x38 . "\n";
  170. printf "%-18s | %7d | %3d%% |\n", "", $sum, 100;
  171. }
  172. if (keys %Responses) {
  173. $sum = Sum(\%Responses);
  174. print "\n**Response result counters ((D=<depth> R=<result>)**\n";
  175. printf "%-18s | %7s | %5s |\n", "Response", "cnt", "ratio";
  176. print "-" x38 . "\n";
  177. foreach my $req (sort keys %Responses) {
  178. printf "%-18s | %7d | %3d%% |\n", $req, $Responses{$req}, int(($Responses{$req} * 100) / $sum);
  179. }
  180. print "-" x38 . "\n";
  181. printf "%-18s | %7d | %3d%% |\n", "", $sum, 100;
  182. }
  183. if (keys %Logins) {
  184. $sum = Sum(\%Logins);
  185. $length = MaxLength(\%Logins);
  186. print "\n**Successful login counters**\n";
  187. printf "%-" . $length . "s | %7s | %5s |\n", "Login", "cnt", "ratio";
  188. print "-" x($length + 20) . "\n";
  189. foreach my $login (sort keys %Logins) {
  190. printf "%-" . $length . "s | %7d | %3d%% |\n", $login, $Logins{$login}, int(($Logins{$login} * 100) / $sum);
  191. }
  192. print "-" x($length + 20) . "\n";
  193. printf "%-" . $length . "s | %7d | %3d%% |\n", "", $sum, 100;
  194. }
  195. if (keys %ResponseTimes) {
  196. print "\n**Response timings (counts, seconds) (D=<depth> R=<result>)**\n";
  197. printf "%-18s | %7s | %7s | %7s | %7s |\n", "Response", "cnt", "min", "max", "avg";
  198. print "-" x60 . "\n";
  199. foreach my $req (sort keys %ResponseTimes) {
  200. printf "%-18s | %7d | %7.3f | %7.3f | %7.3f |\n", $req
  201. , $ResponseTimes{$req}->{'cnt'}
  202. , $ResponseTimes{$req}->{'min'}
  203. , $ResponseTimes{$req}->{'max'}
  204. , $ResponseTimes{$req}->{'sum'} / $ResponseTimes{$req}->{'cnt'};
  205. }
  206. print "-" x60 . "\n";
  207. }
  208. if (keys %ResponseSizes) {
  209. for my $type (sort keys %ResponseSizes) {
  210. print "\n**Response sizes (counts, bytes: $type) (D=<depth> R=<result>)**\n";
  211. printf "%-18s | %7s | %9s | %9s | %9s |\n", "Response", "cnt", "min", "max", "avg";
  212. print "-" x66 . "\n";
  213. foreach my $req (sort keys %{$ResponseSizes{$type}}) {
  214. printf "%-18s | %7d | %9d | %9d | %9d |\n", $req
  215. , $ResponseSizes{$type}->{$req}->{'cnt'}
  216. , $ResponseSizes{$type}->{$req}->{'min'}
  217. , $ResponseSizes{$type}->{$req}->{'max'}
  218. , $ResponseSizes{$type}->{$req}->{'sum'} / $ResponseSizes{$type}->{$req}->{'cnt'};
  219. }
  220. print "-" x66 . "\n";
  221. }
  222. }
  223. if (keys %OtherEvents) {
  224. print "\n**Other Events**\n";
  225. foreach $ThisOne (sort keys %OtherEvents) {
  226. print "$ThisOne: $OtherEvents{$ThisOne} Time(s)\n";
  227. }
  228. }
  229. if (keys %OtherList) {
  230. print "\n**Unmatched Entries**\n";
  231. foreach $ThisOne (sort keys %OtherList) {
  232. print "$ThisOne: $OtherList{$ThisOne} Time(s)\n";
  233. }
  234. }
  235. exit(0);
  236. # vim: shiftwidth=3 tabstop=3 syntax=perl et smartindent