评论

收藏

[JavaScript] zabbix利用API批量添加Screen

开发技术 开发技术 发布于:2021-06-23 22:04 | 阅读数:583 | 评论:0

公司服务器太多,如果要一个一个的手动添加Screen的话,那估计我早累死了,所以呢,懒人必备神器,使用Python写了个批量Screen添加和更新的脚本。
说下此脚本的功能和用处吧1、如果Screen不存在,可以使用此脚本批量添加Screen2、如果Screen存在,也可以使用此脚本批量更新Screen3、此脚本会判断主机是否存在,不存在,会直接退出,然后列出可以使用的主机列表
好了,就这么多了,下面贴代码。
#!/usr/bin/pythoncoding:utf-8使用说明脚本可以不接受参数,不接受参数则是全部主机更新脚本接受一个或两个参数第一个参数是主机,第二参数是Screen名称省略第二个参数,Screen则与主机名称一样./CUScreen.py ./CUScreen.py 主机名称 Screen名称./CUScreen.py Host_Name Screen_Name./CUScreen.py web01 ./CUScreen.py web01 web01_screen
import urllib2,json,sys
def res(url="", values=""):  
data = json.dumps(values)
# 请求API
res = urllib2.Request(url, data, {"Content-Type": "application/json"})
resp = urllib2.urlopen(res, data)
out = json.loads(resp.read())
try:
    error = out["result"]
  except:
    error = out["error"]["data"]
    return error
  return out["result"]
  def auth(url="", username="", password=""):
  values = {
"jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "user": username,
            "password": password,
        },
        "id": 0,
  }
  id = res(url, values)
  return id
  def gethosts(auth=""):
  values = {
"jsonrpc":"2.0",
        "method":"host.get",
        "params":{
            "output":{"hostid":"name"},
        },
        "auth":auth,
        "id": 1
       }
  out = res(url, values)
  print "Host Count :",len(out)
  for host in out:
    print "Host ID : %s Host Name : %s" % (host['hostid'],host['name'])
  return out
  def getgraphs(auth="", hostname=""):
  values = {
"jsonrpc":"2.0",
        "method":"graph.get",
        "params":{
            # 过滤某个主机的Graph
            "filter":{"host":hostname}
        },
        "auth":auth,
        "id": 1
  }
  out = res(url, values)
  # 判断主机是否存在
  if out == []:
    print hostname,"not is exists"
    print "以下是存在的主机:"
    gethosts(auth)
    sys.exit(1)
  # 定义Screenitem的参数,为空列表
  graphs_list = []
  y = 0
  for i in out:
    graphs_list.append(
       {
        "resourcetype": 0,
        # 添加graphid
        "resourceid": i["graphid"],
        # 定义宽度
        "width":1000,
        # 定义高度
        "height":100,
        # 是否为动态图形
        "dynamic":1,
        # 坐标x轴,向右
        "x": 0,
        # 坐标y轴,向下
        "y": y,
       }
    )
    y += 1
  return graphs_list
C代表创建 U代表更新

  def CUScreen(auth="", screen_name="", graphs_list=""):
  # 判断Screen是否存在
  values1 = {
"jsonrpc": "2.0",
        "method": "screen.get",
        "params": {
            "output": "extend",
            # 过滤Screen
            "filter":{"name":screen_name},
        },
        "auth": auth,
        "id": 1
  }
  # 定义创建Screen的数据体
  values2 = {
        "jsonrpc": "2.0",
        "method": "screen.create",
        "params": {
            "name":screen_name,
            # Screen的宽度
            "hsize":1,
            # Screen的长度
            "vsize":len(graphs_list),
            # Screen的item信息
            "screenitems": graphs_list,
        },
        "auth": auth,
        "id": 1
  }
  # 使用values1进行判断
  out = res(url, values1)
  #进行ScreenID判断
  if out == []:
    # ScreenID不存在则创建ScreenID并添加item
    print screen_name,"Screen Name Not is exists, Create Screen Name :",screen_name
    # 使用values2
    out = res(url, values2)
    print screen_name,"Screen Name is Create OK, Screen Item Create OK"
    return out
  else:
    screen_id = out[0]['screenid']
    # 定义更新Screen的数据体
    values3 = {
          "jsonrpc": "2.0",
          "method": "screen.update",
          "params": {
              # ScreenID
              "screenid":screen_id,
              # 计算Screen的行数
              "vsize":len(graphs_list),
              # Screen的item
              "screenitems":graphs_list,
          },
          "auth": auth,
          "id": 1
    }
    # ScreenID存在则更新所有的item
    print screen_name,"Screen Name is exists, Screen ID :",screen_id
    # 使用values3
    out = res(url, values3)
    print screen_name,"Screen Item Update OK"
    return out
  if __name__ == "__main__":
  # zabbix的API地址,不同环境的不一样
  url = "http://zabbix.xxx.com/api_jsonrpc.php"
  # zabbix的登陆账号
  username = "xxx"
  # zabbix的登陆密码
  password = "xxx"
  # 默认不输入参数则全部主机都进行更新
  if len(sys.argv) == 1:
# 获取认证ID
    auth = auth(url,username,password)
    for i in gethosts(auth):
      # 获取GraphID
      graphs_list = getgraphs(auth,i['name'])
      # 使用主机名称(hostname变量)当Screen名称
      CUScreen(auth,i['name'],graphs_list)
  # 如果你输入一个参数,Screen名称与主机名称一样
  if len(sys.argv) == 2:
    # 主机名称,赋值为hostname变量
    hostname = sys.argv[1]
    # 获取认证ID
    auth = auth(url,username,password)
    # 获取GraphID
    graphs_list = getgraphs(auth,hostname)
    # 使用主机名称(hostname变量)当Screen名称
    CUScreen(auth,hostname,graphs_list)
  # 如果你输入二个参数,第一个为主机名称,第二个为Screen名称
  if len(sys.argv) == 3:
    # 主机名称,赋值为hostname变量
    hostname = sys.argv[1]
    # Screen名称,赋值为screen_name变量
    screen_name = sys.argv[2]
    # 获取认证ID
    auth = auth(url,username,password)
    # 获取GraphID
    graphs_list = getgraphs(auth,hostname)
    # 使用screen_name(screen_name变量)来当Screen名称
       CUScreen(auth,screen_name,graphs_list)



DSC0000.gif

关注下面的标签,发现更多相似文章