-1

i need to create a top 5 of interfaces that went up/down based on %LINK-3-UPDOWN from a log file. and also need to count the amount of ICMP packets that are stopped based on amount of %SEC-6-IPACCESSLOGDP. log file looks like this:

Sep 22 15:12:09 145.89.109.1 : %SEC-6-IPACCESSLOGP: list 120 denied tcp 80.82.77.33(0) -> 145.89.109.49(0), 1 packet
Sep 22 16:11:15 145.89.109.11 28w6d: %LINK-3-UPDOWN: Interface GigabitEthernet1/20, changed state to up
Sep 22 16:11:15 145.89.109.11 28w6d: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet1/20, changed state to up
Sep 22 15:16:09 145.89.109.1 : %SEC-6-IPACCESSLOGP: list 120 denied tcp 216.158.238.186(0) -> 145.89.109.49(0), 1 packet
Sep 22 15:17:10 145.89.109.1 : %SEC-6-IPACCESSLOGP: list 120 denied tcp 184.105.139.98(0) -> 145.89.109.49(0), 1 packet
Sep 22 15:22:10 145.89.109.1 : %SEC-6-IPACCESSLOGS: list 78 denied 145.89.110.15 1 packet
Sep 22 16:20:46 145.89.109.11 28w6d: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet1/20, changed state to down
Sep 22 16:20:46 145.89.109.11 28w6d: %LINK-3-UPDOWN: Interface GigabitEthernet1/20, changed state to down

My code is as followed but i am not getting the result i want:

infile = open("router1.log","r")              #Open log bestand in "read modus"
dictionary = {}                               #Maak lege dictionary aan
for line in infile:                           #For-loop die elke regel afgaat in log-bestand
    try:
        naam = line.split(":")[3]             #variable naam die regel split naar een lijst met index 3
        naam2 = line.split(":")[4]            #variable naam die regel split naar een lijst met index 4
        if naam.strip()in dictionary.keys():  #"Als" naam zich bevindt in dictionary voer onderstaande uit:
            dictionary[naam.strip()]+=1
        else:                                 #Anders voer onderstaan uit:
            dictionary[naam.strip()]=0
    except:
        continue
recsto
  • 1
  • 2
  • 1
    Please put in an effort and at least translate the comments so that everyone understand what you want. Also post the output that you would expect. – Bram Vanroy Jun 17 '20 at 09:54
  • Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – Roshin Raphel Jun 17 '20 at 10:15
  • Get rid of all excessive code first: you don't need the `try-except` clause (it will hide any errors in the `try` section making it very hard to debug your code), you are not using `naam2` at all. Instead of using `naam.strip()` three times, you can do it once upfront. This all makes it much easier for yourself. Also, you need to close your file, or use `with open('router1.log', 'r') as infile:` instead (to close it automatically). – Ronald Jun 17 '20 at 10:47

1 Answers1

0

If I had interpreted your question correctly, your problem here is that you are unable to obtain the correct count values.

To resolve that, you want set your first occurrence of a particular log issue to value 1 instead of 0. So your else statement should be:

else:
    dictionary[naam.strip()]=1

If you do not do that your counts will always be lesser by 1, hope that helps!