0

I have the following program snippet

my $nfdump_command = "nfdump -M /data/nfsen/profiles-data/live/upstream1  -T  -R ${syear}/${smonth}/${sday}/nfcapd.${syear}${smonth}${sday}0000:${eyear}/${emonth}/${eday}/nfcapd.${eyear}${emonth}${eday}2355 -n 100 -s ip/bytes -N -o csv -q | awk 'BEGIN { FS = \",\" } ; { if (NR > 1) print \$5, \$10 }'";
syslog("info", $nfdump_command);

my %args;
Nfcomm::socket_send_ok ($socket, \%args);
my @nfdump_output = `$nfdump_command`;
my %domain_name_to_bytes;
my %domain_name_to_ip_addresses;

syslog("info", Dumper(\@nfdump_output));

foreach my $a_line (@nfdump_output) {
    syslog("info", "LINE: " . $a_line);
}

Bug: @nfdump_output is empty. enter image description here

The $nfdump_command is correct and it printing output when ran individually

enter image description here

SurenNihalani
  • 1,398
  • 2
  • 15
  • 28
  • 2
    If you do the command in your shell, and add `1>/tmp/file`, does `/tmp/file` contain the desired output? If not, likely your command is printing to STDERR rather than STDOUT. The easiest way to capture STDERR or STDOUT+STDERR is with the module `Cature::Tiny`. – David-SkyMesh Jan 04 '14 at 07:24
  • 2
    Without being able to run your command it is hard to say. Few questions - can you print the variable $nfdump_command. What is the output from nfdump if you remove awk. – justintime Jan 04 '14 at 07:26
  • @justintime, I have printed the $nfdump_command in the screenshot. Output without awk: http://pastie.org/private/zsdki76zq25mp2azwzhyg – SurenNihalani Jan 04 '14 at 07:35
  • @David-SkyMesh: No, `awk`'s `print` writes to standard output by default. (And besides, if the output were going to STDERR, then we'd see it in the console, exactly because it wouldn't be captured by the backticks.) – ruakh Jan 04 '14 at 07:36
  • @David-SkyMesh, Yes, The file does contain the output. http://pastie.org/private/s0jbcrvndrryqwyvuz8q – SurenNihalani Jan 04 '14 at 07:37
  • I'm wondering if the shell that Perl is launching for the backticks differs in some important way from the shell that you're starting out in. If you run `echo $SHELL` and `perl -e 'print qx/echo \$SHELL/'`, do they both print the same thing? – ruakh Jan 04 '14 at 07:45
  • Both say nfsen-destination-plugin git:(master) ✗ ➜ perl -e 'print qx/echo \$SHELL/' /bin/zsh nfsen-destination-plugin git:(master) ✗ ➜ echo $SHELL /bin/zsh – SurenNihalani Jan 04 '14 at 08:12
  • Sorry, correction: according to [this Stack Overflow answer](http://stackoverflow.com/a/3327022/978917), `perl -e 'print qx/echo \$SHELL/'` will print the name of your login shell, regardless of what shell is actually used for backticks. Can you try `perl -e 'print qx/echo \$0/'` instead? – ruakh Jan 04 '14 at 08:18
  • @ruakh, It's sh nfsen-destination-plugin git:(master) ✗ ➜ perl -e 'print qx/echo \$0/' sh – SurenNihalani Jan 04 '14 at 08:30
  • @ruakh, Ran sh from zsh and then copy pasted the same command: http://pastie.org/private/ghgnwzy52gb7k9cvjxo6g I see valid output. – SurenNihalani Jan 04 '14 at 08:33
  • Well then, I'm out of ideas, sorry. :-/ – ruakh Jan 04 '14 at 08:35
  • Can you look at [$?](http://perldoc.perl.org/perlvar.html#Error-Variables) to check the exit value of the backtick process? – dms Jan 04 '14 at 08:39
  • I added { my @nfdump_output = `$nfdump_command`; syslog("info", "EXIT: $_"); } And output was empty: { Jan 4 14:17:14 suren-VirtualBox nfsen[5098]: EXIT: } – SurenNihalani Jan 04 '14 at 08:48
  • $? has the exit value of the backtick process. If it is not 0 then you can possibly decode it for some hint as to what went wrong. I don't think $_ does you much good in this situation. – dms Jan 04 '14 at 08:58
  • @dms, My bad, I got -1 when I printed $? – SurenNihalani Jan 04 '14 at 09:30
  • @ruakh Re: awk printing to stdout -- that assumes execution got that far. – David-SkyMesh Jan 05 '14 at 07:31

1 Answers1

0

This program was working for sometime and then it broke. Couldn't figure out why. After moving my development setup to another virtual machine, I found out that using absolute path to nfdump fixes it

SurenNihalani
  • 1,398
  • 2
  • 15
  • 28