{"id":1710,"date":"2024-03-11T03:54:31","date_gmt":"2024-03-11T06:54:31","guid":{"rendered":"https:\/\/blog.abratel.com.br\/?p=1710"},"modified":"2024-12-18T18:39:52","modified_gmt":"2024-12-18T21:39:52","slug":"asterisk-server-monitoring-trunk-sending-email-when-goes-offline","status":"publish","type":"post","link":"https:\/\/blog.abratel.com.br\/?p=1710","title":{"rendered":"Asterisk Server Monitoring Trunk (Sending email when goes offline)"},"content":{"rendered":"<p>Author: Ulisses Feres<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n\n# Function to send an email using sendmail\nsend_email() {\nlocal subject=&quot;$1&quot;\nlocal body=&quot;$2&quot;\nlocal recipient=&quot;$3&quot;\nlocal sender=&quot;$4&quot;\n\n(\necho &quot;Subject: $subject&quot;\necho &quot;From: $sender&quot;\necho -e &quot;\\n$body&quot;\n) | \/usr\/sbin\/sendmail &quot;$recipient&quot;\n}\n\n# Asterisk command to get SIP peer status\npeer_status=$(asterisk -x 'sip show peer Abratel-ISP1' | grep -i status | cut -d' ' -f11)\n\n# Define previous status and email recipients\nprevious_status=$(cat \/tmp\/peer_status_previous_status)\nemail_recipient=(&quot;umemail@gmail.com&quot; &quot;outroemail@gmail.com&quot;)\nemail_sender=&quot;PBX &amp;amp;amp;amp;lt;ipbx@email.com.br&amp;amp;amp;amp;gt;&quot;\nalert_sent_flag=&quot;\/tmp\/peer_status_alert_sent&quot;\n# debug_log=&quot;\/tmp\/peer_status_debug.log&quot;&amp;amp;amp;amp;lt;\/ipbx@email.com.br&amp;amp;amp;amp;gt;\n\n# Print current and previous status for debugging\n# echo &quot;Current Status: $peer_status&quot;\n# echo &quot;Previous Status: $previous_status&quot;\n\n# Log current status to debug file\n# echo &quot;Current Status: $peer_status&quot; &amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;gt; &quot;$debug_log&quot;\n\n# Check if the status is not &quot;OK&quot;\nif [ &quot;$peer_status&quot; != &quot;OK&quot; ]; then\nemail_subject=&quot;Tronco Status Alert&quot;\nemail_body=&quot;O tronco com todas as linhas esta atualmente offline.&quot;\nadditional_info=&quot;Status: $peer_status&quot;\n\n# Check if the status has changed from the previous check or alert not sent\nif [ &quot;$peer_status&quot; != &quot;$previous_status&quot; ] &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; [ ! -e &quot;$alert_sent_flag&quot; ]; then\n# Send email only once when the status changes\nsend_email &quot;$email_subject&quot; &quot;$email_body\\n$additional_info&quot; &quot;$email_recipient&quot; &quot;$email_sender&quot;\n\n# Create the flag file to indicate that an alert has been sent\ntouch &quot;$alert_sent_flag&quot;\n\n# echo &quot;Email sent: $email_subject&quot;\n# else\n# echo &quot;Email not sent: Status unchanged or alert already sent.&quot;\nfi\nelse\n# Check if the status has changed from the previous check\nif [ &quot;$peer_status&quot; != &quot;$previous_status&quot; ]; then\n# Send notification when the status changes back to &quot;OK&quot;\nsend_email &quot;$email_subject&quot; &quot;The SIP peer Abratel-ISP1 is now back online.&quot; &quot;$email_recipient&quot; &quot;$email_sender&quot;\n\n# Remove the flag file to reset the alert status\nrm -f &quot;$alert_sent_flag&quot;\n\n# echo &quot;Notification sent: SIP peer is back online.&quot;\n# else\n# echo &quot;No status change detected.&quot;\nfi\nfi\n\n# Update the previous status\necho &quot;$peer_status&quot; &amp;amp;amp;amp;amp;gt; \/tmp\/peer_status_previous_status\n<\/pre>\n<p>Execute Permission:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nchmod +x \/etc\/asterisk\/script_checa_trunks\n<\/pre>\n<p>Creating a crontab for it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n*\/10 * * * * root \/etc\/asterisk\/script_checa_trunks\n<\/pre>\n<p>+++++++++++++++++++++++++++++++++++++++++++++++++<\/p>\n<p>Simple one to check all register status and alert when change to something different than Register.<\/p>\n<p>\/etc\/asterisk\/script_checa_trunks<\/p>\n<p>#!\/bin\/bash<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nemail=&quot;email1@gmail.com, email2@gmail.com\n\/usr\/sbin\/asterisk -rx 'sip show registry' | grep &quot;No Authentication&quot; &amp;amp;amp;amp;gt; \/tmp\/allExtensions.txt\n\/usr\/sbin\/asterisk -rx 'sip show registry' | grep &quot;Request Sent&quot; &amp;amp;amp;amp;gt;&amp;amp;amp;amp;gt; \/tmp\/allExtensions.txt\nofflineExtensions=$(cat \/tmp\/allExtensions.txt)\n\ncount=$(cat \/tmp\/allExtensions.txt | wc -l)\nif [ $count -gt 0 ]; then\n(\necho -e &quot;Subject: Atencao para Troncos Desconectados\\nFrom: PBX Abratel &amp;amp;amp;lt;ipbx@abratel.com.br&amp;amp;amp;gt;&quot;\nprintf 'Troncos desconectados: \\n''\\n'&quot;${offlineExtensions}&quot;\n) | \/usr\/sbin\/sendmail ${email}\nfi\n<\/pre>\n<p>FOR PJSIP Script:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nFirst create a file:\nroot@:\/etc\/asterisk#  touch \/tmp\/peer_status_previous_status\n\nScript:\nroot@:\/etc\/asterisk#  cat \/etc\/asterisk\/check_pjsip.sh\n#!\/bin\/bash\n\n# Function to send an email using sendmail\nsend_email() {\nlocal subject=&quot;$1&quot;\nlocal body=&quot;$2&quot;\nlocal recipient=&quot;$3&quot;\nlocal sender=&quot;$4&quot;\n\n(\necho &quot;Subject: $subject&quot;\necho &quot;From: $sender&quot;\necho -e &quot;\\n$body&quot;\n) | \/usr\/sbin\/sendmail &quot;$recipient&quot;\n}\n\n# Asterisk command to get SIP peer status\npeer_status=$(asterisk -x 'pjsip show registrations' | tail -n +5 | head -n -3 | awk '{print $3}')\n\n# Define previous status and email recipients\nprevious_status=$(cat \/tmp\/peer_status_previous_status)\nemail_recipient=(&quot;uferes@scilexholding.com&quot;)\nemail_sender=&quot;PBX &amp;amp;lt;pbx@scilexholding.com.br&amp;amp;gt;&quot;\nalert_sent_flag=&quot;\/tmp\/peer_status_alert_sent&quot;\n# debug_log=&quot;\/tmp\/peer_status_debug.log&quot;&amp;amp;lt;\/pbx@scilexholding.com.br&amp;amp;gt;\n\n# Print current and previous status for debugging\n# echo &quot;Current Status: $peer_status&quot;\n# echo &quot;Previous Status: $previous_status&quot;\n\n# Log current status to debug file\n# echo &quot;Current Status: $peer_status&quot; &amp;amp;amp;gt;&amp;amp;amp;gt; &quot;$debug_log&quot;\n\n# Check if the status is not &quot;OK&quot;\nif [ &quot;$peer_status&quot; != &quot;Registered&quot; ]; then\nemail_subject=&quot;Trunk Status Alert Offline&quot;\nemail_body=&quot;The SIP.UP trunk is currently offline.&quot;\nadditional_info=&quot;Status: $peer_status&quot;\n\n# Check if the status has changed from the previous check or alert not sent\nif [ &quot;$peer_status&quot; != &quot;$previous_status&quot; ] &amp;amp;amp;amp;&amp;amp;amp;amp; [ ! -e &quot;$alert_sent_flag&quot; ]; then\n# Send email only once when the status changes\nsend_email &quot;$email_subject&quot; &quot;$email_body\\n$additional_info&quot; &quot;$email_recipient&quot; &quot;$email_sender&quot;\n#trying to reload the pjsip to get this back\nasterisk -x 'module reload res_pjsip.so'\n# Create the flag file to indicate that an alert has been sent\ntouch &quot;$alert_sent_flag&quot;\n\n# echo &quot;Email sent: $email_subject&quot;\n# else\n# echo &quot;Email not sent: Status unchanged or alert already sent.&quot;\nfi\nelse\n# Check if the status has changed from the previous check\nemail_subject=&quot;Trunk Status Alert - OnLine&quot;\nemail_body=&quot;The SIP.UP trunk is currently online.&quot;\nadditional_info=&quot;Status: $peer_status&quot;\n\nif [ &quot;$peer_status&quot; != &quot;$previous_status&quot; ]; then\n# Send notification when the status changes back to &quot;OK&quot;\nsend_email &quot;$email_subject&quot; &quot;$email_body\\n$additional_info&quot; &quot;$email_recipient&quot; &quot;$email_sender&quot;\n\n# Remove the flag file to reset the alert status\nrm -f &quot;$alert_sent_flag&quot;\n\n# echo &quot;Notification sent: SIP peer is back online.&quot;\n# else\n# echo &quot;No status change detected.&quot;\nfi\nfi\n\n# Update the previous status\necho &quot;$peer_status&quot; &amp;amp;amp;gt; \/tmp\/peer_status_previous_status\n<\/pre>\n<p>Remember to create a cron task, on this case, every 10 min:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nroot@:\/etc\/asterisk# vim \/etc\/crontab\n*\/10 * * * * root \/etc\/asterisk\/check_pjsip.sh\n<\/pre>\n<p>Check the image since most of the time the paste script breaks.<\/p>\n<p>==================================================================<\/p>\n<p>Another script:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n\nsend_email() {\nlocal subject=&quot;$1&quot;\nlocal body=&quot;$2&quot;\nlocal recipient=&quot;$3&quot;\nlocal sender=&quot;$4&quot;\n\n(\necho &quot;Subject: $subject&quot;\necho &quot;From: $sender&quot;\necho -e &quot;\\n$body&quot;\n) | \/usr\/sbin\/sendmail &quot;$recipient&quot;\n}\n\npeer_status=$(asterisk -x 'sip show peer Abratel-ISP1' | grep -i status | cut -d' ' -f11)\n\nprevious_status=$(cat \/tmp\/peer_status_previous_status_giga)\nemail_recipient=(&quot;sas@asas.com&quot;,&quot;sasa@gmail.com&quot;,&quot;sasas@gmail.com&quot;)\nemail_sender=&quot;Abratel PBX &lt;ipbx@server.com.br&gt;&quot;\nalert_sent_flag=&quot;\/tmp\/peer_status_alert_sent_giga&quot;&lt;\/ipbx@server.com.br&gt;\n\nif [ &quot;$peer_status&quot; != &quot;OK&quot; ]; then\nemail_subject=&quot;Tronco Status Alert! Interconexao Giga++ Muriae!&quot;\nemail_body=&quot;balbalblabla&quot;\nadditional_info=&quot;Status do tronco: $peer_status&quot;\n\nif [ &quot;$peer_status&quot; != &quot;$previous_status&quot; ] &amp;amp;&amp;amp; [ ! -e &quot;$alert_sent_flag&quot; ]; then\nsend_email &quot;$email_subject&quot; &quot;$email_body\\n$additional_info&quot; &quot;$email_recipient&quot; &quot;$email_sender&quot;\nasterisk -rx &quot;sip reload&quot;\ntouch &quot;$alert_sent_flag&quot;\nfi\nelse\nif [ &quot;$peer_status&quot; != &quot;$previous_status&quot; ]; then\nsend_email &quot;Tronco restabelecido (Interconexao Muriae Giga++&quot; &quot;O tronco InterconexaoMuriae esta novamente online.&quot; &quot;$email_recipient&quot; &quot;$email_sender&quot;\nrm -f &quot;$alert_sent_flag&quot;\nfi\nfi\n\necho &quot;$peer_status&quot; &amp;gt; \/tmp\/peer_status_previous_status_giga\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Author: Ulisses Feres Execute Permission: Creating a crontab for it: +++++++++++++++++++++++++++++++++++++++++++++++++ Simple one to check all register status and alert when change to something different than Register. \/etc\/asterisk\/script_checa_trunks #!\/bin\/bash FOR PJSIP Script: Remember to create a cron task, on this case, every 10 min: Check&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1718,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1710"}],"collection":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1710"}],"version-history":[{"count":17,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1710\/revisions"}],"predecessor-version":[{"id":1761,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1710\/revisions\/1761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/media\/1718"}],"wp:attachment":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}