async def _get_monitor_metadata( self, dbcon: DBConnection) -> Optional[Dict[int, Dict[str, str]]]: include_metadata = require_bool(get_request_param( self.request, 'include_metadata', error_if_missing=False), convert=True) or False if not include_metadata: return None if 'id' in self.request.rel_url.query: metadata_models = await metadata.get_metadata_for_object( dbcon, 'active_monitor', require_int(cast(str, get_request_param(self.request, 'id')))) elif 'meta_key' in self.request.rel_url.query: meta_key = require_str(get_request_param(self.request, 'meta_key')) meta_value = require_str( get_request_param(self.request, 'meta_value')) metadata_models = await metadata.get_metadata_for_object_metadata( dbcon, meta_key, meta_value, 'active_monitor', 'active_monitors') elif 'monitor_group_id' in self.request.rel_url.query: metadata_models = await monitor_group.get_active_monitor_metadata_for_monitor_group( dbcon, require_int( cast(str, get_request_param(self.request, 'monitor_group_id')))) else: metadata_models = await metadata.get_metadata_for_object_type( dbcon, 'active_monitor') metadata_dict = {} # type: Dict[int, Dict[str, str]] for metadata_model in metadata_models: if metadata_model.object_id not in metadata_dict: metadata_dict[metadata_model.object_id] = {} metadata_dict[metadata_model.object_id][ metadata_model.key] = metadata_model.value return metadata_dict
async def get(self) -> web.Response: dbcon = self.request.app['dbcon'] if 'id' in self.request.rel_url.query: monitor_group_id = require_int( get_request_param(self.request, 'id')) monitor_group_item = await monitor_group.get_monitor_group( dbcon, monitor_group_id) monitor_group_list = [ ] # type: Iterable[object_models.MonitorGroup] if monitor_group_item: monitor_group_list = [monitor_group_item] metadata_list = await metadata.get_metadata_for_object( dbcon, 'monitor_group', monitor_group_id) elif 'meta_key' in self.request.rel_url.query: meta_key = require_str(get_request_param(self.request, 'meta_key')) meta_value = require_str( get_request_param(self.request, 'meta_value')) monitor_group_list = await monitor_group.get_monitor_groups_for_metadata( dbcon, meta_key, meta_value) metadata_list = await metadata.get_metadata_for_object_metadata( dbcon, meta_key, meta_value, 'monitor_group', 'monitor_groups') else: monitor_group_list = await monitor_group.get_all_monitor_groups( dbcon) metadata_list = await metadata.get_metadata_for_object_type( dbcon, 'monitor_group') return web.json_response( apply_metadata_to_model_list(monitor_group_list, metadata_list))
async def post(self) -> web.Response: request_data = await self.request.json() contact_id = await create_contact( self.request.app['dbcon'], require_str(request_data.get('name', None), allow_none=True), require_str(request_data.get('email', None), allow_none=True), require_str(request_data.get('phone', None), allow_none=True), cast(bool, require_bool(request_data.get('active', True)))) return web.json_response(contact_id)
async def delete(self) -> web.Response: object_type = cast( str, require_str(get_request_param(self.request, 'object_type'))) object_id = cast( int, require_int(get_request_param(self.request, 'object_id'))) key = cast(str, require_str(get_request_param(self.request, 'key'))) await bindata.delete_bindata(self.request.app['dbcon'], object_type, object_id, key) return web.Response(text='')
async def post(self) -> web.Response: object_type = cast( str, require_str(get_request_param(self.request, 'object_type'))) object_id = cast( int, require_int(get_request_param(self.request, 'object_id'))) key = cast(str, require_str(get_request_param(self.request, 'key'))) value = await self.request.read() await bindata.set_bindata(self.request.app['dbcon'], object_type, object_id, key, value) return web.Response(text='')
async def get(self) -> web.Response: # noinspection PyUnusedLocal q_args = () # type: Tuple if 'monitor_id' in self.request.rel_url.query: if 'only_active' in self.request.rel_url.query: q = """select id, monitor_id, start_ts, end_ts, alert_msg from active_monitor_alerts where monitor_id=%s and end_ts=0 order by start_ts desc""" else: q = """select id, monitor_id, start_ts, end_ts, alert_msg from active_monitor_alerts where monitor_id=%s order by start_ts desc""" monitor_id = require_int( get_request_param(self.request, 'monitor_id')) q_args = (monitor_id, ) ret = await self._get_alerts(q, q_args) elif 'meta_key' in self.request.rel_url.query: if 'only_active' in self.request.rel_url.query: q = """select alert.id, alert.monitor_id, alert.start_ts, alert.end_ts, alert.alert_msg from object_metadata as meta left join active_monitors on active_monitors.id=meta.object_id right join active_monitor_alerts as alert on alert.monitor_id=active_monitors.id where meta.key=%s and meta.value=%s and meta.object_type="active_monitor" and alert.end_ts=0 order by alert.start_ts desc""" else: q = """select alert.id, alert.monitor_id, alert.start_ts, alert.end_ts, alert.alert_msg from object_metadata as meta left join active_monitors on active_monitors.id=meta.object_id right join active_monitor_alerts as alert on alert.monitor_id=active_monitors.id where meta.key=%s and meta.value=%s and meta.object_type="active_monitor" order by alert.start_ts desc""" meta_key = require_str(get_request_param(self.request, 'meta_key')) meta_value = require_str( get_request_param(self.request, 'meta_value')) q_args = (meta_key, meta_value) ret = await self._get_alerts(q, q_args) else: if 'only_active' in self.request.rel_url.query: q = """select id, monitor_id, start_ts, end_ts, alert_msg from active_monitor_alerts where end_ts=0 order by start_ts desc""" else: q = """select id, monitor_id, start_ts, end_ts, alert_msg from active_monitor_alerts order by start_ts desc""" ret = await self._get_alerts(q, ()) return web.json_response(ret)
async def get(self) -> web.Response: object_type = cast( str, require_str(get_request_param(self.request, 'object_type'))) object_id = cast( int, require_int(get_request_param(self.request, 'object_id'))) key = cast(str, require_str(get_request_param(self.request, 'key'))) ret = await bindata.get_bindata(self.request.app['dbcon'], object_type, object_id, key) if ret is None: raise errors.NotFound() return web.Response(body=ret)
async def post(self) -> web.Response: request_data = await self.request.json() monitor_def = await create_active_monitor_def( self.request.app['active_monitor_manager'], cast(str, require_str(request_data['name'])), cast(str, require_str(request_data['description'])), cast(bool, require_bool(request_data['active'])), cast(str, require_str(request_data['cmdline_filename'])), cast(str, require_str(request_data['cmdline_args_tmpl'])), cast(str, require_str(request_data['description_tmpl']))) if not monitor_def: raise errors.InvalidData('invalid monitor def arguments') return web.json_response(monitor_def.id)
async def post(self) -> web.Response: request_data = await self.request.json() monitor_group_id = await monitor_group.create_monitor_group( self.request.app['dbcon'], require_int(request_data.get('parent_id', None), allow_none=True), require_str(request_data.get('name', None), allow_none=True)) return web.json_response(monitor_group_id)
async def delete(self) -> web.Response: request_data = await self.request.json() await metadata.delete_metadata( self.request.app['dbcon'], require_str(request_data.get('object_type')), require_int(request_data.get('object_id')), require_list(request_data.get('keys', None), allow_none=True)) return web.json_response(True)
async def post(self) -> web.Response: request_data = await self.request.json() await metadata.update_metadata( self.request.app['dbcon'], require_str(request_data.get('object_type')), require_int(request_data.get('object_id')), require_dict(request_data.get('metadict'), str)) return web.json_response(True)
async def get(self) -> web.Response: object_type = cast( str, require_str(get_request_param(self.request, 'object_type'))) object_id = cast( int, require_int(get_request_param(self.request, 'object_id'))) metadict = await metadata.get_metadata(self.request.app['dbcon'], object_type, object_id) return web.json_response(metadict)
async def put(self) -> web.Response: request_data = await self.request.json() monitor_def = self._get_request_monitor_def(self.request) monitor_def.set_arg( object_models.ActiveMonitorDefArg( id=0, active_monitor_def_id=monitor_def.id, name=cast(str, require_str(request_data['name'])), display_name=cast(str, require_str(request_data['display_name'])), description=cast(str, require_str(request_data['description'])), required=cast(bool, require_bool(request_data['required'])), default_value=cast(str, require_str(request_data['default_value'])), )) return web.json_response(True)
async def _get_monitor_ids(self, dbcon: DBConnection) -> List[int]: if 'id' in self.request.rel_url.query: ids = [ require_int(cast(str, get_request_param(self.request, 'id'))) ] elif 'meta_key' in self.request.rel_url.query: meta_key = require_str(get_request_param(self.request, 'meta_key')) meta_value = require_str( get_request_param(self.request, 'meta_value')) active_monitor_models = await active_sql.get_active_monitors_for_metadata( dbcon, meta_key, meta_value) ids = [monitor.id for monitor in active_monitor_models] elif 'monitor_group_id' in self.request.rel_url.query: monitor_group_id = require_int( get_request_param(self.request, 'monitor_group_id')) active_monitor_models = await monitor_group.get_active_monitors_for_monitor_group( dbcon, monitor_group_id) ids = [monitor.id for monitor in active_monitor_models] else: active_monitor_models = await active_sql.get_all_active_monitors( dbcon) ids = [monitor.id for monitor in active_monitor_models] return ids
async def get(self) -> web.Response: dbcon = self.request.app['dbcon'] if 'id' in self.request.rel_url.query: contact_id = require_int(get_request_param(self.request, 'id')) c = await contact.get_contact(dbcon, contact_id) contact_list = [] # type: Iterable[object_models.Contact] if c: contact_list = [c] metadata_list = await metadata.get_metadata_for_object( dbcon, 'contact', contact_id) elif 'meta_key' in self.request.rel_url.query: meta_key = require_str(get_request_param(self.request, 'meta_key')) meta_value = require_str( get_request_param(self.request, 'meta_value')) contact_list = await contact.get_contacts_for_metadata( dbcon, meta_key, meta_value) metadata_list = await metadata.get_metadata_for_object_metadata( dbcon, meta_key, meta_value, 'contact', 'contacts') else: contact_list = await contact.get_all_contacts(dbcon) metadata_list = await metadata.get_metadata_for_object_type( dbcon, 'contact') return web.json_response( apply_metadata_to_model_list(contact_list, metadata_list))
async def post(self) -> None: request_data = await self.request.json() args = require_dict(request_data['args'], str, None) if request_data.get('use_monitor_def_name', False): monitor_def = get_monitor_def_by_name( self.request.app['active_monitor_manager'], require_str(request_data['monitor_def'])) else: monitor_def = self.request.app[ 'active_monitor_manager'].monitor_defs.get( require_int(request_data['monitor_def'])) if not monitor_def: raise errors.InvalidData('Monitor def not found') monitor = await create_active_monitor( self.request.app['active_monitor_manager'], args, monitor_def) if not monitor: raise errors.InvalidData('invalid monitor arguments') return web.json_response(monitor.id)
async def delete(self) -> web.Response: monitor_def = self._get_request_monitor_def(self.request) await monitor_def.delete_arg( require_str(get_request_param(self.request, 'name'))) return web.json_response(True)