16. Create a screen using the Zabbix API

Source: Internet
Author: User

Zabbix's screen function allows graph to be aggregated and displayed uniformly,

Our requirement is to aggregate the same item of the same host group, such as a screen showing the memory usage of all hosts in the same set, to achieve a similar ganglia effect,

Because of the large number of servers, we call the Zabbix API to create them.


The reference address is as follows: Thank you, Daniel.

Https://www.zabbix.org/wiki/Python_script_to_create_Screen_from_all_Items/Graphs_of_a_host

http://caiguangguang.blog.51cto.com/1652935/1387401

http://john88wang.blog.51cto.com/2165294/1538456


The script is as follows: for v2.2.11

Import jsonimport urllib2import argparsedef 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 ',               ' method ':  ' Hostgroup.get ',               ' params ':  {                    ' output ':  ' extend ',                  &nBSP; ' Filter ':  {' name ': groupname},                    ' selecthosts ':  [' HostID ']               },                ' auth ': auth,                ' id ':  ' 2 '     }    output =  Requestjson (url, values)     for host in output[' result '][0][' hosts ']:         host_list.append (host[' HostID ')     return  host_listdef 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 '     values =  ({' Jsonrpc ':  ' 2.0 ',                 ' method ':  ' Graph.get ',                 ' params ':  {                     select: [selecttype,  ' name '],                     ' output ':  [' graphid ',  ' name '],                     ' Hostids ': host_list,                     ' filter ':  {' Name ': name_list},                     ' SortField ':  ' name '                 },                 ' auth ': auth,                 ' id ':  ' 3 '                &NBSP,     output = requestjson (url, values)      bb = sorted (output[' result ')     graphs = []    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 ':  ' 300 ' ,             ' height ':  ',              ' x ':  str (x),              ' y ':  str (y),              ' colspan ':  ' 0 ',              ' rowspan ':  ' 0 ',             ' elements ':   ' 0 ',             ' valign ':  ' 0 ',              ' halign ':  ' 0 ',              ' style ':  ' 0 ',            &nbSP; ' URL ':  ',             ' dynamic ':  str ( dynamic)         })         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)     output = requestjson (url, values) def main ():     url =  ' http://10.0.0.141/zabbix/api_jsonrpc.php '     username =  ' Admin '     password =  ' __q& (0sh1znadd '     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 servers-g ' Network traffic on Em1 '-C 4-n ' servers Network traffic on EM1 '


This article is from the "11062687" blog, please be sure to keep this source http://11072687.blog.51cto.com/11062687/1739870

16. Create a screen using the Zabbix API

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.