Skip to content

Commit

Permalink
Merge pull request #140 from DMTF/Event-Destination-Testing
Browse files Browse the repository at this point in the history
Added 'rf_test_event_listener.py' tool to manually build event payloads
  • Loading branch information
mraineri authored Feb 23, 2024
2 parents bea00d9 + ac6159a commit 3a8cf77
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,64 @@ The tool will log into the service specified by the *rhost* argument using the c
It will then perform the requested method on the specified URI with an optional body, specified by the *method*, *request*, and *body* arguments.
It will then display the response of the operation from the service.
### Test Event Listener
```
usage: rf_test_event_listener.py [-h] --listener LISTENER [--file FILE]
[--id ID] [--name NAME] [--context CONTEXT]
[--eventtype EVENTTYPE] [--eventid EVENTID]
[--severity SEVERITY] [--message MESSAGE]
[--messageid MESSAGEID]
[--timestamp TIMESTAMP] [--header name value]

A tool to help verify a Redfish event listener

required arguments:
--listener LISTENER, -l LISTENER
The absolute URI of the Redfish event listener (with
scheme)

optional arguments:
-h, --help show this help message and exit
--file FILE, -file FILE
The filepath to a JSON file containing the event
payload; if this argument is specified, all other
arguments controlling the event data is ignored
--id ID, -id ID The value to specify in the Id property of the event
--name NAME, -name NAME
The value to specify in the Name property of the event
--context CONTEXT, -context CONTEXT
The value to specify in the Context property of the
event
--eventtype EVENTTYPE, -eventtype EVENTTYPE
The value to specify in the EventType property of the
event
--eventid EVENTID, -eventid EVENTID
The value to specify in the EventId property of the
event
--severity SEVERITY, -severity SEVERITY
The value to specify in the Severity property of the
event
--message MESSAGE, -message MESSAGE
The value to specify in the Message property of the
event
--messageid MESSAGEID, -messageid MESSAGEID
The value to specify in the MessageId property of the
event
--timestamp TIMESTAMP, -timestamp TIMESTAMP
The value to specify in the EventTimestamp property of
the event
--header name value, -header name value
Name-value pairs of HTTP headers to provide with the
request
```
Example: `rf_test_event_listener.py -l https://redfishlistener.contoso.org`
If the *file* argument is present, the payload is constructed entirely from the contents of the referenced file.
If the *file* argument is not present, all other arguments are used to build the event payload.
Once the event payload is constructed, the tool will perform a `POST` operations to the URI specified by the *listener* argument with additional HTTP headers specified by the *header* argument.
## Release Process
1. Go to the "Actions" page
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
redfish>=3.2.1
XlsxWriter>=1.2.7
XlsxWriter>=1.2.7
requests
90 changes: 90 additions & 0 deletions scripts/rf_test_event_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#! /usr/bin/python
# Copyright Notice:
# Copyright 2019-2024 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Tacklebox/blob/main/LICENSE.md

"""
Redfish Test Event Listener
File : rf_test_event_listener.py
Brief : This script performs POST operations with event payloads to help verify
an event listener.
"""

import argparse
import json
import requests
from datetime import datetime

event_headers = {
"Content-Type": "application/json"
}

event_payload = {
"@odata.type": "#Event.v1_7_0.Event",
"Id": "1",
"Name": "Sample Event",
"Context": "Sample Event for Listener",
"Events": [
{
"EventType": "Other",
"EventId": "1",
"Severity": "OK",
"MessageSeverity": "OK",
"Message": "Test message.",
"MessageId": "Resource.1.3.TestMessage"
}
]
}

argget = argparse.ArgumentParser( description = "A tool to help verify a Redfish event listener" )
argget.add_argument( "--listener", "-l", type = str, required = True, help = "The absolute URI of the Redfish event listener (with scheme)" )
argget.add_argument( "--file", "-file", type = str, help = "The filepath to a JSON file containing the event payload; if this argument is specified, all other arguments controlling the event data is ignored" )
argget.add_argument( "--id", "-id", type = str, help = "The value to specify in the Id property of the event" )
argget.add_argument( "--name", "-name", type = str, help = "The value to specify in the Name property of the event" )
argget.add_argument( "--context", "-context", type = str, help = "The value to specify in the Context property of the event" )
argget.add_argument( "--eventtype", "-eventtype", type = str, help = "The value to specify in the EventType property of the event" )
argget.add_argument( "--eventid", "-eventid", type = str, help = "The value to specify in the EventId property of the event" )
argget.add_argument( "--severity", "-severity", type = str, help = "The value to specify in the Severity property of the event" )
argget.add_argument( "--message", "-message", type = str, help = "The value to specify in the Message property of the event" )
argget.add_argument( "--messageid", "-messageid", type = str, help = "The value to specify in the MessageId property of the event" )
argget.add_argument( "--timestamp", "-timestamp", type = str, help = "The value to specify in the EventTimestamp property of the event" )
argget.add_argument( "--header", "-header", type = str, nargs = 2, metavar = ( "name", "value" ), action = "append", help = "Name-value pairs of HTTP headers to provide with the request" )
args = argget.parse_args()

# Update the event payload based on the specified arguments
if args.file:
with open( args.file ) as json_file:
event_payload = json.load(json_file)
else:
if args.id:
event_payload["Id"] = args.id
if args.name:
event_payload["Name"] = args.name
if args.context:
event_payload["Context"] = args.context
if args.eventtype:
event_payload["Events"][0]["EventType"] = args.eventtype
if args.eventid:
event_payload["Events"][0]["EventId"] = args.eventid
if args.severity:
event_payload["Events"][0]["Severity"] = args.severity
event_payload["Events"][0]["MessageSeverity"] = args.severity
if args.message:
event_payload["Events"][0]["Message"] = args.message
if args.messageid:
event_payload["Events"][0]["MessageId"] = args.messageid
if args.timestamp:
event_payload["Events"][0]["EventTimestamp"] = args.timestamp
else:
event_payload["Events"][0]["EventTimestamp"] = datetime.now().replace( microsecond = 0 ).astimezone().isoformat()

# Update the HTTP headers based on the specified arguments
if args.header:
for header in args.header:
event_headers[header[0]] = header[1]

# Send the request
response = requests.post( args.listener, json = event_payload, headers = event_headers, timeout = 15, verify = False )
print( "Listener responded with {} {}".format( response.status_code, response.reason ) )
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ def run(self):
"scripts/rf_raw_request.py",
"scripts/rf_sensor_list.py",
"scripts/rf_sys_inventory.py",
"scripts/rf_test_event_listener.py",
"scripts/rf_update.py",
"scripts/rf_virtual_media.py"
],
install_requires = [ "redfish>=3.2.1", "XlsxWriter>=1.2.7" ],
install_requires = [ "redfish>=3.2.1", "XlsxWriter>=1.2.7", "requests" ],
cmdclass={
'pyinstaller': Pyinstaller
}
Expand Down

0 comments on commit 3a8cf77

Please sign in to comment.