Applies To
- Zenoss 5.x
Summary
Creating events using PowerShell and the JSON API results in events that are easier to read, parse and understand. This KB provides an example of how to perform this task.
Background
Events that are generated in response to errors can be difficult to read or identify as errors.
The following example shows a non-JSON API FAIL POST error. Notice that this error doesn't look like an error because it shows no standard red text powershell text. This error was generated because the headers were created with a bad password:
PS C:\Users\fgreene> Invoke-WebRequest -UseBasicParsing -Uri "https://zenoss5.v5.fg.loc/zport/dmd/evconsole_router" -Headers &headers -Method Post -Body $JSON -ContentType "application/json" StatusCode : 200 StatusDescription : OK Content : <html> <head> <script type='text/javascript'>window.mod_pagespeed_start = Number(new Date());</script><link rel="shortcut icon" type="image/x-icon" href="/zport/dmd/favicon.ico"/> ... RawContent : HTTP/1.1 200 OK Vary: Accept-Encoding X-Frame-Options: SAMEORIGIN X-Page-Speed: 1.11.33.4-0 X-Xss-Protection: 1; mode=block Transfer-Encoding: chunked Cache-Control: max-age=0, no-cache Content... Forms : Headers : {[Vary, Accept-Encoding], [X-Frame-Options, SAMEORIGIN], [X-Page-Speed, 1.11.33.4-0], [X-Xss-Protection, 1; mode=block]...} Images : {} InputFields : {@{outerHTML=<input type="hidden" name="came_from" value="https://zenoss5.v5.fg.loc/zport/dmd/evconsole_router"/>; tagName=INPUT; type=hidden; name=came_from; value=https://zenoss5.v5.fg.loc/zport/dmd/evconsole_router}, @{outerHTML=<input type="hidden" name="submitted" value="true"/>; tagName=INPUT; type=hidden; name=submitted; value=true}, @{outerHTML=<input id="fragment" type="hidden" name="fragment" value=""/>; tagName=INPUT; id=fragment; type=hidden; name=fragment; value=}, @{outerHTML=<input id="username" type="text" name="__ac_name" class="inputbox" style="top:160px"/>; tagName=INPUT; id=username; type=text; name=__ac_name; class=inputbox; style=top:160px}...} Links : {} ParsedHtml : RawContentLength : 6766
Procedure
Perform the following to create a JSON API event:
- Build headers for authorization:
$user = "admin" $pass = "Zenoss!2" $pair = "${user}:${pass}" $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $base64 = [System.Convert]::ToBase64String($bytes) $basicAuthValue = "Basic $base64" $headers = @{ Authorization = $basicAuthValue }
- Create the JSON format (nested JSON in PowerShell):
$hash = @([Ordered]@{ action = "EventsRouter"; method = "add_event" data = @( [Ordered]@{ summary = "hulloPS" device = "test-rhel6.zenoss.loc" component = "drive1" severity = "Critical" evclasskey = "" evclass = "/App" } ) type = "rpc" tid = 1 } ) $JSON = $hash | convertto-json -Depth 2
- Test the output:
PS C:\Users\zuser> $JSON
Example output looks simpler, like JSON:
{ "action": "EventsRouter", "method": "add_event", "data": [ { "summary": "hulloPS", "device": "test-rhel6.zenoss.loc", "component": "drive1", "severity": "Critical", "evclasskey": "", "evclass": "/App" } ], "type": "rpc", "tid": 1 } PS C:\Users\zuser>
- Ignore SSL certificates until the correct certificates are in place:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
- Post the code:
Invoke-WebRequest -UseBasicParsing -Uri "https://zenoss5.v5.fg.loc/zport/dmd/evconsole_router" -Headers $headers -Method Post -Body $JSON -ContentType "application/json"
- After successfully posting the code, the error displays are shorter and easier to read:
PS C:\Users\zuser> Invoke-WebRequest -UseBasicParsing -Uri "https://zenoss5.v5.fg.loc/zport/dmd/evconsole_router" -Hea ders $headers -Method Post -Body $JSON -ContentType "application/json" StatusCode : 200 StatusDescription : OK Content : {"uuid": "bb324921-cb7c-471f-82ac-e6da7c5fb5c7", "action": "EventsRouter", "result": {"msg": "Created event", "success": true}, "tid": 1, "type": "rpc", "method": "add_event"} RawContent : HTTP/1.1 200 OK Vary: Accept-Encoding X-Frame-Options: SAMEORIGIN X-Xss-Protection: 1; mode=block Content-Length: 175 Content-Type: application/json Date: Fri, 31 Mar 2017 02:23:51 GMT Server: ... Forms : Headers : {[Vary, Accept-Encoding], [X-Frame-Options, SAMEORIGIN], [X-Xss-Protection, 1; mode=block], [Content-Length, 175]...} Images : {} InputFields : {} Links : {} ParsedHtml : RawContentLength : 175
Comments