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

+ Recent posts