最近公司上线OTRS,感觉还不错,顾问居然还给了数据库的结构图,闲下来就写了这个php脚本给Opsview用来监控OTRS支持队列的大小。别问我为什么用php…因为我实在是不会pel….
测试环境:
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION=”Ubuntu 12.04.4 LTS”
废话不说,先帖脚本:
<?php
# Tomas Tang on 18/02/2014
# Check the OTRS support queue size
# Disable error report
error_reporting(0);
# get arguments
$procname = $argv[0];
$queuename = $argv[1];
$warnlevel = $argv[2];
$critilevel = $argv[3];
# keep it for future develop
#$QUEUE = array(
# “allnew”,
# “escalated”,
# “second_level_support”,
# “first_level_support”,
#);
# set query SQL and help
if(“$queuename”==’allnew’){
# all the tickets with state new but not in delete queue
$sql = “select count(1) from ticket where ticket_state_id=1 and queue_id!=3;”;
}elseif(“$queuename”==’escalated’){
# not in use yet, to be finished
$sql = “select count(1) from ticket where ticket_state_id=1 and queue_id!=3;”;
}elseif(“$queuename”==’first_level_support’){
# all tickets which are not (pending to) closed
$sql = “select count(1) from ticket where ticket_state_id in (1,4,6,11) and queue_id=5;”;
}elseif(“$queuename”==’second_level_support’){
# all tickets which are not (pending to) closed
$sql = “select count(1) from ticket where ticket_state_id in (1,4,6,11) and queue_id=6;”;
}else{
echo “Usage: $procname <QueueName> <WarnNumber> <CriticalNumber>n”;
echo ” QueueName: Queueu name to query, right now only in “allnew”,”escalated”,”first_level_support” and “second_level_support”n”;
echo ” WarnNumber: Tickets quantity start to be warn, requiredn”;
echo ” CriticalNumber: Tickets quantity start to be critical, must greater than WarnNumber, requiredn”;
exit(3);
}
# Double check query SQL
if(empty($sql)){
echo “UNKNOWN – Unknown errorn”;
exit(3);
}
# Check arguments again.
if(!is_numeric(“$warnlevel”)||!is_numeric(“$critilevel”)){
echo “UNKNOWN – Incorrect arguments.n”;
exit(3);
}elseif($warnlevel>=$critilevel){
echo “UNKNOWN – Incorrect warn/critical level.n”;
exit(3);
}
# connect to database
$conn = @mysql_connect(“localhost”,”readonly”,”readonly”) or die(“Cannot connect to DB”);
mysql_select_db(“otrs”,$conn);
# fetch data
$result = mysql_query(“$sql”,$conn) or die(“Query failed”);
$result = mysql_result($result,0);
# close db
mysql_close($conn);
if ($result>=$critilevel){
echo “CRITICAL – Queue size is $result, queue is huge!!!n”;
exit(2);
}elseif($result>=$warnlevel){
echo “WARN – Queue size is $result, queue is big!n”;
exit(1);
}else{
echo “OK – Queue size is $result, queue is OK.n”;
exit(0);
}
?>
脚本本身没什么好看的,不过最开始用来传参的东西还是蛮好玩的,这种脚本不能跟在web里面一直的传参,只能通过这个叫$argv的数组。
把脚本保存为
修改/etc/snmp/snmpd.conf,加上下面几行
exec first_level_support /usr/local/bin/check_otrs_queue first_level_support 30 50
exec second_level_support /usr/local/bin/check_otrs_queue second_level_support 10 15
最后就是使用check_snmp_exec.sh检测了~~