python 通过zabbix api来获取当前trigger(告警)并打印出信息
2017-08-18 18:03
621 查看
直接看代码
#--encoding=utf-8 from pyzabbix import ZabbixAPI ###pyzabbix class pyzabbixAPI(object): def __init__(self): self.prioritytostr = {'0':'ok','1':'信息','2':'警告','3':'严重'} #告警级别 def login(self): ''' 进行认证 返回 api 接口 ''' zapi = ZabbixAPI('http://zabbixdomain.com') zapi.login('user', 'pwd') return zapi def getCurIssue(self,zapi): ''' 获取所有最近有问题的trigger 返回trigger的信息列表: ['trigger1','trigger2',......] ''' triggers = zapi.trigger.get( only_true=1, skipDependent=1, monitored=1, active=1, output='extend', expandDescription=1, selectHosts=['host'], ) # 获取未确认的trigger unack_triggers = zapi.trigger.get( only_true=1, skipDependent=1, monitored=1, active=1, output='extend', expandDescription=1, selectHosts=['host'], withLastEventUnacknowledged=1, ) unack_trigger_ids = [t['triggerid'] for t in unack_triggers] for t in triggers: t['unacknowledged'] = True if t['triggerid'] in unack_trigger_ids else False # 每个trigger信息格式 :[时间] 级别:ip - 详情 是否确认 triggerlist = [] for t in triggers: if int(t['value']) == 1: triggerlist.append("[{0}] {1} : {2}({3}) - {4} {5}".format( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(t['lastchange']))), self.prioritytostr[t['priority']], t['hosts'][0]['host'], self.getHostgroupName(zapi,t['hosts'][0]['host']), t['description'], '(Unack)' if t['unacknowledged'] else '' ) ) return triggerlist def getHostgroupName(self,zapi,hostname): ''' 通过hostname(即ip)获取host所在的监控组名 返回由组名组成的字符串 ''' groups = zapi.host.get( search={"name":hostname}, selectGroups=['name'], output=['groups'] )[0]['groups'] groupname = [group['name'] for group in groups] return ' '.join(groupname)
if __name__ == '__main__': papi = pyzabbixAPI() zapi = papi.login() print '\n'.join(papi.getCurIssue(zapi))
相关文章推荐
- python通过zabbix api获取主机
- 通过python获取kvm虚拟机的监控信息(基于libvirt API)
- 通过python获取kvm虚拟机的监控信息(基于libvirt API)
- Python从ZabbixAPI获取信息及实现Zabbix-API 监控的方法
- C语言程序获取或者打印当前语句所在的行号、当前源文件的文件名、程序编译的日期等信息(__LINE__、__FUNCTION__、__FILE__、__DATE__、__TIME__)
- 通过 GPS 信息获取当前所在城市名的代码
- 通过 GPS 信息获取当前所在城市名的代码
- activiti 工作流会签 / 多人审批时一人通过也可 / 在当前节点获取下一节点的信息 / 流程部署之后发布之前获取所有节点的信息 / 流程启动开始之前传送之后节点办理人 / 同一结点设置多个监听
- 使用 python 获取 Linux 系统信息(通过dmidecode命令)
- 使用python编写脚本获取手机当前应用apk的信息
- 利用Zabbix API 获取各个分组下的服务器列表以及详细信息
- Python: 通过Inspect模块获取函数上下文信息.
- zabbix 通过python脚本将报警信息发送到rabbitMQ
- 使用 python 获取 Linux 的 IP 信息(通过 ifconfig 命令)
- PB通过GDI API获取图片的信息
- 通过豆瓣Api,输入ISBN获取图书信息
- C++ 通过WIN32 API 获取逻辑磁盘详细信息
- Java通过调用API获取zabbix监控
- 用python3.X免费使用face++的api来获取身份证照片上的信息
- 通过python脚本获取服务器硬件信息