1. 程式人生 > >mysql用視圖和事件實現數據間隔插入數據到表

mysql用視圖和事件實現數據間隔插入數據到表

where int pri sele 間隔 let global bsp services

創建視圖

CREATE ALGORITHM = UNDEFINED DEFINER = `root`@`192.168.*.*` SQL SECURITY DEFINER VIEW `perfdata` AS SELECT
	`nagios_hosts`.`display_name` AS `hostname`,
	`nagios_services`.`display_name` AS `servicename`,
	`nagios_servicechecks`.`end_time` AS `timeline`,
	`getPerfKey` (
		`nagios_servicechecks`.`perfdata`,
		1
	) AS `perfkey`,
	`getPerfValue` (
		`nagios_servicechecks`.`perfdata`,
		1
	) AS `perfvalue`
FROM
	(
		(
			`nagios_hosts`
			LEFT JOIN `nagios_services` ON (
				(
					`nagios_hosts`.`host_object_id` = `nagios_services`.`host_object_id`
				)
			)
		)
		LEFT JOIN `nagios_servicechecks` ON (
			(
				`nagios_services`.`service_object_id` = `nagios_servicechecks`.`service_object_id`
			)
		)
	)
WHERE
	(
		(
			`nagios_servicechecks`.`end_time` BETWEEN (now() + INTERVAL -(6) MINUTE)
			AND now()
		)
		AND `nagios_hosts`.`display_name` IN (
			SELECT DISTINCT
				`nagios_hosts`.`display_name`
			FROM
				(
					`nagios_services`
					LEFT JOIN `nagios_hosts` ON (
						(
							`nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id`
						)
					)
				)
		)
		AND `nagios_services`.`display_name` IN (
			SELECT DISTINCT
				`nagios_services`.`display_name`
			FROM
				(
					`nagios_services`
					LEFT JOIN `nagios_hosts` ON (
						(
							`nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id`
						)
					)
				)
		)
		AND (
			`getPerfValue` (
				`nagios_servicechecks`.`perfdata`,
				1
			) <> ‘‘
		)
	)
UNION
	SELECT
		`nagios_hosts`.`display_name` AS `hostname`,
		`nagios_services`.`display_name` AS `servicename`,
		`nagios_servicechecks`.`end_time` AS `timeline`,
		`getPerfKey` (
			`nagios_servicechecks`.`perfdata`,
			2
		) AS `perfkey`,
		`getPerfValue` (
			`nagios_servicechecks`.`perfdata`,
			2
		) AS `perfvalue`
	FROM
		(
			(
				`nagios_hosts`
				LEFT JOIN `nagios_services` ON (
					(
						`nagios_hosts`.`host_object_id` = `nagios_services`.`host_object_id`
					)
				)
			)
			LEFT JOIN `nagios_servicechecks` ON (
				(
					`nagios_services`.`service_object_id` = `nagios_servicechecks`.`service_object_id`
				)
			)
		)
	WHERE
		(
			(
				`nagios_servicechecks`.`end_time` BETWEEN (now() + INTERVAL -(6) MINUTE)
				AND now()
			)
			AND `nagios_hosts`.`display_name` IN (
				SELECT DISTINCT
					`nagios_hosts`.`display_name`
				FROM
					(
						`nagios_services`
						LEFT JOIN `nagios_hosts` ON (
							(
								`nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id`
							)
						)
					)
			)
			AND `nagios_services`.`display_name` IN (
				SELECT DISTINCT
					`nagios_services`.`display_name`
				FROM
					(
						`nagios_services`
						LEFT JOIN `nagios_hosts` ON (
							(
								`nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id`
							)
						)
					)
			)
			AND (
				`getPerfValue` (
					`nagios_servicechecks`.`perfdata`,
					2
				) <> ‘‘
			)
		)
	UNION
		SELECT
			`nagios_hosts`.`display_name` AS `hostname`,
			`nagios_services`.`display_name` AS `servicename`,
			`nagios_servicechecks`.`end_time` AS `timeline`,
			`getPerfKey` (
				`nagios_servicechecks`.`perfdata`,
				3
			) AS `perfkey`,
			`getPerfValue` (
				`nagios_servicechecks`.`perfdata`,
				3
			) AS `perfvalue`
		FROM
			(
				(
					`nagios_hosts`
					LEFT JOIN `nagios_services` ON (
						(
							`nagios_hosts`.`host_object_id` = `nagios_services`.`host_object_id`
						)
					)
				)
				LEFT JOIN `nagios_servicechecks` ON (
					(
						`nagios_services`.`service_object_id` = `nagios_servicechecks`.`service_object_id`
					)
				)
			)
		WHERE
			(
				(
					`nagios_servicechecks`.`end_time` BETWEEN (now() + INTERVAL -(6) MINUTE)
					AND now()
				)
				AND `nagios_hosts`.`display_name` IN (
					SELECT DISTINCT
						`nagios_hosts`.`display_name`
					FROM
						(
							`nagios_services`
							LEFT JOIN `nagios_hosts` ON (
								(
									`nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id`
								)
							)
						)
				)
				AND `nagios_services`.`display_name` IN (
					SELECT DISTINCT
						`nagios_services`.`display_name`
					FROM
						(
							`nagios_services`
							LEFT JOIN `nagios_hosts` ON (
								(
									`nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id`
								)
							)
						)
				)
				AND (
					`getPerfValue` (
						`nagios_servicechecks`.`perfdata`,
						3
					) <> ‘‘
				)
			)

創建表

CREATE TABLE `getPerfdata` (
  `NO` bigint(20) NOT NULL AUTO_INCREMENT,
  `hostname` varchar(255) DEFAULT NULL,
  `servicename` varchar(255) DEFAULT NULL,
  `timeline` datetime DEFAULT NULL,
  `perfkey` varchar(255) DEFAULT NULL,
  `perfvalue` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`NO`),
  KEY `NO` (`NO`)
) ENGINE=InnoDB AUTO_INCREMENT=16739 DEFAULT CHARSET=utf8;

打開全局事件調度

mysql> set global event_scheduler=on;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@event_scheduler;
+-------------------+
| @@event_scheduler |
+-------------------+
| ON                |
+-------------------+
1 row in set (0.00 sec

  

創建事件實現視圖數據按照計劃插入表

CREATE DEFINER = `root`@`192.168.*.*` EVENT `insertPerfdata` ON SCHEDULE EVERY 6 MINUTE STARTS ‘2019-01-29 11:24:16‘ ON COMPLETION PRESERVE ENABLE DO
	INSERT INTO nagios.getPerfdata (
		hostname,
		servicename,
		timeline,
		perfkey,
		perfvalue
	) SELECT
		*
	FROM
		nagios.perfdata

  

  

  

  

mysql用視圖和事件實現數據間隔插入數據到表