Skip to content
阿德的博客
Go back

iTop学习笔记--REST集成

早先有同事通过直接修改iTop数据库实现集成,由于这种方式略感粗暴(一旦类对象变化或版本升级等原因导致数据库表结构变化,比较容易出现问题),所以测试了iTop官方安装文档推荐的REST/JSON集成方式,这里使用python实现。

POST参数

要调用对应的action,通过下面几个参数给/webservices/rest.php

Argument

Description

Defaut value

version

Version of the API. It is a way to make sure that the targetted iTop server can deliver some functionality, and it ensures stability for your scripts: as long as a version is available, the operations will remain unchanged, with the exception of the following cases: bug fixes, modification in the returned messages, new elements into the returned JSON structure.

-

auth_user

User login

-

auth_pwd

User password

-

json_data

Structure containing all the information required to process your request. In particular, the requested operation is given here.

callback

If set, then JSON-P (JSON with padding) is used

在实际使用参数时,须要注意:

python实现

这里测试实现了直接脚本里写了一个参数列表,POST到iTop实现ticket创建。生产环境可以通过传递相应参数来实现不通内容的传递。

'''
Created on Nov 6, 2017

@author: edxi
'''

import json
import urllib.request, urllib.error, urllib.parse

data = dict(auth_user="admin", auth_pwd="itopadminpass", version="1.3", json_data=json.dumps({
   "operation": "core/create",
   "comment": "Synchronization from python script",
   "class": "UserRequest",
   "output_fields": "id, friendlyname",
   "fields":
   {
      "org_id": "SELECT Organization WHERE name = \"MyOrg\"",
      "caller_id":
      {
         "name": "xi",
         "first_name": "erde",
      },
      "title": "Got a problem!",
      "description": "It's generated by python script"
   }
}))

req = urllib.request.Request('http://192.168.145.130/webservices/rest.php',
                             data=urllib.parse.urlencode(data).encode("utf-8"))  # this will make the method "POST"
response = urllib.request.urlopen(req)

print(response.read().decode('utf-8'))

Share this post on:

Previous Post
Zabbix学习笔记--CentOS7安装selinux修改(转)
Next Post
iTop学习笔记--安装和初始化配置