一、使用Navicat创建Event
1、创建一个存储过程让定时任务Event调用:
打开Navicat查询界面:
![](https://i-blog.csdnimg.cn/blog_migrate/7ff3cab8a40ff4ae79298108f8731947.png)
![](https://i-blog.csdnimg.cn/blog_migrate/dad5f5ecfd100179056ee79485add7d8.png)
在上述存储过程中将自定义的sql语句填写到
BEGIN与END之间。
2、查看并打开event_scheduler调度事件:
再上图查询窗口执行查看:
show variables like '%sche%';
![](https://i-blog.csdnimg.cn/blog_migrate/851e314a5f4f56f6bae110168bc2ba52.png)
注:如果
event_scheduler值为OFF则使用set global event_scheduler =1;打开。
3、创建Event调度任务:
![](https://i-blog.csdnimg.cn/blog_migrate/7c33004ac573d9ca0f297b618f2929be.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1ce9806fc53d21e426ad44ae2401c4ff.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c376fb817ee01a33af5484b7fcc6cfa1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8d85d7a424404640d4c5ceafe3a65599.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d9b12810676cae40965eaf534745c311.png)
保存完毕,则在1中定义的存储过程scheduler_sync()将会被每隔10秒执行一次!
![](https://i-blog.csdnimg.cn/blog_migrate/255b4b992b98e2959ec8bc6c28a94656.png)
二、使用纯SQL语句创建定时任务Event。
1、进入到mysql ,并开启event_schduler:
![](https://i-blog.csdnimg.cn/blog_migrate/d6e4c0dcc55a348a9471ac6b24c3c997.png)
set global event_scheduler =1;
show variables like '%event%';
|
2、进入wishstack创建存储过程(本次测试数据库为wishstack):
CREATE PROCEDURE sync_date_2_monitor()
BEGIN
/*-- 下述加入自定义的mysql处理逻辑 --*/
/*-- ----------------------------
-- 先删除 alert_msg 数据库表数据
-- 再同步 alert_msg 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.alarm_msg;
INSERT INTO monitor_screen.alarm_msg(id, alarm_id, resource_type, alarm_type, created_time, content, `level`)
SELECT UUID() AS id,
id AS alarm_id,
"2" AS resource_type,
CASE alarm_type
WHEN "state" THEN "状态告警"
END AS "alarm_type",
created_time,
description AS content,
CASE `level`
WHEN "general" THEN "1"
WHEN "serious" THEN "2"
WHEN "general" THEN "3"
END AS "level"
FROM alarm;
/*-- ----------------------------
-- 先删除 overall_statistic 数据库表数据
-- 再同步 overall_statistic 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.overall_statistic;
INSERT INTO monitor_screen.overall_statistic(id, user_num_online, user_num_all, tenant_num, business_num, vm_num)
select a.id, b.user_num_online, c.user_num_all, d.tenant_num, e.business_num, f.vm_num FROM
(SELECT UUID() AS id) AS a join
(SELECT COUNT(id) AS user_num_online from `user` WHERE login_state=0 AND deleted=0) AS b join
(SELECT COUNT(id) AS user_num_all from `user` WHERE deleted=0) AS c join
(SELECT COUNT(id) AS tenant_num FROM `tenant` WHERE deleted=0 ) AS d join
(SELECT COUNT(id) AS business_num FROM `business` WHERE deleted=0 ) AS e join
(SELECT COUNT(id) AS vm_num FROM `instance` WHERE deleted=0 ) AS f ;
/*-- ----------------------------
-- 先删除 tenant_res_spnd_t5 数据库表数据
-- 再同步 tenant_res_spnd_t5 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.tenant_res_spnd_t5;
INSERT INTO monitor_screen.tenant_res_spnd_t5
SELECT UUID() AS id,tenant.`name` AS tenant_name, vm_num
FROM (
(SELECT tenant_id, COUNT(id) AS vm_num FROM instance WHERE instance.deleted != 1 GROUP BY tenant_id) AS a
LEFT JOIN
tenant
ON a.tenant_id=tenant.id AND tenant.deleted != 1
);
/*-- ----------------------------
-- 先删除 resource_spending 数据库表数据
-- 再同步 resource_spending 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.resource_spending;
INSERT INTO monitor_screen.resource_spending(id, created_time, `hour`, cpu_use_ratio, memory_use_ratio)
SELECT UUID() AS id,
created_time,
date_format(created_time, '%H') AS `hour`,
AVG(cpu_rate) AS cpu_use_ratio,
AVG(memory_rate) AS memory_use_ratio
FROM instance
GROUP BY date_format(created_time, '%Y-%m-%d %H');
/*-- ----------------------------
-- 先删除 cloud_tenant 数据库表数据
-- 再同步 cloud_tenant 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.cloud_tenant;
INSERT INTO monitor_screen.cloud_tenant
SELECT
UUID() AS id,
tenant.`id` AS tenant_id,
tenant.`name` AS tenant_name,
NULL AS lon,
NULL AS lat,
COUNT(DISTINCT CASE WHEN `user`.login_state = 0 AND `user`.deleted != 1 THEN `user`.id END) AS online_user_num,
COUNT(DISTINCT CASE WHEN instance.`status` = 'active' AND instance.deleted != 1 THEN instance.id END) AS online_vm_num,
COUNT(DISTINCT CASE WHEN `user`.deleted != 1 THEN `user`.id END) AS user_num,
COUNT(DISTINCT CASE WHEN `business`.deleted != 1 THEN business.id END) AS business_num,
COUNT(DISTINCT CASE WHEN instance.deleted != 1 THEN instance.id END) AS vm_num,
COUNT(DISTINCT CASE WHEN alarm.`status` != 'fix' THEN alarm.id END) AS sec_alarm_num,
AVG( instance.cpu_rate) AS vcpu_use_ratio,
AVG( instance.memory_rate) AS memory_use_ratio
FROM tenant LEFT JOIN tenant_user ON tenant_user.tenant_id = tenant.id
LEFT JOIN `user` ON tenant_user.user_id = `user`.id
LEFT JOIN business ON business.tenant_id = tenant.id
LEFT JOIN alarm ON alarm.tenant_uuid = tenant.uuid
LEFT JOIN instance ON instance.tenant_id = tenant.id
GROUP BY tenant.id;
/*-- ----------------------------
-- 先删除 data_center 数据库表数据
-- 再同步 data_center 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.data_center;
INSERT INTO monitor_screen.data_center
SELECT
UUID() AS id,
provider.id AS provider_id,
provider.`name` AS provider_name,
NULL AS lon,
NULL AS lat,
COUNT(DISTINCT virtual_server.id) AS computing_server_num,
COUNT(DISTINCT storage_device.id) AS storage_server_num,
COUNT(DISTINCT network_device.id) AS net_server_num,
COUNT(DISTINCT instance.id) AS visual_machine_num,
SUM(flavor.vcpus) AS visual_cpu_num,
SUM(flavor.memory_mb) AS visual_memory_capacity,
SUM(flavor.disk) AS visual_disc_capacity
FROM provider LEFT JOIN virtual_server ON provider.id = virtual_server.provider_id
LEFT JOIN storage_device ON provider.id = storage_device.provider_id
LEFT JOIN network_device ON provider.id = network_device.provider_id
LEFT JOIN instance ON provider.id = instance.provider_id
LEFT JOIN flavor ON instance.flavor_uuid = flavor.uuid
LEFT JOIN physical_resource ON provider.id = physical_resource.provider_id
GROUP BY provider.id;
/*-- ----------------------------
-- 先删除 link 数据库表数据
-- 再同步 link 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.link;
INSERT INTO monitor_screen.link
SELECT
UUID() AS id,
tenant_id,
provider_id
FROM tenant_provider;
/*-- ----------------------------
-- 先删除 vm_resource_statistic 数据库表数据
-- 再同步 vm_resource_statistic 更新相关数据
-- ----------------------------*/
DELETE FROM monitor_screen.vm_resource_statistic;
INSERT INTO monitor_screen.vm_resource_statistic
SELECT
UUID() AS id,
SUM(vcpu_used) AS `assigned_vcpu_num`,
SUM(memory_used) AS `assigned_vmemory_capacity`,
SUM(storage_used) AS `assigned_vdisc_capacity`,
SUM(memory) AS `vmemory_capacity`,
SUM(`storage`) AS `vdisk_capacity`
FROM physical_resource;
END
|
上述绿色内容请填入相应的存储过程逻辑代码!!!
3、测试当前存储过程是否正确(没有报错则说明存储过程创建成功):
![](https://i-blog.csdnimg.cn/blog_migrate/e67ad65c1d9bcdd45bbde9d306ea43ae.png)
4、创建Event事件:
CREATE EVENT `My_Sync_Event` /*-- Event事件名称 --*/
ON SCHEDULE EVERY
10 SECOND
/*-- 每隔10秒调度一次
--*/
ON COMPLETION PRESERVE
/*--
当本次event到期了,event会被disable,但是该event还是会存在--*/
ENABLE
/*-- 开启Event调度
--*/
DO
CALL scheduler_sync();
/*-- 调度任务调用上述创建的存储过程,周期执行任务
--*/
|
5、对于Event的常用操作:
查看:
SHOW EVENTS;
![](https://i-blog.csdnimg.cn/blog_migrate/8662aa1f355121af6a36fb772486a666.png)
开启事件:
ALTER EVENT
My_Sync_EventON
COMPLETION PRESERVE ENABLE;
关闭事件:
ALTER EVENT
My_Sync_Event
ON
COMPLETION PRESERVE DISABLE;
![](https://i-blog.csdnimg.cn/blog_migrate/4c401c1824bc45499cda41d06a9ed13b.png)
删除事件:
DROP EVENT IF EXISTS my_scheduler_event;
![](https://i-blog.csdnimg.cn/blog_migrate/bfd3e44e98f0c4fc3748e5d62e53a970.png)
也可以在linux下用脚本实现:
#//usr/bin/sh
mysql -uroot -p123456 -e "source ./monitor_screen.sql"
mysql -uroot -p123456 -e "source ./monitor_screen_sync.sql"
mysql -uroot -p123456 -e "source ./monitor_screen.sql"
mysql -uroot -p123456 -e "source ./monitor_screen_sync.sql"