def list(self,request): ret = CeleryFlowerApi().getAllWorkerRegisterTasks() if ret["result"] == 1: logError().error(ret["msg"]) return Response(data=ret["msg"],status=status.HTTP_500_INTERNAL_SERVER_ERROR) registered_tasks_list = ret["data"] return Response(data=registered_tasks_list, status=status.HTTP_200_OK)
def delete_servers_relate_monitor_items(self, app_obj): ''' 由于应用混布,所以同一个服务器会关联多个应用,因此删除 服务器关联的监控项时要考虑这个服务器是否被其他应用使用 删除仅属于本应用的服务器与监控项的关联,而不影响 同属于其他应用下的某个服务器与监控项的关联 ''' ret = {"status": 0} monitor_item_relate_apps = AppConfigModel.objects.filter( zabbixitemmodel__isnull=False).exclude(name__exact=app_obj.name) delete_server_obj_list = [ server for server in app_obj.server.all() for app in server.appconfigmodel_set.exclude( name__exact=app_obj.name) if app not in monitor_item_relate_apps ] try: ZabbixHostItemModel.objects.filter( server__in=delete_server_obj_list).delete() except Exception as e: ret["status"] = 1 ret["msg"] = "ZabbixHostItemModel 删除 app: %s 关联 server 的监控项失败,请查看日志...." % ( app_obj.name) logError().error( "ZabbixHostItemModel 删除 app: %s 关联的 server: %s 失败, 错误信息: %s" % (app_obj.name, delete_server_obj_list, e.args)) return ret
def to_internal_value(self, data): if data.get("source") != 'zabbix': return data try: event_obj = Events.objects.db_manager("zabbix").get( eventid__exact=data.get("eventid")) data["detail"] = ' last value: %s' % (data.pop("last_value")) if data.get("status") == 'PROBLEM': server_ip = data.pop("hostip") server_ip = server_ip if server_ip != '127.0.0.1' else '192.168.1.79' server_obj = ServerModel.objects.get( manager_ip__exact=server_ip) trigger_obj = Triggers.objects.db_manager("zabbix").get( triggerid__exact=event_obj.objectid) data["server"] = server_obj data["trigger_name"] = trigger_obj.description data["start_time"] = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(event_obj.clock)) else: data["status"] = "OK" data["end_time"] = datetime.strptime(data.get("end_time"), "%Y.%m.%d %H:%M:%S") except Exception as e: msg = "初始化 zabbix post 过来的数据异常,错误信息: %s" % (e.args) logError().error(msg) raise serializers.ValidationError(msg) return data
def __init__(self): aliyun_conf = getMyConf(section="aliyun_config") if aliyun_conf["result"] == 0: accessKeyId = aliyun_conf["oops_conf"]["haha"] accessSecret = aliyun_conf["oops_conf"]["hehe"] else: logError().error(aliyun_conf["msg"]) accessKeyId = '' accessSecret = '' self.clt = client.AcsClient(accessKeyId, accessSecret, 'cn-beijing')
def create(self,request): print("request.data: ", request.data) if not request.data.get("task"): return Response(data="前端未传入参数 task",status=status.HTTP_500_INTERNAL_SERVER_ERROR) task = request.data.get("task") data = {} data["args"] = eval(request.data.get("args")) data["kwargs"] = eval(request.data.get("kwargs")) ret = CeleryFlowerApi().execTaskNow(task,data) if ret["result"] == 1: logError().error(ret["msg"]) return Response(data=ret["msg"],status=status.HTTP_500_INTERNAL_SERVER_ERROR) registered_tasks_list = ret["data"] return Response(data=registered_tasks_list, status=status.HTTP_200_OK)
def getZabbixItemxAxisDelay(self, server_list, item_key, time_begin, time_end): ''' 根据查询的时间跨度以及监控项采集周期确定X 轴的取值范围,时间跨度和X轴数据点间隔分配关系如下: 0-0.5 天 : 每1分钟一个点 0.5-1 天 : 每5分钟一个点 1-3 天 : 每10分钟一个点 3-7 天 : 每15分钟一个点 7-X 天 : 每60分钟一个点,直接查询 trends 表 ''' ret = {"status": 0} time_span = time_end - time_begin try: max_item_delay = int( ZabbixHostItemModel.objects.filter( server__in=server_list, item_key__exact=item_key).aggregate( item_delay_max=Max('item_delay'))["item_delay_max"]) except Exception as e: ret["status"] = 1 msg = "ZabbixHostItemModel 获取 item_key: %s 的 delay 失败" % (item_key) ret["msg"] = msg logError().error(msg) return ret if time_span <= 0.5 * 24 * 60 * 60: time_strftime = '%Y-%m-%d %H:%M' ret["max_item_delay"] = 1 * 60 if max_item_delay <= 1 * 60 else max_item_delay elif time_span > 0.5 * 24 * 60 * 60 and time_span <= 1 * 24 * 60 * 60: time_strftime = '%Y-%m-%d %H:%M' ret["max_item_delay"] = 5 * 60 if max_item_delay <= 5 * 60 else max_item_delay elif time_span > 1 * 24 * 60 * 60 and time_span <= 3 * 24 * 60 * 60: time_strftime = '%Y-%m-%d %H:%M' ret["max_item_delay"] = 10 * 60 if max_item_delay <= 10 * 60 else max_item_delay elif time_span > 3 * 24 * 60 * 60 and time_span <= 7 * 24 * 60 * 60: time_strftime = '%Y-%m-%d %H:%M' ret["max_item_delay"] = 15 * 60 if max_item_delay <= 15 * 60 else max_item_delay else: ret["max_item_delay"] = 60 * 60 if max_item_delay <= 60 * 60 else max_item_delay time_strftime = '%Y-%m-%d %H:00' time_begin = self.unixtime_to_minutes_or_hours(time_begin, 3600) time_end = self.unixtime_to_minutes_or_hours(time_end, 3600) ret["x_data"] = [ time.strftime(time_strftime, time.localtime(i)) for i in range(time_begin, time_end, ret["max_item_delay"]) ] return ret
def server_relate_monitor_item(self, item_obj, server_obj): ret = {"status": 0} zabbix_host_item_data = {} zabbix_host_item_data["itemid"] = item_obj.itemid zabbix_host_item_data["hostid"] = item_obj.hostid.hostid zabbix_host_item_data["item_key"] = item_obj.key_field zabbix_host_item_data["item_name"] = item_obj.name zabbix_host_item_data["value_type"] = item_obj.value_type zabbix_host_item_data["item_units"] = item_obj.units zabbix_host_item_data["server"] = server_obj if item_obj.value_type == 0: zabbix_host_item_data["history_model_name"] = 'History' zabbix_host_item_data["trends_model_name"] = 'Trends' elif item_obj.value_type == 1: zabbix_host_item_data["history_model_name"] = 'HistoryStr' elif item_obj.value_type == 2: zabbix_host_item_data["history_model_name"] = 'HistoryLog' elif item_obj.value_type == 3: zabbix_host_item_data["history_model_name"] = 'HistoryUint' zabbix_host_item_data["trends_model_name"] = 'TrendsUint' else: zabbix_host_item_data["history_model_name"] = 'HistoryText' if item_obj.delay.lower().endswith('h'): zabbix_host_item_data["item_delay"] = eval( item_obj.delay[:-1]) * 3600 elif item_obj.delay.lower().endswith('m'): zabbix_host_item_data["item_delay"] = eval( item_obj.delay[:-1]) * 60 elif item_obj.delay.lower().endswith('s'): zabbix_host_item_data["item_delay"] = eval(item_obj.delay[:-1]) else: zabbix_host_item_data["item_delay"] = eval(item_obj.delay) print("zabbix_host_item_data: ", zabbix_host_item_data) try: zabbix_host_item_obj = ZabbixHostItemModel.objects.create( **zabbix_host_item_data) except Exception as e: ret["status"] = 1 msg = "ZabbixHostItemModel 创建 服务器: %s 关联 item: %s 的对象失败, 错误信息: %s" % ( server_obj.manager_ip, item_obj.key_field, e.args) ret["msg"] = msg logError().error(msg) return ret
def add_servers_relate_monitor_items(self, app_obj, item_key): ''' 添加服务器与监控项的关联: 1. 先检查这个服务器是否已经被关联了; 2. 要去 zabbix 上获取 该服务器的相关配置信息,如:itemid,hostid,delay,units,value_type 以及存储在哪个表 ''' ret = {"status": 0} add_server_obj_list = [ server for server in app_obj.server.all() if server not in ServerModel.objects.filter( zabbixhostitemmodel__isnull=False, zabbixhostitemmodel__item_key__exact=item_key).distinct() ] for server_obj in add_server_obj_list: try: host_obj = Interface.objects.db_manager("zabbix").get( ip__exact=server_obj.manager_ip, port__exact=10050) except Interface.DoesNotExist: ret["status"] = 1 msg = "Zabbix 上 Interface 表中查不到 IP: %s 端口: 10050 的对象,请检查 zabbix 是否接入了该主机以及zabbix_agent 的端口是不是 10050...." % ( server_obj.manager_ip) ret["msg"] = msg logError().error(msg) continue try: item_obj = Items.objects.db_manager("zabbix").get( hostid__exact=host_obj.hostid, key_field__exact=item_key) except Items.DoesNotExist: ret["status"] = 1 msg = "Zabbix 上 Items 表中查不到 hostid: %s key_: %s 的对象,请检查 zabbix 该主机是否添加了该监控项...." % ( host_obj.hostid, item_key) ret["msg"] = msg logError().error(msg) return ret ret = self.server_relate_monitor_item(item_obj, server_obj) if ret["status"] == 1: break return ret
def check_ip(self, network_obj, ips_list): ''' 检查 IP对象是不是存在,否则就创建,同时更新网卡对象与IP对象的关联关系 ''' ip_obj_list = [] for ip in ips_list: try: ip_obj = network_obj.ipaddrmodel_set.get( ip__exact=ip.get("ip"), netmask__exact=ip.get("netmask")) except IpAddrModel.DoesNotExist: ''' 创建 IpAddr 对象 ''' logError().error( "IpAddrModel 中 查不到 ip: %s netmask: %s 的对象,所以需要创建这个对象..." % (ip['ip'], ip['netmask'])) ip["networkcard"] = network_obj ip_obj = IpAddrModel.objects.create(**ip) finally: ip_obj_list.append(ip_obj) network_obj.ipaddrmodel_set.set(ip_obj_list) ''' 删除掉未绑定网卡的IP地址 ''' IpAddrModel.objects.filter(networkcard_id__isnull=True).delete()
def execRequest(self,uri,method="GET",data=None): ret = {"result": 0} request_url = self.url + uri try: if method == "GET": r = requests.get(request_url, headers=self.headers, auth=(self.user, self.password)) else: r = requests.post(request_url, headers=self.headers, data=json.dumps(data), auth=(self.user, self.password)) except Exception as e: msg = "请求 RabbitMQ API 接口失败,请确认 RabbitMQ 是否运行....." ret["result"] = 1 ret["msg"] = msg logError().error(msg) return ret if int(r.status_code) != 200: msg = "请求 RabbitMQ API 接口的返回值是非 200,错误信息: %s" %(r.content) ret["result"] = 1 ret["msg"] = msg logError().error(msg) return ret ret["data"] = r.json() return ret
def aliyunDescribeInstances(self, **kwargs): ''' 获取 ECS 所有实例的详细信息 者指定实例的详细信息: 通过传参 InstanceIds = ["实例id"] ''' request = DescribeInstancesRequest.DescribeInstancesRequest() request.set_accept_format('json') request.add_query_param('PageSize', 100) if kwargs: for k, v in kwargs.items(): request.add_query_param(str(k), str(v)) try: response = self.clt.do_action_with_exception(request) except Exception as e: logError().error("获取阿里云上 ECS 实例信息异常,错误信息: %s" % (e)) return ret = { i["NetworkInterfaces"]["NetworkInterface"][0]["PrimaryIpAddress"]: { "idc": i["ZoneId"], "cpu_count": "%s 核" % (i["Cpu"]), "online_time": i["CreationTime"], "expired_time": i["ExpiredTime"], "charge_type": i["InstanceChargeType"], "instance_id": i["InstanceId"], "instance_name": i["InstanceName"], "instance_type": i["InstanceType"], "mem": "%.2f GB" % (i["Memory"] / 1024.0), "os_version": i["OSName"], "uuid": "%s" % (i["SerialNumber"].upper()), "server_brand": "Alibaba Cloud", "server_model": "Alibaba Cloud ECS", "type": "cloud", "idrac_ip": "0.0.0.0", "status": i["Status"], "hostname": i["HostName"], "public_ip": i["PublicIpAddress"].get("IpAddress")[0] if i["PublicIpAddress"].get("IpAddress") else i["EipAddress"]["IpAddress"], "manager_ip": i["NetworkInterfaces"]["NetworkInterface"][0] ["PrimaryIpAddress"], "network": [{ "status": "up", "ips": [{ "ip": i["NetworkInterfaces"]["NetworkInterface"][0] ["PrimaryIpAddress"], "netmask": "255.255.240.0" }], "mac": i["NetworkInterfaces"]["NetworkInterface"][0] ["MacAddress"], "name": "eth0" }] } for i in json.loads(response)["Instances"]["Instance"] } return ret