联系方式

联系方式

联系方式主要用途有两个

  1. 可以控制用户信息中展示哪些联系方式,把不会用到的联系方式可以关闭
  2. 有时候用户自己新增加的通知媒介,比如公司内部的im,需要配置用户的联系方式,可以在这里创建

联系方式1

新增联系方式后可以在创建用户-联系方式选择。

联系方式2

使用场景

用户自己创建的通知媒介和联系方式主要在自定义通知脚本中使用,下面是一个脚本调用举例,你可以参考样例脚本,对接自己的内部通信工具。

系统会把告警事件的内容encode成json,然后通过stdin的方式传给通知脚本,脚本示例如下:

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. import json
  5. import urllib2
  6. reload(sys)
  7. sys.setdefaultencoding('utf8') # 设置默认编码为utf-8
  8. class Sender(object):
  9. @classmethod
  10. # 方法send_newtalk,newtalk要和配置的通知媒介一致;
  11. def send_newtalk(cls, payload):
  12. try:
  13. #print("Starting send_newtalk".encode('utf-8'))
  14. # 从payload中获取事件数据
  15. event = payload.get('event')
  16. # 获取通知用户对象
  17. users = event.get("notify_users_obj")
  18. # 获取告警规则名称
  19. rule_name = event.get("rule_name")
  20. # 默认事件状态为“触发
  21. event_state = "Triggered"
  22. # 如果事件已恢复,则状态为“恢复”
  23. if event.get("is_recovered"):
  24. event_state = "Recovered"
  25. # 用于存储Dingtalk机器人令牌和用户电话
  26. tokens = {}
  27. phones = {}
  28. #print("Extracting user information".encode('utf-8'))
  29. # 遍历用户,收集电话和Dingtalk令牌
  30. for u in users:
  31. if u.get("phone"):
  32. phones[u.get("phone")] = 1
  33. contacts = u.get("contacts")
  34. # dingtalk_robot_token,需要根据需求替换对应名称,在夜莺通知设置-联系方式设定名称,用户里面配置参数
  35. if contacts.get("dingtalk_robot_token", ""):
  36. tokens[contacts.get("dingtalk_robot_token", "")] = 1
  37. # 设置请求头
  38. headers = {
  39. "Content-Type": "application/json;charset=utf-8"
  40. }
  41. #print("Tokens: {}".format(tokens).encode('utf-8'))
  42. #print("Phones: {}".format(phones).encode('utf-8'))
  43. # 遍历令牌,构建请求并发送到Dingtalk
  44. for t in tokens:
  45. url = "https://oapi.dingtalk.com/robot/send?access_token={}".format(t)
  46. body = {
  47. "msgtype": "markdown",
  48. "markdown": {
  49. "title": "{} - {}".format(event_state, rule_name),
  50. "text": "{} {}".format(payload.get('tpls').get("dingtalk.tpl", "dingtalk.tpl not found"), ' '.join(["@"+i for i in phones.keys()]))
  51. },
  52. "at": {
  53. "atMobiles": list(phones.keys()),
  54. "isAtAll": False
  55. }
  56. }
  57. #print("Sending request to URL: {}".format(url).encode('utf-8'))
  58. #print("Request body: {}".format(body).encode('utf-8'))
  59. data = json.dumps(body)
  60. req = urllib2.Request(url, data=data, headers=headers)
  61. req.get_method = lambda: "POST"
  62. response = urllib2.urlopen(req)
  63. result = response.read()
  64. #print("Response status code: {}".format(response.getcode()).encode('utf-8'))
  65. #print("Response text: {}".format(result).encode('utf-8'))
  66. # 捕获所有异常,防止程序崩溃
  67. except Exception as e:
  68. #print("Error in send_newtalk: {}".format(e).encode('utf-8'))
  69. def main():
  70. try:
  71. #print("Reading payload from stdin".encode('utf-8'))
  72. payload = json.load(sys.stdin)
  73. with open(".payload", 'w') as f:
  74. f.write(json.dumps(payload, indent=4))
  75. #print("Payload written to .payload file".encode('utf-8'))
  76. for ch in payload.get('event').get('notify_channels'):
  77. send_func_name = "send_{}".format(ch.strip())
  78. #print("Processing channel: {}, function name: {}".format(ch, send_func_name).encode('utf-8'))
  79. if not hasattr(Sender, send_func_name):
  80. #print("Function {} not found".format(send_func_name).encode('utf-8'))
  81. continue
  82. send_func = getattr(Sender, send_func_name)
  83. send_func(payload)
  84. except Exception as e:
  85. print("Error in main: {}".format(e).encode('utf-8'))
  86. def hello():
  87. print("hello nightingale".encode('utf-8'))
  88. if __name__ == "__main__":
  89. if len(sys.argv) == 1:
  90. main()
  91. elif sys.argv[1] == "hello":
  92. hello()
  93. else:
  94. print("I am confused".encode('utf-8'))

示例脚本中有很多 print 打印的输出,主要是为了方便调试使用。如果开启,可通过 n9e 的日志进行查看,日志调试在 INFO.log,报错状态在 ERROR.log,过滤关键词 event_script_notify

脚本日志