diff --git a/services/__init__.py b/services/__init__.py index 1957165..db466fa 100755 --- a/services/__init__.py +++ b/services/__init__.py @@ -8,7 +8,7 @@ from .last_check import LastCheck from .video_check import VideoCheck from .monitor import Monitor -from .snmp_agent import Snmp +from .snmp_agent import Snmp, AgentSnmp # Create the Logger logger = logging.getLogger(__name__) diff --git a/services/last_check.py b/services/last_check.py index cee1a6f..31a892b 100755 --- a/services/last_check.py +++ b/services/last_check.py @@ -8,6 +8,7 @@ from BLL.log import Log as LogBLL from config.config import SYSTEM from BLL.profile import Profile as ProfileBLL +from services import AgentSnmp as LocalSnmp class LastCheck(object): """docstring for LastCheck""" @@ -43,6 +44,7 @@ def check_source(self, source, last_status, id, agent, name, type): 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 += " " @@ -58,6 +60,12 @@ def check_source(self, source, last_status, id, agent, name, type): 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 = source + "-" + type, name = channel, status = check) + child_thread = threading.Thread(target=local_snmp.set) + child_thread.start() + child_thread_list.append(child_thread) + """ Wait for update database complete """ diff --git a/services/snmp_agent.py b/services/snmp_agent.py index e82d8ba..15342ea 100755 --- a/services/snmp_agent.py +++ b/services/snmp_agent.py @@ -4,6 +4,7 @@ from config.config import SYSTEM from utils import DateTime from BLL.profile import Snmp as SnmpBLL +from utils.file import Snmp as LocalSnmp class Snmp(object): """docstring for Snmp""" @@ -128,3 +129,65 @@ def set(self): else: self.create_snmp_at_broadcast_timeout() +class AgentSnmp(object): + def __init__(self, profile = None, name = None, status = None): + self.logger = logging.getLogger(__name__) + self.profile = profile + self.name = name + self.status = status + + def get_line_posision(self): + snmp = LocalSnmp() + profile_list = snmp.read_profile() + count = 0 + for line in profile_list.split('\n'): + line = line.strip() + count += 1 + if line == self.profile: + break + return count + + def update_profile(self, posision = None): + snmp = LocalSnmp() + channel_list = snmp.read_profile() + new_channel_list = "" + count = 0 + for line in channel_list.split('\n'): + line = line.strip() + count += 1 + if count != posision and line: + new_channel_list = new_channel_list + line + "\n" + new_channel_list = new_channel_list + self.profile + return snmp.update_profile(new_channel_list) + + def update_status(self, posision = None): + snmp = LocalSnmp() + status_list = snmp.read_status() + new_status_list = "" + count = 0 + for line in status_list.split('\n'): + line = line.strip() + count += 1 + if count != posision and line: + new_status_list = new_status_list + line + "\n" + new_status_list = new_status_list + str(self.status) + return snmp.update_status(new_status_list) + + def update_name(self, posision = None): + snmp = LocalSnmp() + name_list = snmp.read_name() + new_name_list = "" + count = 0 + for line in name_list.split('\n'): + line = line.strip() + count += 1 + if count != posision and line: + new_name_list = new_name_list + line + "\n" + new_name_list = new_name_list + self.name + return snmp.update_name(new_name_list) + + def set(self): + posision = self.get_line_posision() + self.update_profile(posision) + self.update_status(posision) + self.update_name(posision) diff --git a/services/video_check.py b/services/video_check.py index 7ff53ab..ae8a05e 100755 --- a/services/video_check.py +++ b/services/video_check.py @@ -13,6 +13,7 @@ from BLL.profile import Profile as ProfileBLL from BLL.log import Log as LogBLL from config.config import SYSTEM +from services import AgentSnmp as LocalSnmp class VideoCheck(object): """docstring for VideoCheck""" @@ -76,6 +77,11 @@ def update_data(self, video_status, source_status): 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 """ diff --git a/utils/file.py b/utils/file.py index 5614735..aeaecf4 100755 --- a/utils/file.py +++ b/utils/file.py @@ -43,3 +43,43 @@ def append(self, text): else: print "replicate" return 1 + +class Snmp: + def __init__(self): + self.channel_status = "snmp/agent/channel_status" + self.channel_name = "snmp/agent/channel_name" + self.channel_profile = "snmp/agent/channel_profile" + + def read_profile(self, file_path = None): + file_path = self.channel_profile + with open(file_path, "r") as text_file: + return text_file.read() + + def read_status(self, file_path = None): + file_path = self.channel_status + with open(file_path, "r") as text_file: + return text_file.read() + + def read_name(self, file_path = None): + file_path = self.channel_name + with open(file_path, "r") as text_file: + return text_file.read() + + def update_profile(self, text = None): + f = open(self.channel_profile, 'w') + f.write(text) + f.close() + return 0 + + def update_status(self, text = None): + f = open(self.channel_status, 'w') + f.write(text) + f.close() + return 0 + + def update_name(self, text = None): + f = open(self.channel_name, 'w') + f.write(text) + f.close() + return 0 +