What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
300.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Pure Python Script for BGP Peer State Sensor

Votes:

1

Your Vote:

Up

Down

This is a pure python script for polling BGP Peer states where the BGP4-MIB is available for polling.
It will report the current state of all BGP peers, and send a warning if any state other than "ESTABLISHED" is polled. This has been tested against Cisco routers , but it should work for any router/switch running IEEE MIBS
Disclaimer: Use at your own risk, preferably after testing thoroughly.
This script was tested on the local probe. See PRTG documentation for running python scripts on a remote probe

Steps:

Pip upgrade and install pysnmp
Open a command prompt, change directory to the following: cd C:\Program Files (x86)\PRTG Network Monitor\python\Scripts Run the following commands to upgrade pip and install pysnmp and pysnmp SMIs pip install pip --upgrade pip install pysnmp pysmi
Copy the following text into a text file, and save it as C:\Program Files (x86)\PRTG Network Monitor\lookups\custom\customsensor.bgp4.peerstatus.ovl

<?xml version="1.0" encoding="UTF-8"?>
<ValueLookup id="customsensor.bgp4.peerstatus" desiredValue="6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PaeValueLookup.xsd">
	<Lookups>
		<SingleInt state="Error" value="1">Idle</SingleInt>
		<SingleInt state="Error" value="2">Connect</SingleInt>
		<SingleInt state="Error" value="3">Active</SingleInt>
		<SingleInt state="Error" value="4">Open Sent</SingleInt>
		<SingleInt state="Error" value="5">Open Confirm</SingleInt>
		<SingleInt state="Ok" value="6">Established</SingleInt>
	</Lookups>
</ValueLookup>

Load Lookups in the PRTG Web Dashboard:
Setup > System Administration > Load Lookups and File Lists > Go!

Copy the following text into a text file, and save it as C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\python\bgp_peers.py

from pysnmp.hlapi import *
from prtg.sensor.result import CustomSensorResult
from prtg.sensor.units import ValueUnit
import sys
import re
import json

prtg_params = json.loads(sys.argv[1])


def bgp_peer_walk(host):
  oid = '.1.3.6.1.2.1.15.3.1.2'
  reg_ex = re.compile('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ = [1-6]')
  bgp_peer_dict = {}

  for (errorIndication,
   errorStatus,
   errorIndex,
   varBinds) in nextCmd(SnmpEngine(), 
                        CommunityData(prtg_params['snmpcommv2']),
                        UdpTransportTarget((host, 161)),
                        ContextData(),                                                           
                        ObjectType(ObjectIdentity(oid)),
                        lexicographicMode=False):
    if errorIndication:
      csr = CustomSensorResult(text="Python Script pysnmp error")
      csr.error = f"Python Script pysnmp error: {errorIndication}"
      print(csr.json_result)
      sys.exit()
    elif errorStatus:
      csr = CustomSensorResult(text="Python Script pysnmp error")
      csr.error = f"Python Script pysnmp error: {errorStatus}"
      print(csr.json_result)            
      sys.exit()
    else:
      for varBind in varBinds:
        bgp_peers = reg_ex.search(str(varBind))
        ip, state = str(bgp_peers.group(0)).split(' = ')
        bgp_peer_dict[ip] = state
  return(bgp_peer_dict)

def build_csr(peer_dict):
  try:
    csr = CustomSensorResult()
    for peer in peer_dict:
      if peer_dict[peer] is '6':
        csr.add_channel(name = peer, value = 6, unit = ValueUnit.CUSTOM, is_warning = 0, value_lookup="customsensor.bgp4.peerstatus")
      if peer_dict[peer] is '5':
        csr.add_channel(name = peer, value = 5, unit = ValueUnit.CUSTOM, is_warning = 1, value_lookup="customsensor.bgp4.peerstatus")
      if peer_dict[peer] is '4':
        csr.add_channel(name = peer, value = 4, unit = ValueUnit.CUSTOM, is_warning = 1, value_lookup="customsensor.bgp4.peerstatus")
      if peer_dict[peer] is '3':
        csr.add_channel(name = peer, value = 3, unit = ValueUnit.CUSTOM, is_warning = 1, value_lookup="customsensor.bgp4.peerstatus")
      if peer_dict[peer] is '2':
        csr.add_channel(name = peer, value = 2, unit = ValueUnit.CUSTOM, is_warning = 1, value_lookup="customsensor.bgp4.peerstatus")
      if peer_dict[peer] is '1':
        csr.add_channel(name = peer, value = 1, unit = ValueUnit.CUSTOM, is_warning = 1, value_lookup="customsensor.bgp4.peerstatus")
    print(csr.json_result)
  except Exception as e:
    csr = CustomSensorResult(text="Python build_csr execution error")
    csr.error = f"Python Script execution error: {e}"
    print(csr.json_result)

build_csr(bgp_peer_walk(prtg_params['host']))


To setup the sensor, Click Add Sensor, and search for Python Script Advanced.
On the configuration page, rename the Sensor, select the Transmit SNMP credentials bullet, and click save.

bgp bgp-peer custom-python-script python-script-advanced-sensor

Created on Mar 23, 2021 4:12:32 PM by  Ted Theodore Logan (1) 1

Last change on Sep 28, 2021 12:37:00 PM by  Luciano Lingnau [Paessler]



1 Reply

Votes:

0

Your Vote:

Up

Down

Hello Ted,

Thank you very much for sharing your script with the community.

Have a great day.

Regards.

Created on Mar 24, 2021 6:37:01 AM by  Florian Lesage [Paessler Support]



Please log in or register to enter your reply.


Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.