现在的位置: 首页运维|运维监控>正文
cacti短信报警–高级篇
发表于515 天前 运维|运维监控 暂无评论 ⁄ 被围观 +

【描述】

网上有很多提示cacti的短信报警,但用起来都感觉不怎样,故而自己觉得亲自修改cacti的源码来实现短信报警机制。实现方式很简单,通过触发URL来实现手机短信报警。不过短信发送需要收取费用,采用了第三方的短信接口。好了,废话不多说,看具体实现方式吧。

【实现方式】

老规矩,按照settings(V0.5)和thold(V0.4.2)这两个插件。这里就不在详细描述了,之前以前介绍过了,不清楚的朋友可以看下我之前写的博文。

第一步:打开plugins\settings\setup.php

修改:

$tabs['mail'] = 'Mail / Mobile';

在$temp数组下增加如下代码:

"settings_mobile_header" => array(
	"friendly_name" => "Mobile Options",
	"method" => "spacer",
	),
"settings_mobile_number" => array(
	"friendly_name" => "Mobile Number",
	"description" => "This is the China Mobile Number that the dead host notifications will be sent to.",
	"method" => "textbox",
	"max_length" => 255,
	),

到此setup.php修改完成,打开web 界面可以看到如下:


支持多个号码,号码之间用英文逗号分隔

第二步:将内容发送到手机上,具体操作如下:

打开plugins\thold\thold_functions.php

修改thold_mail 这个函数,大约在1350行,在下面这段代码前加入以下代码

if ($Mailer->send($text) == false) {
      print 'ERROR: ' . $Mailer->error() . "\n";
      return $Mailer->error();
}
 
return '';

加入的代码为:

	//发送短信	
	preg_match("/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/",$subject,$ip); 
	preg_match('/-(.*)]/',$subject,$content);
	$flag = stristr($subject,'normal');
	$content = str_replace('-','',str_replace('','',$content[0]));
	if ($flag) {
		$content = $ip[0] . $content . ' is OK';
	} else {	
		$content = $ip[0] . $content . ' has problem';
	}	
	thold_mms($content);

将发送到手机上的短信内容做了处理。大家可以根据自己的实际情况做修改
这下面最主要的就是thold_mms这个函数了,将这个函数添加到thold_mail 函数下
代码如下:

/* mms */
function thold_mms($content) {
    $number = db_fetch_row("SELECT value FROM settings WHERE name = 'settings_mobile_number'");
    $mobile_string = $number['value'];	
    $mms_content = iconv("UTF-8","GB2312",$content);
    $API_Endpoint="http://service.winic.org/sys_port/gateway/";
    $nvpStr ="id=jiunile&pwd=jiunile&to=".$mobile_string."&content=".urlencode($mms_content)."&time=";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpStr);
    $response = curl_exec($ch);
    if(curl_errno($ch)){
        $curl_error_no >=curl_errno($ch) ;
        $curl_error_msg>=curl_error($ch);
    }else{
        curl_close($ch);
    }
    return $response;
}

$nvpStr来自http://winic.org提供。

给我留言


/ 快捷键:Ctrl+Enter
不想听你唠叨×