Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:47:57+00:00 2026-06-14T19:47:57+00:00

I’ve developed a small Nagios monitoring scripts which basically runs a tcpdump on a

  • 0

I’ve developed a small Nagios monitoring scripts which basically runs a tcpdump on a given interface and port, and looks for a particular string in the first 10 captured packets. I’m monitoring a system which may hang and flood my server with a particular message.

I’m not a professional Perl programmer, but I believe I’ve treated all expections I could.

Running this script locally ends just fine, and returns the console to me. However, when I try to run it via my Nagios server, via ssh (ssh user@host -i private_key ‘/path/script.pl’), the script is executed sucessfully, I get the exit message, however, ssh does not exit. I have to either Ctrl+C or hit a few returns to get bash back to me. Running it with check_by_ssh yelds me a plugin timeout error, for obvious reasons.

I’m pretty sure it has something to do with the fork() I’m using, but I don’t know what is wrong with it.

#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;

my $RC_OK = 0;
my $RC_WARNING = 1;
my $RC_CRITICAL = 2;
my $RC_UNKNOWN = 3;

my $GREP_RC = undef;

my $PORT = undef;
my $INT = undef;
my $STRING = undef;

my $PID = undef;

# Handler principal de alarme de timeout
$SIG{ALRM} = sub {
    print "UNKNOWN: Main script timed out!\n";
    exit $RC_UNKNOWN;
};

# Inicio contagem global
alarm(8);

# Coleta parametros
GetOptions ("port=s" => \$PORT,
            "interface=s" => \$INT,
            "string=s" => \$STRING);

# Sanity check de parametros
if((not defined $PORT) || (not defined $STRING)) {
    print "Usage: ./check_stratus.pl -p=PORT -i=INTERFACE -s=STRING\n";
    exit $RC_UNKNOWN;
}

# Capturando pelo tcpdump
defined($PID = fork()) or die "Problema ao criar o fork: $!\n";
if ($PID == 0) {

    # Handler secundario de alarme de timeout
    $SIG{ALRM} = sub {
        exit 1;
    };

    # Captura no maximo por 5 segundos, ou 10 pacotes
    alarm(5);

    `sudo /usr/sbin/tcpdump -nX -s 2048 -c 10 -i $INT port $PORT > /tmp/capture.txt 2>&1`;

    # Checando se o tcpdump rodou com sucesso
    if ($? != 0) {
        print "Erro ao executar \"/usr/sbin/tcpdump -nX -s 2048 -c 1 -i $INT port $PORT > /tmp/capture.txt\", verifique o arquivo de saida para mais detalhes.\n";
        exit $RC_UNKNOWN;
    }
    exit $RC_OK;
}

# Espera o filho encerar...
waitpid($PID, 0);

# Verificando se o arquivo capturado esta ok
`/bin/ls /tmp/capture.txt`;

if ($? != 0) {
    print "Erro ao encontrar o arquivo /tmp/capture.txt\n";
    exit $RC_UNKNOWN;
}

# Executando grep da string em cima da captura
`/bin/grep $STRING /tmp/capture.txt`;

# Verificando resultado do grep
if ($? == 0) {
    print "Foi encontrada a string \"$STRING\" na captura do tcpdump escutando na interface $INT e na porta $PORT!\n";
    exit $RC_CRITICAL;
}

if ($? == 256) {
    print "Nao foi encontrada a string \"$STRING\" na captura do tcpdump escutando na interface $INT e na porta $PORT.\n";
    exit $RC_OK;
} else {
    print "Erro desconhecido! Codigo do grep foi $?\n";
    exit $RC_UNKNOWN;
}

Any help is deeply appreciated.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:47:58+00:00Added an answer on June 14, 2026 at 7:47 pm

    look here:

    #!/usr/bin/perl
    use strict;
    my $PID;
    defined($PID = fork()) or die "no fork works";
    if ($PID == 0) {
    
        # Handler secundario de alarme de timeout
        $SIG{ALRM} = sub {
            exit 1;
        };
    
        # Captura no maximo por 5 segundos, ou 10 pacotes
        alarm(1);
        `sleep 100`;
    }
    waitpid($PID, 0);
    

    /tmp$ ps xawww |grep sleep
     1705 pts/2    S+     0:00 grep sleep
    host:/tmp$ time /tmp/test.pl
    
    real    0m1.008s
    user    0m0.000s
    sys     0m0.004s
    host:/tmp$ ps xawww |grep sleep
     1708 pts/2    S      0:00 sleep 100
     1710 pts/2    S+     0:00 grep sleep
    

    The problem appears because your system fork a new process and that process not get signal from parent process.

    Solution is just use exec() instead of `` or system() as exec() does not fork new process:

        alarm(1);
        exec("sleep 100");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.