自愈脚本获取告警内容

Viewed 85

在告警规则配置触发执行自愈脚本执行的时候,如何获取这条告警信息的内容或者如何查看是哪台设备告警的

2 Answers

告警消息会通过 stdin 的方式传给自愈脚本,脚本里从 stdin 获取即可,具体是啥格式,打印出来看看就知道了。比如拿 python 脚本举例:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
import json

if __name__ == "__main__":
    payload = json.load(sys.stdin)
    with open(".payload", 'w') as f:
        f.write(json.dumps(payload, indent=4))

    # some logic

json.load(sys.stdin) 就是从stdin读取内容

执行这句报错了
Traceback (most recent call last):
File "/tmp/test.py", line 6, in
payload = json.load(sys.stdin)
File "/usr/lib64/python3.6/json/init.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib64/python3.6/json/init.py", line 354, in loads
return _default_decoder.decode(s)

File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)