/** * 添加告警 basic * * @access public * @return void */ public function add_basic($alert_id = null) { $property_basic = $this->get_alert_operator()->get_alert_basic_property(); $alert_name = $property_basic->get_alert_name(); $this->_validate($alert_name); if (!isset($alert_id)) { $alert_id = \lib\sequence\sw_sequence::get_next_global(SWAN_TBN_alert_BASIC); } $property_basic->set_alert_id($alert_id); $property_basic->set_alert_name($alert_name); $attributes = $property_basic->attributes(); $require_fields = array('alert_id', 'alert_name'); $this->_check_require($attributes, $require_fields); $this->__db->insert(SWAN_TBN_alert_BASIC, $attributes); return $alert_id; }
/** * 添加设备 key * * @access public * @return void */ public function add_key($device_id = null) { $property = $this->get_device_operator()->get_device_key_property(); $device_name = $property->get_device_name(); $this->_validate($device_name); if (!isset($device_id)) { $device_id = \lib\sequence\sw_sequence::get_next_global(SWAN_TBN_DEVICE_KEY); } $property->set_device_id($device_id); $property->set_device_name($device_name); $attributes = $property->attributes(); $require_fields = array('device_name', 'device_id'); $this->_check_require($attributes, $require_fields); $this->__db->insert(SWAN_TBN_DEVICE_KEY, $attributes); return $device_id; }
/** * 添加监控适配器 archive * * @access public * @return void */ public function add_archive(\lib\member\property\sw_madapter_archive $property) { $property_basic = $this->get_madapter_operator()->get_madapter_basic_property(); $attributes = $property_basic->attributes(); if (!isset($attributes['madapter_id'])) { throw new sw_exception("unknow madapter id."); } $madapter_id = $attributes['madapter_id']; $attributes = $property->attributes(); $this->_validate($madapter_id); if (!isset($attributes['archive_id'])) { $archive_id = \lib\sequence\sw_sequence::get_next_madapter($madapter_id, SWAN_TBN_MADAPTER_ARCHIVE); } else { $archive_id = $attributes['archive_id']; } $property->set_archive_id($archive_id); $property->set_madapter_id($madapter_id); $attributes = $property->attributes(); $require_fields = array('archive_id', 'madapter_id', 'cf_type', 'xff', 'steps', 'rows', 'title'); $this->_check_require($attributes, $require_fields); $this->__db->insert(SWAN_TBN_MADAPTER_ARCHIVE, $attributes); return $archive_id; }
/** * 添加监控适配器 metric * * @access public * @return void */ public function add_metric(\lib\member\property\sw_madapter_metric $property) { $property_basic = $this->get_madapter_operator()->get_madapter_basic_property(); $attributes = $property_basic->attributes(); if (!isset($attributes['madapter_id'])) { throw new sw_exception("unknow madapter id."); } $madapter_id = $attributes['madapter_id']; $attributes = $property->attributes(); $this->_validate($madapter_id, $attributes['metric_name']); if (!isset($attributes['metric_id'])) { $metric_id = \lib\sequence\sw_sequence::get_next_madapter($madapter_id, SWAN_TBN_MADAPTER_METRIC); } else { $metric_id = $attributes['metric_id']; } $property->set_metric_id($metric_id); $property->set_metric_name($attributes['metric_name']); $property->set_madapter_id($madapter_id); $attributes = $property->attributes(); $require_fields = array('metric_id', 'madapter_id', 'metric_name', 'collect_every', 'time_threshold'); $this->_check_require($attributes, $require_fields); $this->__db->insert(SWAN_TBN_MADAPTER_METRIC, $attributes); return $metric_id; }
/** * 添加设备 监控器 * * @access public * @return void */ public function add_monitor(\lib\member\property\sw_device_monitor $monitor_property) { $property_key = $this->get_device_operator()->get_device_key_property(); $key_attributes = $property_key->attributes(); if (!isset($key_attributes['device_id'])) { throw new sw_exception('Unknow device id.'); } $monitor_params = $monitor_property->get_monitor_params(); $madapter_basic_property = $monitor_property->get_madapter_basic(); $madapter_basic = $madapter_basic_property->attributes(); $attributes = $monitor_property->attributes(); if (!isset($attributes['monitor_name'])) { throw new sw_exception('must defined `monitor_name`'); } // 判断是否已经存在 $this->exists($key_attributes['device_id'], $madapter_basic['madapter_id']); $select = $this->__db->select()->from(SWAN_TBN_DEVICE_MONITOR, 'count(*)')->where('device_id=? AND monitor_name=? '); if ($this->__db->fetch_one($select, array($key_attributes['device_id'], $attributes['monitor_name'])) > 0) { throw new sw_exception('already exists this item.'); } $monitor_id = \lib\sequence\sw_sequence::get_next_device($key_attributes['device_id'], SWAN_TBN_DEVICE_MONITOR); $this->__db->begin_transaction(); // 设备 监控器 主表 $attributes['monitor_id'] = $monitor_id; $attributes['device_id'] = $key_attributes['device_id']; $attributes['madapter_id'] = $madapter_basic['madapter_id']; unset($attributes['monitor_params']); $require_fields = array('monitor_id', 'device_id', 'madapter_id', 'monitor_name'); try { $this->_check_require($attributes, $require_fields); $this->__db->insert(SWAN_TBN_DEVICE_MONITOR, $attributes); } catch (\swan\exception\sw_exception $e) { $this->__db->rollback(); throw new sw_exception($e); } if (!isset($monitor_params) || empty($monitor_params)) { $this->__db->commit(); return $monitor_id; } // 监控器参数值设置 foreach ($monitor_params as $param) { $param_attributes = $param->attributes(); if (!isset($param_attributes['attr_id'])) { $this->__db->rollback(); throw new sw_exception('must define attr_id for add param'); } $select = $this->__db->select()->from(SWAN_TBN_MADAPTER_ATTRIBUTE, 'count(*)')->where('attr_id=? AND madapter_id=? '); if ($this->__db->fetch_one($select, array($param_attributes['attr_id'], $madapter_basic['madapter_id'])) == 0) { $this->__db->rollback(); throw new sw_exception('not exists this attribute.'); } $param_attributes['device_id'] = $key_attributes['device_id']; $param_attributes['monitor_id'] = $monitor_id; $require_fields = array('monitor_id', 'device_id', 'attr_id', 'value'); try { $this->_check_require($param_attributes, $require_fields); $this->__db->insert(SWAN_TBN_MONITOR_PARAM, $param_attributes); } catch (\swan\exception\sw_exception $e) { $this->__db->rollback(); throw new sw_exception($e); } } $this->__db->commit(); return $monitor_id; }