浏览量 7341
2018/03/03 02:31
config/mysql_config.json
{
"ip":"127.0.0.1",
"port":3306,
"username":"root",
"password":"root",
"database":"monitor",
"charset":"utf8"
}
config/monitor.json
{
"3":["10.80.3.16","net.if","eth0","-1d","800","300"],
"4":["10.80.3.16","system.cpu.util","","-1d","600","200"],
"5":["10.80.3.16","system.cpu.load","","-1d","800","300"],
"6":["10.80.3.16","vm.memory.size","","-1d","800","300"]
}
dbtool.py
# -*- coding:utf-8 -*-
#!/bin/env python
import sys
import logw
import json
import time
import os
import MySQLdb
class imageMaker(object):
def __init__(self, zb, **dic):
self.ip = dic['ip']
self.monitortype = dic['monitortype']
self.monitorsub = dic['monitorsub']
self.timespan = dic['timespan']
self.width = str(dic['width'])
self.height = str(dic['height'])
pwd = os.getcwd()
logname = sys.argv[0].split("/")[len(sys.argv[0].split("/")) - 1][0:-3]
logdir = '{p}/log/'.format(p=pwd)
if not os.path.exists(logdir):
os.mkdir(logdir)
logw.log_w(20, logdir + self.ip + self.monitortype + self.monitorsub + logname + '.log')
self.zabbixtool = zb
print "开始获取所有监控项信息"
self.allitemdictlist = zb.getAllItem()
def getMonitorItemData(self):
param_list = []
sql = """insert into zabbix_trans_log(clock,clocktime,hostid,hostname,itemid,itemname,value) select %s,%s,%s,%s,%s,%s,%s
from dual where not exists(select 1 from zabbix_trans_log where clocktime=%s and itemid=%s)"""
for item in self.allitemdictlist:
if 'net' in self.monitortype:
if self.monitortype in item['itemname'] and self.ip in item['hostname']:
print self.zabbixtool.getItemHistory(item['itemid'], data_type=3)
clock = self.zabbixtool.getItemHistory(item['itemid'], data_type=3)[0]['clock']
clocktime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(clock)))
hostid = item['hostid']
hostname = item['hostname']
itemid = item['itemid']
itemname = item['itemname']
value = self.zabbixtool.getItemHistory(item['itemid'], data_type=3)[0]['value']
param = (clock, clocktime, hostid, hostname, itemid, itemname, value,clocktime,itemid)
param_list.append(param)
self.writeDb(sql, param)
if 'cpu.util' in self.monitortype:
if self.monitortype in item['itemname'] and self.ip in item['hostname']:
clock = self.zabbixtool.getItemHistory(item['itemid'], data_type=0)[0]['clock']
clocktime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(clock)))
hostid = item['hostid']
hostname = item['hostname']
itemid = item['itemid']
itemname = item['itemname']
value = self.zabbixtool.getItemHistory(item['itemid'], data_type=0)[0]['value']
param = (clock, clocktime, hostid, hostname, itemid, itemname, value,clocktime,itemid)
param_list.append(param)
self.writeDb(sql, param)
if 'cpu.load' in self.monitortype:
if self.monitortype in item['itemname'] and self.ip in item['hostname']:
clock = self.zabbixtool.getItemHistory(item['itemid'], data_type=0)[0]['clock']
clocktime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(clock)))
hostid = item['hostid']
hostname = item['hostname']
itemid = item['itemid']
itemname = item['itemname']
value = self.zabbixtool.getItemHistory(item['itemid'], data_type=0)[0]['value']
param = (clock, clocktime, hostid, hostname, itemid, itemname, value,clocktime,itemid)
param_list.append(param)
self.writeDb(sql, param)
if 'vm.memory.size' in self.monitortype:
if self.monitortype in item['itemname'] and self.ip in item['hostname']:
if 'available' in item['itemname'] or 'total' in item['itemname']:
zb_gethistory = self.zabbixtool.getItemHistory(item['itemid'], data_type=3)[0]
clock = zb_gethistory['clock']
clocktime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(clock)))
hostid = item['hostid']
hostname = item['hostname']
itemid = item['itemid']
itemname = item['itemname']
value = zb_gethistory['value']
param = (clock,clocktime,hostid,hostname, itemid, itemname, value,clocktime,itemid)
param_list.append(param)
self.writeDb(sql, param)
if 'vfs.fs.size' in self.monitortype:
if self.monitortype in item['itemname'] and self.ip in item['hostname'] and 'pfree' not in item['itemname'] and ',used' not in item['itemname']:
zb_gethistory = self.zabbixtool.getItemHistory(item['itemid'], data_type=3)[0]
clock = zb_gethistory['clock']
clocktime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(clock)))
hostid = item['hostid']
hostname = item['hostname']
itemid = item['itemid']
itemname = item['itemname']
value = zb_gethistory['value']
param = (clock,clocktime,hostid,hostname, itemid, itemname, value,clocktime,itemid)
param_list.append(param)
self.writeDb(sql, param)
return param_list
def writeDb(self,sql,param):
'''
写数据库信息
'''
sqlconfigfile = open('.' + os.sep + 'config' + os.sep + 'mysql_config.json')
sqlconfigdict = json.load(sqlconfigfile)
conn = MySQLdb.connect(host=sqlconfigdict['ip'], port=sqlconfigdict['port'],
user=sqlconfigdict['username'], passwd=sqlconfigdict['password'],
db=sqlconfigdict['database'], charset=sqlconfigdict['charset'])
cur = conn.cursor()
try:
cur.execute(sql, param)
conn.commit()
except Exception , e:
print e
conn.rollback()
cur.close()
conn.close()
logw.py
# -*- coding:utf-8 -*-
#!/bin/env python
import logging
def log_w(_debuglevel, _filename):
logging.basicConfig(level=_debuglevel,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=_filename,
filemode='a')
run.py
# -*- coding:utf-8 -*-
#!/bin/env python
from zabbixTool import *
from dbtool import imageMaker
import json
import os
import time
from multiprocessing import Pool
def multirun(args):
pid = os.getpid()
print "当前进程pid号 %s " % pid
api_url = 'http://10.10.10.10/zabbix/api_jsonrpc.php'
user_name = 'admin'
pass_word = 'password'
uid = args[0]
zb = zabbixTool(api_url=api_url, user_name=user_name, pass_word=pass_word, uid=uid) # 初始化参数
print "当前进程参数列表 %s " % args
immk = imageMaker(zb, ip=args[1], monitortype=args[2], monitorsub=args[3], timespan=args[4], width=args[5],
height=args[6])
while True:
try:
immk.getMonitorItemData()
except Exception , e:
print 'immk.getMonitorItemData()',e
zb.logOut()
time.sleep(61)
if __name__ == '__main__':
args = []
jfile = open('.'+os.sep+'config'+os.sep+'monitor.json')
jdict = json.load(jfile) # 获取配置文件信息
for key in jdict:
jdict[key].insert(0, key)
args.append(jdict[key]) # 以列表的方式获取配置参数
print "获取配置文件列表 \n %s" % args
try:
monitorpool = Pool(len(args) + 1) # 以多进程的方式启动调用上方multirun函数
monitorpool.map_async(multirun, args)
monitorpool.close()
monitorpool.join()
except Exception as e:
print e
zabbixTool.py
# -*- coding: utf-8 -*-
# !/usr/bin/python
import json
import urllib2
import os
class zabbixTool(object):
def __init__(self, api_url, user_name, pass_word, uid):
self._api_url = api_url
self._user_name = user_name
self._pass_word = pass_word
self._uid = uid
self._auid = self.getAuthId()
@property
def api_url(self):
return self._api_url
@property
def user_name(self):
return self._user_name
@property
def pass_word(self):
return self._pass_word
@property
def uid(self):
return self._uid
def postData(self, jdata):
"""
post方法
"""
req = urllib2.Request(self._api_url, jdata, {"Content-Type": "application/json"})
response = urllib2.urlopen(req)
content = json.load(response)
return content['result']
def getAuthId(self):
'''
获取认证Auth
:return:
'''
dict_data = {}
dict_data['jsonrpc'] = '2.0'
dict_data['method'] = 'user.login'
dict_data['params'] = {'user':self._user_name, 'password':self._pass_word}
dict_data['id'] = self._uid
jdata = json.dumps(dict_data)
content = self.postData(jdata)
print 'getAuthId:', content, os.getpid()
return content
def getHostGroupList(self):
'''
获取主机组列表
:return:
'''
dict_data = {}
dict_data['jsonrpc'] = '2.0'
dict_data['method'] = 'hostgroup.get'
dict_data['params'] = {'output':['groupid' ,'name']}
dict_data['auth'] = self._auid
dict_data['id'] = self._uid
jdata = json.dumps(dict_data)
content = self.postData(jdata)
# print 'GetHostGroup:', content
return content
def getHostList(self,groupid):
'''
获取主机列表
'''
dict_data = {}
dict_data['jsonrpc'] = '2.0'
dict_data['method'] = 'host.get'
dict_data['params'] = {'output':['hostid' ,'name'],'groupids':groupid}
dict_data['auth'] = self._auid
dict_data['id'] = self._uid
jdata = json.dumps(dict_data)
content = self.postData(jdata)
# print '主机列表:gethostone:', content
return content
def getItemList(self,hostid):
'''
获取某个主机中某项监控对于id号
'''
dict_data = {}
dict_data['jsonrpc'] = '2.0'
dict_data['method'] = 'item.get'
dict_data['params'] = {'output':['itemids' ,'key_'],'hostids':hostid}
dict_data['auth'] = self._auid
dict_data['id'] = self._uid
jdata = json.dumps(dict_data)
content = self.postData(jdata)
# print 'getitem:', content
return content
def getItemHistory(self,itemid,data_type=3):
dict_data = {}
dict_data['jsonrpc'] = '2.0'
dict_data['method'] = 'history.get'
dict_data['params'] = {'output':'extend', 'history':data_type, 'itemids':itemid, 'sortfield':'clock',
'sortorder':'DESC', 'limit':1}
dict_data['auth'] = self._auid
dict_data['id'] = self._uid
jdata = json.dumps(dict_data)
content = self.postData(jdata)
# print 'getItemHistory:', content
return content
def getGraph(self,itemid):
dict_data = {}
dict_data['jsonrpc'] = '2.0'
dict_data['method'] = 'graph.get'
dict_data['params'] = {'output': 'extend','itemids':itemid,"sortfield": "name"}
dict_data['auth'] = self._auid
dict_data['id'] = self._uid
jdata = json.dumps(dict_data)
content = self.postData(jdata)
# print 'getGraph:', content
return content
def logOut(self):
"""
退出
:param uid:
:param authid:
:return:
"""
dict_data = {}
dict_data['method'] = 'user.logOut'
dict_data['id'] = self._uid
dict_data['jsonrpc'] = '2.0'
dict_data['params'] = []
dict_data['auth'] = self._auid
jdata = json.dumps(dict_data)
content = self.postData(jdata)
print 'logOut:', content
return content
def getAllItem(self):
'''
获取数据总控制函数
:return:
'''
# 获取主机组列表
hostgrouplist = self.getHostGroupList()
# 获取主机组id列表
hostgroupidlist = []
for hostgroup in hostgrouplist:
hostgroupidlist.append(hostgroup['groupid'])
print '主机组id列表:%s' % hostgroupidlist
# 获取主机列表
hostlist = []
for hostgroupid in hostgroupidlist:
hostlist.extend(self.getHostList(hostgroupid))
# print type(hostlist)
print '主机列表: %s' % hostlist
# 主机id和主机名字典化
hostdict = {}
for host in hostlist:
# print host['hostid'] # 主机id号
hostdict[host['hostid']] = host['name']
# 获取监控指标对于id号
itemdictlist = []
for id in hostdict:
itemlist = self.getItemList(id)
for item in itemlist:
itemdict = {}
itemdict['hostid'] = id
itemdict['hostname'] = hostdict[id]
itemdict['itemid'] = item['itemid']
itemdict['itemname'] = item['key_']
itemdictlist.append(itemdict)
print "监控指标对于id号"
# print '监控指标对于id号', itemdictlist
return itemdictlist
上一篇 搜索 下一篇