LoginSignup
6
6

More than 5 years have passed since last update.

nagiosメールが来たらAsteriskで電話をかける

Last updated at Posted at 2014-06-18

構成例

Asterisk側の準備

  1. asteriskで4にダイヤルすると自分の携帯に電話がかかるようにする。
  2. http://myhome.com:8888/cgi-bin/callme.py にアクセスが来たら
    1. pythonからasteriskで4にダイヤル。
    2. アクセス元制限
  3. http://myhome.com:8888/ が落ちないようにmonitにサービス登録

nagios検知した後

  1. nagios検知
  2. nagios@server.com -> svadmin@provider.com へメールが届く。
  3. /home/svadmin/.procmailrc にてルール記述
    1. curl http://myhome.com:8888/cgi-bin/callme.py
    2. メール転送

上記内容で実践

Asterisk側の準備

  1. asteriskで4にダイヤルすると自分の携帯(09011111111)に電話がかかるようにする。
/srv/asterisk11/etc/asterisk/extensions.lua
default = {
   ["4"] = function()
           app.Dial("SIP/09011111111@050plus,15")
   end;
};
  1. http://myhome.com:8888/cgi-bin/callme.py にアクセスが来たら
    1. pythonからasteriskで4にダイヤル。
    2. アクセス元制限
/srv/callme/cgi-bin/callme.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import commands

ip = os.environ['REMOTE_ADDR']
# provider.com のIPを222.111.99.88とする。
ip_allow = "222.111.99.88"

def print_html():
  print "Content-type: text/html"
  print """
    <html>
    <head><meta charset="utf-8"/></head>
    <body>
"""

def call():
  if ip == ip_allow:
    exe = "/srv/asterisk11/sbin/asterisk -x 'console dial 4'"
    print commands.getoutput(exe)
  else:
    print "access denied."

print_html()
call()
  1. http://myhome.com:8888/ が落ちないようにmonitにサービス登録
/srv/callme/nagios2call.sh
#!/bin/sh
set -e
cd /srv/asterisk_nagios
python -m CGIHTTPServer 8888
/srv/callme/nagios2call_stop.sh
#!/bin/sh
set -eu
PID=pgrep -f "python -m CGIHTTPServer 8888"
/bin/kill -9 ${PID}
/etc/monit/monitrc.d/nagios2asterisk
check host callme with address localhost
    start program = "/srv/callme/nagios2call.sh"
      as uid asterisk and gid asterisk
    stop  program = "/srv/callme/nagios2call_stop.sh"
    if failed host localhost port 8888 then restart
    if 5 restarts within 5 cycles then timeout
シンボリックリンクを貼る
$ sudo ln -s /etc/monit/monitrc.d/nagios2asterisk /etc/monit/conf.d/
$ sudo /etc/init.d/monit restart

nagios検知した後

  1. nagios検知
  2. nagios@server.com -> svadmin@provider.com へメールが届く。
  3. /home/svadmin/.procmailrc にてルール記述
    1. curl http://myhome.com:8888/cgi-bin/callme.py
    2. メール転送
~/bin/callme.sh
curl http://myhome.com:8888/cgi-bin/callme.py
~/.forward
"| IFS=' ' && exec /usr/bin/procmail -f- || exit 75 "
~/.procmailrc
:0 c
*^From:.*nagios@.*
| /home/svadmin/bin/nagios2call.sh

:0 c
*^From:.*nagios@.*
! svadmin+nagios@gmail.com
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6