Chinaunix首页 | 论坛 | 博客
  • 博客访问: 339366
  • 博文数量: 52
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 577
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-27 14:21
个人简介

知道自己该干嘛,知道自己能干嘛

文章分类

全部博文(52)

文章存档

2019年(1)

2018年(8)

2017年(2)

2016年(11)

2015年(3)

2014年(10)

2013年(17)

我的朋友

分类: 系统运维

2014-10-23 17:09:07


        好久没有写笔记了,今天记录一下,现在的环境是,我们有了 zabbix 监控平台,可以对服务器的日常运行进行报警,我们采用 redmine 去记录处理的流程与具体细节,可以看出问题处理的到那一个环节,也方便日后对问题的追踪与查询。 
               
               现在的报警通知模式是 :
               zabbix srver --> stmp_mail --> 监控组 --> 手动 create issue 分派给相应的工程师 --> redmine notify engineer by mail 
              
               这是我们在触发了一个 event的通知流程,问题来了 ,呵呵 可能最近这句话你听过很多次了,风靡个大Q群
 
               我们能否让 zabbix event 产生后自动在 redmine issue 创建呢,既然我们就想法就去找寻解决问题的办法
 
               第一 我想到了 api , redmine 是否提供 api 是否提供 对 create issue 的支持, 好 走起

  1.        Creating an issue
  2.             
  3.        POST /issues.[format]
  4.        Parameters:
  5.        issue - A hash of the issue attributes:
  6.        project_id
  7.        tracker_id
  8.        status_id
  9.        priority_id
  10.        subject
  11.        description
  12.        category_id
  13.        fixed_version_id - ID of the Target Versions (previously called 'Fixed Version' and still referred to as such in the API)
  14.        assigned_to_id - ID of the user to assign the issue to (currently no mechanism to assign by name)
  15.        parent_issue_id - ID of the parent issue
  16.        custom_fields - See Custom fields
  17.        watcher_user_ids - Array of user ids to add as watchers (since 2.3.0)
  18.        Attachments can be added when you create an issue, see Attaching files.
  19.        Examples:
  20.        POST /issues.json
           {
                 "issue": {
                 "project_id": 1,
                 "subject": "Example",
                 "priority_id": 4
                          }
           }
               nice, 古话说的好 “好的开始是成功的一半“,我们已经找到了方式,再去寻找如何调用,如何做redmine的登陆验证
                         
  1.        

    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脚本如下:
            
  1. #!/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #Fuction : Use redmine rest api Auto create issue
  4. import json
  5. import requests
  6. import sys
  7.                                                                                                            
  8. def ZBXevent_RDMissue(Subject,Content):
  9.              payload = {'issue': {'project_id': 'yunweibu','subject': Subject,'description': Content,'priority_id': '3','tracker_id': '11','assigned_to_id': '56','status_id': '23'}}
  10.              r = requests.post(url,data=json.dumps(payload),headers=headers)
  11.                                                                                              
  12. if __name__=="__main__":
  13.             api_key = 'e919d42f122f7baf88a7238520dfc478fb0c8b86'
  14.             url = " address/projects/yunweibu/issues.json?key=" + api_key
  15.             headers = {'content-type': 'application/json'}
  16.             Subject = sys.argv[2]
  17.             Content = sys.argv[3]
  18.             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


    

阅读(2743) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~