Getting clear uptime data from Cisco router

Hello

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": "172.16.0.1"}, "R2": {"RouterTwo": "172.16.0.2"}, "R3": {"RouterThree": "172.16.0.3"}, "R4": {"RouterFour": "172.16.0.4"}}

#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")
      time.sleep(3)
      redispatch(net_connect, device_type="cisco_ios")
      net_connect.enable
      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)
      endresult.insert(0,subkey)
      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")
        else:
            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 172.16.0.1
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 172.16.0.2
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 172.16.0.3
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 172.16.0.4
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:
      pass
   else:
      print("oh no! it's been down in the last 4 weeks! you'd better check this router")
else:
   print("This router has been down in the last week!!!")

Skomentuj

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

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. 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