From a9bcac04c8070c1f0ed008243fec3adb057e6bd6 Mon Sep 17 00:00:00 2001 From: Danealau Date: Fri, 23 Jul 2021 16:26:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=D0=BB=D0=B8=D0=B7=20=D0=BB=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - логи теперь анализируются за период, а не с определенного времени --- bin/sdr.py | 65 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/bin/sdr.py b/bin/sdr.py index c7da202..424c0be 100755 --- a/bin/sdr.py +++ b/bin/sdr.py @@ -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 @@ -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 # ######################################################################################################################## @@ -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() @@ -274,24 +274,29 @@ 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): @@ -299,16 +304,19 @@ def update(cls): 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 ######################################################################################################################## @@ -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: @@ -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]] @@ -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]] @@ -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 = {} @@ -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()