def set(self): date_time = DateTime() now = date_time.get_now() HH = date_time.get_hour(now) if HH > SYSTEM["broadcast_time"]["FROM"] and HH < SYSTEM["broadcast_time"]["TO"]: self.create_snmp_at_broadcast_time() else: self.create_snmp_at_broadcast_timeout()
def update_data(self, video_status, source_status): date_time = DateTime() opdate = date_time.get_now() child_thread_list = [] profile = ProfileBLL() profile_data = { "video": video_status, "agent": self.agent, "ip": self.ip } self.logger.debug( "update profile data: video{0} - agent{1} - ip{2}".format( video_status, self.agent, self.ip)) child_thread = threading.Thread(target=profile.put, args=( self.id, profile_data, )) child_thread.start() child_thread_list.append(child_thread) human_readable_status = self.get_human_readable_status(source_status) message = """%s %s (ip:%s) %s in host: %s (%s)""" % ( self.name, self.type, self.source, human_readable_status, self.ip, self.agent) log_data = { "host": self.protocol + "://" + self.source, "tag": "status", "msg": message } rslog = { "sev": "Critical", "jname": self.name, "type": self.type, "res": self.source, "desc": human_readable_status, "cat": "Communication", "host": self.agent, "opdate": opdate, "cldate": opdate } self.logger.critical(json.dumps(rslog)) log = LogBLL() child_thread = threading.Thread(target=log.post, args=(log_data, )) child_thread.start() child_thread_list.append(child_thread) """Update local snmp IPTV""" local_snmp = LocalSnmp(profile=self.source + "-" + self.type, name=self.name, status=2) child_thread = threading.Thread(target=local_snmp.set) child_thread.start() child_thread_list.append(child_thread) """ Wait for update database complete """ for child_thread in child_thread_list: child_thread.join() return 0
def check_source(self, source, last_status, id, agent, name, type): """ Get status of profile, if stastus not change then update check equal 1. Ffmpeg: Use Ffprobe to check stastus profile (source) and return flag 0 is down 1 is up 2 is video error 3 is audio eror """ ffmpeg = Ffmpeg() check = ffmpeg.check_source(source) # print "%s : %s"%(check, last_status) self.logger.debug("Curent :%s <> Last: %s, %s %s %s"%(check, last_status, source, name, type)) if check != last_status: date_time = DateTime() opdate = date_time.get_now() time.sleep(SYSTEM["BREAK_TIME"]) self.logger.debug("Recheck : %s %s %s"%(source, name, type)) recheck = ffmpeg.check_source(source) if recheck == check: status = {0: "DOWN ", 1: "UP ", 2: "VIDEO ERROR", 3: "AUDIO ERROR"} [check] """ Update status and write log """ child_thread_list = [] profile = ProfileBLL() profile_data = {"status": check, "agent": agent, "ip": SYSTEM["HOST"]} child_thread = threading.Thread(target=profile.put, args=(id, profile_data,)) child_thread.start() child_thread_list.append(child_thread) """Append log""" channel = """%s %s"""%(name, type) while len(channel) < 22: channel += " " while len(source) < 27: source += " " ip_config = SYSTEM["HOST"] while len(ip_config) < 16: ip_config += " " message = """%s (ip:%s) %s in host: %s (%s)""" % (channel, source, status, ip_config, agent) cldate = date_time.get_now() rslog = { "sev" : "Critical", "jname" : name, "type" : type, "res" : source, "desc" : status, "cat" : "Communication", "host" : agent, "opdate" : opdate, "cldate" : cldate } self.logger.critical(json.dumps(rslog)) log_data = {"host": source, "tag": "status", "msg": message} log = LogBLL() child_thread = threading.Thread(target=log.post, args=(log_data,)) child_thread.start() child_thread_list.append(child_thread) """Update local snmp IPTV""" if "origin" or "4500" in agent: self.logger.debug("%s is core probe"%(agent)) time.sleep(2) snmp = Snmp() child_thread = threading.Thread(target=snmp.set) child_thread.start() child_thread_list.append(child_thread) """ Wait for update database complete """ for child_thread in child_thread_list: child_thread.join() return 1 return 0
def put(self, request, pk): data = request.body data = json.loads(data) self.logger.debug("message: %s" % (str(data))) #Status if ("status" in data): self.logger.debug("message: update status -->%s" % (str(data))) agent_name = data["agent"] profile_agent = self.get_object(pk) date_time = DateTime() if not profile_agent: self.logger.info("Not found profile_agent_id: %s" % (str(pk))) return HttpResponse(status=400) if profile_agent.status != data["status"]: profile_agent.status = data["status"] profile_agent.last_update = date_time.get_now() profile_agent.save() self.logger.debug("message: update status --> success") if "Origin" in agent_name or "4500" in agent_name or "ott" in agent_name: self.logger.info("%s is core probe" % (agent_name)) if PUSH_ALARM_CORE: self.logger.info("Push alarm to scc is active") time.sleep(0.5) snmp = Snmp(str(data["ip"]).replace(' ', '')) alarm_status, msg = snmp.check_agent() data = { "ishost": False, "queueServiceName": "Check_Agent_IPTV_Status", "queueHost": agent_name, "msg": str(msg), "AlertStatus": alarm_status, "agentId": int(pk) } self.logger.debug("alarm contain: %s" % (str(data))) scc = Scc() scc.post(data) return HttpResponse(status=202) else: self.logger.info("%s is not core probe" % (agent_name)) if PUSH_ALARM_PROBE: self.logger.info("Push alarm to scc is active") time.sleep(0.5) snmp = Snmp(str(data["ip"]).replace(' ', '')) alarm_status, msg = snmp.check_agent() data = { "ishost": False, "queueServiceName": "Check_Agent_IPTV_Status", "queueHost": agent_name, "msg": str(msg), "AlertStatus": alarm_status, "agentId": int(pk) } self.logger.debug("alarm contain: %s" % (str(data))) scc = Scc() scc.post(data) return HttpResponse(status=202) else: self.logger.warning("Value not change: %s" % (str(pk))) return HttpResponse(status=400) # querry = "update profile_agent set status=%s,last_update=unix_timestamp() where id=%s;"%(data['status'],pk) # RabbitMQQueue().push_query(querry) # return HttpResponse(status=202) #video elif ('video' in data): self.logger.debug("message: update video status -->%s" % (str(data))) agent_name = data["agent"] profile_agent = self.get_object(pk) date_time = DateTime() if not profile_agent: self.logger.warning("message: profile id %s not exist" % (str(pk))) return HttpResponse(status=400) if profile_agent.video != data["video"]: profile_agent.video = data["video"] profile_agent.last_update = date_time.get_now() profile_agent.save() self.logger.debug("message: update video status --> success") if "Origin" in agent_name or "4500" in agent_name or "ott" in agent_name: """ Push to SCC """ self.logger.info("%s is core probe" % (agent_name)) if PUSH_ALARM_CORE: self.logger.info("Push alarm to scc is active") time.sleep(0.5) snmp = Snmp(str(data["ip"]).replace(' ', '')) alarm_status, msg = snmp.check_agent() data = { "ishost": False, "queueServiceName": "Check_Agent_IPTV_Status", "queueHost": agent_name, "msg": str(msg), "AlertStatus": alarm_status, "agentId": int(pk) } self.logger.debug("alarm contain: %s" % (str(data))) scc = Scc() scc.post(data) return HttpResponse(status=202) else: self.logger.info("%s is not core probe" % (agent_name)) if PUSH_ALARM_PROBE: self.logger.info("Push alarm to scc is active") time.sleep(0.5) snmp = Snmp(str(data["ip"]).replace(' ', '')) alarm_status, msg = snmp.check_agent() data = { "ishost": False, "queueServiceName": "Check_Agent_IPTV_Status", "queueHost": agent_name, "msg": str(msg), "AlertStatus": alarm_status, "agentId": int(pk) } self.logger.debug("alarm contain: %s" % (str(data))) scc = Scc() scc.post(data) return HttpResponse(status=202) else: self.logger.warning("Value not change: %s" % (str(pk))) return HttpResponse(status=400) # querry="update profile_agent set video=%s,last_update=unix_timestamp() where id=%s;"%(data['video'],pk) # RabbitMQQueue().push_query(querry) # return HttpResponse(status=202) #dropframe elif ('dropframe' in data) and len(data) == 1: self.logger.debug("message: update dropframe -->%s" % (str(data))) querry = "update profile_agent set dropframe=%s,last_update=unix_timestamp() where id=%s;" % ( data['dropframe'], pk) RabbitMQQueue().push_query(querry) self.logger.info( "message: update dropframe added to RabbitMQQueue: %s" % (querry)) return HttpResponse(status=202) #discontinuity elif ('discontinuity' in data) and len(data) == 1: self.logger.debug("message: update discontinuity -->%s" % (str(data))) querry = "update profile_agent set discontinuity=%s,last_update=unix_timestamp() where id=%s;" % ( data['discontinuity'], pk) RabbitMQQueue().push_query(querry) self.logger.info( "message: update dropframe added to RabbitMQQueue: %s" % (querry)) return HttpResponse(status=202) #analyzer_status elif ('analyzer_status' in data) and len(data) == 1: self.logger.debug("message: update analyzer_status -->%s" % (str(data))) querry = "update profile_agent set analyzer_status=%s,last_update=unix_timestamp() where id=%s;" % ( data['analyzer_status'], pk) RabbitMQQueue().push_query(querry) self.logger.info( "message: update dropframe added to RabbitMQQueue: %s" % (querry)) return HttpResponse(status=202) return HttpResponse(status=400)