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