import datetime
import os

class Data:
    oid = ''
    line = ''
    oidnum = []

def snmp_oid_compare(in_name1, len1, in_name2, len2):
    name1 = in_name1
    name2 = in_name2

    # len = minimum of len1 and len2
    len_val = min(len1, len2)

    # find first non-matching OID
    for _ in range(len_val):
        if name1[0] != name2[0]:
            return -1 if name1[0] < name2[0] else 1
        name1 = name1[1:]
        name2 = name2[1:]

    # both OIDs equal up to length of shorter OID
    if len1 < len2:
        return -1
    elif len2 < len1:
        return 1
    else:
        return 0

dir_dump = "./dumps"

def load_dumpfile(lst):
    if not os.path.isdir(dir_dump):
        print("\n dir [./dumps] not found. exit.")
        exit(0)

    filelist = os.listdir(dir_dump)
    for li in filelist:
        fullpath = dir_dump + "/" + li
        f = open(fullpath, 'r')
        lines = f.readlines()
        i = 0
        for line in lines:
            i += 1
            if 'No Such Object' in line or line[0] != '.':
                print("[skip] not start dot(.) or failed oid: {}".format(line))
                continue
            tokens = line.split("=")

            data = Data()
            data.oid = tokens[0].rstrip()
            data.line = line
            data.oidnum = list(map(int, filter(None, data.oid.split("."))))
            lst.append(data)
        f.close()

def quicksort_oid(lst):
    if len(lst) <= 1:
        return lst
    else:
        pivot = lst[len(lst) // 2]
        less = [x for x in lst if snmp_oid_compare(x.oidnum, len(x.oidnum), pivot.oidnum, len(pivot.oidnum)) < 0]
        equal = [x for x in lst if snmp_oid_compare(x.oidnum, len(x.oidnum), pivot.oidnum, len(pivot.oidnum)) == 0]
        greater = [x for x in lst if snmp_oid_compare(x.oidnum, len(x.oidnum), pivot.oidnum, len(pivot.oidnum)) > 0]
        return quicksort_oid(less) + equal + quicksort_oid(greater)

def main():
    my_list = []
    t1 = datetime.datetime.now()
    load_dumpfile(my_list)
    t2 = datetime.datetime.now()
    print('\nDuration load_dumpfile: {}'.format(t2 - t1))

    sorted_list = quicksort_oid(my_list)
    t3 = datetime.datetime.now()
    print('\nDuration quicksort_oid: {}'.format(t3 - t2))

    print("\n")
    i = 0
    file_path = "output.txt"
    with open(file_path, "w") as file:
        for item in sorted_list:
            i += 1
            file.write(f"{item.line}")

    print('Total process lines : {}'.format(i))

if __name__ == "__main__":
    start_time = datetime.datetime.now()
    main()
    end_time = datetime.datetime.now()
    print('Duration: {}'.format(end_time - start_time))

'까벨로퍼 > PYTHON' 카테고리의 다른 글

[python] snmp oid 정렬 프로그램  (0) 2023.12.13
[python] oid sort  (0) 2023.03.13
[python] json 에서 특정 키가 존재하는지  (0) 2022.06.09

요구 : 디렉토리내 모든 snmp dump 파일을 읽어서 oid 별로 정렬하여 하나의 파일로 생성

import datetime
import os

class Data:
    oid = ''
    line = ''

def snmp_oid_compare(in_name1, len1, in_name2, len2):
    name1 = in_name1
    name2 = in_name2

    # len = minimum of len1 and len2
    len_val = min(len1, len2)

    # find first non-matching OID
    for _ in range(len_val):
        if name1[0] != name2[0]:
            return -1 if name1[0] < name2[0] else 1
        name1 = name1[1:]
        name2 = name2[1:]

    # both OIDs equal up to length of shorter OID
    if len1 < len2:
        return -1
    elif len2 < len1:
        return 1
    else:
        return 0

dir_dump = "./dumps"
def main():
    my_list = []

    if not os.path.isdir(dir_dump):
        print("\n dir [./dumps] not found. exit.")
        exit(0)

    filelist = os.listdir(dir_dump)
    for li in filelist:
        fullpath = dir_dump + "/" + li
        f = open(fullpath, 'r')
        lines = f.readlines()
        i = 0
        for line in lines:
            i += 1
            if 'No Such Object' in line or line[0] != '.':
                print("[skip] not start dot(.) or failed oid: {}".format(line))
                continue
            tokens = line.split("=")

            data = Data()
            data.oid = tokens[0].rstrip()
            data.line = line
            new_oid = list(map(int, filter(None, data.oid.split("."))))
            found = False
            for j, item in enumerate(my_list):
                stored_oid = list(map(int, filter(None, item.oid.split("."))))
                result = snmp_oid_compare( stored_oid, len(stored_oid), new_oid, len(new_oid))
                if result >= 0:
                    my_list.insert(j, data)
                    found = True
                    print("정렬 line:%s,idx:%s,new:%s,old:%s" % (i, j, data.oid, item.oid))
                    break
            if not found:
                my_list.append(data)
        f.close()

    print("\n")
    i = 0
    file_path = "output.txt"
    with open(file_path, "w") as file:
        for item in my_list:
            i += 1
            file.write(f"{item.line}")

    print('Total process lines : {}'.format(i))

if __name__ == "__main__":
    start_time = datetime.datetime.now()
    main()
    end_time = datetime.datetime.now()
    print('Duration: {}'.format(end_time - start_time))
import requests, json, urllib3, datetime, sys
import os
import re

class Line:
    def __init__(self, line):
        self.no=0
        self.line=line


dir_dump = "./dumps"

def main():
    lst = []
    dic = dict()

    if os.path.isdir(dir_dump) == False:
        print("\n dir [./dumps] not found. exit.")
        exit(0)

    filelist = os.listdir(dir_dump)
    for li in filelist:
        fullpath = dir_dump + "/" + li
        f = open(fullpath, 'r')
        lines = f.readlines()
        for line in lines:
            if 'No Such Object' in line or line[0] != '.':
                print("[skip] not start dot(.) or failed oids: {}".format(line))
                continue
            tokens = line.split("=")
            keys = tokens[0].strip()
            keys = keys[1:]
            o = Line(line)
            dic[keys] = o
        f.close()
    
    # list로 변환
    lst = [*dic]
    # oid 문자열을 숫자 기반으로 정렬
    lst = sorted(lst, key=lambda y: tuple(int(x) for x in y.split('.')))
    print(lst)
    for index, value in enumerate(lst):
        if dic.get(value) is not None:
            dic[value].no = index

    # 키로 정렬
    sorted_dict = sorted(dic.items(), key = lambda item: item[1].no, reverse = False)
    print("\n")
    i = 0
    wf = open("result.txt", 'w')
    for li in sorted_dict:
        i = i+1
        wf.write(li[1].line)
    wf.close()

    print('Total process lines : {}'.format(i))

if __name__ == "__main__":
    start_time = datetime.datetime.now()
    main()
    end_time = datetime.datetime.now()

    print('Duration: {}'.format(end_time - start_time))


def _is_json_key(json, key):
    try:
        tmp = json[key]
    except KeyError:
        return False
    return True

 

'까벨로퍼 > PYTHON' 카테고리의 다른 글

[python] quick sort 를 사용한 snmp oid 정렬 예제  (0) 2023.12.13
[python] snmp oid 정렬 프로그램  (0) 2023.12.13
[python] oid sort  (0) 2023.03.13

+ Recent posts