Skip to content

Commit

Permalink
Изменен анализ логов
Browse files Browse the repository at this point in the history
- логи теперь анализируются за период, а не с определенного времени
  • Loading branch information
Danealau authored and Danealau committed Jul 23, 2021
1 parent e2b16a9 commit a9bcac0
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions bin/sdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import traceback
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from datetime import datetime, timedelta
from enum import Enum
from telnetlib import Telnet
from typing import Optional, List
Expand Down Expand Up @@ -43,7 +43,6 @@ def last_seen_int(self):
return int(self.last_seen) if self.last_seen.isnumeric() else 0



########################################################################################################################
# For process call logs #
########################################################################################################################
Expand Down Expand Up @@ -264,6 +263,7 @@ def start_calls(cls):
@classmethod
def stop_calls(cls):
since = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
until = since
try:
with open(cls.__FILE_NAME, "r") as f:
lines = f.readlines()
Expand All @@ -274,41 +274,49 @@ def stop_calls(cls):
pass

with open(cls.__FILE_NAME, "w") as f:
f.writelines([cls.__STOP_STATUS, "\n", since])
f.writelines([cls.__STOP_STATUS, "\n", since, "\n", until])

@classmethod
def get_since_data(cls):
def get_period(cls):
since = None
until = None
try:
with open(cls.__FILE_NAME, "r") as f:
lines = f.readlines()
if len(lines) == 2:
return lines[1].strip()

if len(lines) >= 2:
since = lines[1].strip()
if lines[0].strip() == cls.__STOP_STATUS and len(lines) == 3:
until = lines[2].strip()
until = datetime.strptime(until, "%Y-%m-%d %H:%M:%S") + timedelta(seconds=30)
until = until.strftime("%Y-%m-%d %H:%M:%S")
except IOError:
pass

return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return since, until


class SmsTimestamp:
__FILE_NAME = os.path.dirname(os.path.abspath(__file__)) + "/sms_timestamp"
__sms_period_time = 30

@classmethod
def update(cls):
with open(cls.__FILE_NAME, "w") as f:
f.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

@classmethod
def get_since_data(cls):
def get_period(cls):
since = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
with open(cls.__FILE_NAME, "r") as f:
lines = f.readlines()
if len(lines) == 1:
return lines[0].strip()
since = lines[0].strip()
except IOError:
pass

return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
until = datetime.strptime(since, "%Y-%m-%d %H:%M:%S") + timedelta(seconds=cls.__sms_period_time)
until = until.strftime("%Y-%m-%d %H:%M:%S")
return since, until


########################################################################################################################
Expand Down Expand Up @@ -536,7 +544,7 @@ def _get_filtered_subscribers(self, exclude=False, include=False, exclude_2sim=T
exclude_2sim_list = []
if exclude_2sim:
for idx, subscriber_1 in enumerate(all_subscibers):
for subscriber_2 in all_subscibers[idx+1:]:
for subscriber_2 in all_subscibers[idx + 1:]:
diff_cnt = sum([1 if subscriber_1.imei[ch_idx] != subscriber_2.imei[ch_idx] else 0 for ch_idx
in range(len(subscriber_1.imei))])
if diff_cnt <= 2:
Expand Down Expand Up @@ -770,13 +778,18 @@ def _process_logs(self, lines: List[str]):
return all_logs

def calls_status(self):
since = CallTimestamp.get_since_data()
result_records = {}
since, until = CallTimestamp.get_period()

if since is None:
return result_records

res = subprocess.run(["bash", "-c", f"journalctl -u osmo-msc --since='{since}'"], capture_output=True)
until_str = "" if until is None else f"--until='{until}'"
res = subprocess.run(["bash", "-c", f"journalctl -u osmo-msc --since='{since}' {until_str}"],
capture_output=True)
lines = res.stdout.decode("UTF-8").split("\n")
records = self._process_logs(lines)

result_records = {}
if len(records) > 0:
last_record = records[list(records.keys())[-1]]

Expand All @@ -790,13 +803,18 @@ def calls_status(self):
return result_records

def calls_status_show(self):
since = CallTimestamp.get_since_data()
result_records = {}
since, until = CallTimestamp.get_period()

if since is None:
return result_records

res = subprocess.run(["bash", "-c", f"journalctl -u osmo-msc --since='{since}'"], capture_output=True)
until_str = "" if until is None else f"--until='{until}'"
res = subprocess.run(["bash", "-c", f"journalctl -u osmo-msc --since='{since}' {until_str}"],
capture_output=True)
lines = res.stdout.decode("UTF-8").split("\n")
records = self._process_logs(lines)

result_records = {}
if len(records) > 0:
last_record = records[list(records.keys())[-1]]

Expand All @@ -805,10 +823,11 @@ def calls_status_show(self):
return result_records

def sms_statuses(self):
since = SmsTimestamp.get_since_data()
since, until = SmsTimestamp.get_period()

res = subprocess.run(["bash", "-c", f"journalctl -u osmo-msc --since='{since}' | grep 'stat:DELIVRD'"],
capture_output=True)
res = subprocess.run(
["bash", "-c", f"journalctl -u osmo-msc --since='{since}' --until='{until}' | grep 'stat:DELIVRD'"],
capture_output=True)
lines = res.stdout.decode("UTF-8").split("\n")
records = {}

Expand Down Expand Up @@ -993,7 +1012,7 @@ def handover(self):
subparsers.add_parser("bts", help="get active bts")
subparsers.add_parser("channels", help="get total tch/f channel count")
subparsers.add_parser("handover", help="Do handover")
ho_parser = subparsers.add_parser("ho_count", help="Do handover")
ho_parser = subparsers.add_parser("ho_count", help="Set need handover count")
ho_parser.add_argument("count", help="need handover count", type=int)

args = arg_parser.parse_args()
Expand Down

0 comments on commit a9bcac0

Please sign in to comment.