注:也可以修改配置檔案改善,但是效果不大,徹底解決辦法就是做資料表分區
Zabbix的history相關資料表資料太大,執行表分區操作過程
一、查詢zabbix資料庫大小
mysql> select table_schema, concat(truncate(sum(data_length)/1024/1024,2),' mb') as data_size,concat(truncate(sum(index_length)/1024/1024,2),'mb') as index_size from information_schema.tables group by table_schema order by data_size desc;
+--------------------+------------+------------+
| table_schema | data_size | index_size |
+--------------------+------------+------------+
| zabbix | 8326.06 mb | 3031.85mb |
| jumpserver | 26.57 mb | 8.21mb |
| mysql | 2.49 mb | 0.21mb |
| information_schema | 0.15 mb | 0.00mb |
| sys | 0.01 mb | 0.00mb |
| performance_schema | 0.00 mb | 0.00mb |
+--------------------+------------+------------+
6 rows in set (0.40 sec)
**發現Zabbix資料占用了8G空間**
二、查詢Zabbix資料庫裡所有表大小
mysql> select table_name, (data_length+index_length)/1024/1024 as total_mb,table_rows from information_schema.tables where table_schema='zabbix' order by total_mb desc;
+----------------------------+---------------+------------+
| table_name | total_mb | table_rows |
+----------------------------+---------------+------------+
| history_uint | 5487.29687500 | 56075683 |
| history | 4843.39062500 | 51787332 |
| trends_uint | 526.92187500 | 7440029 |
| history_str | 251.39062500 | 2393973 |
| trends | 215.87500000 | 2978883 |
| items | 6.21875000 | 10033 |
| events | 4.93750000 | 7922 |
| alerts | 4.67187500 | 5030 |
| items_applications | 2.35937500 | 11510 |
| triggers | 1.89062500 | 4359 |
| auditlog | 1.85937500 | 6803 |
| images | 1.53125000 | 138 |
| functions | 0.67187500 | 4746 |
| item_discovery | 0.57812500 | 3921 |
| graphs_items | 0.51562500 | 3773 |
| graphs | 0.50000000 | 1561 |
| auditlog_details | 0.42187500 | 2620 |
| event_recovery | 0.32812500 | 2936 |
| applications | 0.29687500 | 2345 |
| item_preproc | 0.29687500 | 3668 |
| application_template | 0.28125000 | 1989 |
| sessions | 0.23437500 | 1064 |
| hosts | 0.17187500 | 167 |
| profiles | 0.15625000 | 564 |
| sysmaps_elements | 0.15625000 | 95 |
| mappings | 0.12500000 | 820 |
| widget_field | 0.10937500 | 170 |
| graph_discovery | 0.10937500 | 887 |
| problem | 0.09375000 | 104 |
| item_condition | 0.07812500 | 446 |
| sysmaps | 0.07812500 | 13 |
| trigger_depends | 0.07812500 | 401 |
| httptest | 0.07812500 | 41 |
| event_suppress | 0.06250000 | 0 |
| escalations | 0.06250000 | 8 |
| sysmaps_links | 0.06250000 | 0 |
| scripts | 0.06250000 | 3 |
| trigger_discovery | 0.06250000 | 648 |
| hostmacro | 0.06250000 | 297 |
| group_prototype | 0.06250000 | 7 |
| acknowledges | 0.06250000 | 207 |
| slides | 0.04687500 | 0 |
| rights | 0.04687500 | 4 |
| maintenances_groups | 0.04687500 | 0 |
| host_discovery | 0.04687500 | 2 |
| httpstepitem | 0.04687500 | 123 |
| item_application_prototype | 0.04687500 | 23 |
| autoreg_host | 0.04687500 | 0 |
| task | 0.04687500 | 0 |
| maintenances | 0.04687500 | 0 |
| dservices | 0.04687500 | 30 |
| sysmap_element_trigger | 0.04687500 | 0 |
| tag_filter | 0.04687500 | 0 |
| drules | 0.04687500 | 8 |
| slideshows | 0.04687500 | 0 |
| users_groups | 0.04687500 | 5 |
| services_links | 0.04687500 | 0 |
| opcommand_hst | 0.04687500 | 0 |
| optemplate | 0.04687500 | 0 |
| interface | 0.04687500 | 82 |
| slideshow_usrgrp | 0.04687500 | 0 |
| opcommand_grp | 0.04687500 | 0 |
| opmessage_usr | 0.04687500 | 22 |
| sysmaps_link_triggers | 0.04687500 | 0 |
| service_alarms | 0.04687500 | 0 |
| opmessage_grp | 0.04687500 | 4 |
| hosts_templates | 0.04687500 | 263 |
| icon_mapping | 0.04687500 | 0 |
| application_prototype | 0.04687500 | 23 |
| dashboard_usrgrp | 0.04687500 | 0 |
| media | 0.04687500 | 9 |
| hosts_groups | 0.04687500 | 211 |
| icon_map | 0.04687500 | 0 |
| application_discovery | 0.04687500 | 8 |
| sysmap_usrgrp | 0.04687500 | 0 |
| dashboard_user | 0.04687500 | 0 |
| opgroup | 0.04687500 | 0 |
| httptestitem | 0.04687500 | 123 |
| sysmap_user | 0.04687500 | 0 |
| screens | 0.04687500 | 12 |
| actions | 0.04687500 | 16 |
| screen_usrgrp | 0.04687500 | 0 |
| maintenances_windows | 0.04687500 | 0 |
| correlation | 0.04687500 | 0 |
| slideshow_user | 0.04687500 | 0 |
| config | 0.04687500 | 0 |
| screen_user | 0.04687500 | 0 |
| maintenances_hosts | 0.04687500 | 0 |
| group_discovery | 0.03125000 | 0 |
| conditions | 0.03125000 | 30 |
| sysmap_element_url | 0.03125000 | 0 |
| problem_tag | 0.03125000 | 0 |
| regexps | 0.03125000 | 5 |
| httpstep_field | 0.03125000 | 26 |
| interface_discovery | 0.03125000 | 0 |
| services_times | 0.03125000 | 0 |
| maintenance_tag | 0.03125000 | 0 |
| history_text | 0.03125000 | 22 |
| graph_theme | 0.03125000 | 4 |
| httpstep | 0.03125000 | 41 |
| dhosts | 0.03125000 | 31 |
| widget | 0.03125000 | 23 |
| users | 0.03125000 | 3 |
| services | 0.03125000 | 0 |
| hstgrp | 0.03125000 | 27 |
| history_log | 0.03125000 | 0 |
| dchecks | 0.03125000 | 8 |
| valuemaps | 0.03125000 | 75 |
| opcommand | 0.03125000 | 0 |
| usrgrp | 0.03125000 | 8 |
| media_type | 0.03125000 | 11 |
| opmessage | 0.03125000 | 30 |
| globalmacro | 0.03125000 | 0 |
| trigger_tag | 0.03125000 | 0 |
| screens_items | 0.03125000 | 51 |
| proxy_history | 0.03125000 | 0 |
| corr_condition_group | 0.03125000 | 0 |
| expressions | 0.03125000 | 10 |
| dashboard | 0.03125000 | 3 |
| proxy_dhistory | 0.03125000 | 0 |
| corr_condition | 0.03125000 | 0 |
| operations | 0.03125000 | 32 |
| httptest_field | 0.03125000 | 0 |
| sysmap_url | 0.03125000 | 0 |
| proxy_autoreg_host | 0.03125000 | 0 |
| sysmap_shape | 0.03125000 | 0 |
| opconditions | 0.03125000 | 0 |
| event_tag | 0.03125000 | 0 |
| corr_operation | 0.03125000 | 0 |
| timeperiods | 0.01562500 | 0 |
| corr_condition_tagvalue | 0.01562500 | 0 |
| task_remote_command_result | 0.01562500 | 0 |
| corr_condition_tagpair | 0.01562500 | 0 |
| task_remote_command | 0.01562500 | 0 |
| corr_condition_tag | 0.01562500 | 0 |
| task_close_problem | 0.01562500 | 0 |
| task_check_now | 0.01562500 | 0 |
| housekeeper | 0.01562500 | 0 |
| ids | 0.01562500 | 52 |
| globalvars | 0.01562500 | 0 |
| dbversion | 0.01562500 | 0 |
| task_acknowledge | 0.01562500 | 0 |
| opinventory | 0.01562500 | 0 |
| host_inventory | 0.01562500 | 20 |
+----------------------------+---------------+------------+
144 rows in set (0.02 sec)
mysql>
**發現history和trends相關表資料量很大,一個是曆史資料,一個是趨勢資料需要對這些表進行分區操作,提高效率**
三、下載下傳分區腳本
連結:https://pan.baidu.com/s/1L0E968kKVrBx71OQBAqYRA
提取碼:dytm
四、編輯腳本相關參數
腳本預設詳情資料保留30天,趨勢資料保留12個月,如需修改,請修改以下内容:
daily_history_min=30
monthly_history_min=12
腳本預設連接配接資料庫資訊,更改成你的:
DBHOST=localhost
DBUSER=zabbix
DBPASS=zabbix
[[email protected]_server zabbix-4.0.3]# chmod +x partitiontables_gt_zbx34.sh
五、執行分區腳本
停止zabbix_server服務
[[email protected]_server zabbix-4.0.3]# service zabbix_server stop
Shutting down zabbix_server: [确定]
[[email protected]_server zabbix-4.0.3]# service zabbix_server status
zabbix_server 已停
執行腳本
[[email protected]_server zabbix-4.0.3]# ./partitiontables_gt_zbx34.sh
Ready to partition tables.
Ready to update permissions of Zabbix user to create routines
Enter root DB user:
輸入資料庫使用者名和密碼
Enter root DB user: root
Enter root password: xxxxxxxx
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1133 (42000) at line 1: Can't find any matching row in the user table
Do you want to backup the database (recommended) (Y/n): n
Are you certain you have a backup (y/N):
y
Ready to proceed:
Starting yearly partioning at: 2019
and ending at: 2019
With 30 days of daily history
Ready to proceed (Y/n):
y
Altering table: history
Altering table: history_log
Altering table: history_str
Altering table: history_text
Altering table: history_uint
Altering table: trends
Altering table: trends_uint
Creating monthly partitions for table: trends
Creating monthly partitions for table: trends_uint
Creating daily partitions for table: history
Creating daily partitions for table: history_log
Creating daily partitions for table: history_str
Creating daily partitions for table: history_text
Creating daily partitions for table: history_uint
Ready to apply script to database, this may take a while.(Y/n):
Ready to apply script to database, this may take a while.(Y/n):
y
mysql: [Warning] Using a password on the command line interface can be insecure.
Altering tables
history
history_log
history_str
history_text
history_uint
trends
trends_uint
trends
trends_uint
history
history_log
history_str
history_text
history_uint
Installing procedures
If Zabbix Version = 2.0
Do you want to update the /etc/zabbix/zabbix_server.conf
to disable housekeeping (Y/n):
這裡會耗費很長時間,大概是1個小時左右
If Zabbix Version = 2.0
Do you want to update the /etc/zabbix/zabbix_server.conf
to disable housekeeping (Y/n): n
Do you want to update the crontab (Y/n): y
The crontab entry can be either in /etc/cron.daily, or added
to the crontab for root
Do you want to add this to the /etc/cron.daily directory (Y/n): y
Enter email of who should get the daily housekeeping reports: [email protected]
[[email protected]_server zabbix-4.0.3]#
到此,腳本執行完畢
六、檢視生成的定時任務
上面的腳本會在/etc/cron.daily目錄下生成一個名稱為zabbixhousekeeping的腳本
[[email protected]_server zabbix-4.0.3]# vim /etc/cron.daily/zabbixhousekeeping
1 #!/bin/bash
2 /usr/local/zabbix/cron.d/housekeeping.sh
裡面是執行了usr/local/zabbix/cron.d/housekeeping.sh腳本,打開這個腳本
[[email protected]_server zabbix-4.0.3]# vim /usr/local/zabbix/cron.d/housekeeping.sh
#!/bin/bash
[email protected]
tmpfile=/tmp/housekeeping$$
date >$tmpfile
/usr/bin/mysql --skip-column-names -B -h localhost -uroot -pxxxxxx zabbix -e "CALL create_zabbix_partitions();" >>$tmpfile 2>&1
/usr/bin/mail -s "Zabbix MySql Partition Housekeeping" $MAILTO <$tmpfile
rm -f $tmpfile
這個腳本是調用了Mysql的建立分區表存儲過程
七、檢視分區結果
在刷一遍第一步的指令,就可以看到空間縮小了
表分區成功,重新開機zabbix服務
[[email protected]_server zabbix-4.0.3]# service zabbix_server start
Starting zabbix_server: [确定]
[[email protected]_server zabbix-4.0.3]#