在日常的监控中,我们除了日常的zabbix操作外,我们有的时候还涉及到与其他公司 进行数据对接。由于别的公司的数据对接很多时候并不是按照zabbix的数据结构 (尤其是大型厂家,或是专业监控厂家,并不会直接使用zabbix,多数是自己开发 或是对其他监控软件进行二次开发之类),在这种需求基础上,我们就需要整理下 zabbix的数据库,将需要的数据提取出来,并转化成我们需要的格式。
zabbix异常报警信息会随着异常恢复而消失,需要做触发器存到自己的数据库中
1.zabbix数据库中创建表(数据库:mysql-5.7)
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
— —————————-
— Table structure for alerts_report
— —————————-
DROP TABLE IF EXISTS `alerts_report`;
CREATE TABLE `alerts_report` (
`reportid` int(11) NOT NULL AUTO_INCREMENT,
`reportip` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`reporttype` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`alarmid` int(11) NOT NULL,
`alarmname` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`alarmlevel` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`alarmstat` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`alarmtime` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`alarmcause` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`sendstatus` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`reportid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
— —————————-
— Records of alerts_report
— —————————-
SET FOREIGN_KEY_CHECKS = 1;
2.创建触发器,实时同步zabbix库的报警信息
DROP TRIGGER IF EXISTS alerts_report_event;
CREATE TRIGGER alerts_report_event
AFTER INSERT ON zabbix.`events`
FOR EACH ROW
BEGIN
INSERT INTO zabbix.alerts_report (
zabbix.alerts_report.reportip,
zabbix.alerts_report.reporttype,
zabbix.alerts_report.alarmid,
zabbix.alerts_report.alarmname,
zabbix.alerts_report.alarmlevel,
zabbix.alerts_report.alarmstat,
zabbix.alerts_report.alarmtime
)
SELECT
zabbix.`hosts`.`host`,
CONCAT(‘服务器’),
zabbix.`triggers`.triggerid,
zabbix.`triggers`.description,
zabbix.`triggers`.priority,
zabbix.`events`.`value`,
FROM_UNIXTIME(zabbix.`events`.clock)
FROM
zabbix.`hosts`,
zabbix.`triggers`,
zabbix.`events`,
zabbix.items,
zabbix.functions,
zabbix.hstgrp,
zabbix.hosts_groups
WHERE
zabbix.`hosts`.hostid = zabbix.hosts_groups.hostid
AND zabbix.hosts_groups.groupid = zabbix.hstgrp.groupid
AND zabbix.`triggers`.triggerid = zabbix.`events`.objectid
AND zabbix.`hosts`.hostid = zabbix.items.hostid
AND zabbix.items.itemid = zabbix.functions.itemid
AND zabbix.functions.triggerid = zabbix.`triggers`.triggerid
AND zabbix.`events`.eventid=new.eventid;
END;
查看触发器命令:
SHOW TRIGGERS
删除触发器命令:
DROP TRIGGER IF EXISTS alerts_report_event;