Debian Codegraph @

log/show_message

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Data::Dumper;
7
8 my $QPDIR = get_qp_dir();
9 my $logfile = "$QPDIR/log/main/current";
10
11 my $is_ip = 0;
12 my $search = $ARGV[0];
13
14 if (!$search) {
15 die "\nusage: $0 [ ip_address | PID ]\n\n";
16 }
17
18 if ($search =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/) {
19
20 #print "it's an IP\n";
21 $is_ip++;
22 }
23
24 open my $LOG, '<', $logfile or die "unable to open $logfile\n";
25
26 if ($is_ip) { # look for the connection start message for the IP
27 my $ip_matches;
28 while (defined(my $line = <$LOG>)) {
29 next if !$line;
30 my ($tai, $pid, $mess) = split /\s/, $line, 3;
31 if ('Connection from ' eq substr($mess, 0, 16)) {
32 my ($ip) = (split /\s+/, $mess)[-1]; # IP is last word
33 $ip = substr $ip, 1, -1; # trim off brackets
34 if ($ip eq $search) {
35 $ip_matches++;
36 $search = $pid;
37 $is_ip = 0;
38 }
39 }
40 }
41 seek $LOG, 0, 0;
42 die "no pid found for ip $search\n" if $is_ip;
43 print "showing the last of $ip_matches connnections from $ARGV[0]\n";
44 }
45
46 print "showing QP message PID $search\n";
47
48 while (defined(my $line = <$LOG>)) {
49 next if !$line;
50 my ($tai, $pid, $mess) = split /\s/, $line, 3;
51 next if !$pid;
52 print $mess if ($pid eq $search);
53 }
54 close $LOG;
55
56 sub get_qp_dir {
57 foreach my $user (qw/ qpsmtpd smtpd /) {
58 my ($homedir) = (getpwnam($user))[7] or next;
59
60 if (-d "$homedir/plugins") {
61 return "$homedir";
62 }
63 foreach my $s (qw/ smtpd qpsmtpd qpsmtpd-dev /) {
64 if (-d "$homedir/$s/plugins") {
65 return "$homedir/$s";
66 }
67 }
68 }
69 if (-d "./plugins") {
70 return Cwd::getcwd();
71 }
72 }