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      Run Keyword If  'snmp://${snmp_ip_port}' == '${snmp_mgr}'
179      ...  Run Keywords  Set Local Variable  ${is_snmp_found}  ${True}
180      ...  AND  Redfish.Delete  ${snmp_mgr_uri}
181      ...  AND  Exit For Loop
182    END
183
184    Pass Execution If  ${is_snmp_found} == ${False}
185    ...  SNMP Manager: ${snmp_mgr_ip}:${snmp_port} is not configured on BMC
186
187    # Check if the SNMP manager is really deleted from BMC.
188    ${status}=  Run Keyword And Return Status
189    ...  Verify SNMP Manager Configured On BMC  ${snmp_mgr_ip}  ${snmp_port}
190
191    Should Be Equal  ${status}  ${False}  msg=SNMP manager is not deleted in the backend.
192
193
194Create Error On BMC And Verify Trap
195    [Documentation]  Generate error on BMC and verify if trap is sent.
196    [Arguments]  ${event_log}=${CMD_INTERNAL_FAILURE}  ${expected_error}=${SNMP_TRAP_BMC_INTERNAL_FAILURE}
197
198    # Description of argument(s):
199    # event_log       Event logs to be created.
200    # expected_error  Expected error on SNMP.
201
202    Configure SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${SNMP_DEFAULT_PORT}  ${HTTP_CREATED}
203
204    Start SNMP Manager
205
206    # Generate error log.
207    BMC Execute Command  ${event_log}
208
209    SSHLibrary.Switch Connection  snmp_server
210    ${SNMP_LISTEN_OUT}=  Read  delay=1s
211
212    Delete SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${SNMP_DEFAULT_PORT}
213
214    # Stop SNMP manager process.
215    SSHLibrary.Execute Command  sudo killall snmptrapd
216
217    # Sample SNMP trap:
218    # 2021-06-16 07:05:29 xx.xx.xx.xx [UDP: [xx.xx.xx.xx]:58154->[xx.xx.xx.xx]:xxx]:
219    # DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (2100473) 5:50:04.73
220    #   SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.49871.1.0.0.1
221    #  SNMPv2-SMI::enterprises.49871.1.0.1.1 = Gauge32: 369    SNMPv2-SMI::enterprises.49871.1.0.1.2 = Opaque:
222    # UInt64: 1397718405502468474     SNMPv2-SMI::enterprises.49871.1.0.1.3 = INTEGER: 3
223    #      SNMPv2-SMI::enterprises.49871.1.0.1.4 = STRING: "xxx.xx.xx Failure"
224
225    ${lines}=  Split To Lines  ${SNMP_LISTEN_OUT}
226    ${trap_info}=  Get From List  ${lines}  -1
227    ${snmp_trap}=  Split String  ${trap_info}  \t
228
229    Verify SNMP Trap  ${snmp_trap}  ${expected_error}
230
231    [Return]  ${snmp_trap}
232
233
234Verify SNMP Trap
235    [Documentation]  Verify SNMP trap.
236    [Arguments]  ${snmp_trap}  ${expected_error}=${SNMP_TRAP_BMC_INTERNAL_FAILURE}
237
238    # Description of argument(s):
239    # snmp_trap       SNMP trap collected on SNMP manager.
240    # expected_error  Expected error on SNMP.
241
242    # Verify all the mandatory fields of trap.
243    Should Contain  ${snmp_trap}[0]  DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks:
244    Should Be Equal  ${snmp_trap}[1]  SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.49871.1.0.0.1
245    Should Match Regexp  ${snmp_trap}[2]  SNMPv2-SMI::enterprises.49871.1.0.1.1 = Gauge32: \[0-9]*
246    Should Match Regexp  ${snmp_trap}[3]  SNMPv2-SMI::enterprises.49871.1.0.1.2 = Opaque: UInt64: \[0-9]*
247    Should Match Regexp  ${snmp_trap}[4]  SNMPv2-SMI::enterprises.49871.1.0.1.3 = INTEGER: \[0-9]
248    Should Be Equal  ${snmp_trap}[5]  SNMPv2-SMI::enterprises.49871.1.0.1.4 = STRING: "${expected_error}"
249
250
251Start SNMP Manager
252    [Documentation]  Start SNMP listener on the remote SNMP manager.
253
254    Open Connection And Log In  ${SNMP_MGR1_USERNAME}  ${SNMP_MGR1_PASSWORD}
255    ...  alias=snmp_server  host=${SNMP_MGR1_IP}
256
257    # Clean SNMP managers running in the background.
258    SSHLibrary.Execute Command  killall snmptrapd
259
260    # The execution of the SNMP_TRAPD_CMD is necessary to cause SNMP to begin
261    # listening to SNMP messages.
262    SSHLibrary.write  ${SNMP_TRAPD_CMD} &
263
264
265Create Error On BMC And Verify Trap On Non Default Port
266    [Documentation]  Generate error on BMC and verify if trap is sent to non default port.
267    [Arguments]  ${event_log}=${CMD_INTERNAL_FAILURE}  ${expected_error}=${SNMP_TRAP_BMC_INTERNAL_FAILURE}
268
269    # Description of argument(s):
270    # event_log       Event logs to be created.
271    # expected_error  Expected error on SNMP.
272
273    Configure SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${NON_DEFAULT_PORT1}
274
275    Start SNMP Manager On Specific Port  ${SNMP_MGR1_IP}  ${NON_DEFAULT_PORT1}
276
277    # Generate error log.
278    BMC Execute Command  ${event_log}
279
280    SSHLibrary.Switch Connection  snmp_server
281    ${SNMP_LISTEN_OUT}=  Read  delay=1s
282
283    Delete SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${NON_DEFAULT_PORT1}
284
285    # Stop SNMP manager process.
286    SSHLibrary.Execute Command  sudo killall snmptrapd
287
288    # Sample SNMP trap:
289    # 2021-06-16 07:05:29 xx.xx.xx.xx [UDP: [xx.xx.xx.xx]:58154->[xx.xx.xx.xx]:xxx]:
290    # DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (2100473) 5:50:04.73
291    #   SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.49871.1.0.0.1
292    #  SNMPv2-SMI::enterprises.49871.1.0.1.1 = Gauge32: 369    SNMPv2-SMI::enterprises.49871.1.0.1.2 = Opaque:
293    # UInt64: 1397718405502468474     SNMPv2-SMI::enterprises.49871.1.0.1.3 = INTEGER: 3
294    #      SNMPv2-SMI::enterprises.49871.1.0.1.4 = STRING: "xxx.xx.xx Failure"
295
296    ${lines}=  Split To Lines  ${SNMP_LISTEN_OUT}
297    ${trap_info}=  Get From List  ${lines}  -1
298    ${snmp_trap}=  Split String  ${trap_info}  \t
299
300    Verify SNMP Trap  ${snmp_trap}  ${expected_error}
301
302    [Return]  ${snmp_trap}
303
304
305Start SNMP Manager On Specific Port
306    [Documentation]  Start SNMP listener on specific port on the remote SNMP manager.
307    [Arguments]  ${snmp_mgr_ip}  ${snmp_port}
308
309    # Description of argument(s):
310    # snmp_mgr_ip  SNMP manager IP.
311    # snmp_port    Network port on which SNMP manager need to run.
312
313    ${ip_and_port}=  Catenate  ${snmp_mgr_ip}:${snmp_port}
314
315    Open Connection And Log In  ${SNMP_MGR1_USERNAME}  ${SNMP_MGR1_PASSWORD}
316    ...  alias=snmp_server  host=${SNMP_MGR1_IP}
317
318    # The execution of the SNMP_TRAPD_CMD is necessary to cause SNMP to begin
319    # listening to SNMP messages.
320    SSHLibrary.write  ${SNMP_TRAPD_CMD} ${ip_and_port} &
321
322
323Generate Error On BMC And Verify Trap
324    [Documentation]  Generate error on BMC and verify if trap is sent.
325    [Arguments]  ${event_log}=${CMD_INTERNAL_FAILURE}  ${expected_error}=${SNMP_TRAP_BMC_INTERNAL_FAILURE}
326
327    # Description of argument(s):
328    # event_log       Event logs to be created.
329    # expected_error  Expected error on SNMP.
330
331    Start SNMP Manager
332
333    # Generate error log.
334    BMC Execute Command  ${event_log}
335
336    SSHLibrary.Switch Connection  snmp_server
337    ${SNMP_LISTEN_OUT}=  Read  delay=1s
338
339    Delete SNMP Manager Via Redfish  ${SNMP_MGR1_IP}  ${SNMP_DEFAULT_PORT}
340
341    # Stop SNMP manager process.
342    SSHLibrary.Execute Command  sudo killall snmptrapd
343
344    # Sample SNMP trap:
345    # 2021-06-16 07:05:29 xx.xx.xx.xx [UDP: [xx.xx.xx.xx]:58154->[xx.xx.xx.xx]:xxx]:
346    # DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (2100473) 5:50:04.73
347    #   SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.49871.1.0.0.1
348    #  SNMPv2-SMI::enterprises.49871.1.0.1.1 = Gauge32: 369    SNMPv2-SMI::enterprises.49871.1.0.1.2 = Opaque:
349    # UInt64: 1397718405502468474     SNMPv2-SMI::enterprises.49871.1.0.1.3 = INTEGER: 3
350    #      SNMPv2-SMI::enterprises.49871.1.0.1.4 = STRING: "xxx.xx.xx Failure"
351
352    ${lines}=  Split To Lines  ${SNMP_LISTEN_OUT}
353    ${trap_info}=  Get From List  ${lines}  -1
354    ${snmp_trap}=  Split String  ${trap_info}  \t
355
356    Verify SNMP Trap  ${snmp_trap}  ${expected_error}
357
358    [Return]  ${snmp_trap}
359
360