Opsgenie powershell 警报帖子不起作用

Posted

技术标签:

【中文标题】Opsgenie powershell 警报帖子不起作用【英文标题】:Opsgenie powershell alert post not working 【发布时间】:2020-02-13 22:22:24 【问题描述】:

我已使用 Python 成功发布警报,但无法让我的 powershell 警报创建工作。我在回复中只收到了一堵 html 墙,没有创建警报。消息是唯一的必填字段。 这是我正在使用的,但它不起作用

$api = "XXX"
$URI = "https://api.opsgenie.com/v2/alerts"
$head = @"Authorization" = "GenieKey $api"
$body = @
            message = "testing";
            responders = 
                ]@
                    name = "TEAMNAMEHERE";
                    type = "team"
                ]

         | ConvertTo-Json

$request = Invoke-RestMethod -Uri $URI -Method Post -Headers $head -ContentType "application/json" -Body $body
$request

这是我编写的 python 代码,它运行良好。

import requests
import json


def CreateOpsGenieAlert(api_token):
    header = 
        "Authorization": "GenieKey " + api_token,
        "Content-Type": "application/json"
    

    body = json.dumps("message": "testing",
                       "responders": [
                           
                               "name": "TEAMNAMEHERE",
                               "type": "team"
                           
                       ]
                       
                      )
    try:
        response = requests.post("https://api.opsgenie.com/v2/alerts",
                                headers=header,
                                data=body)
        jsontemp = json.loads(response.text)
        print(jsontemp)

        if response.status_code == 202:
            return response
    except:
        print('error')

    print(response)


CreateOpsGenieAlert(api_token="XXX")

编辑:所以我发现它与我的“响应者”部分有关。它与 [ ]...有关,但我无法弄清楚到底是什么。如果我删除它们,它将无法正常工作。如果我把第一个转过来,它就行不通了。我可以收到创建成功的警报,但是我不断收到以下错误:

] : The term ']' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At \\file\Tech\user\powershell scripts\.not working\OpsGenieAlert.ps1:7 char:17
+                 ]@
+                 ~
    + CategoryInfo          : ObjectNotFound: (]:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

【参考方案1】:

您需要将您的 $body 转换为 JSON

$api = "XXX"
$URI = "https://api.opsgenie.com/v2/alerts"
# Declare an empty array 
$responders = @()

# Add a new item to the array
$responders += @
    name = "TEAMNAMEHERE1"
    type = "team1"

$responders += @
    name = "TEAMNAMEHERE2"
    type = "team2"


$body = @
    message    = "testing"
    responders = $responders
 | ConvertTo-Json

$invokeRestMethodParams = @
    'Headers'     = @
        "Authorization" = "GenieKey $api"
    
    'Uri'         = $URI
    'ContentType' = 'application/json'
    'Body'        = $body
    'Method'      = 'Post'


$request = Invoke-RestMethod @invokeRestMethodParams

【讨论】:

在正文末尾添加转换为 json 不起作用。仍然是 HTML 文本墙。 问题出在“响应者”周围,可能是格式化。我只是不知道它是什么 我已经更新了答案,以澄清您对 Powershell 中数组的疑问 + $request + ~ 在散列文字的键后缺少“=”运算符。在 \\ae-file\Tech\path\powershell scripts\.not working\OpsGenieAlert.ps1:45 char:9 + $request + ~ 哈希文字不完整。我以前从未做过喷溅,所以不完全确定在这里寻找什么,但这是我复制你提供的内容时遇到的错误 Invoke-RestMethod :远程服务器返回错误:(422)无法处理的实体。在 line:13 char:12 + $request = Invoke-RestMethod -Uri $URI -Method Post -Headers $head -C ... + ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest :HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

以上是关于Opsgenie powershell 警报帖子不起作用的主要内容,如果未能解决你的问题,请参考以下文章

用于获取 Azure 监视器警报规则的 Powershell 命令不起作用

PowerShell 发送美观的Vsphere DataStore警报

Linux 一键部署alertmanager

Linux 一键部署alertmanager

Linux 一键部署alertmanager

OpsGenie 不会在 AWS 仪表板或 terraform 中自动确认 SNS 订阅