วันเสาร์ที่ 15 พฤษภาคม พ.ศ. 2553

script asterisk for nagios

พอดีช่วงนี้ ว่างๆ เลยนั่งเขียน script สำหรับ monitor asterisk ที่รองรับ snmp ไว้ใช้สำหรับ nagios มาฝากเพื่อนๆ พี่ๆ กันทุกคนครับ
ส่วนเครื่องที่ nagios ที่จะใช้ script นี้ได้นั้นต้องติดตั้ง php-snmp ไว้ด้วยหน่ะครับ ถ้าไม่แน่ใจให้ดูที่ phpinfo ครับ แล้ว search คำว่า "snmp" ดูหน่ะครับว่า ได้ enable ไว้หรือ ยังครับ
หรือถ้าบน linux เองก็ใช้ command line เอาเลยก็ได้คับ
#rpm -q php-snmp

ส่วนวิธีการเรียกใช้งาน script นี้นั้น ก็เพียงแค่ส่ง paremeter ต่างๆเข้าไปครับ เช่น

./monitor_asterisk_snmp.php host community type [warning] [cirtical]

host = คือ ชื่อ domain หรือ ip address ของเครื่อง asterisk ที่จะทำการ monitor
community = คือ key snmp ของเครื่อง asterisk
type = คือ ค่าที่เราจะทำการ monitor ครับซึ่งก็มีหลายค่าด้วยกัน เช่น sip , iax2 , zap , channels in use , etc
[warning] = คือ ค่า warning สำหรับใช้กับ nagios ในการ alert warning ครับ ค่านี้ ต้องเป็นตัวเลขจำนวนเต็มน่ะครับ และเป็นจำนวนเต็มบวก น่ะครับ
[critical] = คือ ค่า critical สำหรับใช้กับ nagios ในการ alert critical ครับ ค่านี้ ต้องมีค่าเป็นเลขจำนวนเต็ม และมีค่ามากกว่า ค่า warning ด้วยน่ะครับ

script นี้ นั้นรองรับกับ Asterisk version 1.4 กับ 1.6 ครับ
ซึ่งการจะใช้ ค่า type อะไรนั้น version 1.4 กับ 1.6 ก็มีค่า type แตกต่างกันไปครับ
ถ้าเราไม่รู้ว่ามี type อะไรบ้างที่เราสามารถ จะ monitor ได้นั้นให้เรา ใส่ตรง type เป็น help แทนครับ
script จะแจ้งให้ทราบ ถึง วิธีการใช้งาน script พร้อมทั้งบอกว่า ว่าเครื่องปลายทางที่จะทำการ monitor ว่าเป็น asterisk version อะไร
รวมไปถึง type อะไรบ้างที่เราสามารถ monitor ได้เช่น

For Asterisk 1.4

Asterisk version : "1.4.14"
Use : ./monitor_asterisk14.php host community type [warning] [critical]
host or ip address : your ip address
community : community for snmp
type : phone ( Standard Linux Telephony API Driver )
mgcp ( Media Gateway Control Protocol (MGCP) )
agent ( Call Agent Proxy Channel)
skinny ( Skinny Client Control Protocol (Skinny) )
sip ( Session Initiation Protocol (SIP) )
iax2 ( Inter Asterisk eXchange Driver (Ver 2) )
zap ( Zapata Telephony Driver w/PRI )
console ( OSS Console Channel Driver )
local ( Local Proxy Channel Driver )
channels_inuse ( Channels In Use (all types) )


For Asterisk 1.6

Asterisk version : "1.6.2.6"
Use : ./monitor_asterisk14.php host community type [warning] [critical]
host or ip address : your ip address
community : community for snmp
type : dahdi ( DAHDI Telephony Driver w/PRI )
mgcp ( Media Gateway Control Protocol (MGCP) )
bridge ( Bridge Interaction Channel )
sip ( Session Initiation Protocol (SIP) )
ustm ( UNISTIM Channel Driver )
console ( OSS Console Channel Driver )
local ( Local Proxy Channel Driver )
agent ( Call Agent Proxy Channel )
phone ( Standard Linux Telephony API Driver )
skinny ( Skinny Client Control Protocol (Skinny) )
iax2 ( iax2 Inter Asterisk eXchange Driver (Ver 2) )
h323 ( Objective Systems H323 Channel Driver )
channel_inuse ( Channels In Use (all types) )
active_calls ( Active Calls )
calls_processed ( Calls Processed )


เช่น ถ้าเราต้องการ monitor type calls_processed (ในที่นี้เครื่องปลายทางผมใช้ asterisk1.6 น่ะครับ)

# ./monitor_asterisk_snmp.php xxx.xxx.xxx public calls_processed
21[root@localhost ~]#


xxx.xxx.xxx = คือ ip address ของเครื่องปลายทางครับ

ส่วนค่า 21 คือค่า ของ calls_processed ครับ


ส่วนวิธีการ config nagios นั้นผมจะไม่ขอกล่าวถึงน่ะครับ เพราะ เราแค่เรียกใช้งาน script นี้ แล้ว ส่ง parameter ที่เราจะทำการ monitor เข้าไปครับ

File : monitor_asterisk_snmp.php

#!/usr/bin/php

4){
$host = $_SERVER["argv"][1];
$community = $_SERVER["argv"][2];
$type = $_SERVER["argv"][3];
$warning = $_SERVER["argv"][4];
$critical = $_SERVER["argv"][5];
}else{
echo "Use : ./monitor_asterisk_snmp.php host community type [warning] [critical]\n";
echo "type = sip , iax2 , dahdi , active_calls , channels_inuse , ... etc or help\n";
exit();
}


if(!empty($warning)){
if($critical ==""){
echo "Please, input critical\n";
exit();
}
if($warning >= $critical){
echo "Please, input critcal more than warning \n";
exit();
}
if(!is_numeric($warning)){
echo "Plase, input warning is integer only \n";
exit();
}
if(!is_numeric($critical)){
echo "Pleaes, input critical is integer only \n";
exit();
}
}
// check asterisk version
$ver = snmpget("$host","$community",".1.3.6.1.4.1.22736.1.1.1.0",200 );
if($ver !=""){
$ver = explode(" ",$ver);
$ast_ver = $ver[1];
$ver = substr($ast_ver,1,3);
}else{
echo "Wrong : Access denine\n";
exit();
}

if($ver=="1.6"){
$ver = "1_6";
}elseif($ver=="1.4"){
$ver = "1_4";
}

$rs_snmp="";
if($ver =="1_4"){
switch ($type){
case "phone" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.1";
break;
case "mgcp" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.2";
break;
case "agent" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.3";
break;
case "skinny" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.4";
break;
case "sip" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.5";
break;
case "iax2" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.6";
break;
case "zap" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.7";
break;
case "console" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.8";
break;
case "local" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.9";
break;
case "channels_inuse" :
$oid = ".1.3.6.1.4.1.22736.1.5.1.0";
break;
default :
$msg_err = "Asterisk version : $ast_ver\n";
$msg_err .= "Use : ./monitor_asterisk14.php host community type [warning] [critical]\n";
$msg_err .= "host or ip address : your ip address\n";
$msg_err .= "community : community for snmp \n";
$msg_err .= "type : phone ( Standard Linux Telephony API Driver )\n";
$msg_err .= "\t mgcp ( Media Gateway Control Protocol (MGCP) ) \n";
$msg_err .= "\t agent ( Call Agent Proxy Channel) \n";
$msg_err .= "\t skinny ( Skinny Client Control Protocol (Skinny) ) \n";
$msg_err .= "\t sip ( Session Initiation Protocol (SIP) )\n";
$msg_err .= "\t iax2 ( Inter Asterisk eXchange Driver (Ver 2) ) \n";
$msg_err .= "\t zap ( Zapata Telephony Driver w/PRI ) \n";
$msg_err .= "\t console ( OSS Console Channel Driver ) \n";
$msg_err .= "\t local ( Local Proxy Channel Driver ) \n";
$msg_err .= "\t channels_inuse ( Channels In Use (all types) )\n";
echo $msg_err;
exit( STATUS_UNKNOWN );
}// end switch

}elseif($ver == "1_6"){
switch ($type){
case "channels_inuse" :
$oid = ".1.3.6.1.4.1.22736.1.5.1.0";
break;
case "active_calls" :
$oid = ".1.3.6.1.4.1.22736.1.2.5.0";
break;
case "calls_processed";
$oid = ".1.3.6.1.4.1.22736.1.2.6.0";
break;
case "dahdi" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.1";
break;
case "mgcp" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.2";
break;
case "bridge" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.3";
break;
case "sip" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.4";
break;
case "ustm" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.5";
break;
case "console" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.6";
break;
case "local" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.7";
break;
case "agent" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.8";
break;
case "phone" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.9";
break;
case "skinny" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.10";
break;
case "iax2" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.11";
break;
case "h323" :
$oid = ".1.3.6.1.4.1.22736.1.5.4.1.7.12";
break;
default :
$msg_err = "Asterisk version : $ast_ver\n";
$msg_err .= "Use : ./monitor_asterisk14.php host community type [warning] [critical]\n";
$msg_err .= "host or ip address : your ip address\n";
$msg_err .= "community : community for snmp \n";
$msg_err .= "type : dahdi ( DAHDI Telephony Driver w/PRI )\n";
$msg_err .= "\t mgcp ( Media Gateway Control Protocol (MGCP) ) \n";
$msg_err .= "\t bridge ( Bridge Interaction Channel ) \n";
$msg_err .= "\t sip ( Session Initiation Protocol (SIP) ) \n";
$msg_err .= "\t ustm ( UNISTIM Channel Driver )\n";
$msg_err .= "\t console ( OSS Console Channel Driver ) \n";
$msg_err .= "\t local ( Local Proxy Channel Driver ) \n";
$msg_err .= "\t agent ( Call Agent Proxy Channel ) \n";
$msg_err .= "\t phone ( Standard Linux Telephony API Driver ) \n";
$msg_err .= "\t skinny ( Skinny Client Control Protocol (Skinny) )\n";
$msg_err .= "\t iax2 ( iax2 Inter Asterisk eXchange Driver (Ver 2) )\n";
$msg_err .= "\t h323 ( Objective Systems H323 Channel Driver )\n";
$msg_err .= "\t channel_inuse ( Channels In Use (all types) )\n";
$msg_err .= "\t active_calls ( Active Calls )\n";
$msg_err .= "\t calls_processed ( Calls Processed )\n";
echo $msg_err;
exit( STATUS_UNKNOWN );
}// end switch

} // end if check version

$rs_snmp = snmpget("$host","$community","$oid");
$rs_snmp = explode(" ",$rs_snmp);

if($warning != "" && $critical != ""){
if($rs_snmp[1] >= $critical){
echo $rs_snmp[1];
exit ( STATUS_CRITICAL );
}elseif ($rs_snmp[1] >= $warning){
echo $rs_snmp[1];
exit ( STATUS_WARNING );
}else{
echo $rs_snmp[1];
exit ( STATUS_OK );
} // end if

}else{
echo $rs_snmp[1];
exit( STATUS_OK );
}// end if($warning <> "" && $critical <> "")

?>

ป้ายกำกับ:

0 ความคิดเห็น:

แสดงความคิดเห็น

สมัครสมาชิก ส่งความคิดเห็น [Atom]

<< หน้าแรก