Use Zabbix Api and ZabbixApi

Source: Internet
Author: User

Use Zabbix Api and ZabbixApi
API usage

Zabbix official documentation: https://www.zabbix.com/documentation/2.2/manual/api,

Zabbix API is based on JSON-RPC 2.0 specifications, specific implementation can choose any of your favorite programming language, can use Perl, Ruby, PHP and so on.

This article uses python as an example. Python zabbix has many api modules and is easy to use.

The zabbix module of each language and github connection are listed below.

Data Process

The following flowchart represents a typical workflow of Zabbix API work. Authentication (method user. login) is a mandatory step to obtain the authentication ID. This ID allows us to call any methods that are permitted by the API. The user. logout method is not mentioned in the previous example. This is also the reason why one verification ID can be reused. Using the user. logout method will invalidate the authentication ID, and subsequent operations will no longer be able to use this ID.

Python
  • Py-zabbixby Alexey Dubkov-Zabbix Modules for Python (PyPIPy-zabbix, No python3)

  • ZabbixPythonApiby Frank Yao-Zabbix API for Python (no python3)

  • Zabbixby gescheit-a Python library (PyPIZabbix-api)

  • PyZabbixby Luke Cyca-a Python module (PyPIPyzabbix, Depends-on requests)

  • Zabbix_apiby Grigoriy Netsman-scripts for creating and deleting hosts (depends on zabbix-api)

  • Zabbix-clientby Jes ús Losada-a Python library (PyPIZabbix-client)

  • Zabbix-api-erigonesby Erigones-a Python library (PyPIZabbix-api-erigones)

  • PyZabbixSenderby Kurt Momberg-a zabbix_sender replacement for Python.

Ruby
  • Zabbix APIby nelsonab (latest code seems to be on github)-a Ruby wrapper

  • Rubixby Dhruv Bansal-a Ruby library for working with the API and both retrieving and sending data to Zabbix server

  • Zabbixapiby Express 42-a Ruby gem, see README on github

  • Zabbyby Farzad Farid-a Ruby library and client for Zabbix

Perl
  • Zabbix-APIby SFR-ZABBIX-Perl distribution to access the Zabbix API

  • ZabbixAPIby Tomohiro Ikeda-a Perl library

  • Zabipiby Andrey Konovalov-Monitoring: Zabipi module that lets you use official Zabbix API documentation to create Perl applications interacting with Zabbix. contains additional methods (such as queue. get) and hacks (such as expandNames parameter for item. get ). invalid examples of usage quota ded in distributive.

  • Net-Zabbixby ksyz-Perl wrapper for Zabbix API

  • Zabbix-API-Clientby Matsumoto Ryosuke-Zabbix API client for Perl

Java
  • Zabbix-api by hengyunabc-Java library to access Zabbix API

  • Zabbix-sender by hengyunabc-Java library to use Zabbix sender protocol

PHP
  • PhpZabbixApi by confirm IT solutions GmbH-a PHP wrapper class and a wrapper code generator

  • Microzabbixapiconnector by Alex Kashin-a Micro-Zabbix-Api-Connector with proxy usage support

PowerShell
  • ZabbixPosh Api by simsaull-A Zabbix PowerShell Module

  • Zabbix by Benjamin RIOUAL-An other Zabbix module, based on Invoke-RestMethod

JavaScript
  • Jqzabbix by Kodai Terashima-jQuery plugin for Zabbix API

  • Zabbix. js by Kristoffer Berdal-a library based around request. js

C #
  • C # api library by cheezus-a C # library for. NET 2.0

  • C # Library by HenriqueCaires-a C # library for. NET 4.5

Go
  • Zabbixby Ryan Day-Zabbix API for Go

  • Go-zabbix "by Alexey Dubkov"-Zabbix Packages for Go

  • Zabbix-senderAlexey Palazhchenko-push data to Zabbix server's trapper items from Go application

  • Zabbix. goAlexey Palazhchenko-Zabbix API for Go

 

Automation Overview
Currently, we use the pyzabbix module and json to define the template file.

The following describes the usage (for api reference, refer to the Manual on the official website ):

1 #! /Usr/bin/env python 2 # jiayun 3 # version 1.3 4 from pyzabbix import ZabbixAPI 5 import json 6 import OS, sys 7 import re, time 8 import logging 9 rule = json. load (file ('d: \ pycharm \ project \ REGION Manage Script \ qn_rolerule.json ') # template file 10 def login (): 11 zapi = ZabbixAPI ("http: // 10.4.0.247 ") # log on to zabbix 12 zapi. login ("admin", "zabbix") 13 return zapi 14 def get_hostgroups (group_name): 15 return zap I. hostgroup. get (search = {"name": group_name}, output = "extend") # search for the input group and extract the group id 16 def get_hosts (groupid ): 17 groupids = [groupid] 18 return zapi. host. get (groupids = groupids, output = "extend") # return all host information under the Group id 19 def get_drules (): 20 return zapi. drule. get (output = "extend") 21 def get_templates_by_names (template_names): 22 return zapi. template. get (filter = {"host": template_names}) 23 def create_group (gro Up_name): # create group 24 if not zapi. hostgroup. exists (name = group_name): 25 zapi. hostgroup. create (name = group_name) 26 def create_host (group_name, host_name, ip): # create a host and attach the specified template 27 groups = get_hostgroups (group_name) 28 host_name = host_name.lower () 29 ip_tail = ip. split (". ") [-1] 30 domain =" server-"+ ip_tail + ". 0. "+ host_name + ". ustack. in "31 for hostgroup in groups: 32 groupid = hostgroup ['groupid'] 33 ip_tail = ip. Split (". ") [-1] 34 role = None 35 for ru in rule: 36 range = rule [ru] ['range'] 37 if"-"in range: 38 head = range. split ("-") [0] 39 tail = range. split ("-") [1] 40 if int (ip_tail) <= int (tail) and int (ip_tail)> = int (head): 41 role = ru 42 else: 43 if ip_tail = range: 44 role = ru 45 template_names = rule [role] ['templates'] 46 template_ids = get_templates_by_names (template_names) 47 print domain, ip, Groupid, template_ids 48 zapi. host. create (host = domain, interfaces = [{49 "type": 1, 50 "main": 1, 51 "useip": 1, 52 "ip": ip, 53 "dns": "", 54 "port": '000000' 55}], groups = [{"groupid": groupid}], templates = template_ids) 56 print "Add Successfull !!!!! "57 # logging.info (" % s, % s Add Successfull !!!!! "% (Domain, ip, groupid, template_ids) 58 def create_macro (group_name, traffic, value): # create macro. Different hosts have different macro 59 groups = get_hostgroups (group_name) 60 for group in groups: 61 hosts = get_hosts (group ['groupid']) 62 for host in hosts: 63 hostname = host ["name"] 64 hostid = host ["hostid"] 65 if not re. search ("^ server", hostname): continue 66 m = re. search ("[0-9] +", hostname ). group () 67 if m = "1": continue 68 if m in ['64', '65', '66', '67']: 69 zapi. host. update (hostid = hostid, macros = [{"macro": "{$ indium}", "value": "35000"}, 70 {"macro ": "{$ OUP}", "value": "35000"}, 71 {"macro": "{$ INT}", "value ": "% s" % traffic}, 72 {"macro": "{$ OUT}", "value": "% s" % traffic}, 73 {"macro ": "{$ PDISK}", "value": "% s" % value}]) 74 else: 75 zapi. host. update (hostid = hostid, macros = [{"macro": "{$ PDISK}", "value": "% s" % value}]) 76 print hostname, hostid, m, traff Ic, value 77 if _ name _ = "_ main _": 78 zapi = login () 79 region = "qn" 80 host_list = ["31 ", "32", "35", "36", "39", "40", "44", "45", "46", "47", "48 ", "49", "50", "53", "54", "61", "62", "63", "64", "65 ", 81 "68", "69", "70", "71", "72", "73", "74", "75", "76 ", "77", "79", "80", "81", "82", "83", "84", "85", "86", "87 ", "88", "89", "90", "91"] # Add a host. We do not recommend that you use discovery 82 ip_list = host_list 83 if type (ip_list) = str: 84 print "% s Must be a list, Please checking !!! "% Sys. argv [2] 85 sys. exit () 86 group_name = "Region [% s 0]" % region. upper () 87 if not zapi. hostgroup. exists (name = group_name): 88 create_group (group_name) 89 ip = {"qn": "10.4.0. "} 90 if region in ip: 91 for num in ip_list: 92 value =" 20 "93 traffic =" 300 M "94 ipaddress = ip [region] + str (num) 95 print group_name, region, ipaddress 96 create_host (group_name, region, ipaddress) # pass the parameter to function 97 time. sleep (5) 98 create_macro (group_name, traffic, value) 99 else: 100 print "you input region error, please checking"

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.