Create Zabbix screen (aggregate graphics) using a Python script.

Source: Internet
Author: User
Tags python script

Original article: http://11072687.blog.51cto.com/11062687/1739870

Recently found an article " Create screen using Zabbix API", and then download the script to test, found two questions:

1.3.x version is not supported.

2. The host name of the added screen is not arranged in order and is not convenient for querying.

I changed it, let's support the zabbix3.x version, and output screen (aggregate graphics) by machine name arrangement.

#!/bin/env pythonimport jsonimport urllib2import argparseimport rere_digits =  re.compile (R ' (\d+) ') def emb_numbers (s):     pieces=re_digits.split (s)      pieces[1::2]=map (Int,pieces[1::2])     return piecesdef sort_ Strings_with_emb_numbers2 (alist):     return sorted (alist, key=emb_numbers) def  requestjson (url, values):     data = json.dumps (values)      req = urllib2. Request (url, data, {' content-type ':  ' Application/json-rpc '})     res =  urllib2.urlopen (Req, data)     output = json.loads (Res.read ())      return outputdef authenticate (Url, username, password):     values = {' Jsonrpc ':  ' 2.0 ',               ' method ':  ' User.login ',                ' params ': {                    ' user ': username,                    ' Password ': password               },                ' id ':  ' 0 '     }     output = requestjson (url, values)     return output[' result ']def gethosts (Groupname, url, auth):    host_list = {}     values = {' Jsonrpc ':  ' 2.0 ',              &nbsP; ' Method ':  ' Hostgroup.get ',                ' params ': {                    ' output ':  ' extend ',                    ' filter ':  {' name ': groupname},                    ' selecthosts ':  [' Host ']              },                ' auth ': auth,                ' id ':  ' 2 '     }     output = requestjson (url, values)     for host  in output[' Result ' [0][' hosts ']:        host_list[host[' Host ']]= (host[' HostID '])       #return  host_list    hosts_sort = []     for host in sort_strings_with_emb_numbers2 (Host_list.keys ()):         hosts_sort.append (Host_list[host])     return  hosts_ Sortdef getgraphs (host_list, name_list, url, auth, columns, graphtype=0,  dynamic=0):    if  (graphtype == 0):         selecttype = [' Graphid ']        select =   ' selectgraphs '     if  (graphtype == 1):         selecttype = [' itemid ',  ' value_type ']         select =  ' SelectiTEMs '     graphs = []    for host in host_list :        values =  ({' Jsonrpc ':  ' 2.0 ',                     ' method ':   ' Graph.get ',                     ' params ': {                        select: [selecttype,  ' name '] ,                         ' output ':  [' graphid ',  ' name '],                         ' Hostids ':  host,                         ' filter ':  {' name ': name_list},                         ' SortField ':  ' Name '                     },                     ' auth ': auth,                     ' id ':  ' 3 '                     })          Output = requestjson (url, values)         bb =  sorted (output[' result '])         if  (graphtype == 0):             for i in bb:                 graphs.append (i[' Graphid '))          if  (graphtype == 1):             for i in bb:                 if int (i[' value_type ')  in  (0, 3):                      graphs.append (i[' itemid '))     graph_list = []     x = 0    y = 0    for graph in  graphs:         graph_list.append ({             ' ResourceType ': graphtype,             ' ResourceID ': graph,             ' width ':  ',             ' height ':  ',              ' x ':  str (x),              ' y ':  str (y),              ' colspan ':  ' 1 ',              ' rowspan ':  ' 1 ',         })          x += 1        if x ==  int (columns):             x = 0             y += 1    return graph_listdef  Screencreate (url, auth, screen_name, graphids, columns):     columns  = int (columns)     if len (graphids)  % columns == 0:         vsize = len (Graphids)  / columns     else:        vsize =  (Len (graphids)  /  columns)  + 1    values = {' jsonrpc ':  ' 2.0 ',                ' method ':  ' screen.create ',                ' params ': [{                    ' name ': screen_name,                    ' hsize ':  columns,                    ' Vsize ': vsize,                    ' Screenitems ': []               }],               ' Auth ': auth,               ' id ':  2               }     for i in graphids:        values[' params '][0][' Screenitems '].append (i)  &NBSp;  output = requestjson (url, values) Def main ():     url  =  ' http://ip/api_jsonrpc.php '     username =  ' admin '      password =  ' xxxxxxxxx '     auth = authenticate (url, username ,  password)     host_list = gethosts (Groupname, url, auth)      graph_ids = getgraphs (host_list, graphname, url, auth, columns )     screencreate (url, auth, screen_name, graph_ids, columns) if _ _name__ ==  ' __main__ ':     parser = argparse. Argumentparser ()     parser.add_argument ('-G ',  dest= ' groupname ',  nargs= ' + ',  Metavar= ' groupname ',  type=str, help= ' which group you want to select ')      parser.add_argument ('-G ',  dest= ' Graphname ',  nargs= ' + ',  metavar= ' graphname ',  type=str, help= ' which graph  you want to select ')     parser.add_argument ('-C ',  dest= ' columns ' ,  metavar= ' columns ',  type=int, help= ' the screen columns ')      Parser.add_argument ('-n ',  dest= ' screen_name ',  metavar= ' screen_name ',  type=str, help= ' the  screen name ')     args = parser.parse_args ()      groupname = args.groupname    graphname = args.graphname     columns = args.columns    screen_name = args.screen_name     main ()

How to use:

python create_screen.py-g hadoop-g ' Network traffic on bond0 '-C 4-n ' Hadoop bond0 '

-G to display the Zabbix group.

-G to display the Zabbix graphic.
-C Displays several columns, note to adjust the script: ' Width ':, ' height ': parameter to set the size.

-N the name displayed in screen.

The above command means: The Network traffic on each server in the HADDOP group on the Bond0 graph, followed by 4 graphics per column displayed in the Haddop bond0 screen.

This article is from the "Blue Blood attackers" blog, so be sure to keep this source http://fuckwindows.blog.51cto.com/57401/1907272

Create Zabbix screen (aggregate graphics) using a Python script.

Related Article

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.