12
12
import threading
13
13
import time
14
14
15
+ from socket import AF_INET , AF_INET6
15
16
from minigraph import parse_device_desc_xml
16
17
from portconfig import get_child_ports
17
18
from sonic_py_common import device_info , multi_asic
@@ -1814,6 +1815,21 @@ def vrf_add_management_vrf(config_db):
1814
1815
return None
1815
1816
config_db .mod_entry ('MGMT_VRF_CONFIG' , "vrf_global" , {"mgmtVrfEnabled" : "true" })
1816
1817
mvrf_restart_services ()
1818
+ """
1819
+ The regular expression for grep in below cmd is to match eth0 line in /proc/net/route, sample file:
1820
+ $ cat /proc/net/route
1821
+ Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
1822
+ eth0 00000000 01803B0A 0003 0 0 202 00000000 0 0 0
1823
+ """
1824
+ cmd = "cat /proc/net/route | grep -E \" eth0\s+00000000\s+[0-9A-Z]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+202\" | wc -l"
1825
+ proc = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE )
1826
+ output = proc .communicate ()
1827
+ if int (output [0 ]) >= 1 :
1828
+ cmd = "ip -4 route del default dev eth0 metric 202"
1829
+ proc = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE )
1830
+ proc .communicate ()
1831
+ if proc .returncode != 0 :
1832
+ click .echo ("Could not delete eth0 route" )
1817
1833
1818
1834
def vrf_delete_management_vrf (config_db ):
1819
1835
"""Disable management vrf in config DB"""
@@ -1833,6 +1849,8 @@ def snmpagentaddress(ctx):
1833
1849
config_db .connect ()
1834
1850
ctx .obj = {'db' : config_db }
1835
1851
1852
+ ip_family = {4 : AF_INET , 6 : AF_INET6 }
1853
+
1836
1854
@snmpagentaddress .command ('add' )
1837
1855
@click .argument ('agentip' , metavar = '<SNMP AGENT LISTENING IP Address>' , required = True )
1838
1856
@click .option ('-p' , '--port' , help = "SNMP AGENT LISTENING PORT" )
@@ -1842,13 +1860,43 @@ def add_snmp_agent_address(ctx, agentip, port, vrf):
1842
1860
"""Add the SNMP agent listening IP:Port%Vrf configuration"""
1843
1861
1844
1862
#Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|<port>|<vrf>
1863
+ if not clicommon .is_ipaddress (agentip ):
1864
+ click .echo ("Invalid IP address" )
1865
+ return False
1866
+ config_db = ctx .obj ['db' ]
1867
+ if not vrf :
1868
+ entry = config_db .get_entry ('MGMT_VRF_CONFIG' , "vrf_global" )
1869
+ if entry and entry ['mgmtVrfEnabled' ] == 'true' :
1870
+ click .echo ("ManagementVRF is Enabled. Provide vrf." )
1871
+ return False
1872
+ found = 0
1873
+ ip = ipaddress .ip_address (agentip )
1874
+ for intf in netifaces .interfaces ():
1875
+ ipaddresses = netifaces .ifaddresses (intf )
1876
+ if ip_family [ip .version ] in ipaddresses :
1877
+ for ipaddr in ipaddresses [ip_family [ip .version ]]:
1878
+ if agentip == ipaddr ['addr' ]:
1879
+ found = 1
1880
+ break ;
1881
+ if found == 1 :
1882
+ break ;
1883
+ else :
1884
+ click .echo ("IP addfress is not available" )
1885
+ return
1886
+
1845
1887
key = agentip + '|'
1846
1888
if port :
1847
1889
key = key + port
1890
+ #snmpd does not start if we have two entries with same ip and port.
1891
+ key1 = "SNMP_AGENT_ADDRESS_CONFIG|" + key + '*'
1892
+ entry = config_db .get_keys (key1 )
1893
+ if entry :
1894
+ ip_port = agentip + ":" + port
1895
+ click .echo ("entry with {} already exists " .format (ip_port ))
1896
+ return
1848
1897
key = key + '|'
1849
1898
if vrf :
1850
1899
key = key + vrf
1851
- config_db = ctx .obj ['db' ]
1852
1900
config_db .set_entry ('SNMP_AGENT_ADDRESS_CONFIG' , key , {})
1853
1901
1854
1902
#Restarting the SNMP service will regenerate snmpd.conf and rerun snmpd
0 commit comments