xref: /openbmc/openbmc-test-automation/lib/snmp/redfish_snmp_utils.robot (revision 037407d32b7ca72f455e7daf70bf8db5502ddd0c)
1Documentation  Utility for SNMP configurations via Redfish.
2
3*** Settings ***
4
5Resource                ../../lib/utils.robot
6Resource                ../../lib/connection_client.robot
7Library                 ../../lib/gen_misc.py
8Library                 ../../lib/utils.py
9
10
11*** Keywords ***
12
13Get SNMP Manager List
14    [Documentation]  Get the list of SNMP managers and return IP addresses and ports.
15
16    # Get the list of SNMP manager URIs.
17    @{snmp_mgr_uris}=  Get SNMP Child URIs
18
19    ${snmp_mgr_list}=  Create List
20
21    FOR  ${snmp_mgr_uri}  IN  @{snmp_mgr_uris}
22      # Sample output:
23      # {
24      #  "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp1",
25      #  "@odata.type": "#EventDestination.v1_7_0.EventDestination",
26      #  "Context": "",
27      #  "Destination": "snmp://xx.xx.xx.xx:162",
28      #  "EventFormatType": "Event",
29      #  "Id": "snmp1",
30      #  "Name": "Event Destination snmp1",
31      #  "Protocol": "SNMPv2c",
32      #  "SubscriptionType": "SNMPTrap"
33
34      ${resp}=  Redfish.Get  ${snmp_mgr_uri}
35      ${snmp_mgr}=  Get From Dictionary  ${resp.dict}  Destination
36      Append To List  ${snmp_mgr_list}  ${snmp_mgr}
37    END
38
39    [Return]  ${snmp_mgr_list}
40
41
42Configure SNMP Manager Via Redfish
43    [Documentation]  Configure SNMP manager on BMC via Redfish.
44    [Arguments]  ${snmp_mgr_ip}  ${snmp_port}  ${valid_status_codes}=${HTTP_CREATED}
45
46    # Description of argument(s):
47    # snmp_mgr_ip  SNMP manager IP address
48    # snmp_port  SNMP manager port
49    # valid_status_code  expected code
50
51    ${snmp_mgr_data}=  Create Dictionary  Destination=snmp://${snmp_mgr_ip}:${snmp_port}
52    ...  SubscriptionType=${snmp_function}  Protocol=${snmp_version}
53
54    Redfish.Post  ${subscription_uri}  body=&{snmp_mgr_data}
55    ...  valid_status_codes=[${valid_status_codes}]
56
57
58Verify SNMP Manager Configured On BMC
59    [Documentation]  Verify SNMP manager configured on BMC.
60    [Arguments]  ${snmp_mgr_ip}  ${snmp_port}
61
62    # Description of argument(s):
63    # snmp_mgr_ip  SNMP manager IP address
64    # snmp_port  SNMP manager port
65
66    # Get the list of SNMP managers that are configured on BMC.
67    @{snmp_mgr_list}=  Get SNMP Manager List
68
69    ${snmp_ip_port}=  Catenate  ${snmp_mgr_ip}:${snmp_port}
70
71    List Should Contain Value  ${snmp_mgr_list}  snmp://${snmp_ip_port}
72    ...  msg=SNMP manager is not configured.
73
74
75Get SNMP Child URIs
76    [Documentation]  Get the list of all SNMP manager URIs.
77
78    # Sample output of SNMP URI:
79    # {
80    #  "@odata.id": "/redfish/v1/EventService/Subscriptions",
81    #  "@odata.type": "#EventDestinationCollection.EventDestinationCollection",
82    #  "Members": [
83    #    {
84    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp6"
85    #    },
86    #    {
87    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp2"
88    #    },
89    #    {
90    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp9"
91    #    },
92    #    {
93    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp1"
94    #    },
95    #    {
96    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp8"
97    #    },
98    #    {
99    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp4"
100    #    },
101    #    {
102    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp7"
103    #    },
104    #    {
105    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp5"
106    #    },
107    #    {
108    #      "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp3"
109    #    }
110    #  ],
111    #  "Members@odata.count": 9,
112    #  "Name": "Event Destination Collections"
113
114    # Get the list of child URIs.
115    @{snmp_mgr_uris}=  Redfish.Get Members List  ${subscription_uri}  filter=snmp
116
117    [Return]  ${snmp_mgr_uris}
118
119
120Delete SNMP Manager Via Redfish
121    [Documentation]  Delete SNMP manager.
122    [Arguments]  ${snmp_mgr_ip}  ${snmp_port}
123
124    # Description of argument(s):
125    # snmp_mgr_ip  SNMP manager IP.
126    # snmp_port    Network port where SNMP manager is listening.
127
128    ${is_snmp_found}=  Set Variable  ${False}
129    ${snmp_ip_port}=  Catenate  ${snmp_mgr_ip}:${snmp_port}
130
131    # Get the list of SNMP manager URIs.
132    @{snmp_mgr_uris}=  Get SNMP Child URIs
133
134    # Find the SNMP manager URI that has IP and port configured.
135    FOR  ${snmp_mgr_uri}  IN  @{snmp_mgr_uris}
136      # Sample output:
137      # {
138      #  "@odata.id": "/redfish/v1/EventService/Subscriptions/snmp1",
139      #  "@odata.type": "#EventDestination.v1_7_0.EventDestination",
140      #  "Context": "",
141      #  "Destination": "snmp://xx.xx.xx.xx:162",
142      #  "EventFormatType": "Event",
143      #  "Id": "snmp1",
144      #  "Name": "Event Destination snmp1",
145      #  "Protocol": "SNMPv2c",
146      #  "SubscriptionType": "SNMPTrap"
147
148      # Find the SNMP manager that has matching destination details.
149      ${snmp_mgr}=  Redfish.Get Attribute  ${snmp_mgr_uri}  Destination
150
151      # Delete the SNMP manager if the requested IP & ports are found
152      # and mark is_snmp_found to true.
153      Run Keyword If  'snmp://${snmp_ip_port}' == '${snmp_mgr}'
154      ...  Run Keywords  Set Local Variable  ${is_snmp_found}  ${True}
155      ...  AND  Redfish.Delete  ${snmp_mgr_uri}
156      ...  AND  Exit For Loop
157    END
158
159    Pass Execution If  ${is_snmp_found} == ${False}
160    ...  SNMP Manager: ${snmp_mgr_ip}:${snmp_port} is not configured on BMC
161
162    # Check if the SNMP manager is really deleted from BMC.
163    ${status}=  Run Keyword And Return Status
164    ...  Verify SNMP Manager Configured On BMC  ${snmp_mgr_ip}  ${snmp_port}
165
166    Should Be Equal  ${status}  ${False}  msg=SNMP manager is not deleted in the backend.
167
168
169Create Error On BMC And Verify Trap
170    [Documentation]  Generate error on BMC and verify if trap is sent.
171    [Arguments]  ${event_log}=${CMD_INTERNAL_FAILURE}  ${expected_error}=${SNMP_TRAP_BMC_INTERNAL_FAILURE}
172
173    # Description of argument(s):
174    # event_log       Event logs to be created.
175    # expected_error  Expected error on SNMP.
176
177    Configure SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${SNMP_DEFAULT_PORT}  ${HTTP_CREATED}
178
179    Start SNMP Manager
180
181    # Generate error log.
182    BMC Execute Command  ${event_log}
183
184    SSHLibrary.Switch Connection  snmp_server
185    ${SNMP_LISTEN_OUT}=  Read  delay=1s
186
187    Delete SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${SNMP_DEFAULT_PORT}
188
189    # Stop SNMP manager process.
190    SSHLibrary.Execute Command  sudo killall snmptrapd
191
192    # Sample SNMP trap:
193    # 2021-06-16 07:05:29 xx.xx.xx.xx [UDP: [xx.xx.xx.xx]:58154->[xx.xx.xx.xx]:xxx]:
194    # DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (2100473) 5:50:04.73
195    #   SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.49871.1.0.0.1
196    #  SNMPv2-SMI::enterprises.49871.1.0.1.1 = Gauge32: 369    SNMPv2-SMI::enterprises.49871.1.0.1.2 = Opaque:
197    # UInt64: 1397718405502468474     SNMPv2-SMI::enterprises.49871.1.0.1.3 = INTEGER: 3
198    #      SNMPv2-SMI::enterprises.49871.1.0.1.4 = STRING: "xxx.xx.xx Failure"
199
200    ${lines}=  Split To Lines  ${SNMP_LISTEN_OUT}
201    ${trap_info}=  Get From List  ${lines}  -1
202    ${snmp_trap}=  Split String  ${trap_info}  \t
203
204    Verify SNMP Trap  ${snmp_trap}  ${expected_error}
205
206    [Return]  ${snmp_trap}
207
208
209Verify SNMP Trap
210    [Documentation]  Verify SNMP trap.
211    [Arguments]  ${snmp_trap}  ${expected_error}=${SNMP_TRAP_BMC_INTERNAL_FAILURE}
212
213    # Description of argument(s):
214    # snmp_trap       SNMP trap collected on SNMP manager.
215    # expected_error  Expected error on SNMP.
216
217    # Verify all the mandatory fields of trap.
218    Should Contain  ${snmp_trap}[0]  DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks:
219    Should Be Equal  ${snmp_trap}[1]  SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.49871.1.0.0.1
220    Should Match Regexp  ${snmp_trap}[2]  SNMPv2-SMI::enterprises.49871.1.0.1.1 = Gauge32: \[0-9]*
221    Should Match Regexp  ${snmp_trap}[3]  SNMPv2-SMI::enterprises.49871.1.0.1.2 = Opaque: UInt64: \[0-9]*
222    Should Match Regexp  ${snmp_trap}[4]  SNMPv2-SMI::enterprises.49871.1.0.1.3 = INTEGER: \[0-9]
223    Should Be Equal  ${snmp_trap}[5]  SNMPv2-SMI::enterprises.49871.1.0.1.4 = STRING: "${expected_error}"
224
225
226Start SNMP Manager
227    [Documentation]  Start SNMP listener on the remote SNMP manager.
228
229    Open Connection And Log In  ${SNMP_MGR1_USERNAME}  ${SNMP_MGR1_PASSWORD}
230    ...  alias=snmp_server  host=${SNMP_MGR1_IP}
231
232    # The execution of the SNMP_TRAPD_CMD is necessary to cause SNMP to begin
233    # listening to SNMP messages.
234    SSHLibrary.write  ${SNMP_TRAPD_CMD} &
235
236
237Create Error On BMC And Verify Trap On Non-Default Port
238    [Documentation]  Generate error on BMC and verify if trap is sent to non default port.
239    [Arguments]  ${event_log}=${CMD_INTERNAL_FAILURE}  ${expected_error}=${SNMP_TRAP_BMC_INTERNAL_FAILURE}
240
241    # Description of argument(s):
242    # event_log       Event logs to be created.
243    # expected_error  Expected error on SNMP.
244
245    Configure SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${NON_DEFAULT_PORT1}
246
247    Start SNMP Manager On Specific Port  ${SNMP_MGR1_IP}  ${NON_DEFAULT_PORT1}
248
249    # Generate error log.
250    BMC Execute Command  ${event_log}
251
252    SSHLibrary.Switch Connection  snmp_server
253    ${SNMP_LISTEN_OUT}=  Read  delay=1s
254
255    Delete SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${NON_DEFAULT_PORT1}
256
257    # Stop SNMP manager process.
258    SSHLibrary.Execute Command  sudo killall snmptrapd
259
260    # Sample SNMP trap:
261    # 2021-06-16 07:05:29 xx.xx.xx.xx [UDP: [xx.xx.xx.xx]:58154->[xx.xx.xx.xx]:xxx]:
262    # DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (2100473) 5:50:04.73
263    #   SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.49871.1.0.0.1
264    #  SNMPv2-SMI::enterprises.49871.1.0.1.1 = Gauge32: 369    SNMPv2-SMI::enterprises.49871.1.0.1.2 = Opaque:
265    # UInt64: 1397718405502468474     SNMPv2-SMI::enterprises.49871.1.0.1.3 = INTEGER: 3
266    #      SNMPv2-SMI::enterprises.49871.1.0.1.4 = STRING: "xxx.xx.xx Failure"
267
268    ${lines}=  Split To Lines  ${SNMP_LISTEN_OUT}
269    ${trap_info}=  Get From List  ${lines}  -1
270    ${snmp_trap}=  Split String  ${trap_info}  \t
271
272    Verify SNMP Trap  ${snmp_trap}  ${expected_error}
273
274    [Return]  ${snmp_trap}
275
276
277Start SNMP Manager On Specific Port
278    [Documentation]  Start SNMP listener on specific port on the remote SNMP manager.
279    [Arguments]  ${snmp_mgr_ip}  ${snmp_port}
280
281    # Description of argument(s):
282    # snmp_mgr_ip  SNMP manager IP.
283    # snmp_port    Network port on which SNMP manager need to run.
284
285    ${ip_and_port}=  Catenate  ${snmp_mgr_ip}:${snmp_port}
286
287    Open Connection And Log In  ${SNMP_MGR1_USERNAME}  ${SNMP_MGR1_PASSWORD}
288    ...  alias=snmp_server  host=${SNMP_MGR1_IP}
289
290    # The execution of the SNMP_TRAPD_CMD is necessary to cause SNMP to begin
291    # listening to SNMP messages.
292    SSHLibrary.write  ${SNMP_TRAPD_CMD} ${ip_and_port} &
293