本文整理汇总了Python中pyzabbix.ZabbixAPI.do_request方法的典型用法代码示例。如果您正苦于以下问题:Python ZabbixAPI.do_request方法的具体用法?Python ZabbixAPI.do_request怎么用?Python ZabbixAPI.do_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyzabbix.ZabbixAPI
的用法示例。
在下文中一共展示了ZabbixAPI.do_request方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_web_scenario
# 需要导入模块: from pyzabbix import ZabbixAPI [as 别名]
# 或者: from pyzabbix.ZabbixAPI import do_request [as 别名]
def create_web_scenario(self,name,url,group,hostid,applicationid, url_name='Homepage',status='200'):
request = ZabbixAPI.do_request(self, 'webcheck.get', params={ "filter": {"name": name}})
if request['result']:
print('Host "%s" already registered' % name)
sys.exit(1)
else:
try:
ZabbixAPI.do_request(self, 'webcheck.create',params={"name": name,"hostid": hostid,"applicationid": applicationid, "delay": '60',"retries": '3', "steps": [ { 'name': url_name, 'url': url,'status_codes': status, 'no': '1'} ] } )
triggers = create_trigger(auth,name,url,group)
except Exception, e:
print(e)
sys.exit(1)
示例2: ZabbixEventAck
# 需要导入模块: from pyzabbix import ZabbixAPI [as 别名]
# 或者: from pyzabbix.ZabbixAPI import do_request [as 别名]
class ZabbixEventAck(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
return
def status_change(self, alert, status, text):
if alert.event_type != 'zabbixAlert':
return
if alert.status == status or not status in ['ack', 'closed']:
return
event_id = alert.attributes.get('eventId', None)
trigger_id = alert.attributes.get('triggerId', None)
if not event_id:
LOG.error('Zabbix: eventId missing from alert attributes')
return
# login to zabbix
zabbix_api_url = ZABBIX_API_URL or alert.attributes.get('zabbixUrl', DEFAULT_ZABBIX_API_URL)
self.zapi = ZabbixAPI(zabbix_api_url)
self.zapi.login(ZABBIX_USER, ZABBIX_PASSWORD)
LOG.debug('Zabbix: acknowledge (%s) event=%s, resource=%s (triggerId=%s, eventId=%s) ', status, alert.event, alert.resource, trigger_id, event_id)
if status == 'ack':
try:
r = self.zapi.event.get(objectids=trigger_id, acknowledged=False, output='extend', sortfield='clock', sortorder='DESC', limit=10)
event_ids = [e['eventid'] for e in r]
except ZabbixAPIException:
event_ids = None
LOG.debug('Zabbix: status=ack; triggerId %s => eventIds %s', trigger_id, ','.join(event_ids))
try:
LOG.debug('Zabbix: ack all events for trigger...')
r = self.zapi.event.acknowledge(eventids=event_ids, action=(ACTION_ACK | ACTION_MSG), message='%s: %s' % (status, text))
except ZabbixAPIException:
try:
LOG.debug('Zabbix: ack all failed, ack only the one event')
r = self.zapi.event.acknowledge(eventids=event_id, action=(ACTION_ACK | ACTION_MSG), message='%s: %s' % (status, text))
except ZabbixAPIException as e:
raise RuntimeError("Zabbix: ERROR - %s", e)
finally:
self.zapi.do_request('user.logout')
LOG.debug('Zabbix: event.acknowledge(ack) => %s', r)
text = text + ' (acknowledged in Zabbix)'
elif status == 'closed':
try:
r = self.zapi.event.get(objectids=trigger_id, acknowledged=True, output='extend', sortfield='clock', sortorder='DESC', limit=10)
event_ids = [e['eventid'] for e in r]
except ZabbixAPIException:
event_ids = None
LOG.debug('Zabbix: status=closed; triggerId %s => eventIds %s', trigger_id, ','.join(event_ids))
try:
LOG.debug('Zabbix: close all events for trigger...')
r = self.zapi.event.acknowledge(eventids=event_ids, action=(ACTION_CLOSE | ACTION_MSG), message='%s: %s' % (status, text))
except ZabbixAPIException:
try:
LOG.debug('Zabbix: ack all failed, close only the one event')
r = self.zapi.event.acknowledge(eventids=event_id, action=(ACTION_CLOSE | ACTION_MSG), message='%s: %s' % (status, text))
except ZabbixAPIException as e:
raise RuntimeError("Zabbix: ERROR - %s", e)
finally:
self.zapi.do_request('user.logout')
LOG.debug('Zabbix: event.acknowledge(closed) => %s', r)
text = text + ' (closed in Zabbix)'
return alert, status, text
示例3: MonitoringDataCP
# 需要导入模块: from pyzabbix import ZabbixAPI [as 别名]
# 或者: from pyzabbix.ZabbixAPI import do_request [as 别名]
class MonitoringDataCP():
zapi = None
def __init__(self):
self.zapi = ZabbixAPI(config.zabbix_uri)
def __zabbix_login(self):
self.zapi.login(config.zabbix_user,config.zabbix_pass)
def __zabbix_logout(self):
self.zapi.do_request('user.logout')
def __check_timestamp(self,timestamp,now_time,ts_type):
# If timestamp_type is 1.
if ts_type == const.TYPE_TS_REP:
# to replace timestamp with now_time if timestamp is smaller than "now_time - interval"
if int(timestamp) < int(now_time) - interval:
return str(now_time)
return str(timestamp)
def __get_monitoring_data(self,node_name,node_type,now_time):
logger.debug("get monitoring-data.")
# Query item[status]
item_sts = ""
target_item_list = None
if node_type == const.TYPE_NODE_SRV:
item_sts = const.ZBX_SRV_STS
target_item_list = config.zabbix_cp_mon_item_srv_list
elif node_type == const.TYPE_NODE_VM:
item_sts = const.ZBX_VM_STS
target_item_list = config.zabbix_cp_mon_item_vm_list
item_sts = item_sts + '[{0}]'.format(node_name)
items = self.zapi.item.get(output=['hostid','lastclock','lastvalue']
,filter={"key_":item_sts})
if not len(items):
logger.warn('item is not found.({0})'.format(item_sts))
return None
elif len(items) != 1:
logger.warn('item is not one.({0})'.format(item_sts))
return None
logger.debug(items)
# Print out each datapoint
val_list = list()
for item in items:
val_dict = dict()
val_dict['status'] = item['lastvalue']
val_dict['timestamp'] = self.__check_timestamp(item['lastclock'],now_time,const.TYPE_TS_REP)
val_list.append(val_dict)
hostid = item['hostid']
for target_item in target_item_list:
# Query item[set in the configuration file values]
items = self.zapi.item.get(output=['lastclock','lastvalue']
,filter={"hostid":hostid,"key_":target_item['item-name']})
# If nothing was found, try getting it from history
if not len(items):
logger.warn('item is not found.({0})'.format(target_item['item-name']))
continue
elif len(items) != 1:
logger.warn('item is not one.({0})'.format(item_sts))
logger.debug(items)
# Print out each datapoint
for item in items:
val_dict = dict()
val_dict[target_item['data-name']] = item['lastvalue']
val_dict['timestamp'] = self.__check_timestamp(item['lastclock'],now_time,target_item['ts-type'])
val_list.append(val_dict)
res_dict = {'node_name':node_name,'node_type':node_type,'val_list':val_list}
return res_dict
def main(self):
# get now time.(UTC:0)
now_time = calendar.timegm(datetime.utcnow().timetuple())
try:
print(COL_NAME + ' -start-')
logger.info(COL_NAME + ' -start-')
# open topology database connection.
tpldb_setup()
# open zabbix connection.
self.__zabbix_login()
node_list =list()
# get all of the VM from DB.
vm_list = get_all_vm()
if vm_list:
node_list.extend(vm_list)
# get all of the Server from DB.
srv_list = get_all_srv()
if srv_list:
#.........这里部分代码省略.........