【问题标题】:Scrape Alertmanager API抓取警报管理器 API
【发布时间】:2020-04-14 18:59:07
【问题描述】:
我有一个默认安装 Prometheus 和 Alertmanager 的 Openshift 3.11 集群。
我想编写一个 python 脚本来抓取 Alertmanager API 端点,以便我可以解析数据并将其传递给我们运营团队的第三方监控工具。
我的问题是要访问我需要针对 oauth 进行身份验证的 API。我如何在 python 中做到这一点?
【问题讨论】:
标签:
python
oauth
openshift
prometheus-alertmanager
okd
【解决方案1】:
我不知道警报管理器与 Reddit API 相比是否有什么不同,但是当我为此设置机器人时,我必须先在他们的 OAuth 页面上注册它,然后我需要使用代码它给了我,用我的其余用户数据(登录名和密码以及应用程序的名称)请求访问令牌然后我可以使用该身份验证来联系 API 端点。
这是我从 Reddit 请求访问令牌的函数:
def AuthRequest():
import requests
import requests.auth
import json
client_auth = requests.auth.HTTPBasicAuth('Application ID code', 'OAuth secret code')
post_data = {"grant_type": "password", "username": "Your_Username", "password": "Your_Password"}
headers = {"User-Agent": "Name_Of_Application"}
response = requests.post("https://www.reddit.com/api/v1/access_token", auth=client_auth, data=post_data, headers=headers)
return response.json()
这是联系端点并从中获取数据的代码:
import requests
import requests.auth
import json
currentComments = []
headers = {"Authorization": auth['token_type'] + " " + auth['access_token'], "User-Agent": "Your_Application_Name"}
mentions = requests.get("https://oauth.reddit.com/message/unread.json", headers=headers).json()
请注意,“auth”只是来自身份验证令牌的“响应”。我希望这会有所帮助,我真的不知道这与 alertmanager 有何不同,因为我从来没有真正使用过它。
【解决方案2】:
我找到了适合我的解决方法。
我需要创建一个服务帐号
oc create serviceaccount <serviceaccount name> -n <your-namespace>
然后为它创建一个集群角色绑定
oc create clusterrolebinding <name for your role> \
--clusterrole=cluster-monitoring-view \
--serviceaccount=<your-namespace>:<serviceaccount name>
从 SA 获取令牌,然后在 curl 中使用它
oc sa get-token <serviceaccount name> -n <your-namespace>