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