Interacting with the Collaboration Endpoint Api using Python – Configuring SNMP & Syslog

There are limitations on the values that can be configured via Cisco Unified Communications Manager Administration for Cisco Video Conference devices. For example, In order to configure SNMP monitoring you would need to login to the endpoint and configure the settings manually which can can take a considerable amount of time for a large number of endpoints.
The following code demonstrates how you can configure the appropriate settings using the Collaboration Endpoint Api.

Full Code
 
  1. from requests import Session
  2. import requests
  3. requests.packages.urllib3.disable_warnings()
  4. import xmltodict
  5. # Define the Api user credential
  6. user = "apiuser"
  7. # Define the Api Password Credential
  8. pwd = "apipass"
  9. # Define the url for the put request
  10. put_xml = "/putxml"
  11. # HTTP Content Header
  12. headers = {"Content-Type": "text/xml"}
  13. # Define the first SNMP Server
  14. snmp_server_1 = "10.10.10.1"
  15. # Define the second SNMP Server
  16. snmp_server_2 = "10.10.10.2"
  17. # Define the Syslog Server
  18. syslog_server= "10.10.10.3"
  19. # Define the Syslog Port
  20. syslog_port = "514"
  21. # Define the SNMP Read Only Community
  22. snmp_ro_community = "mysnmpstring"
  23. # Define the SNMP Mode
  24. snmp_mode = "ReadOnly"
  25. # Define our Logging Structure to hold the parameters
  26. logging = {
  27. "Configuration": {
  28. "Logging": {
  29. # The Configuration Section for external logging
  30. "External": {
  31. # Select mode ON
  32. "Mode": "On",
  33. # Use standard Syslog / no TLS
  34. "Protocol": "Syslog",
  35. # Disable TlsVerify for cosmetic purposes
  36. "TlsVerify": "Off",
  37. # Configure the Server Address and the Port
  38. "Server": {
  39. "Address": syslog_server,
  40. "Port": syslog_port
  41. }
  42. }
  43. }
  44. }
  45. }
  46. # Define our Snmp Structure to hold the parameters
  47. snmp = {
  48. "Configuration": {
  49. "NetworkServices": {
  50. "SNMP": {
  51. # Configure the SNMP Read Only Community
  52. "CommunityName": snmp_ro_community,
  53. # Configure the SNMP hosts that can talk to the device
  54. "Host 1 Address": snmp_server_1,
  55. "Host 2 Address": snmp_server_2,
  56. # Configure the SNMP Mode
  57. "Mode": snmp_mode,
  58. }
  59. }
  60. }
  61. }
  62. # Define a function to configure the logging parameters
  63. def configure_logging(vc):
  64. # Build the url
  65. url = "https://" + vc + put_xml
  66. # Make the POST request
  67. r = requests.post(
  68. # Build the url
  69. url=url,
  70. # Add the Content Headers
  71. headers=headers,
  72. # Disable certificate validation
  73. verify=False,
  74. # Provide the Basic auth credentials
  75. auth=(user, pwd),
  76. # Parse the dictionary structure and add the generate XML Body to the request
  77. data=xmltodict.unparse(logging)
  78. )
  79. # Print the output
  80. print(r.status_code, r.text)
  81. # Define a function to configure the logging parameters
  82. def configure_snmp(vc):
  83. # Build the url
  84. url = "https://" + vc + put_xml
  85. # Make the POST request
  86. r = requests.post(
  87. # Build the url
  88. url=url,
  89. # Add the Content Headers
  90. headers=headers,
  91. # Disable certificate validation
  92. verify=False,
  93. # Provide the Basic auth credentials
  94. auth=(user, pwd),
  95. # Parse the dictionary structure and add the generate XML Body to the request
  96. data=xmltodict.unparse(snmp)
  97. )
  98. # Print the output
  99. print(r.status_code, r.text)
  100. def main():
  101. # Define the input filename of ip addresses
  102. file_name = "vc1.txt"
  103. # Open the input file
  104. print("Opening input file .. {}".format(file_name))
  105. vc_hosts = open(file_name, "r").read().split("\n")
  106. # Iterate through our Hosts
  107. for vc in vc_hosts:
  108. print("#" * 100)
  109. # Create a Try & Accept Loop To Catch non responsive devices
  110. try:
  111. print("Configuring SNMP & Syslog on device ....'{}'".format(vc))
  112. # Call the Configure Logging & Syslog Functions
  113. configure_logging(vc)
  114. configure_snmp(vc)
  115. except Exception as e:
  116. print(str(e))
  117. else:
  118. # When we have a compatible session
  119. print("Configuration completed successfully...")
  120. if __name__ == "__main__":
  121. main()
Mitch

Author Mitch

I am an independent IT Consultant with specialities in Software Development, Enterprise Unified Communication, Network and Security platforms. In addition to my day to day work, I develop bespoke applications and I hope that through ucdevops.com I can build relationships with clients, business partners and fellow engineers by providing solutions to complex problems through the use of programming.

More posts by Mitch
"