Freepbx script to monitor freepbx status

hello
i need help. i am running freepbx and i would like to setup a bash script to check if Asterisk is up and the script should send an email with the status to a predifined email. has anyone created such a script before? asterisk is started via fwconsole , so if i do services asterisk status it shows as not running.

go back to terminal or login to your pbx via ssh to be in the centOS environment:
You should find at least (which finally doesn’t give you the answer you expect):
[root@freepbx ~]# service asterisk status
● asterisk.service - LSB: Asterisk PBX
Loaded: loaded (/etc/rc.d/init.d/asterisk; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@freepbx ~]#

In your current environment what is asterisk -rvvv saying?

In general you may run fwconsole reload anytime and catch the result. If it doesn’t reload, it’s not running.
Is there any specific reason why you want to show the status? Once fwconsole start gives you no error, it is running.

generally untested but something like this on a cron…

[root@freepbx ~]# asterisk -h
Asterisk 20.4.0, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Usage: asterisk [OPTIONS]
Valid Options:
-V Display version number and exit
-C Use an alternate configuration file
-G Run as a group other than the caller
-U Run as a user other than the caller
-c Provide console CLI
-d Increase debugging (multiple d’s = more debugging)
-f Do not fork
-F Always fork
-g Dump core in case of a crash
-h This help screen
-i Initialize crypto keys at startup
-L Limit the maximum load average before rejecting new calls
-M Limit the maximum number of calls to the specified value
-m Mute debugging and console output on the console
-n Disable console colorization. Can be used only at startup.
-p Run as pseudo-realtime thread
-q Quiet mode (suppress output)
-r Connect to Asterisk on this machine
-R Same as -r, except attempt to reconnect if disconnected
-s Connect to Asterisk via socket (only valid with -r)
-t Record soundfiles in /var/tmp and move them where they
belong after they are done
-T Display the time in [Mmm dd hh:mm:ss] format for each line
of output to the CLI. Cannot be used with remote console mode.

-v Increase verbosity (multiple v’s = more verbose)
-x Execute command (implies -r)
-X Enable use of #exec in asterisk.conf
-W Adjust terminal colors to compensate for a light background

[root@freepbx ~]# asterisk -vvv
Asterisk already running on /var/run/asterisk/asterisk.ctl. Use ‘asterisk -r’ to connect.
[root@freepbx ~]#

so yea if i run “service asterisk status” i get the output showing service is not running. i can connect to asterisk using asterisk -vvvvr. the reason why i want a way to know if asterisk is up or not is that i want to create a script maybe a bash script or some kind of script that can be executed periodically i.e 5 times a day, and this script will send an email to say asterisk is up or asterisk is down.

now since i cant user the script to execute anythign along the lines " services asterisk status" i have to look for another way, it could be through fwconsole or it could be through some other way.

i saw the jist of the script and when i try to execute the command it doest work properly for me

[root@noname~]# /usr/sbin/asterisk -f -U asterisk -G asterisk -vvvg -c
Unable to access the running directory (Permission denied). Changing to ‘/’ for compatibility.
Asterisk already running on /var/run/asterisk/asterisk.ctl. Use ‘asterisk -r’ to connect.

so i tried runnign as as root

[root@noname ~]# /usr/sbin/asterisk -f -U root -G root -vvvg -c
Asterisk already running on /var/run/asterisk/asterisk.ctl. Use ‘asterisk -r’ to connect.

This might not be helpful to you as it does require additional infrastructure setup but we monitor this using Zabbix.

1 Like

Just a guess: For me it’s much more important to be informed once one of my trunks is down. (Indirect: This also is saying that asterisk is still running. Otherwise trunks would not be online).
The script is mentioned in one of my freepbx 2.4 books, and I updated it for current 15 or 16 version.
It’s plain based on asterisk, and in fact has nothing to do with freepbx. The name of the script should be place down there on the cover-config page of each trunk. It must be set executable (chmod +x)
The script is running only, once this trunk is down (for what reason ever) and is doing a reload
You may copy/change the filename and email body as you like if you have more than one trunk.
And in addition you my read out the trunks status before and after the reload
For testing you can comment out the reload and let the sh-script run by hand

#!/bin/bash
#path: /var/lib/asterisk/agi-bin/trunks-failure.sh
[email protected]
DATE=$(date)
HOST=$(hostname)
#/usr/sbin/asterisk -rx “reload”
fwconsole reload
SIPTRUNKS=$(/usr/sbin/asterisk -rx ‘pjsip show registrations’)
IAXTRUNKS=$(/usr/sbin/asterisk -rx ‘iax2 show peers’)
MESSAGE=“A trunk failure forced a reload on $HOST”
MESSAGE=“$MESSAGE\nDATE: $DATE”
MESSAGE=“$MESSAGE\nCurrent Trunk Status:”
MESSAGE=“$MESSAGE\n\nSIP Trunks:\n$SIPTRUNKS”
MESSAGE=“$MESSAGE\n\nIAX2 Trunks:\n$IAXTRUNKS”
echo -e “$MESSAGE” | mail -s “Trunk Failure” “$EMAIL”

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.