Skip to content

Commit

Permalink
when channel change status, update local snmp
Browse files Browse the repository at this point in the history
  • Loading branch information
hhthuongbtr committed Feb 7, 2018
1 parent 61d218c commit 8872043
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
8 changes: 8 additions & 0 deletions services/last_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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 += " "
Expand All @@ -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
"""
Expand Down
63 changes: 63 additions & 0 deletions services/snmp_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
6 changes: 6 additions & 0 deletions services/video_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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
"""
Expand Down
40 changes: 40 additions & 0 deletions utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8872043

Please sign in to comment.