[Home]

Summary:ASTERISK-08065: [patch] perl based safe_asterisk
Reporter:Martino Dell'Ambrogio (tillo)Labels:
Date Opened:2006-11-03 16:17:27.000-0600Date Closed:2007-09-06 14:19:30
Priority:MajorRegression?No
Status:Closed/CompleteComponents:Core/General
Versions:Frequency of
Occurrence
Related
Issues:
Environment:Attachments:
Description:I've put this in our asterisk.conf:

[options]
verbose => 4
alwaysfork => yes

Asterisk doesn't fork, even with verbose={1-3}, nofork=no and console=no.

Someone was talking about a CLI switch "-F" in Asterisk 1.2.7 but it's missing in Asterisk 1.2.12.1. I guess 'alwaysfork' was removed too.

If we force it to fork with start-stop-daemon's option "--background" asterisk works fine but keep running with 100% CPU consuming.

****** ADDITIONAL INFORMATION ******

/etc/asterisk/asterisk.conf:
(directories block here)
[options]
verbose => 4
alwaysfork => yes
timestamp => yes
runuser => asterisk
rungroup => asterisk

Last three lines from the command "/sbin/start-stop-daemon --start --pidfile /var/run/asterisk.pid --exec /sbin/asterisk":

[Nov  3 22:34:03]  [app_waitforsilence.so][Nov  3 22:34:03]  => (Wait For Silence)
[Nov  3 22:34:03]   == Registered application 'WaitForSilence'
[Nov  3 22:34:03] Asterisk Ready.
^C (here asterisk doesn't fork and I've to kill -TERM it with CTRL+C)
[Nov  3 22:34:09] Executing last minute cleanups
[Nov  3 22:34:09]   == Destroying musiconhold processes
Comments:By: Joshua C. Colp (jcolp) 2006-11-03 16:38:08.000-0600

alwaysfork and the -F option only exist in the 1.4 branch and in trunk, they are not part of 1.2 thus why they don't work for you. If you would like to explore the 100% CPU usage issue then you'll need to do some testing - ie: does it spike to 100% if you run it normally from the CLI or only happens when run under that instance?

By: Martino Dell'Ambrogio (tillo) 2006-11-03 16:56:06.000-0600

I guessed that it uses 100% of CPU whenever it doesn't have direct stdout access to the tty.
If forked with the start-stop-daemon --background switch it goes to 100%, but if forked with bash '&' special command stdout is still printed out and Asterisk is working fine.

Do you have a solution to make Asterisk be verbose but to force it to fork, or maybe something to make it work fine without having direct tty stdout access?

By: Joshua C. Colp (jcolp) 2006-11-09 10:37:55.000-0600

I personally don't have a solution for this issue, you could try asking on the users list and see how/if people deal with it. Sadly I don't think I can be of any more help to you.

By: Martino Dell'Ambrogio (tillo) 2006-11-09 11:46:55.000-0600

I solved the problem using a perl script and perl's fork capability.

This perl script does exactly the same as the contrib/safe_asterisk provided script, but better and in perl, that's why I named it the same.

Changes:
- I use a more portable 'sendmail' command instead of mailx's 'mail'. Embedded systems usually don't have mailx.
- I use 'tee' to log to the virtual console AND to a logfile. tee is included in busybox for embedded systems.

Warning:
- Non-standard Proc::Daemon perl module is needed, but a 'fork and exit;' can be used instead.

/sbin/safe_asterisk:
------------------------------------------------------
#!/bin/perl

use strict;
use warnings;

use Proc::Daemon;

Proc::Daemon::Init;

$SIG{INT} = 'IGNORE';
$SIG{HUP} = 'IGNORE';
$SIG{TERM} = 'IGNORE';

my $ttyNumber='9';
my $mailFrom='asterisk@servername';
my $mailTo='admin@companyname';
my $machineName='servername';
my $cliParams='-vvvv -n -T -U asterisk -G asterisk';
my $tty='';
my $retval=0;

if ($ttyNumber ne '')
{
       if (-c '/dev/tty' . $ttyNumber)
       {
            $tty='tty' . $ttyNumber;
       }
       else
       {
            $tty='';
       }
}

sub _sendEmail
{
       my $retval = $_[0];

       if ($retval > 128)
       {
            $retval -= 128;
            system('echo -e "Subject: Asterisk Died\nFrom: \"Asterisk\" <' . $mailFrom . '>\nTo: <' . $mailTo . '>\n\nAsterisk on ' . $machineName . ' exited on signal ' . $retval . '.  Might want to take a peek." |/bin/sendmail -t');
       }
       else
       {
            system('echo -e "Subject: Asterisk Died\nFrom: <' . $mailFrom . '>\nTo: <' . $mailTo . '>\n\nAsterisk on ' . $machineName . ' exited with return ' . $retval . '. Might want to take a peek." |/bin/sendmail -t');
       }
}

while(1)
{
       if ($tty ne '')
       {
            system('stty sane -F /dev/' . $tty);
            system('mknod /tmp/asterisk.log.pipe p');
            system('/bin/tee /var/log/asterisk.log >/dev/' . $tty . ' </tmp/asterisk.log.pipe &');
            $retval = system('/sbin/asterisk ' . $cliParams . ' >/tmp/asterisk.log.pipe 2>&1');
       }
       else
       {
            $retval = system('/sbin/asterisk ' . $cliParams . ' >/var/log/asterisk.log 2>&1');
       }

       if ($retval == 0)
       {
            exit 0;
       }
       else
       {
            _sendEmail($retval);
       }

       sleep 4;
}
-------------------------------------------

I'm sure this will be useful to others too.

By: Joshua C. Colp (jcolp) 2006-11-09 21:48:10.000-0600

If you would like to get this into trunk people may find it useful in comparison to the regular safe_asterisk. Just need to confirm you have a disclaimer on file.

By: Martino Dell'Ambrogio (tillo) 2006-11-12 14:28:43.000-0600

It's all yours.

The sendmail command should be written better than that, and maybe avoided if the email address is not specified, as for the tty...
It's a good basic script where to construct something better. I'll let a better programmer than me to do it.

I forgot something important, it doesn't need bash (which is extremely huge and useless in an embedded system): it's compatible with busybox's ASH, unlike the original one.

By: Brandon Kruse (bkruse) 2007-01-08 03:32:52.000-0600

Tillo: Though I thank you for contributing to the asterisk community.

Rewriting the already adapted bash script would be VERY easy to do, to make it compatible with the already symlinked "bash" -> busybox

As long as you do not use functions, you should be OK.

I can upload safe_asterisk thats busybox compatible if you wish.

By: Martino Dell'Ambrogio (tillo) 2007-01-08 03:45:11.000-0600

Thank you very much bkruse, but since our distro is completely based on Perl, we prefer to use it for safe_asterisk.

You can still upload your script if you feel like someone else is interested, and I'm sure there are. Maybe your translated script, if compatible with busybox's bash, BASH, csh and Sun's sh, should be a replacement for the official one.

By: Brandon Kruse (bkruse) 2007-01-08 03:59:39.000-0600

I agree, the main thing to keep in mind is functions and where you put those little brackets :P

If anyone is interested I will give it some love and upload it


-bkruse

By: Serge Vecher (serge-v) 2007-03-13 10:04:33

ping

By: Brandon Kruse (bkruse) 2007-03-13 12:47:31

Hey serge.

The perl based script would be great to have around, but, at the same time, all systems dont have perl, especially small compact systems(hint hint)

So it would be best to stay with the POSIX/MSH/Busybox compatible script that is already availible(i think?)

-bkruse

By: Michiel van Baak (mvanbaak) 2007-09-06 14:19:08

Closing due to lack of interest.

If you want this included please reopen this bugreport and upload the script with a valid license.