Last active
November 2, 2018 18:47
-
-
Save wgzhao/8064677 to your computer and use it in GitHub Desktop.
Create a zabbix screen by specific item or graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # encoding:utf8 | |
| __Author__ = 'zhaoweiguo<wgzhao@gmail.com>' | |
| ''' | |
| create zabbix screen,specified graph name,rows x cols | |
| script will get all hosts whose include graph name ,then write screens and screens_items table | |
| Usage: $0 -s <screen name> -g <graph name> [ -c <cols> ] [-d ] [ -t ] | |
| ''' | |
| from optparse import OptionParser | |
| import MySQLdb as mydb | |
| from sys import exit | |
| global cur | |
| try: | |
| conn = mydb.connect('127.0.0.1','user','password','zabbix') | |
| cur = conn.cursor() | |
| except Exception,err: | |
| print "%s" % str(err) | |
| #exit(1) | |
| def getGraph(graphname): | |
| ''' | |
| get graph id list from graph name | |
| ''' | |
| try: | |
| cur.execute("select graphid from graphs where name = '%s' and templateid > 0" % (graphname,)) #filter template | |
| data = cur.fetchall() | |
| if data: | |
| return data | |
| else: | |
| return False | |
| except Exception,err: | |
| print "connect to database error %s" % (str(err)) | |
| def hasScreenName(screenname): | |
| ''' | |
| has exists screen name ,return true ,otherwise return false | |
| ''' | |
| cur.execute("select screenid from screens where name = '%s'" % screenname ) | |
| screenid=cur.fetchone() | |
| if screenid: | |
| return True | |
| else: | |
| return False | |
| def putScreen(screenname,columns,rows): | |
| ''' | |
| insert screen name into screens table | |
| return screenid | |
| ''' | |
| #insert a new screen name | |
| #get max screenid | |
| try: | |
| cur.execute('select max(screenid) from screens limit 1') | |
| screenid=cur.fetchone()[0] + 1 | |
| cur.execute("insert into screens(`screenid`,`name`,`hsize`,`vsize`) values(%d,'%s',%d,%d) " % (screenid,screenname,columns,rows)) | |
| conn.commit() | |
| return screenid | |
| except Exception,myerr: | |
| return False | |
| def putScreenItems(screenid,graphids,totalnumber,rows,columns,dynamic): | |
| #get max screenitemid | |
| try: | |
| cur.execute('select max(screenitemid) from screens_items limit 1') | |
| screenitemid = cur.fetchone()[0] + 1 | |
| num=0 | |
| for y in range(rows): | |
| for x in range(columns): | |
| sql="insert into screens_items(`screenitemid`,`screenid`,`resourceid`,`x`,`y`,`dynamic`) values(%d,%d,%d,%d,%d,%d)" % (screenitemid,screenid,graphids[num][0],x,y,dynamic) | |
| cur.execute(sql) | |
| screenitemid+=1 | |
| num+=1 | |
| if num >= totalnumber: | |
| break | |
| conn.commit() | |
| return True | |
| except Exception,myerr: | |
| return False | |
| def listGraphs(): | |
| ''' | |
| get all graph names and print them | |
| ''' | |
| cur.execute('select DISTINCT name from graphs where templateid >0') | |
| graphList = cur.fetchall() | |
| cols=3 #print 3 graph names each line | |
| id=1 | |
| for g in graphList: | |
| print '"' + g[0] +'"' + "\t", | |
| if id >cols: | |
| id=1 | |
| id+=1 | |
| def main(): | |
| # parser = argparse.ArgumentParser(description='Create Zabbix screen from all of a host Items or Graphs.') | |
| parser = OptionParser(description='Create Zabbix screen from all of a host Items or Graphs.',\ | |
| version='0.1',\ | |
| usage='%prog [options]') | |
| parser.add_option('-s', dest='screenname', type=str, | |
| help='Screen name in Zabbix. Put quotes around it if you want spaces in the name.') | |
| parser.add_option('-g',dest='graphname',type=str, | |
| help='graph name in Zabbix,you can get it through find graph table') | |
| parser.add_option('-c', dest='columns', type=int, default=2, | |
| help='number of columns in the screen (default: 2)') | |
| parser.add_option('-d', dest='dynamic', action='store_true', | |
| help='enable for dynamic screen items (default: disabled)') | |
| parser.add_option('-l', dest='list',action='store_true',help='list all graph name and id') | |
| (options,args) = parser.parse_args() | |
| if options.screenname == None or options.graphname == None: | |
| parser.print_help() | |
| if options.list: | |
| listGraphs() | |
| exit(0) | |
| screen_name = options.screenname | |
| graph_name = options.graphname | |
| columns = options.columns | |
| dynamic = (1 if options.dynamic else 0) | |
| graphids = getGraph(graph_name) | |
| if not graphids: | |
| print "can not find any graph ids by your given name(%s)" % graph_name | |
| exit(2) | |
| cnt = len(graphids) | |
| rows = int(round(cnt * 1.0 / columns )) | |
| if hasScreenName(screen_name): | |
| print "screen name: (%s) has exits." % screen_name | |
| exit(2) | |
| screenid=putScreen(screen_name,columns,rows) | |
| if not screenid: | |
| print "insert screen record failed: %s " % str(myerr) | |
| exit(2) | |
| result=putScreenItems(screenid,graphids,cnt,rows,columns,dynamic) | |
| if not result: | |
| print "insert screen items failed: %s" % str(myerr) | |
| conn.close() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment