好久没有写笔记了,今天记录一下,现在的环境是,我们有了 zabbix 监控平台,可以对服务器的日常运行进行报警,我们采用 redmine 去记录处理的流程与具体细节,可以看出问题处理的到那一个环节,也方便日后对问题的追踪与查询。
现在的报警通知模式是 :
zabbix srver --> stmp_mail --> 监控组 --> 手动 create issue 分派给相应的工程师 --> redmine notify engineer by mail
这是我们在触发了一个 event的通知流程,问题来了 ,呵呵 可能最近这句话你听过很多次了,风靡个大Q群
我们能否让 zabbix event 产生后自动在 redmine issue 创建呢,既然我们就想法就去找寻解决问题的办法
第一 我想到了 api , redmine 是否提供 api 是否提供 对 create issue 的支持, 好 走起
-
Creating an issue
-
-
POST /issues.[format]
-
Parameters:
-
-
issue - A hash of the issue attributes:
-
project_id
-
tracker_id
-
status_id
-
priority_id
-
subject
-
description
-
category_id
-
fixed_version_id - ID of the Target Versions (previously called 'Fixed Version' and still referred to as such in the API)
-
assigned_to_id - ID of the user to assign the issue to (currently no mechanism to assign by name)
-
parent_issue_id - ID of the parent issue
-
custom_fields - See Custom fields
-
watcher_user_ids - Array of user ids to add as watchers (since 2.3.0)
-
Attachments can be added when you create an issue, see Attaching files.
-
-
Examples:
-
POST /issues.json
{
"issue": {
"project_id": 1,
"subject": "Example",
"priority_id": 4
}
}
nice, 古话说的好 “好的开始是成功的一半“,我们已经找到了方式,再去寻找如何调用,如何做redmine的登陆验证
-
A Authentication
Most of the time, the API requires authentication. To enable the API-style authentication, you have to check Enable REST API in Administration -> Settings -> Authentication. Then, authentication can be done in 2 different ways:
-
using your regular login/password via HTTP Basic authentication.
-
using your API key which is a handy way to avoid putting a password in a script. The API key may be attached to each request in one of the following way:
-
passed in as a "key" paramete
-
passed in as a username with a random password via HTTP Basic authentication
-
passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0)
You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout.
按照手册的说明开启我们的api之旅,使用 admin账户登陆redmine
Administration --> Settings --> Authentication --> Enable REST web service Enanble
My account --> 右边 API access key --> Show 记录我们的 api key
我们采用第一种方式,调用 redime api
using your API key which is a handy way to avoid putting a password in a script.
The API key may be attached to each request in one of the following way:
passed in as a "key" parameter
format :
" + api_key
调用api脚本如下:
-
#!/bin/env python
-
# -*- coding:utf-8 -*-
-
#Fuction : Use redmine rest api Auto create issue
-
-
import json
-
import requests
-
import sys
-
-
-
-
def ZBXevent_RDMissue(Subject,Content):
-
-
payload = {'issue': {'project_id': 'yunweibu','subject': Subject,'description': Content,'priority_id': '3','tracker_id': '11','assigned_to_id': '56','status_id': '23'}}
-
-
r = requests.post(url,data=json.dumps(payload),headers=headers)
-
-
-
-
-
if __name__=="__main__":
-
-
api_key = 'e919d42f122f7baf88a7238520dfc478fb0c8b86'
-
url = " address/projects/yunweibu/issues.json?key=" + api_key
-
headers = {'content-type': 'application/json'}
-
-
Subject = sys.argv[2]
-
Content = sys.argv[3]
-
-
ZBXevent_RDMissue(Subject,Content)
zabbix ui 创建新的 media-type, 填入名称 类型 选择 script , 脚本名称,定义一个新的 action,使用我们刚定义的media-type 作为告警方式
测试,随便找一台 zabbix 已监控的主机 ,在 boot 分区 dd 一个文件让空间超过阀值
产生一个 event 事件 :
查看 zabbix Audit log :
可以看出 触发了 action 使用 ZBX-event_RDM-issue 方式去告警
登陆 redmine , 进入相关项目
可以看到 event 相关的 issue 已自动创建 , OK 到此我们 本次的需求已达成,同时也受到了 redmine assgine 邮件
NICE, 完美收官, 通过学习 简化我们的运维流程,建立我们适合自己的运维环境监控跟踪体系。
geo_Cail
参考资料 : http:///projects/redmine/wiki/Rest_api
阅读(2840) | 评论(0) | 转发(0) |