Ejemplo n.º 1
0
 public static function parse($message)
 {
     $msgs = array();
     $fields = explode(AlarmInfoUtil::MSG_SEPARATOR_RIGHT, $message);
     if (FALSE === $fields || 4 != count($fields)) {
         AlarmLog::err(__FILE__, __LINE__, "msg format error!");
         return FALSE;
     }
     $prog = trim($fields[0], AlarmInfoUtil::MSG_SEPARATOR_LEFT);
     $host = trim($fields[1], AlarmInfoUtil::MSG_SEPARATOR_LEFT);
     $ip = trim($fields[2], AlarmInfoUtil::MSG_SEPARATOR_LEFT);
     $from = new AlarmMsgFrom($ip, $host, $prog);
     $contents = $fields[3];
     $msg_array = explode("\n", $contents);
     if (FALSE === $msg_array || 0 === count($msg_array)) {
         AlarmLog::err(__FILE__, __LINE__, "msg format error!");
         return FALSE;
     }
     foreach ($msg_array as $item) {
         $message = new AlarmMsg();
         $ret = $message->initial($from, $item);
         if (FALSE === $ret) {
             AlarmLog::err(__FILE__, __LINE__, "initial AlarmMsg failed!");
             return FALSE;
         }
         $msgs[] = $message;
     }
     return $msgs;
 }
Ejemplo n.º 2
0
 public function initial($from, $msg_content)
 {
     $this->from = $from;
     $fields = explode(AlarmInfoUtil::MSG_SEPARATOR_CONTENT, $msg_content);
     if (FALSE === $fields || count($fields) < 4) {
         AlarmLog::err(__FILE__, __LINE__, "Ilegall format of msg content({$msg_content})!");
         return FALSE;
     }
     $time_field = $fields[0];
     $timestamp = strtotime($time_field);
     if (FALSE === $timestamp || -1 === $timestamp) {
         AlarmLog::err(__FILE__, __LINE__, "Ilegall format of msg content({$msg_content}), time filed:({$time_field})!");
         return FALSE;
     }
     $this->time = $timestamp;
     $this->level = trim($fields[1]);
     $this->location = trim($fields[2]);
     $this->content = trim($fields[3]);
 }
Ejemplo n.º 3
0
 public function load_conf()
 {
     // 0. read conten from file
     $var = file_get_contents(AlarmInfoUtil::STRATEGY_CONF_PATH);
     $tmp_var = addslashes($var);
     eval("?>" . $var);
     // 1. load receiver configure
     foreach ($ALARM_RECEIVER as $conf_name => $conf_receiver) {
         $alarmReceiver = new AlarmReceiver($conf_name, $conf_receiver["email"], $conf_receiver["sms"]);
         $this->_receiver_table[$conf_name] = $alarmReceiver;
     }
     // 2. load bussiness setting
     if (!is_array($ADMIN_SETTING)) {
         AlarmLog::err(__FILE__, __LINE__, "no admin setting exist!");
         return FALSE;
     }
     $BUSSINESS_SETTING[AlarmInfoUtil::STRATEGY_SETTING_ADMIN] = $ADMIN_SETTING;
     foreach ($BUSSINESS_SETTING as $name => $one_set) {
         $setting_receiver_type = $one_set["receiver_type"];
         $receiver_type_array = explode("|", trim($setting_receiver_type, "|"));
         $receive_type = 0;
         foreach ($receiver_type_array as $one_type) {
             switch ($one_type) {
                 case "email":
                     $receive_type |= AlarmInfoUtil::SEND_TYPE_EMAIL_MASK;
                     break;
                 case "sms":
                     $receive_type |= AlarmInfoUtil::SEND_TYPE_SMS_MASK;
                     break;
                 case "qalarm":
                     $receive_type |= AlarmInfoUtil::SEND_TYPE_QALARM_MASK;
                     break;
                 default:
                     AlarmLog::err(__FILE__, __LINE__, "unknown receiver type: {$receive_type}!");
             }
         }
         $is_alarm = AlarmInfoUtil::BUSSINESS_ALARM_ON;
         if (array_key_exists("is_alarm", $one_set) && "off" === $one_set["is_alarm"]) {
             $is_alarm = AlarmInfoUtil::BUSSINESS_ALARM_OFF;
         }
         $one_bussiness = new AlarmBussiness($name, $receive_type, $is_alarm);
         foreach ($one_set["receiver"] as $receiver_name) {
             $one_bussiness->addReceiver($receiver_name);
         }
         $this->_bussiness_setting[$name] = $one_bussiness;
     }
     // 3. load strategy
     if (!array_key_exists("default", $STRATEGYS)) {
         AlarmLog::err(__FILE__, __LINE__, "no default strategy found (default) !");
         return FALSE;
     }
     foreach ($STRATEGYS as $strategy_name => $strategy_info) {
         if (!array_key_exists("receiver", $strategy_info)) {
             AlarmLog::err(__FILE__, __LINE__, "no 'receiver' item found in strategy({$strategy_name})!");
             return FALSE;
         }
         $conf_recv_types = $strategy_info["receiver"];
         $conf_recv_type_array = explode("|", trim($conf_recv_types, "|"));
         if (!in_array("none", $conf_recv_type_array)) {
             if (!array_key_exists("threshold", $strategy_info)) {
                 AlarmLog::err(__FILE__, __LINE__, "no 'threshold' item found in strategy({$strategy_name})!");
                 return FALSE;
             }
         }
         $recv_type = 0;
         foreach ($conf_recv_type_array as $conf_recv_type) {
             switch ($conf_recv_type) {
                 case "bussiness":
                     $recv_type |= AlarmInfoUtil::STRATEGY_RECEIVER_TYPE_BUSSINESS;
                     break;
                 case "admin":
                     $recv_type |= AlarmInfoUtil::STRATEGY_RECEIVER_TYPE_ADMIN;
                     break;
                 case "none":
                     $recv_type |= AlarmInfoUtil::STRATEGY_RECEIVER_TYPE_NONE;
                     break;
                 default:
                     AlarmLog::err(__FILE__, __LINE__, "unknown strategy receiver type ({$conf_recv_type})!");
                     return FALSE;
             }
         }
         if (array_key_exists("class", $strategy_info)) {
             $className = $strategy_info["class"];
             if (!class_exists($className) || !is_subclass_of($className, "AlarmStrategy")) {
                 AlarmLog::err(__FILE__, __LINE__, "strategy process class not found ({$className})!");
                 return FALSE;
             }
         } else {
             $className = "AlarmStrategy";
         }
         $strategy = new $className($strategy_name, $recv_type, $strategy_info["keyword"]);
         if (array_key_exists("threshold", $strategy_info)) {
             foreach ($strategy_info["threshold"] as $level => $count) {
                 $strategy->addLevel($level, $count);
             }
         }
         if ("default" === $strategy_name) {
             $strategy_name = AlarmInfoUtil::STRATEGY_NAME_DEFAULT;
         }
         $this->_strategys[$strategy_name] = $strategy;
     }
 }
Ejemplo n.º 4
0
function qconf_alarm_func($msg)
{
    // 1. Parse message
    echo "receive message";
    //return true;
    $alarmMsgs = AlarmMsgParser::parse($msg);
    if (FALSE === $alarmMsgs) {
        AlarmLog::err(__FILE__, __LINE__, "failed to parse msg!");
        return true;
    }
    //printInfo($alarmMsgs);
    foreach ($alarmMsgs as $item) {
        // 2. pass strategy
        $strategy_control = AlarmStrategyController::getInstance();
        //var_dump($strategy_control);
        if (FALSE === $strategy_control) {
            AlarmLog::err(__FILE__, __LINE__, "failed to get  AlarmStrategyController instance!");
            return true;
        }
        $strategy_control->passStrategy($item);
    }
    return true;
}