Getting clear uptime data from Cisco router


Every larger IT company has some kind of infrastructure to monitor if routers are up or down. But in some companies, this data is relatively hard to get because the monitoring server can be hidden behind some ssl vpn gateways, and it takes up to 10 minutes to see if a router is/has been down, so nobody ever checks it. Due to how event engines are configured, if a router reloads, an incident will not be created and the support team will never know that a router has reloaded.

I don’t like being in the dark so I got down to work.

Of course, it is very easy to write up a script that gets output of show version from a cisco router to see the uptime. But the problem becomes more acute if you have hundreds of routers. Then you’d not be interested in plowing through hundreds of uptime lines, you’d want to get alarms only, like ”watch out, this router has been down recently because uptime is short”.
Because I will be running my script every now and again only, i’m only interested to see if the router has been down in the last 4 weeks so i cut off the details (days, minutes).

I rewrote the last script a bit to do just that: I take output of show version, find the uptime, turn uptime into a score and spit out the status: ok or not ok.

myrouters = {"R1": {"RouterOne": ""}, "R2": {"RouterTwo": ""}, "R3": {"RouterThree": ""}, "R4": {"RouterFour": ""}}

#this loop will go through the dictionary of dictionaries, connect to each router using its IP (subval), issue show version command
for mainkey in myrouters.keys():
    mainval = myrouters[mainkey]
    for subkey in mainval.keys():
      subval = mainval[subkey]
      print('i am now on router ' + subkey + ' , its IP address is ' + subval)
      net_connect.write_channel("connect " + subval + " \n")
      redispatch(net_connect, device_type="cisco_ios")
      result = net_connect.send_command_timing("show ver")

#here i initiate an empty list, because re.findall returns a list

      endresult = []
      endresult = re.findall(r'processor .*?minutes', result)
      endresultstring = endresult[1]

    #This string contains just the phrase 'processor is up 4 weeks'
    #next i query if the phrase contain the word 'week' and if it does, i take this string and make a list of digits from it, join the digits, thus making a number of weeks. There must be a better way to do this... 

      foundweeks = bool(re.findall(r'(week)', endresultstring))
      if foundweeks:
        numberweeks = re.findall(r'.*?weeks', endresultstring)
        listweeks = str(numberweeks)
        stringweeks = str(listweeks[2:23])
        processweeks = re.findall(r'\d', stringweeks)
        numberweeks = ''.join(map(str,processweeks))

    #here i calculate a score (weeks times 25)  
        finalscore = int(numberweeks)*25
        if finalscore>100:
            print("This router is just fine! It has been up over " + str(int(finalscore)/25) + " weeks")
            print("oh no! it's been down in the last 4 weeks! you'd better check this router")

And the output is as follows:

i am now on router R1 whose name is RouterOne and IP is
This router is just fine! It has been up over 11.0 weeks

i am now on router R2 whose name is RouterTwo and IP is
This router is just fine! It has been up over 41.0 weeks

i am now on router R3 whose name is RouterThree and IP is
This router is just fine! It has been up over 29.0 weeks

i am now on router R4 whose name is RouterFour and IP is
This router is just fine! It has been up over 28.0 weeks

Eventually I found this code to be so obnoxiously wordy that I rewrote the last part (taking the number of lines from 12 to 7) changing the logic slightly: if the word ‚week’ is found in the part of uptime output, the output will be empty; otherwise the router has been down lately and something is printed out, depending on whether the number of weeks is between 1 and 4, or below 1.

if bool(re.findall(r'(week)', endresultstring)):
   if int(str(re.findall(r'[0-9]+', endresultstring)[0])) > 4:
      print("oh no! it's been down in the last 4 weeks! you'd better check this router")
   print("This router has been down in the last week!!!")


Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:


Komentujesz korzystając z konta Wyloguj /  Zmień )

Zdjęcie na Google

Komentujesz korzystając z konta Google. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s