示例#1
0
 def get(self, req_data):
     form = MonitorAlertDashGetForm(ImmutableMultiDict(req_data))
     if not form.validate():
         raise Status400(form.errors)
     form_data = form.data.copy()
     region = form_data.pop('region')
     ser_instance = self.db_session.query(MonitorRegion).get(region)
     if not ser_instance:
         raise Status403('region id is invalid')
     server_list = [i.ip for i in ser_instance._serverips]
     form_data['broadband'] = form_data['broadband'].split(',')
     origin_data = es_client.get_region_record(
         settings.opt['ELASTICSEARCH']['INDEX'],
         servers=server_list,
         **form_data)
     for item in origin_data:
         if form_data['datatype'] == 'delay':
             item['level'] = MonitorAlertHandler.set_level(
                 form_data['datatype'], delay=item['delay'])
         elif form_data['datatype'] == 'loss_rate':
             item['level'] = MonitorAlertHandler.set_level(
                 form_data['datatype'], loss_rate=item['loss_rate'])
         else:
             item['level'] = MonitorAlertHandler.set_level(
                 form_data['datatype'], item['delay'], item['loss_rate'])
     return {'return': 0, 'message': 'success', 'instance': origin_data}
示例#2
0
 def get(self, req_data):
     form = MonitorAlertDashDetailForm(ImmutableMultiDict(req_data))
     if not form.validate():
         raise Status400(form.errors)
     form_data = form.data.copy()
     region = form_data.pop('region')
     ser_instance = self.db_session.query(MonitorRegion).get(region)
     if not ser_instance:
         raise Status403('region id is invalid')
     server_list = [i.ip for i in ser_instance._serverips]
     # logger.debug('{0}'.format(form_data))
     form_data['broadband'] = form_data['broadband'].split(',')
     origin_data = es_client.get_province_record(
         settings.opt['ELASTICSEARCH']['INDEX'],
         servers=server_list,
         **form_data)
     # logger.debug('{0}'.format(origin_data))
     ip_total = 0
     avg_delay, avg_loss = 0, 0
     for item in origin_data['county_net']:
         ip_total = ip_total + item['counter']
     for item in origin_data['county_net']:
         avg_delay = avg_delay + item['delay'] * item['counter'] / ip_total
         avg_loss = avg_loss + item['loss_rate'] * item['counter'] / ip_total
         item['ip_perc'] = item['counter'] / ip_total * 100
     return {
         'return': 0,
         'message': 'success',
         'ip_total': ip_total,
         'avg_delay': avg_delay,
         'avg_loss': avg_loss,
         'update_time': origin_data['updatetime'],
         'instance': origin_data['county_net']
     }
 def get(self, req_data):
     if not req_data.has_key('id'):
         raise Status400('id is required')
     result = MonitorRegion._validate(self.db_session, req_data['id'])
     if result['instance'] is None:
         raise Status403(result['message'])
     serialized = [
         AlchemyEncoder().parse_sqlalchemy_object(item)
         for item in result['instance']._serverips
     ]
     return {'instance': serialized, 'result': 0, 'message': 'success'}
 def delete(self, req_data):
     if not req_data.has_key('id'):
         raise Status400('id is required')
     result = MonitorRegion._validate(self.db_session, req_data['id'])
     instance = result['instance']
     if instance is None:
         raise Status403(result['message'])
     instance._delete(self.db_session)
     return {
         'id': '{}'.format(instance.id),
         'result': 0,
         'message': 'success'
     }
 def post(self, req_data):
     form = MonitorServerIpForm(ImmutableMultiDict(req_data))
     if not form.validate():
         raise Status400(form.errors)
     result = MonitorServerIp._validate(self.db_session, **req_data)
     instance = result['instance']
     if instance is None:
         raise Status403(result['message'])
     instance._save(self.db_session)
     return {
         'instance': AlchemyEncoder().parse_sqlalchemy_object(instance),
         'result': 0,
         'message': 'success'
     }
 def put(self, req_data):
     if not req_data.has_key('id'):
         raise Status400('id is required')
     form = MonitorRegionPutForm(data=ImmutableMultiDict(req_data))
     if not form.validate():
         raise Status400(form.errors)
     result = MonitorRegion._validate(self.db_session, req_data['id'],
                                      **req_data)
     instance = result['instance']
     if instance is None:
         raise Status403(result['message'])
     instance._save(self.db_session)
     return {
         'id': '{}'.format(instance.id),
         'result': 0,
         'message': 'success'
     }
示例#7
0
 def delete(self, req_data):
     if not req_data.has_key('id'):
         raise Status400('id is required')
     result = MonitorTrigger._validate(self.db_session, req_data['id'])
     instance = result['instance']
     if instance is None:
         raise Status403(result['message'])
     if len(instance._rule._triggers) == 1:
         # 删除整条告警规则
         for i in instance._rule._actions:
             i._delete(self.db_session, commit=False)
         instance._rule._delete(self.db_session, commit=False)
     instance._delete(self.db_session)
     return {
         'id': '{}'.format(instance.id),
         'result': 0,
         'message': 'success',
     }
示例#8
0
 def get(self, req_data):
     if not req_data.has_key('id'):
         raise Status400('id is required')
     instance = self.db_session.query(MonitorAlertLog).get(
         req_data['id'][0])
     if not instance:
         raise Status403('alert log id is invalid')
     origin_data = AlchemyEncoder().parse_sqlalchemy_object(instance)
     key_list = [
         'start_time', 'region', 'state', 'content_detail', 'rule_name',
         'broadband', 'id'
     ]
     ret_data = {
         key: value
         for key, value in origin_data.iteritems() if key in key_list
     }
     if origin_data['state'] == 'close':
         stop_time = datetime.datetime.strptime(origin_data['stop_time'],
                                                '%Y-%m-%d %H:%M:%S')
         start_time = datetime.datetime.strptime(origin_data['start_time'],
                                                 '%Y-%m-%d %H:%M:%S')
         if stop_time < start_time:
             time_delta = start_time - stop_time
         else:
             time_delta = stop_time - start_time
         ret_data['duration'] = time_delta.seconds / 60
     else:
         now = datetime.datetime.now()
         start_time = datetime.datetime.strptime(origin_data['start_time'],
                                                 '%Y-%m-%d %H:%M:%S')
         time_delta = now - start_time
         ret_data['duration'] = time_delta.seconds / 60
     if origin_data['county'] == '*':
         ret_data['county'] = origin_data['province']
     else:
         ret_data['county'] = '{0}{1}'.format(origin_data['province'],
                                              origin_data['county'])
     return {
         'result': 0,
         'message': 'success',
         'instance': ret_data,
     }