cURL提交json文件

在使用 curl 来POST JSON数据,需要设置请求的 Content-Type 并且在命令行使用 -d 参数来传递JSON数据。这里的 JSON 内容类型是使用 -H "Content-Type: application/json" 命令参数,而 JSON 数据是作为字符串传递。需要注意,在Windows计算机上,JSON数据的双引号前面需要一个反斜杠 ,以下为POST JSON案例:

使用curl提交JSON数据
curl -X POST https://reqbin.com/echo/post/json
   -H 'Content-Type: application/json'
   -d '{"login":"my_login","password":"my_password"}'

Alertmanager起步 中也采用了 POST JSON 方式向 Alertmanager 提交测试告警:

测试alertmanager
alertmanager_ip=192.168.6.115
alertmanager_port=30903

curl -XPOST "http://${alertmanager_ip}:${alertmanager_port}/api/v1/alerts" \
  -d \
  "[{
    \"status\": \"firing\",
    \"labels\": {
      \"alertname\": \"测试告警\",
      \"service\": \"test-service\",
      \"severity\":\"warning\",
      \"instance\": \"testinstance.cloud-atlas.io\"
    },
    \"annotations\": {
      \"summary\": \"延迟过高!\"
    }
  }]"

钉钉支持提交json格式,并且可以传输 markdown 格式,这样可以嵌入图片以及链接:

钉钉json告警文本
{
     "msgtype": "markdown",
     "markdown": {
         "title":"Prometheus告警信息",
         "text": "## 监控指标\n监控描述信息 [监控页面](https://grafana.cloud-atlas.io/)\n\n###### 告警时间 \n"
     },
    "at": {
        "atMobiles": [
            "156xxxx8827",
            "189xxxx8325"
        ],
        "isAtAll": false
    }
}

这样就可以直接向钉钉群机器人提交通知:

钉钉json格式告警
curl -l -H "Content-type: application/json" -X POST -d @alert_test.json "https://oapi.dingtalk.com/robot/send?access_token=XXXXXX"

备注

MarkDown格式的通知中 @功能 我验证没有成功

参考