Skip to main content

Python based Zabbix API utility with helper functions

Project description

pybix

NOTE: This module is still in development and may not be fully stable. Use at own risk.

Description

Python based Zabbix API utility containing helper functions and CLI capabilities.

Takes inspiration from existing Python-Zabbix API modules like lukecyca/pyzabbix and adubkov/py-zabbix.

While this module can be used in a similar way, the aim is to add a few out of the box helper functions and CLI handling for a more "batteries included" module. For example GraphImage as described in usage which enables saving Zabbix graphs which is not possible via the API at this time.

Install

Pip

pip install pybix

Docker

TODO - not yet available.

Requirements

  • Python 3.6 or greater
  • Zabbix 2.0 or greater
    • Only tested on >4.0

Usage

Zabbix API

Refer to Zabbix Offical API object references for objects that can be queried and their parameters.

API structure all uses format like ZAPI.<object>.<action>(<parameters>) e.g. ZAPI.host.get(output='extend').

Import as Python module

Directly
from pybix import ZabbixAPI
ZAPI = ZabbixAPI(url="http://localhost/zabbix")
ZAPI.login(user="Admin", password="zabbix")

# Print all monitored hosts
for host in ZAPI.host.get(output="extend",monitored_hosts=1):
    print(host['host'])

ZAPI.logout() # Explicitly logout to clear Zabbix session
With context manager to handle logout

Note: Login still must be done manually (as in the future we may allow passing existing session, hence might not need to login everytime).

from pybix import ZabbixAPI

with ZabbixAPI() as ZAPI: # using defaults for server
    ZAPI.login() # using defaults for user, password

    # Print all monitored hosts
    for host in ZAPI.host.get(output="extend",monitored_hosts=1):
        print(host['host'])

Zabbix API CLI

Zabbix API CLI Usage
Usage:
    pybix.py <method> [--zabbix-server=ZABBIX_SERVER] [--zabbix-user=ZABBIX_USER]
            [--zabbix-password=ZABBIX_PASSWORD] [--ssl-verify] [(-v | --verbose)] [<args> ...]
    pybix.py (-h | --help)
    pybix.py --version

Arguments:
  method        either Zabbix API reference as '<object>.<action>' or GraphImage API as 'graphimage.<search_type>' (e.g. 'host.get' or 'graphimage.graph_id')
  args          what arguments to pass to API call

Options:
  -h, --help
  --version
  -v, --verbose                      Whether to use verbose logging [default: False]
  --output-path=PATH                 Where to save graphs to default: cwd
  --zabbix-server=ZABBIX_SERVER      [default: https://localhost/zabbix]
  --zabbix-user=ZABBIX_USER          [default: Admin]
  --zabbix-password=ZABBIX_PASSWORD  [default: zabbix]
  --ssl-verify                       Whether to use SSL verification for API [default: True]
Zabbix API CLI Example
python -m pybix host.get filter="{host:server1}" # Get host server1
python -m pybix host.get filter="{host:[server1,server2]}" # Get host server1 and server2
python -m pybix user.get # Get all Users

Graph Image Export

Zabbix does not let you export graphs via API (only the configuration for them). Instead of using ZabbixAPI class, use included GraphImage.

GraphImage Python Example

from pybix import GraphImageAPI
graph = GraphImageAPI(url="http://localhost/zabbix",
                      user="Admin",
                      password="zabbix")
graph.get_by_graphid("4038") # will save to png file in current working directory
graph.get_by_graphname("CPU") # will save any "CPU" graph png images to file in current working directory

GraphImage CLI

GraphImage CLI Usage

Refer to ZabbixAPI usage.

search_types include graph_id, graph_name, item_names, item_keys, item_ids

GraphImage CLI Examples
python -m pybix graphimage.graph_name graph_name=CPU host_names=server1
python -m pybix graphimage.graph_name graph_name=CPU host_names=[server1,server2]
python -m pybix graphimage.item_names item_names=CPU host_names=server1
python -m pybix graphimage.item_keys item_keys=availability.agent.available host_names=server1

# Not as useful, but is what above methods call after calculating id
python -m pybix graphimage.graph_id graph_id=4038 host_names=server1
python -m pybix graphimage.item_ids item_ids=138780,138781 host_names=server1

Known Issues

SSL Verification

  • If server using a self signed cert or serving on HTTPS, will need to use ssl_verify overide

User configuration

  • Zabbix user used during API calls must have viewing rights to queried Zabbix object
    • i.e. appropriate hostgroup read rights to user/usergroup OR super admin
    • If it does not, it will simply return empty results without warning

Graph Items Usage

  • User used to login must have frontend access (i.e. in Zabbix user group, set frontend access to true)
  • No error messages or warnings if graph is invalid (i.e. wrong values used to call it)

Contributing

Feel free to raise any feature requests/problems/improvements as issue or pull request via GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybix-0.0.8.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

pybix-0.0.8-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file pybix-0.0.8.tar.gz.

File metadata

  • Download URL: pybix-0.0.8.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.4

File hashes

Hashes for pybix-0.0.8.tar.gz
Algorithm Hash digest
SHA256 c80d005e3767bb851bf48aba6ddc9439bf478159a25950e9eef43847e50da530
MD5 0e9686545baf1ddc74d51c856899155b
BLAKE2b-256 741d5db87f3d654e6f7960be38d8e8ef835891989f7e02bfc56c077552f7f48e

See more details on using hashes here.

File details

Details for the file pybix-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: pybix-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.4

File hashes

Hashes for pybix-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 b770549a73ba5f1e57684ed1cab9fb2abff0e7104dd3be384e7d952ae5c148c9
MD5 42f01afaa8bd55a65876d2d9234c1509
BLAKE2b-256 269467c4c3e8ff4e9e091358b48a9137fe664eb2d0e585897ec1da137547604c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page