I have a perl script which performs a pgrep
as follows:
use warnings; my @instance = `pgrep -fl varnish`; foreach my $line (@instance) { my $ip; my $port; my $pid; my $pidfile; my $fileconfiguration; my $bakendhost; my $bakendport; my $sharememory; my $user; chomp $line; if ($line =~ m/-Ts+([0-9]+.*)+:(d+)s+/) { $ip = $1; $port = $2; } }
However when launch the script am having the following error/warning:
Complex regular subexpression recursion limit (32766) exceeded at /home/k.sewnundun/test.pl line 16.
Complex regular subexpression recursion limit (32766) exceeded at /home/k.sewnundun/test.pl line 16.
Please find below the debug:
Loading DB routines from perl5db.pl version 1.32 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(/home/k.sewnundun/test.pl:3): 3: my @instance = `pgrep -fl varnish`; DB<1> n main::(/home/k.sewnundun/test.pl:4): 4: foreach my $line (@instance) { DB<1> n main::(/home/k.sewnundun/test.pl:5): 5: my $ip; DB<1> print $line 3066 /opt/rh/varnish4/root/usr/sbin/varnishd -P /var/run-daemon/varnishd-toto4.pid -a 0.0.0.0:8080 -f /etc/varnish/etc/toto4/toto4-current.vcl -u varnish4-toto4 -g varnish4-toto4 -l 80M -T 0.0.0.0:38080 -p thread_pool_min=50 -p thread_pool_max=1000 -p thread_pool_timeout=120 -p vcl_dir=/etc/varnish/etc/toto4 -n /home/varnish/toto4/working -s malloc, -S /etc/varnish/etc/toto4/toto4.secret DB<2> n main::(/home/k.sewnundun/test.pl:6): 6: my $port; DB<2> n main::(/home/k.sewnundun/test.pl:7): 7: my $pid; DB<2> n main::(/home/k.sewnundun/test.pl:8): 8: my $pidfile; DB<2> n main::(/home/k.sewnundun/test.pl:9): 9: my $fileconfiguration; DB<2> n main::(/home/k.sewnundun/test.pl:10): 10: my $bakendhost; DB<2> n main::(/home/k.sewnundun/test.pl:11): 11: my $bakendport; DB<2> n main::(/home/k.sewnundun/test.pl:12): 12: my $sharememory; DB<2> n main::(/home/k.sewnundun/test.pl:13): 13: my $user; DB<2> n main::(/home/k.sewnundun/test.pl:15): 15: chomp $line; DB<2> n main::(/home/k.sewnundun/test.pl:16): 16: if ($line =~ m/-Ts+([0-9]+.*)+:(d+)s+/) { DB<2> n main::(/home/k.sewnundun/test.pl:17): 17: $ip = $1; DB<2> n main::(/home/k.sewnundun/test.pl:18): 18: $port = $2; DB<2> n main::(/home/k.sewnundun/test.pl:5): 5: my $ip; DB<2> print $line 3078 /opt/rh/varnish4/root/usr/sbin/varnishd -P /var/run-daemon/varnishd-toto4.pid -a 0.0.0.0:8080 -f /etc/varnish/etc/toto4/toto4-current.vcl -u varnish4-toto4 -g varnish4-toto4 -l 80M -T 0.0.0.0:38080 -p thread_pool_min=50 -p thread_pool_max=1000 -p thread_pool_timeout=120 -p vcl_dir=/etc/varnish/etc/toto4 -n /home/varnish/toto4/working -s malloc, -S /etc/varnish/etc/toto4/toto4.secret DB<3> n main::(/home/k.sewnundun/test.pl:6): 6: my $port; DB<3> n main::(/home/k.sewnundun/test.pl:7): 7: my $pid; DB<3> n main::(/home/k.sewnundun/test.pl:8): 8: my $pidfile; DB<3> n main::(/home/k.sewnundun/test.pl:9): 9: my $fileconfiguration; DB<3> n main::(/home/k.sewnundun/test.pl:10): 10: my $bakendhost; DB<3> n main::(/home/k.sewnundun/test.pl:11): 11: my $bakendport; DB<3> n main::(/home/k.sewnundun/test.pl:12): 12: my $sharememory; DB<3> n main::(/home/k.sewnundun/test.pl:13): 13: my $user; DB<3> n main::(/home/k.sewnundun/test.pl:15): 15: chomp $line; DB<3> n main::(/home/k.sewnundun/test.pl:16): 16: if ($line =~ m/-Ts+([0-9]+.*)+:(d+)s+/) { DB<3> n main::(/home/k.sewnundun/test.pl:17): 17: $ip = $1; DB<3> n main::(/home/k.sewnundun/test.pl:18): 18: $port = $2; DB<3> n main::(/home/k.sewnundun/test.pl:5): 5: my $ip; DB<3> print $line 3200 /opt/rh/varnish4/root/usr/bin/varnishncsa -a -n toto4 -w /home/logs/varnish/toto4/access.log -D -P /var/run-daemon/varnishncsa-toto4.pid -F %{X-Forwarded-For}i %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i" DB<4> n main::(/home/k.sewnundun/test.pl:6): 6: my $port; DB<4> n main::(/home/k.sewnundun/test.pl:7): 7: my $pid; DB<4> n main::(/home/k.sewnundun/test.pl:8): 8: my $pidfile; DB<4> n main::(/home/k.sewnundun/test.pl:9): 9: my $fileconfiguration; DB<4> n main::(/home/k.sewnundun/test.pl:10): 10: my $bakendhost; DB<4> n main::(/home/k.sewnundun/test.pl:11): 11: my $bakendport; DB<4> n main::(/home/k.sewnundun/test.pl:12): 12: my $sharememory; DB<4> n main::(/home/k.sewnundun/test.pl:13): 13: my $user; DB<4> n main::(/home/k.sewnundun/test.pl:15): 15: chomp $line; DB<4> n main::(/home/k.sewnundun/test.pl:16): 16: if ($line =~ m/-Ts+([0-9]+.*)+:(d+)s+/) { DB<4> n main::(/home/k.sewnundun/test.pl:5): 5: my $ip; DB<4> print $line 9803 varnishd -P /var/run-daemon/varnishd-varnish.pid -a 0.0.0.0:80 -f /etc/varnish/varnish.com/varnish.vcl -u varnish-varnish -l 80M -T 127.0.0.1:33080 -w 200,500,120 -n /home/varnish/varnish/working -s malloc,1G -p user varnish-varnish -p acceptor_sleep_decay 0.9 -p acceptor_sleep_incr 0.001 -p acceptor_sleep_max 0.05 -p auto_restart on -p ban_lurker_sleep 0.0 -p between_bytes_timeout 60.0 -p cli_buffer 8192 -p cli_timeout 5 -p clock_skew 10 -p connect_timeout 0.4 -p critbit_cooloff 180.0 -p default_grace 10 -p default_ttl 120 -p diag_bitmap 0x0 -p esi_syntax 0 -p expiry_sleep 1.0 -p fetch_chunksize 128 -p first_byte_timeout 60.0 -p group varnish-varnish -p http_range_support off -p listen_depth 1024 -p log_hashstring off -p log_local_address off -p lru_interval 2 -p max_esi_depth 5 -p max_restarts 4 -p ping_interval 3 -p pipe_timeout 60 -p prefer_ipv6 off -p queue_max 100 -p rush_exponent 3 -p saintmode_threshold 10 -p send_timeout 600 -p sess_timeout 5 -p sess_workspace 16384 -p session_linger 50 -p session_max 10000 -p shm_reclen 255 -p shm_workspace 8192 -p syslog_cli_traffic on -p vcl_trace off -p vcl_dir -p waiter default DB<5> n main::(/home/k.sewnundun/test.pl:6): 6: my $port; DB<5> n main::(/home/k.sewnundun/test.pl:7): 7: my $pid; DB<5> n main::(/home/k.sewnundun/test.pl:8): 8: my $pidfile; DB<5> n main::(/home/k.sewnundun/test.pl:9): 9: my $fileconfiguration; DB<5> main::(/home/k.sewnundun/test.pl:10): 10: my $bakendhost; DB<5> n main::(/home/k.sewnundun/test.pl:11): 11: my $bakendport; DB<5> n main::(/home/k.sewnundun/test.pl:12): 12: my $sharememory; DB<5> n main::(/home/k.sewnundun/test.pl:13): 13: my $user; DB<5> n main::(/home/k.sewnundun/test.pl:15): 15: chomp $line; DB<5> n main::(/home/k.sewnundun/test.pl:16): 16: if ($line =~ m/-Ts+([0-9]+.*)+:(d+)s+/) { DB<5> n Complex regular subexpression recursion limit (32766) exceeded at /home/k.sewnundun/test.pl line 16. at /home/k.sewnundun/test.pl line 16 main::(/home/k.sewnundun/test.pl:17): 17: $ip = $1; DB<5> q
What could be the issue? Thanks
Advertisement
Answer
This is a good sign that your regex is wrong. As has been noted in the comments – you’re nesting quantifiers which can lead to problems, especially when matches overlap.. (E.g. .
also matches [0-9]
). Several people have mentioned this in the comments – particularly things like ([0-9]+.*)+
– if a match is not found, you end up having to backtrack over the various different combinations of letters/numbers to figure out if a match is present.
You could see this at work if you try something like use re 'debug'
and notice how your regex starts running a crazy number of steps to process.
But this is something of a moot point when you stop parsing the output of pgrep
, and instead use something like Proc::ProcessTable
instead:
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use Proc::ProcessTable; my $ps = Proc::ProcessTable -> new; foreach my $proc ( grep { $_ -> {cmndline} =~ m/varnish/ } @{$ps->table} ) { my %opts = $proc -> {cmndline} =~ m/-(w) (S+)/g; print Dumper $proc; print Dumper %opts; my ( $IP, $port ) = split ( ':', $opts{'T'}); }