From fd98e07cabf8ca508eabfbe624d2afaeca5654b2 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 11:14:15 +0000 Subject: [PATCH 01/40] change layout,, update readme --- README.md | 8 ++++++-- checkscript.sql => orphaned/check.sql | 0 cleanup.sql => orphaned/clean.sql | 0 3 files changed, 6 insertions(+), 2 deletions(-) rename checkscript.sql => orphaned/check.sql (100%) rename cleanup.sql => orphaned/clean.sql (100%) diff --git a/README.md b/README.md index 9041c06..e6d6738 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,21 @@ Zabbix Database Cleanup This repo contains a few useful queries to cleanup old orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. +"Orphaned" queries are the same for Mysql and Postgresql. "Old" are not. + Usage: check how many orphaned rows you have ============================================ These queries are safe to run, but if you have a large database please note that these can take a while (read: a few hours is normal). - user@host-$ mysql zabbix < checkscript.sql + user@host-$ mysql zabbix < orphaned/check.sql +Or + user@host-$ psql zabbix < orphaned/check.sql Usage: delete the orphaned rows =============================== Use the queries above on your won risk. Take *backups* first. Once you have back-ups, go ahead and be brave. - user@host-$ mysql zabbix < cleanup.sql + user@host-$ mysql (or psql) zabbix < orphaned/cleanup.sql diff --git a/checkscript.sql b/orphaned/check.sql similarity index 100% rename from checkscript.sql rename to orphaned/check.sql diff --git a/cleanup.sql b/orphaned/clean.sql similarity index 100% rename from cleanup.sql rename to orphaned/clean.sql From d34e9e9751039e52f206bbcdca096556f6180425 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 11:15:43 +0000 Subject: [PATCH 02/40] remove big heading --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index e6d6738..7876419 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ Zabbix Database Cleanup -======================= This repo contains a few useful queries to cleanup old orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. @@ -7,7 +6,6 @@ This repo contains a few useful queries to cleanup old orphaned data in a Zabbix Usage: check how many orphaned rows you have -============================================ These queries are safe to run, but if you have a large database please note that these can take a while (read: a few hours is normal). @@ -16,7 +14,6 @@ Or user@host-$ psql zabbix < orphaned/check.sql Usage: delete the orphaned rows -=============================== Use the queries above on your won risk. Take *backups* first. Once you have back-ups, go ahead and be brave. From 3898900ef81fc0433b6e98010907048d0d73d06e Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 11:21:46 +0000 Subject: [PATCH 03/40] fix headers --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7876419..138fb5e 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,20 @@ -Zabbix Database Cleanup +### Zabbix Database Cleanup This repo contains a few useful queries to cleanup old orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. "Orphaned" queries are the same for Mysql and Postgresql. "Old" are not. -Usage: check how many orphaned rows you have +### Usage: check how many orphaned rows you have These queries are safe to run, but if you have a large database please note that these can take a while (read: a few hours is normal). user@host-$ mysql zabbix < orphaned/check.sql Or + user@host-$ psql zabbix < orphaned/check.sql -Usage: delete the orphaned rows +### Usage: delete the orphaned rows Use the queries above on your won risk. Take *backups* first. Once you have back-ups, go ahead and be brave. From 654f46cf625c9d55843b80a059f7c3f5353bd8c2 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 11:23:00 +0000 Subject: [PATCH 04/40] fix headers -2 --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 138fb5e..905d75e 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,19 @@ This repo contains a few useful queries to cleanup old orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. -"Orphaned" queries are the same for Mysql and Postgresql. "Old" are not. +"Orphaned" queries are the same for Mysql and Postgresql. "Old" are not, so pay attention. -### Usage: check how many orphaned rows you have +### Check how many orphaned rows you have These queries are safe to run, but if you have a large database please note that these can take a while (read: a few hours is normal). user@host-$ mysql zabbix < orphaned/check.sql -Or +Or (for postgresql) user@host-$ psql zabbix < orphaned/check.sql -### Usage: delete the orphaned rows +### Delete the orphaned rows Use the queries above on your won risk. Take *backups* first. Once you have back-ups, go ahead and be brave. From 2cda0ccc401b6d94ce2da159ffc5aa49d8b3bc88 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 11:27:51 +0000 Subject: [PATCH 05/40] add caution not --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 905d75e..8fb67c2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ -### Zabbix Database Cleanup +## Zabbix Database Cleanup This repo contains a few useful queries to cleanup old orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. "Orphaned" queries are the same for Mysql and Postgresql. "Old" are not, so pay attention. +#### NOTE +The queries are tested against Zabbix 1.8 and 2.0. Aplly with caution to other version. Consider reporting the results, whether succesful or not. + ### Check how many orphaned rows you have From 4b413310caeab27767764fe9f3d5a37d16d3bf03 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 11:33:18 +0000 Subject: [PATCH 06/40] add postgresql old data clean --- old/postgresql/clean.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 old/postgresql/clean.sql diff --git a/old/postgresql/clean.sql b/old/postgresql/clean.sql new file mode 100644 index 0000000..cfc8c79 --- /dev/null +++ b/old/postgresql/clean.sql @@ -0,0 +1,16 @@ +#keep 1 week of history and 3 months of trends + +delete FROM alerts where age(to_timestamp(alerts.clock)) > interval '7 days'; + +delete FROM acknowledges where age(to_timestamp(acknowledges.clock)) > interval '7 days'; + +delete FROM events where age(to_timestamp(events.clock)) > interval '7 days'; + +delete FROM history where age(to_timestamp(history.clock)) > interval '7 days'; +delete FROM history_uint where age(to_timestamp(history_uint.clock)) > interval '7 days' ; +delete FROM history_str where age(to_timestamp(history_str.clock)) > interval '7 days' ; +delete FROM history_text where age(to_timestamp(history_text.clock)) > interval '7 days' ; +delete FROM history_log where age(to_timestamp(history_log.clock)) > interval '7 days' ; + +delete FROM trends where age(to_timestamp(trends.clock)) > interval '90 days'; +delete FROM trends_uint where age(to_timestamp(trends_uint.clock)) > interval '90 days' ; From 60114b9382862533fec416b6e34892bf93e4c4df Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 11:35:28 +0000 Subject: [PATCH 07/40] add unused items cleanup --- unused/postgresql/clean.sql | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 unused/postgresql/clean.sql diff --git a/unused/postgresql/clean.sql b/unused/postgresql/clean.sql new file mode 100644 index 0000000..d60a845 --- /dev/null +++ b/unused/postgresql/clean.sql @@ -0,0 +1,9 @@ +delete from history where itemid not in (select itemid from items where status='0'); +delete from history_uint where itemid not in (select itemid from items where status='0'); +delete from history_str where itemid not in (select itemid from items where status='0'); +delete from history_text where itemid not in (select itemid from items where status='0'); +delete from history_log where itemid not in (select itemid from items where status='0'); + +delete from trends where itemid not in (select itemid from items where status='0'); +delete from trends_uint where itemid not in (select itemid from items where status='0'); + From 59626c7b99014db69b5640766d499d3da182e228 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 16:51:22 +0000 Subject: [PATCH 08/40] add history cleanup and unused items data cleanup --- README.md | 22 ++++++++++++---------- unused/postgresql/clean.sql | 1 - 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8fb67c2..e30700b 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,26 @@ ## Zabbix Database Cleanup This repo contains a few useful queries to cleanup old orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. - +If you have a large database please note that these can take a while (read: a few hours is normal). +Use the queries above on your won risk. Take *backups* first. Once you have back-ups, go ahead and be brave. "Orphaned" queries are the same for Mysql and Postgresql. "Old" are not, so pay attention. -#### NOTE +##### NOTE The queries are tested against Zabbix 1.8 and 2.0. Aplly with caution to other version. Consider reporting the results, whether succesful or not. -### Check how many orphaned rows you have +#### Check how many orphaned rows you have + + user@host-$ psql zabbix < orphaned/check.sql -These queries are safe to run, but if you have a large database please note that these can take a while (read: a few hours is normal). +#### Delete the orphaned rows - user@host-$ mysql zabbix < orphaned/check.sql -Or (for postgresql) + user@host-$ psql zabbix < orphaned/clean.sql - user@host-$ psql zabbix < orphaned/check.sql +#### Delete old data (1 week for history, 3 months for trends - edit sql at your own discretion) -### Delete the orphaned rows + user@host-$ psql zabbix < old/postgresql/clean.sql -Use the queries above on your won risk. Take *backups* first. Once you have back-ups, go ahead and be brave. +#### Delete all history for disabled items - user@host-$ mysql (or psql) zabbix < orphaned/cleanup.sql + user@host-$ psql zabbix < unused/postgresql/clean.sql diff --git a/unused/postgresql/clean.sql b/unused/postgresql/clean.sql index d60a845..9f12bd7 100644 --- a/unused/postgresql/clean.sql +++ b/unused/postgresql/clean.sql @@ -6,4 +6,3 @@ delete from history_log where itemid not in (select itemid from items where stat delete from trends where itemid not in (select itemid from items where status='0'); delete from trends_uint where itemid not in (select itemid from items where status='0'); - From ca984c255a0182c93f2d317cc615f94ff7e10225 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sat, 22 Mar 2014 16:56:36 +0000 Subject: [PATCH 09/40] fix formatting/wording --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e30700b..b4f321f 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,22 @@ ## Zabbix Database Cleanup -This repo contains a few useful queries to cleanup old orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. +This repo contains a few useful queries to cleanup old and/or orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. + If you have a large database please note that these can take a while (read: a few hours is normal). -Use the queries above on your won risk. Take *backups* first. Once you have back-ups, go ahead and be brave. + +Use the queries above on your won risk. Take *backups* first. Once you have backups, go ahead and be brave. + "Orphaned" queries are the same for Mysql and Postgresql. "Old" are not, so pay attention. ##### NOTE The queries are tested against Zabbix 1.8 and 2.0. Aplly with caution to other version. Consider reporting the results, whether succesful or not. -#### Check how many orphaned rows you have +#### Check how many orphaned rows do you have user@host-$ psql zabbix < orphaned/check.sql -#### Delete the orphaned rows +#### Delete orphaned rows user@host-$ psql zabbix < orphaned/clean.sql From 0d39f0a75fa4e6e0194eae63dbd8d2079976bcb6 Mon Sep 17 00:00:00 2001 From: Kenny Freeman Date: Tue, 8 Apr 2014 14:27:31 +1000 Subject: [PATCH 10/40] Update checkscript.sql Added a few AS so it's easier to see where the bad data is --- checkscript.sql | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/checkscript.sql b/checkscript.sql index 1b2ed2e..cb3a9e9 100644 --- a/checkscript.sql +++ b/checkscript.sql @@ -1,48 +1,48 @@ -- Count the amount of rows that would be deleted for all orphaned acknowledge entries -SELECT COUNT(*) FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); -SELECT COUNT(*) FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); +SELECT COUNT(*) AS ACK_MISSING_UID FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS ACK_MISSING_EID FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); -- Count the amount of rows that would be deleted for orphaned alerts entries -SELECT COUNT(*) FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); -SELECT COUNT(*) FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); -SELECT COUNT(*) FROM alerts WHERE NOT userid IN (SELECT userid FROM users); -SELECT COUNT(*) FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); +SELECT COUNT(*) AS ALERTS_MISSING_AID FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); +SELECT COUNT(*) AS ALERTS_MISSING_EID FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); +SELECT COUNT(*) AS ALERTS_MISSING_UID FROM alerts WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS ALERTS_MISSING_MID FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); -- Count the amount of rows that would be deleted for orphaned application entries that no longer map back to a host -SELECT COUNT(*) FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS APPS_MISSING_HID FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); -- Count the amount of rows that would be deleted for orphaned auditlog details (such as logins) -SELECT COUNT(*) FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog); -SELECT COUNT(*) FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS AUDITLOG_MISSING_AID FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog); +SELECT COUNT(*) AS AUDITLOG_MISSING_UID FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); -- Count the amount of rows that would be deleted for orphaned conditions -SELECT COUNT(*) FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); +SELECT COUNT(*) AS COND_MISSING_AID FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); -- Count the amount of rows that would be deleted for orphaned functions -SELECT COUNT(*) FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); -SELECT COUNT(*) FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); +SELECT COUNT(*) AS FUNC_MISSING_IID FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS FUNC_MISSING_TID FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); -- Count the amount of rows that would be deleted for orphaned graph items -SELECT COUNT(*) FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); -SELECT COUNT(*) FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS GRAPHS_MISSING_GID FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); +SELECT COUNT(*) AS GRAPHS_MISSING_IID FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); -- Count the amount of rows that would be deleted for orphaned host_profiles -SELECT COUNT(*) FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts); -SELECT COUNT(*) FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS HOSTPROFS_MISSING_HID FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS HOSTPROFEXT_MISSING_HID FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts); -- Count the amount of rows that would be deleted for orphaned host macro's -SELECT COUNT(*) FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS HOSTMACRO_MISSING_HID FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); -- Count the amount of rows that would be deleted for orphaned item data -SELECT COUNT(*) FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); -SELECT COUNT(*) FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); -SELECT COUNT(*) FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(*) AS ITEMS_MISSING_HID FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS ITEMSAPP_MISSING_AID FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); +SELECT COUNT(*) AS ITEMSAPP_MISSING_IID FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); -- Count the amount of rows that would be deleted for orphaned HTTP check data -SELECT COUNT(*) FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); -SELECT COUNT(*) FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); -SELECT COUNT(*) FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); -SELECT COUNT(*) FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); +SELECT COUNT(*) AS HTTPST_MISSING_HID FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); +SELECT COUNT(*) AS HTTPSTI_MISSING_HSID FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); +SELECT COUNT(*) AS ITEMSSTI_MISSING_IID FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS HTTPT_MISSING_AID FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); -- Count the amount of rows that would be deleted for orphaned maintenance data SELECT COUNT(*) FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); From 96ec8917f5c0794e30594a2491b0940a66630962 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 17 Apr 2014 15:44:59 +0000 Subject: [PATCH 11/40] add scripts to stop zabbix email flood --- README.md | 6 ++++++ stop-emails/delete-alerts.sql | 3 +++ stop-emails/stop-alerts.sql | 2 ++ 3 files changed, 11 insertions(+) create mode 100644 stop-emails/delete-alerts.sql create mode 100644 stop-emails/stop-alerts.sql diff --git a/README.md b/README.md index b4f321f..3089ceb 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,9 @@ The queries are tested against Zabbix 1.8 and 2.0. Aplly with caution to other v #### Delete all history for disabled items user@host-$ psql zabbix < unused/postgresql/clean.sql + +#### Stop zabbix email flood (mysql, pgsql) + + user@host-$ sudo service zabbix-server stop + user@host-$ psql zabbix < stop-email/stop-alerts (use delete-alerts if you're not interested in alert history) + user@host-$ sudo service zabbix-server start diff --git a/stop-emails/delete-alerts.sql b/stop-emails/delete-alerts.sql new file mode 100644 index 0000000..f854c71 --- /dev/null +++ b/stop-emails/delete-alerts.sql @@ -0,0 +1,3 @@ +truncate alerts; +truncate escalations; + diff --git a/stop-emails/stop-alerts.sql b/stop-emails/stop-alerts.sql new file mode 100644 index 0000000..783eb14 --- /dev/null +++ b/stop-emails/stop-alerts.sql @@ -0,0 +1,2 @@ +update alerts set status=2,error='' where status=0 and alerttype=0; +truncate escalations; From faecf6ea9d4c44ce507ac2322d10b2c40e055374 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 28 Apr 2014 18:42:12 +0000 Subject: [PATCH 12/40] change layout, update readme --- README.md | 20 +++++++++---------- orphaned/check.sql => check-orphaned-data.sql | 0 ...lete-alerts.sql => delete-email-alerts.sql | 0 .../clean.sql => delete-old-data.pg.sql | 0 .../clean.sql => delete-orphaned-data.sql | 0 .../clean.sql => delete-unused-data.sql | 0 .../stop-alerts.sql => stop-email-alerts.sql | 0 7 files changed, 10 insertions(+), 10 deletions(-) rename orphaned/check.sql => check-orphaned-data.sql (100%) rename stop-emails/delete-alerts.sql => delete-email-alerts.sql (100%) rename old/postgresql/clean.sql => delete-old-data.pg.sql (100%) rename orphaned/clean.sql => delete-orphaned-data.sql (100%) rename unused/postgresql/clean.sql => delete-unused-data.sql (100%) rename stop-emails/stop-alerts.sql => stop-email-alerts.sql (100%) diff --git a/README.md b/README.md index 3089ceb..18241ac 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,35 @@ -## Zabbix Database Cleanup +## Zabbix SQL repo -This repo contains a few useful queries to cleanup old and/or orphaned data in a Zabbix database. While Zabbix does Housekeeping, it does not clear orphaned data very well. Especially if you come from a long upgrade-line (1.x to 1.6 to 1.8 to 2.x), chances are your database is riddled with orphaned data that is consuming disk space. +This repo contains a few useful queries for Zabbix database, mostly to cleanup old and/or orphaned data. If you have a large database please note that these can take a while (read: a few hours is normal). -Use the queries above on your won risk. Take *backups* first. Once you have backups, go ahead and be brave. +Use the queries above on your own risk. Take *backups* first. -"Orphaned" queries are the same for Mysql and Postgresql. "Old" are not, so pay attention. +Queries named "*.pg.sql" (i.e. delete-old-data.pg.sql) only work for Postgresql. Pay attention. ##### NOTE -The queries are tested against Zabbix 1.8 and 2.0. Aplly with caution to other version. Consider reporting the results, whether succesful or not. +The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other versions. Consider reporting the results, whether succesful or not. #### Check how many orphaned rows do you have - user@host-$ psql zabbix < orphaned/check.sql + user@host-$ myql zabbix < check-orphaned-data.sql #### Delete orphaned rows - user@host-$ psql zabbix < orphaned/clean.sql + user@host-$ myql zabbix < delete-orphaned-data.sql #### Delete old data (1 week for history, 3 months for trends - edit sql at your own discretion) - user@host-$ psql zabbix < old/postgresql/clean.sql + user@host-$ psql zabbix < delete-old-data.pg.sql #### Delete all history for disabled items - user@host-$ psql zabbix < unused/postgresql/clean.sql + user@host-$ psql zabbix < delete-unused-data.sql #### Stop zabbix email flood (mysql, pgsql) user@host-$ sudo service zabbix-server stop - user@host-$ psql zabbix < stop-email/stop-alerts (use delete-alerts if you're not interested in alert history) + user@host-$ psql zabbix < stop-email-alerts.sql (use delete-email-alerts.sql if you're not interested in alert history) user@host-$ sudo service zabbix-server start diff --git a/orphaned/check.sql b/check-orphaned-data.sql similarity index 100% rename from orphaned/check.sql rename to check-orphaned-data.sql diff --git a/stop-emails/delete-alerts.sql b/delete-email-alerts.sql similarity index 100% rename from stop-emails/delete-alerts.sql rename to delete-email-alerts.sql diff --git a/old/postgresql/clean.sql b/delete-old-data.pg.sql similarity index 100% rename from old/postgresql/clean.sql rename to delete-old-data.pg.sql diff --git a/orphaned/clean.sql b/delete-orphaned-data.sql similarity index 100% rename from orphaned/clean.sql rename to delete-orphaned-data.sql diff --git a/unused/postgresql/clean.sql b/delete-unused-data.sql similarity index 100% rename from unused/postgresql/clean.sql rename to delete-unused-data.sql diff --git a/stop-emails/stop-alerts.sql b/stop-email-alerts.sql similarity index 100% rename from stop-emails/stop-alerts.sql rename to stop-email-alerts.sql From 701c053952cee7df1b48b63f995be410391dc433 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 28 Apr 2014 19:27:51 +0000 Subject: [PATCH 13/40] add check-old-data script, add parameters to delete-old-data --- check-old-data.pg.sql | 18 ++++++++++++++++++ delete-old-data.pg.sql | 24 +++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 check-old-data.pg.sql diff --git a/check-old-data.pg.sql b/check-old-data.pg.sql new file mode 100644 index 0000000..ed4301a --- /dev/null +++ b/check-old-data.pg.sql @@ -0,0 +1,18 @@ +-- keep 1 week of history and 3 months of trends +\set history_interval 6 +\set trends_interval 90 + +SELECT count(alertid) AS alerts FROM alerts where age(to_timestamp(alerts.clock)) > (:history_interval * interval '1 day'); + +SELECT count(acknowledgeid) AS acknowledges FROM acknowledges where age(to_timestamp(acknowledges.clock)) > (:history_interval * interval '1 day'); + +SELECT count(eventid) AS events FROM events where age(to_timestamp(events.clock)) > (:history_interval * interval '1 day'); + +SELECT count(itemid) AS history FROM history where age(to_timestamp(history.clock)) > (:history_interval * interval '1 day'); +SELECT count(itemid) AS history_uint FROM history_uint where age(to_timestamp(history_uint.clock)) > (:history_interval * interval '1 day') ; +SELECT count(itemid) AS history_str FROM history_str where age(to_timestamp(history_str.clock)) > (:history_interval * interval '1 day') ; +SELECT count(itemid) AS history_text FROM history_text where age(to_timestamp(history_text.clock)) > (:history_interval * interval '1 day') ; +SELECT count(itemid) AS history_log FROM history_log where age(to_timestamp(history_log.clock)) > (:history_interval * interval '1 day') ; + +SELECT count(itemid) AS trends FROM trends where age(to_timestamp(trends.clock)) > (:trends_interval * interval '1 day'); +SELECT count(itemid) AS trends_uint FROM trends_uint where age(to_timestamp(trends_uint.clock)) > (:trends_interval * interval '1 day') ; diff --git a/delete-old-data.pg.sql b/delete-old-data.pg.sql index cfc8c79..d9113ac 100644 --- a/delete-old-data.pg.sql +++ b/delete-old-data.pg.sql @@ -1,16 +1,18 @@ -#keep 1 week of history and 3 months of trends +-- keep 1 week of history and 3 months of trends +\set history_interval 7 +\set trends_interval 90 -delete FROM alerts where age(to_timestamp(alerts.clock)) > interval '7 days'; +DELETE FROM alerts where age(to_timestamp(alerts.clock)) > (:history_interval * interval '1 day'); -delete FROM acknowledges where age(to_timestamp(acknowledges.clock)) > interval '7 days'; +DELETE FROM acknowledges where age(to_timestamp(acknowledges.clock)) > (:history_interval * interval '1 day'); -delete FROM events where age(to_timestamp(events.clock)) > interval '7 days'; +DELETE FROM events where age(to_timestamp(events.clock)) > (:history_interval * interval '1 day'); -delete FROM history where age(to_timestamp(history.clock)) > interval '7 days'; -delete FROM history_uint where age(to_timestamp(history_uint.clock)) > interval '7 days' ; -delete FROM history_str where age(to_timestamp(history_str.clock)) > interval '7 days' ; -delete FROM history_text where age(to_timestamp(history_text.clock)) > interval '7 days' ; -delete FROM history_log where age(to_timestamp(history_log.clock)) > interval '7 days' ; +DELETE FROM history where age(to_timestamp(history.clock)) > (:history_interval * interval '1 day'); +DELETE FROM history_uint where age(to_timestamp(history_uint.clock)) > (:history_interval * interval '1 day') ; +DELETE FROM history_str where age(to_timestamp(history_str.clock)) > (:history_interval * interval '1 day') ; +DELETE FROM history_text where age(to_timestamp(history_text.clock)) > (:history_interval * interval '1 day') ; +DELETE FROM history_log where age(to_timestamp(history_log.clock)) > (:history_interval * interval '1 day') ; -delete FROM trends where age(to_timestamp(trends.clock)) > interval '90 days'; -delete FROM trends_uint where age(to_timestamp(trends_uint.clock)) > interval '90 days' ; +DELETE FROM trends where age(to_timestamp(trends.clock)) > (:trends_interval * interval '1 day'); +DELETE FROM trends_uint where age(to_timestamp(trends_uint.clock)) > (:trends_interval * interval '1 day') ; From b96c34072f1e35967e386482eebc8ae056a2e5a8 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 28 Apr 2014 19:29:01 +0000 Subject: [PATCH 14/40] keep 1 week of history, as indicated in readme --- check-old-data.pg.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check-old-data.pg.sql b/check-old-data.pg.sql index ed4301a..0e803ce 100644 --- a/check-old-data.pg.sql +++ b/check-old-data.pg.sql @@ -1,5 +1,5 @@ -- keep 1 week of history and 3 months of trends -\set history_interval 6 +\set history_interval 7 \set trends_interval 90 SELECT count(alertid) AS alerts FROM alerts where age(to_timestamp(alerts.clock)) > (:history_interval * interval '1 day'); From d5ba1f43c1b764a57e44feecac008fcc2a24d5dd Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 28 Apr 2014 19:30:20 +0000 Subject: [PATCH 15/40] use more intuitive script names --- README.md | 2 +- delete-email-alerts.sql => stop-and-delete-email-alerts.sql | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename delete-email-alerts.sql => stop-and-delete-email-alerts.sql (100%) diff --git a/README.md b/README.md index 18241ac..39fd4b5 100644 --- a/README.md +++ b/README.md @@ -31,5 +31,5 @@ The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other #### Stop zabbix email flood (mysql, pgsql) user@host-$ sudo service zabbix-server stop - user@host-$ psql zabbix < stop-email-alerts.sql (use delete-email-alerts.sql if you're not interested in alert history) + user@host-$ psql zabbix < stop-email-alerts.sql (use stop-and-delete-email-alerts.sql if you're not interested in alert history) user@host-$ sudo service zabbix-server start diff --git a/delete-email-alerts.sql b/stop-and-delete-email-alerts.sql similarity index 100% rename from delete-email-alerts.sql rename to stop-and-delete-email-alerts.sql From 704524a45364ece4d09519813eb9488f22dfb680 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 28 Apr 2014 19:39:41 +0000 Subject: [PATCH 16/40] add script to check unused data --- check-unused-data.sql | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 check-unused-data.sql diff --git a/check-unused-data.sql b/check-unused-data.sql new file mode 100644 index 0000000..9e67b9c --- /dev/null +++ b/check-unused-data.sql @@ -0,0 +1,8 @@ +SELECT count(itemid) AS history FROM history WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +SELECT count(itemid) AS history_uint FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +SELECT count(itemid) AS history_str FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +SELECT count(itemid) AS history_text FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +SELECT count(itemid) AS history_log FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); + +SELECT count(itemid) AS trends FROM trends WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +SELECT count(itemid) AS trends FROM trends itemid NOT IN (SELECT itemid FROM items WHERE status='0'); From 1413819f0f54af690732a7b39604c0e6d0446ce2 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 28 Apr 2014 23:49:02 +0000 Subject: [PATCH 17/40] add old data scripts for mysql; uppercase SQL words in other files --- check-old-data.my.sql | 16 ++++++++++++++++ check-old-data.pg.sql | 22 +++++++++++----------- delete-old-data.my.sql | 16 ++++++++++++++++ delete-unused-data.sql | 14 +++++++------- stop-and-delete-email-alerts.sql | 5 ++--- stop-email-alerts.sql | 4 ++-- 6 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 check-old-data.my.sql create mode 100644 delete-old-data.my.sql diff --git a/check-old-data.my.sql b/check-old-data.my.sql new file mode 100644 index 0000000..7e12deb --- /dev/null +++ b/check-old-data.my.sql @@ -0,0 +1,16 @@ +-- intervals in days +SET @history_interval = 7; +SET @trends_interval = 90; + +SELECT count(alertid) AS alerts FROM alerts WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +SELECT count(acknowledgeid) AS acknowledges FROM acknowledges WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +SELECT count(eventid) AS events FROM events WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); + +SELECT count(itemid) AS history FROM history WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +SELECT count(itemid) AS history_uint FROM history_uint WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +SELECT count(itemid) AS history_str FROM history_str WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +SELECT count(itemid) AS history_text FROM history_text WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +SELECT count(itemid) AS history_log FROM history_log WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); + +SELECT count(itemid) AS trends FROM trends WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@trends_interval * 24 * 60 * 60); +SELECT count(itemid) AS trends_uint FROM trends_uint WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@trends_interval * 24 * 60 * 60); diff --git a/check-old-data.pg.sql b/check-old-data.pg.sql index 0e803ce..2ccb877 100644 --- a/check-old-data.pg.sql +++ b/check-old-data.pg.sql @@ -1,18 +1,18 @@ --- keep 1 week of history and 3 months of trends +-- intevals in days \set history_interval 7 \set trends_interval 90 -SELECT count(alertid) AS alerts FROM alerts where age(to_timestamp(alerts.clock)) > (:history_interval * interval '1 day'); +SELECT count(alertid) AS alerts FROM alerts WHERE age(to_timestamp(alerts.clock)) > (:history_interval * interval '1 day'); -SELECT count(acknowledgeid) AS acknowledges FROM acknowledges where age(to_timestamp(acknowledges.clock)) > (:history_interval * interval '1 day'); +SELECT count(acknowledgeid) AS acknowledges FROM acknowledges WHERE age(to_timestamp(acknowledges.clock)) > (:history_interval * interval '1 day'); -SELECT count(eventid) AS events FROM events where age(to_timestamp(events.clock)) > (:history_interval * interval '1 day'); +SELECT count(eventid) AS events FROM events WHERE age(to_timestamp(events.clock)) > (:history_interval * interval '1 day'); -SELECT count(itemid) AS history FROM history where age(to_timestamp(history.clock)) > (:history_interval * interval '1 day'); -SELECT count(itemid) AS history_uint FROM history_uint where age(to_timestamp(history_uint.clock)) > (:history_interval * interval '1 day') ; -SELECT count(itemid) AS history_str FROM history_str where age(to_timestamp(history_str.clock)) > (:history_interval * interval '1 day') ; -SELECT count(itemid) AS history_text FROM history_text where age(to_timestamp(history_text.clock)) > (:history_interval * interval '1 day') ; -SELECT count(itemid) AS history_log FROM history_log where age(to_timestamp(history_log.clock)) > (:history_interval * interval '1 day') ; +SELECT count(itemid) AS history FROM history WHERE age(to_timestamp(history.clock)) > (:history_interval * interval '1 day'); +SELECT count(itemid) AS history_uint FROM history_uint WHERE age(to_timestamp(history_uint.clock)) > (:history_interval * interval '1 day') ; +SELECT count(itemid) AS history_str FROM history_str WHERE age(to_timestamp(history_str.clock)) > (:history_interval * interval '1 day') ; +SELECT count(itemid) AS history_text FROM history_text WHERE age(to_timestamp(history_text.clock)) > (:history_interval * interval '1 day') ; +SELECT count(itemid) AS history_log FROM history_log WHERE age(to_timestamp(history_log.clock)) > (:history_interval * interval '1 day') ; -SELECT count(itemid) AS trends FROM trends where age(to_timestamp(trends.clock)) > (:trends_interval * interval '1 day'); -SELECT count(itemid) AS trends_uint FROM trends_uint where age(to_timestamp(trends_uint.clock)) > (:trends_interval * interval '1 day') ; +SELECT count(itemid) AS trends FROM trends WHERE age(to_timestamp(trends.clock)) > (:trends_interval * interval '1 day'); +SELECT count(itemid) AS trends_uint FROM trends_uint WHERE age(to_timestamp(trends_uint.clock)) > (:trends_interval * interval '1 day') ; diff --git a/delete-old-data.my.sql b/delete-old-data.my.sql new file mode 100644 index 0000000..3efadc4 --- /dev/null +++ b/delete-old-data.my.sql @@ -0,0 +1,16 @@ +-- intervals in days +SET @history_interval = 7; +SET @trends_interval = 90; + +DELETE FROM alerts WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +SELECT count(acknowledgeid) AS acknowledges FROM acknowledges WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +DELETE FROM events WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); + +DELETE FROM history WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +DELETE FROM history_uint WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +DELETE FROM history_str WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +DELETE FROM history_text WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +DELETE FROM history_log WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); + +DELETE FROM trends WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@trends_interval * 24 * 60 * 60); +DELETE FROM trends_uint WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@trends_interval * 24 * 60 * 60); diff --git a/delete-unused-data.sql b/delete-unused-data.sql index 9f12bd7..f6fb34f 100644 --- a/delete-unused-data.sql +++ b/delete-unused-data.sql @@ -1,8 +1,8 @@ -delete from history where itemid not in (select itemid from items where status='0'); -delete from history_uint where itemid not in (select itemid from items where status='0'); -delete from history_str where itemid not in (select itemid from items where status='0'); -delete from history_text where itemid not in (select itemid from items where status='0'); -delete from history_log where itemid not in (select itemid from items where status='0'); +DELETE FROM history WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +DELETE FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +DELETE FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +DELETE FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +DELETE FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); -delete from trends where itemid not in (select itemid from items where status='0'); -delete from trends_uint where itemid not in (select itemid from items where status='0'); +DELETE FROM trends WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +DELETE FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); diff --git a/stop-and-delete-email-alerts.sql b/stop-and-delete-email-alerts.sql index f854c71..d9dc1be 100644 --- a/stop-and-delete-email-alerts.sql +++ b/stop-and-delete-email-alerts.sql @@ -1,3 +1,2 @@ -truncate alerts; -truncate escalations; - +TRUNCATE alerts; +TRUNCATE escalations; diff --git a/stop-email-alerts.sql b/stop-email-alerts.sql index 783eb14..2c1c4c4 100644 --- a/stop-email-alerts.sql +++ b/stop-email-alerts.sql @@ -1,2 +1,2 @@ -update alerts set status=2,error='' where status=0 and alerttype=0; -truncate escalations; +UPDATE alerts SET status=2,error='' WHERE status=0 AND alerttype=0; +TRUNCATE escalations; From cf398086dcd1e7e8e8fa8780ecbe6e6ff0aa9cf5 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 28 Apr 2014 23:52:04 +0000 Subject: [PATCH 18/40] update readme to include mysql queries --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 39fd4b5..c460722 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ If you have a large database please note that these can take a while (read: a fe Use the queries above on your own risk. Take *backups* first. -Queries named "*.pg.sql" (i.e. delete-old-data.pg.sql) only work for Postgresql. Pay attention. +Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.sql, respectively. Pay attention. -##### NOTE +#### NOTE The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other versions. Consider reporting the results, whether succesful or not. From 62016fbcc17c54555cd01c94e853758af6a6223d Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Tue, 29 Apr 2014 18:44:54 +0000 Subject: [PATCH 19/40] fix typo in unused data select --- check-unused-data.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check-unused-data.sql b/check-unused-data.sql index 9e67b9c..9b1c419 100644 --- a/check-unused-data.sql +++ b/check-unused-data.sql @@ -5,4 +5,4 @@ SELECT count(itemid) AS history_text FROM history_text WHERE itemid NOT IN (SELE SELECT count(itemid) AS history_log FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); SELECT count(itemid) AS trends FROM trends WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); -SELECT count(itemid) AS trends FROM trends itemid NOT IN (SELECT itemid FROM items WHERE status='0'); +SELECT count(itemid) AS trends_uint FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items WHERE status='0'); From 59d75624b72e47e42a25954e53ca2327a33be55d Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Tue, 29 Apr 2014 18:55:17 +0000 Subject: [PATCH 20/40] fix one more typo in delete --- delete-old-data.my.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/delete-old-data.my.sql b/delete-old-data.my.sql index 3efadc4..dc89b41 100644 --- a/delete-old-data.my.sql +++ b/delete-old-data.my.sql @@ -3,7 +3,7 @@ SET @history_interval = 7; SET @trends_interval = 90; DELETE FROM alerts WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); -SELECT count(acknowledgeid) AS acknowledges FROM acknowledges WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); +DELETE FROM acknowledges WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); DELETE FROM events WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); DELETE FROM history WHERE (UNIX_TIMESTAMP(NOW()) - clock) > (@history_interval * 24 * 60 * 60); From ceba39db9dad8ed4441f321d336d94b5d10d5883 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Tue, 29 Apr 2014 18:59:28 +0000 Subject: [PATCH 21/40] fix readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c460722..e22ba7f 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,11 @@ The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other #### Check how many orphaned rows do you have - user@host-$ myql zabbix < check-orphaned-data.sql + user@host-$ mysql zabbix < check-orphaned-data.sql #### Delete orphaned rows - user@host-$ myql zabbix < delete-orphaned-data.sql + user@host-$ mysql zabbix < delete-orphaned-data.sql #### Delete old data (1 week for history, 3 months for trends - edit sql at your own discretion) From 81500e77922016a8bc0c7cb0f08045c3ef5fff73 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sun, 24 Aug 2014 09:54:27 +0000 Subject: [PATCH 22/40] fix readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e22ba7f..c2afc58 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o If you have a large database please note that these can take a while (read: a few hours is normal). -Use the queries above on your own risk. Take *backups* first. +Use the queries above on your own risk. Take **backups** first. Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.sql, respectively. Pay attention. @@ -31,5 +31,7 @@ The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other #### Stop zabbix email flood (mysql, pgsql) user@host-$ sudo service zabbix-server stop - user@host-$ psql zabbix < stop-email-alerts.sql (use stop-and-delete-email-alerts.sql if you're not interested in alert history) + user@host-$ psql zabbix < stop-email-alerts.sql user@host-$ sudo service zabbix-server start + +Use stop-and-delete-email-alerts.sql if you're not interested in alert history. \ No newline at end of file From a6f4bb673ff5711cd826381f62fb37b452c5d948 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sun, 24 Aug 2014 09:55:08 +0000 Subject: [PATCH 23/40] fix readme 2 --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2afc58..e22886f 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,9 @@ The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other #### Stop zabbix email flood (mysql, pgsql) +(Use stop-and-delete-email-alerts.sql if you're not interested in alert history) + user@host-$ sudo service zabbix-server stop user@host-$ psql zabbix < stop-email-alerts.sql user@host-$ sudo service zabbix-server start -Use stop-and-delete-email-alerts.sql if you're not interested in alert history. \ No newline at end of file From b686c0eafbf4d0e1750b2a6df5528e8c32b8e3f8 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 13:42:06 +0700 Subject: [PATCH 24/40] better support for Zabbix 2.x. Better output for postgres queries --- README.md | 5 +- check-orphaned-data.sql | 82 ------------------ check-orphaned-data.zbx18.sql | 84 +++++++++++++++++++ check-orphaned-data.zbx2x.sql | 79 +++++++++++++++++ ...data.sql => delete-orphaned-data.zbx18.sql | 43 ++++------ delete-orphaned-data.zbx2x.sql | 63 ++++++++++++++ 6 files changed, 242 insertions(+), 114 deletions(-) delete mode 100644 check-orphaned-data.sql create mode 100644 check-orphaned-data.zbx18.sql create mode 100644 check-orphaned-data.zbx2x.sql rename delete-orphaned-data.sql => delete-orphaned-data.zbx18.sql (83%) create mode 100644 delete-orphaned-data.zbx2x.sql diff --git a/README.md b/README.md index e22886f..ec8964d 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,11 @@ The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other #### Delete old data (1 week for history, 3 months for trends - edit sql at your own discretion) - user@host-$ psql zabbix < delete-old-data.pg.sql + user@host-$ psql -A -R ' : ' -P 'footer=off' zabbix < delete-old-data.pg.sql #### Delete all history for disabled items - user@host-$ psql zabbix < delete-unused-data.sql + user@host-$ psql -A -R ' : ' -P 'footer=off' zabbix < delete-unused-data.sql #### Stop zabbix email flood (mysql, pgsql) @@ -35,4 +35,3 @@ The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other user@host-$ sudo service zabbix-server stop user@host-$ psql zabbix < stop-email-alerts.sql user@host-$ sudo service zabbix-server start - diff --git a/check-orphaned-data.sql b/check-orphaned-data.sql deleted file mode 100644 index cb3a9e9..0000000 --- a/check-orphaned-data.sql +++ /dev/null @@ -1,82 +0,0 @@ --- Count the amount of rows that would be deleted for all orphaned acknowledge entries -SELECT COUNT(*) AS ACK_MISSING_UID FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); -SELECT COUNT(*) AS ACK_MISSING_EID FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); - --- Count the amount of rows that would be deleted for orphaned alerts entries -SELECT COUNT(*) AS ALERTS_MISSING_AID FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); -SELECT COUNT(*) AS ALERTS_MISSING_EID FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); -SELECT COUNT(*) AS ALERTS_MISSING_UID FROM alerts WHERE NOT userid IN (SELECT userid FROM users); -SELECT COUNT(*) AS ALERTS_MISSING_MID FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); - --- Count the amount of rows that would be deleted for orphaned application entries that no longer map back to a host -SELECT COUNT(*) AS APPS_MISSING_HID FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); - --- Count the amount of rows that would be deleted for orphaned auditlog details (such as logins) -SELECT COUNT(*) AS AUDITLOG_MISSING_AID FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog); -SELECT COUNT(*) AS AUDITLOG_MISSING_UID FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); - --- Count the amount of rows that would be deleted for orphaned conditions -SELECT COUNT(*) AS COND_MISSING_AID FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); - --- Count the amount of rows that would be deleted for orphaned functions -SELECT COUNT(*) AS FUNC_MISSING_IID FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); -SELECT COUNT(*) AS FUNC_MISSING_TID FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); - --- Count the amount of rows that would be deleted for orphaned graph items -SELECT COUNT(*) AS GRAPHS_MISSING_GID FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); -SELECT COUNT(*) AS GRAPHS_MISSING_IID FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); - --- Count the amount of rows that would be deleted for orphaned host_profiles -SELECT COUNT(*) AS HOSTPROFS_MISSING_HID FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts); -SELECT COUNT(*) AS HOSTPROFEXT_MISSING_HID FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts); - --- Count the amount of rows that would be deleted for orphaned host macro's -SELECT COUNT(*) AS HOSTMACRO_MISSING_HID FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); - --- Count the amount of rows that would be deleted for orphaned item data -SELECT COUNT(*) AS ITEMS_MISSING_HID FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); -SELECT COUNT(*) AS ITEMSAPP_MISSING_AID FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); -SELECT COUNT(*) AS ITEMSAPP_MISSING_IID FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); - --- Count the amount of rows that would be deleted for orphaned HTTP check data -SELECT COUNT(*) AS HTTPST_MISSING_HID FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); -SELECT COUNT(*) AS HTTPSTI_MISSING_HSID FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); -SELECT COUNT(*) AS ITEMSSTI_MISSING_IID FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); -SELECT COUNT(*) AS HTTPT_MISSING_AID FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); - --- Count the amount of rows that would be deleted for orphaned maintenance data -SELECT COUNT(*) FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); -SELECT COUNT(*) FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups); -SELECT COUNT(*) FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); -SELECT COUNT(*) FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts); -SELECT COUNT(*) FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); -SELECT COUNT(*) FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods); - --- Count the amount of rows that would be deleted for orphaned mappings -SELECT COUNT(*) FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps); - --- Count the amount of rows that would be deleted for orphaned media / user items -SELECT COUNT(*) FROM media WHERE NOT userid IN (SELECT userid FROM users); -SELECT COUNT(*) FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); -SELECT COUNT(*) FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp); -SELECT COUNT(*) FROM rights WHERE NOT id IN (SELECT groupid FROM groups); -SELECT COUNT(*) FROM sessions WHERE NOT userid IN (SELECT userid FROM users); -SELECT COUNT(*) FROM user_history WHERE NOT userid IN (SELECT userid FROM users); - --- Count the amount of rows that would be deleted for orphaned screens -SELECT COUNT(*) FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens); - --- Count the amount of rows that would be deleted for orphaned events & triggers -SELECT COUNT(*) FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); -SELECT COUNT(*) FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers); - --- Count the amount of records in the history/trends table for items that no longer exist -SELECT COUNT(itemid) FROM history WHERE itemid NOT IN (SELECT itemid FROM items); -SELECT COUNT(itemid) FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); -SELECT COUNT(itemid) FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); -SELECT COUNT(itemid) FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); -SELECT COUNT(itemid) FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items); -SELECT COUNT(itemid) FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); - -SELECT COUNT(itemid) FROM trends WHERE itemid NOT IN (SELECT itemid FROM items); -SELECT COUNT(itemid) FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items); diff --git a/check-orphaned-data.zbx18.sql b/check-orphaned-data.zbx18.sql new file mode 100644 index 0000000..faa5f7f --- /dev/null +++ b/check-orphaned-data.zbx18.sql @@ -0,0 +1,84 @@ +-- Count the amount of rows that would be deleted for all orphaned acknowledge entries +SELECT COUNT(*) AS acknowedges_missing_user FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS acknowedges_missing_event FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); + +-- Count the amount of rows that would be deleted for orphaned alerts entries +SELECT COUNT(*) AS alerts_missing_action FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); +SELECT COUNT(*) AS alerts_missing_event FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); +SELECT COUNT(*) AS alerts_missing_user FROM alerts WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS alerts_missing_mediatype FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + +-- Count the amount of rows that would be deleted for orphaned application entries that no longer map back to a host +SELECT COUNT(*) AS applications_missing_host FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); + +-- Count the amount of rows that would be deleted for orphaned auditlog details (such as logins) +-- SELECT COUNT(*) AS auditlog_missing_details FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog); -- This takes exponential time, disabled +SELECT COUNT(*) AS auditlog_missing_user FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); + +-- Count the amount of rows that would be deleted for orphaned conditions +SELECT COUNT(*) AS conditions_missing_acction FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); + +-- Count the amount of rows that would be deleted for orphaned functions +SELECT COUNT(*) AS functions_missing_item FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS functions_missing_trigger FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); + +-- Count the amount of rows that would be deleted for orphaned graph items +SELECT COUNT(*) AS graph_items_missing_graphs FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); +SELECT COUNT(*) AS graph_items_missing_items FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); + +-- Count the amount of records in the history/trends table for items that no longer exist +SELECT COUNT(itemid) AS history_missing_item FROM history WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_uint_missing_item FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_log_missing_item FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_str_missing_item FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_sync_missing_item FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_text_missing_item FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned host_profiles +SELECT COUNT(*) AS host_profiles_missing_hosts FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS host_profiles_external_missing_hosts FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts); + +-- Count the amount of rows that would be deleted for orphaned host macro's +SELECT COUNT(*) AS host_macros_missing_hosts FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); + +-- Count the amount of rows that would be deleted for orphaned item data +SELECT COUNT(itemid) AS items_missing_hosts FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS item_apps_missing_apps FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); +SELECT COUNT(*) AS item_apps_missing_items FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned HTTP check data +SELECT COUNT(*) AS http_step_missing_tests FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); +SELECT COUNT(*) AS http_step_items_missing_steps FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); +SELECT COUNT(*) AS http_step_items_missing_items FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS http_test_missing_apps FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); + +-- Count the amount of rows that would be deleted for orphaned maintenance data +SELECT COUNT(*) AS maintenance_group_missing_maintenances FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenance_group_missing_groups FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups); +SELECT COUNT(*) AS maintenances_hosts_missing_maintenances FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenances_hosts_missing_hosts FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS maintenances_windows_missing_maintenances FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenances_windows_missing_windows FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods); + +-- Count the amount of rows that would be deleted for orphaned mappings +SELECT COUNT(*) AS mappings_missing_valuemap FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps); + +-- Count the amount of rows that would be deleted for orphaned media / user items +SELECT COUNT(*) AS media_missing_user FROM media WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS media_missing_type FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); +SELECT COUNT(*) AS rights_missing_user FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp); +SELECT COUNT(*) AS rights_missing_group FROM rights WHERE NOT id IN (SELECT groupid FROM groups); + +-- Count the amount of rows that would be deleted for orphaned screens +SELECT COUNT(*) AS screens_items_missing_screen FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens); + +SELECT COUNT(*) AS sessions_missing_user FROM sessions WHERE NOT userid IN (SELECT userid FROM users); + +SELECT COUNT(itemid) AS trends_missing_item FROM trends WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS trends_uint_missing_item FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned events & triggers +SELECT COUNT(*) AS trigger_down_dependency_missing_trigger FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); +SELECT COUNT(*) AS trigger_up_dependency_missing_trigger FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers); + +SELECT COUNT(*) AS user_history_missing_user FROM user_history WHERE NOT userid IN (SELECT userid FROM users); diff --git a/check-orphaned-data.zbx2x.sql b/check-orphaned-data.zbx2x.sql new file mode 100644 index 0000000..c7f951a --- /dev/null +++ b/check-orphaned-data.zbx2x.sql @@ -0,0 +1,79 @@ +-- Count the amount of rows that would be deleted for all orphaned acknowledge entries +SELECT COUNT(*) AS acknowedges_missing_user FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS acknowedges_missing_event FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); + +-- Count the amount of rows that would be deleted for orphaned alerts entries +SELECT COUNT(*) AS alerts_missing_action FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); +SELECT COUNT(*) AS alerts_missing_event FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); +SELECT COUNT(*) AS alerts_missing_user FROM alerts WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS alerts_missing_mediatype FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + +-- Count the amount of rows that would be deleted for orphaned application entries that no longer map back to a host +SELECT COUNT(*) AS applications_missing_host FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); + +-- Count the amount of rows that would be deleted for orphaned auditlog details (such as logins) +-- SELECT COUNT(*) AS auditlog_missing_details FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog); -- This takes exponential time, disabled +SELECT COUNT(*) AS auditlog_missing_user FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); + +-- Count the amount of rows that would be deleted for orphaned conditions +SELECT COUNT(*) AS conditions_missing_acction FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); + +-- Count the amount of rows that would be deleted for orphaned functions +SELECT COUNT(*) AS functions_missing_item FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS functions_missing_trigger FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); + +-- Count the amount of rows that would be deleted for orphaned graph items +SELECT COUNT(*) AS graph_items_missing_graphs FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); +SELECT COUNT(*) AS graph_items_missing_items FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); + +-- Count the amount of records in the history/trends table for items that no longer exist +SELECT COUNT(itemid) AS history_missing_item FROM history WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_uint_missing_item FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_log_missing_item FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_str_missing_item FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_text_missing_item FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned host macro's +SELECT COUNT(*) AS host_macros_missing_hosts FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); + +-- Count the amount of rows that would be deleted for orphaned item data +SELECT COUNT(itemid) AS items_missing_hosts FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS item_apps_missing_apps FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); +SELECT COUNT(*) AS item_apps_missing_items FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned HTTP check data +SELECT COUNT(*) AS http_step_missing_tests FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); +SELECT COUNT(*) AS http_step_items_missing_steps FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); +SELECT COUNT(*) AS http_step_items_missing_items FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS http_test_missing_apps FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); + +-- Count the amount of rows that would be deleted for orphaned maintenance data +SELECT COUNT(*) AS maintenance_group_missing_maintenances FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenance_group_missing_groups FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups); +SELECT COUNT(*) AS maintenances_hosts_missing_maintenances FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenances_hosts_missing_hosts FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS maintenances_windows_missing_maintenances FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenances_windows_missing_windows FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods); + +-- Count the amount of rows that would be deleted for orphaned mappings +SELECT COUNT(*) AS mappings_missing_valuemap FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps); + +-- Count the amount of rows that would be deleted for orphaned media / user items +SELECT COUNT(*) AS media_missing_user FROM media WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS media_missing_type FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); +SELECT COUNT(*) AS rights_missing_user FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp); +SELECT COUNT(*) AS rights_missing_group FROM rights WHERE NOT id IN (SELECT groupid FROM groups); + +-- Count the amount of rows that would be deleted for orphaned screens +SELECT COUNT(*) AS screens_items_missing_screen FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens); + +SELECT COUNT(*) AS sessions_missing_user FROM sessions WHERE NOT userid IN (SELECT userid FROM users); + +SELECT COUNT(itemid) AS trends_missing_item FROM trends WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS trends_uint_missing_item FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned events & triggers +SELECT COUNT(*) AS trigger_down_dependency_missing_trigger FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); +SELECT COUNT(*) AS trigger_up_dependency_missing_trigger FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers); + +SELECT COUNT(*) AS user_history_missing_user FROM user_history WHERE NOT userid IN (SELECT userid FROM users); diff --git a/delete-orphaned-data.sql b/delete-orphaned-data.zbx18.sql similarity index 83% rename from delete-orphaned-data.sql rename to delete-orphaned-data.zbx18.sql index bb2253d..68c4fd0 100644 --- a/delete-orphaned-data.sql +++ b/delete-orphaned-data.zbx18.sql @@ -1,50 +1,44 @@ --- Delete all orphaned acknowledge entries DELETE FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); DELETE FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); --- Delete orphaned alerts entries DELETE FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); DELETE FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); DELETE FROM alerts WHERE NOT userid IN (SELECT userid FROM users); DELETE FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); --- Delete orphaned application entries that no longer map back to a host DELETE FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); --- Delete orphaned auditlog details (such as logins) -DELETE FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog); DELETE FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); --- Delete orphaned conditions DELETE FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); --- Delete orphaned functions DELETE FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); DELETE FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); --- Delete orphaned graph items DELETE FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); DELETE FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); --- Delete orphaed host_profiles +DELETE FROM history WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); + DELETE FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts); DELETE FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts); --- Delete orphaned host macro's DELETE FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); --- Delete orphaned item data DELETE FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); DELETE FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); DELETE FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); --- Delete orphaned HTTP check data DELETE FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); DELETE FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); DELETE FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); DELETE FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); --- Delete orphaned maintenance data DELETE FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); DELETE FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups); DELETE FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); @@ -52,31 +46,22 @@ DELETE FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts); DELETE FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); DELETE FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods); --- Delete orphaned mappings DELETE FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps); --- Delete orphaned media / user items DELETE FROM media WHERE NOT userid IN (SELECT userid FROM users); DELETE FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + DELETE FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp); DELETE FROM rights WHERE NOT id IN (SELECT groupid FROM groups); -DELETE FROM sessions WHERE NOT userid IN (SELECT userid FROM users); -DELETE FROM user_history WHERE NOT userid IN (SELECT userid FROM users); --- Screens DELETE FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens); --- Events & triggers -DELETE FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); -DELETE FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers); - --- Count the amount of records in the history/trends table for items that no longer exist -DELETE FROM history WHERE itemid NOT IN (SELECT itemid FROM items); -DELETE FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); -DELETE FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); -DELETE FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); -DELETE FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items); -DELETE FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM sessions WHERE NOT userid IN (SELECT userid FROM users); DELETE FROM trends WHERE itemid NOT IN (SELECT itemid FROM items); DELETE FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items); + +DELETE FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); +DELETE FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers); + +DELETE FROM user_history WHERE NOT userid IN (SELECT userid FROM users); diff --git a/delete-orphaned-data.zbx2x.sql b/delete-orphaned-data.zbx2x.sql new file mode 100644 index 0000000..554c2b2 --- /dev/null +++ b/delete-orphaned-data.zbx2x.sql @@ -0,0 +1,63 @@ +DELETE FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); +DELETE FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); + +DELETE FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); +DELETE FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); +DELETE FROM alerts WHERE NOT userid IN (SELECT userid FROM users); +DELETE FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + +DELETE FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); + +DELETE FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); + +DELETE FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); + +DELETE FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); +DELETE FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); + +DELETE FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); +DELETE FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); + +DELETE FROM history WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); + +DELETE FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); + +DELETE FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); +DELETE FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); +DELETE FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); + +DELETE FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); +DELETE FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); +DELETE FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); +DELETE FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); + +DELETE FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +DELETE FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups); +DELETE FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +DELETE FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts); +DELETE FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +DELETE FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods); + +DELETE FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps); + +DELETE FROM media WHERE NOT userid IN (SELECT userid FROM users); +DELETE FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + +DELETE FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp); +DELETE FROM rights WHERE NOT id IN (SELECT groupid FROM groups); + +DELETE FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens); + +DELETE FROM sessions WHERE NOT userid IN (SELECT userid FROM users); + +DELETE FROM trends WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items); + +DELETE FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); +DELETE FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers); + +DELETE FROM user_history WHERE NOT userid IN (SELECT userid FROM users); From 7b6f328486aaa51bf8f6bb1e73fcab01ecfbae3d Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 14:22:43 +0700 Subject: [PATCH 25/40] Better readme --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ec8964d..e438c9c 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ -## Zabbix SQL repo +# Zabbix SQL repo This repo contains a few useful queries for Zabbix database, mostly to cleanup old and/or orphaned data. -If you have a large database please note that these can take a while (read: a few hours is normal). +### Important notes -Use the queries above on your own risk. Take **backups** first. +If you have a large database please note that these can take a while (read: a few hours is normal). -Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.sql, respectively. Pay attention. +Use the queries on your own risk. Take **backups** first. The queries were tested against Zabbix 1.8-2.2. -#### NOTE -The queries were tested against Zabbix 1.8 and 2.0. Apply with caution to other versions. Consider reporting the results, whether succesful or not. +Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.sql, respectively. Some are also Zabbix version specific. Filenames are self-explaining. +### Usage #### Check how many orphaned rows do you have From 043f08245e99c31ac819a6d130cd996b8e981679 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 14:24:39 +0700 Subject: [PATCH 26/40] Better readme 2 --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e438c9c..b29b27e 100644 --- a/README.md +++ b/README.md @@ -14,24 +14,24 @@ Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.s #### Check how many orphaned rows do you have - user@host-$ mysql zabbix < check-orphaned-data.sql + # mysql zabbix < check-orphaned-data.sql #### Delete orphaned rows - user@host-$ mysql zabbix < delete-orphaned-data.sql + # mysql zabbix < delete-orphaned-data.sql #### Delete old data (1 week for history, 3 months for trends - edit sql at your own discretion) - user@host-$ psql -A -R ' : ' -P 'footer=off' zabbix < delete-old-data.pg.sql + # psql -A -R ' : ' -P 'footer=off' zabbix < delete-old-data.pg.sql #### Delete all history for disabled items - user@host-$ psql -A -R ' : ' -P 'footer=off' zabbix < delete-unused-data.sql + # psql -A -R ' : ' -P 'footer=off' zabbix < delete-unused-data.sql #### Stop zabbix email flood (mysql, pgsql) (Use stop-and-delete-email-alerts.sql if you're not interested in alert history) - user@host-$ sudo service zabbix-server stop - user@host-$ psql zabbix < stop-email-alerts.sql - user@host-$ sudo service zabbix-server start + # sudo service zabbix-server stop + # psql zabbix < stop-email-alerts.sql + # sudo service zabbix-server start From 343563c984c17aff9c0426ad5319b8fd0b8d307a Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 15:18:45 +0700 Subject: [PATCH 27/40] add toc --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b29b27e..5d90b4a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup old and/or orphaned data. +- [Important notes] (#important-notes) +- [Usage] (#usage) + * Check orphaned rows (#check-orphaned-rows) + * Delete orphaned rows (#delete-orphaned-rows) + ### Important notes If you have a large database please note that these can take a while (read: a few hours is normal). @@ -12,7 +17,7 @@ Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.s ### Usage -#### Check how many orphaned rows do you have +#### Check orphaned rows # mysql zabbix < check-orphaned-data.sql From 243ca8eacc33bade1ce43603f8a0c7180793c921 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 15:20:13 +0700 Subject: [PATCH 28/40] add toc --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5d90b4a..41c4c99 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,8 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o - [Important notes] (#important-notes) - [Usage] (#usage) - * Check orphaned rows (#check-orphaned-rows) - * Delete orphaned rows (#delete-orphaned-rows) - + * [Check orphaned rows] (#check-orphaned-rows) + * [Delete orphaned rows] (#delete-orphaned-rows) ### Important notes If you have a large database please note that these can take a while (read: a few hours is normal). From 5e2c89a8fa288093d3d5bd96feb6bb0ac55b8cc5 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 15:38:56 +0700 Subject: [PATCH 29/40] better format for readme --- README.md | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 41c4c99..d452387 100644 --- a/README.md +++ b/README.md @@ -4,38 +4,54 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o - [Important notes] (#important-notes) - [Usage] (#usage) - * [Check orphaned rows] (#check-orphaned-rows) - * [Delete orphaned rows] (#delete-orphaned-rows) + * [Orphaned data] (#orphaned-data) + * [Old data] (#check-old-data) + * [Usused data] (#unused-data) + * [Stop email flood] (#stop-email-flood) + ### Important notes -If you have a large database please note that these can take a while (read: a few hours is normal). +* If you have a large database please note that these can take a while (a few hours is normal). +* Use the queries on your own risk. Take **backups** first. The queries were (mostly) tested against Zabbix 1.8-2.2. +* Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.sql, respectively. Some are also Zabbix version specific. Filenames are self-explaining. +* Patches are welcome. -Use the queries on your own risk. Take **backups** first. The queries were tested against Zabbix 1.8-2.2. +### Usage -Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.sql, respectively. Some are also Zabbix version specific. Filenames are self-explaining. +#### Orphaned data -### Usage +Orphaned data is history belonging to deleted hosts and similar. + + mysql zabbix < check-orphaned-data.sql + psql -A -R ' : ' -P 'footer=off' zabbix < check-orphaned-data.zbx2x.sql + + mysql zabbix < delete-orphaned-data.sql + psql -A -R ' : ' -P 'footer=off' zabbix < delete-orphaned-data.zbx2x.sql -#### Check orphaned rows +#### Old data - # mysql zabbix < check-orphaned-data.sql +This set of queries allows you to delete all data older than a specified period. Default is 1 week for history, 3 months for trends - edit sql at your own discretion. -#### Delete orphaned rows + mysql zabbix < check-old-data.sql + psql -A -R ' : ' -P 'footer=off' zabbix < check-old-data.pg.sql - # mysql zabbix < delete-orphaned-data.sql + mysql zabbix < delete-old-data.sql + psql -A -R ' : ' -P 'footer=off' zabbix < delete-old-data.pg.sql -#### Delete old data (1 week for history, 3 months for trends - edit sql at your own discretion) +#### Unused data - # psql -A -R ' : ' -P 'footer=off' zabbix < delete-old-data.pg.sql +This deletes all history for disabled items. May come in handy when you disable a significant number of items and no longer need the collected data. -#### Delete all history for disabled items + mysql zabbix < check-unused-data.sql + psql -A -R ' : ' -P 'footer=off' zabbix < check-unused-data.sql - # psql -A -R ' : ' -P 'footer=off' zabbix < delete-unused-data.sql + mysql zabbix < delete-unused-data.sql + psql -A -R ' : ' -P 'footer=off' zabbix < delete-unused-data.sql -#### Stop zabbix email flood (mysql, pgsql) +#### Stop email flood (Use stop-and-delete-email-alerts.sql if you're not interested in alert history) - # sudo service zabbix-server stop - # psql zabbix < stop-email-alerts.sql - # sudo service zabbix-server start + sudo service zabbix-server stop + psql zabbix < stop-email-alerts.sql + sudo service zabbix-server start From 43d8a36af66644406233150b36f7b24c5d59b518 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 15:40:08 +0700 Subject: [PATCH 30/40] fix typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d452387..c35198a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o - [Important notes] (#important-notes) - [Usage] (#usage) * [Orphaned data] (#orphaned-data) - * [Old data] (#check-old-data) + * [Old data] (#old-data) * [Usused data] (#unused-data) * [Stop email flood] (#stop-email-flood) @@ -20,7 +20,7 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o #### Orphaned data -Orphaned data is history belonging to deleted hosts and similar. +Orphaned data is history which belongs to deleted hosts and similar. mysql zabbix < check-orphaned-data.sql psql -A -R ' : ' -P 'footer=off' zabbix < check-orphaned-data.zbx2x.sql From 0f818b6a36c188607dc173c138ea4fce493d23b2 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 16:28:38 +0700 Subject: [PATCH 31/40] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c35198a..6cf91ef 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o - [Usage] (#usage) * [Orphaned data] (#orphaned-data) * [Old data] (#old-data) - * [Usused data] (#unused-data) + * [Unused data] (#unused-data) * [Stop email flood] (#stop-email-flood) ### Important notes From 66d6cd6f44286ec34499fc6c8f340888f726ff0e Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Thu, 9 Apr 2015 16:51:23 +0700 Subject: [PATCH 32/40] one more typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6cf91ef..521d174 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,10 @@ Orphaned data is history which belongs to deleted hosts and similar. This set of queries allows you to delete all data older than a specified period. Default is 1 week for history, 3 months for trends - edit sql at your own discretion. - mysql zabbix < check-old-data.sql + mysql zabbix < check-old-data.my.sql psql -A -R ' : ' -P 'footer=off' zabbix < check-old-data.pg.sql - mysql zabbix < delete-old-data.sql + mysql zabbix < delete-old-data.my.sql psql -A -R ' : ' -P 'footer=off' zabbix < delete-old-data.pg.sql #### Unused data From 4cc1dabef7ea4eae3b3cccb5b0e130c15d504c70 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 3 Aug 2015 14:55:05 +0000 Subject: [PATCH 33/40] add LLD triggers removal --- README.md | 10 ++++++++++ check-lld-triggers.my.sql | 13 +++++++++++++ check-lld-triggers.pg.sql | 3 +++ delete-lld-triggers.my.sql | 4 ++++ delete-lld-triggers.pg.sql | 3 +++ 5 files changed, 33 insertions(+) create mode 100644 check-lld-triggers.my.sql create mode 100644 check-lld-triggers.pg.sql create mode 100644 delete-lld-triggers.my.sql create mode 100644 delete-lld-triggers.pg.sql diff --git a/README.md b/README.md index 521d174..2981078 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o * [Old data] (#old-data) * [Unused data] (#unused-data) * [Stop email flood] (#stop-email-flood) + * [LLD triggers] (#lld-triggers) ### Important notes @@ -55,3 +56,12 @@ This deletes all history for disabled items. May come in handy when you disable sudo service zabbix-server stop psql zabbix < stop-email-alerts.sql sudo service zabbix-server start + +#### LLD triggers + +Zabbix will create automatically create "Mounted Filesystem Discovery" triggers, which you can't delete or remove from the web interface. These queries will allow you to delete them all at once (disk space and inodes). + mysql zabbix < check-lld-triggers.my.sql + psql -A -R ' : ' -P 'footer=off' zabbix < check-lld-triggers.pg.sql + + mysql zabbix < delete-lld-triggers.my.sql + psql -A -R ' : ' -P 'footer=off' zabbix < delete-lld-triggers.pg.sql diff --git a/check-lld-triggers.my.sql b/check-lld-triggers.my.sql new file mode 100644 index 0000000..ed49106 --- /dev/null +++ b/check-lld-triggers.my.sql @@ -0,0 +1,13 @@ +SELECT count(triggerid) +AS diskspace +FROM triggers +WHERE description LIKE 'Free disk space is less than 20|%%' +ESCAPE '|' +AND NOT description LIKE '%{#FSNAME}%'; + +SELECT count(triggerid) +AS inodes +FROM triggers +WHERE description LIKE 'Free disk space is less than 20|%%' +ESCAPE '|' +AND NOT description LIKE '%{#FSNAME}%'; diff --git a/check-lld-triggers.pg.sql b/check-lld-triggers.pg.sql new file mode 100644 index 0000000..cd4ab56 --- /dev/null +++ b/check-lld-triggers.pg.sql @@ -0,0 +1,3 @@ +SELECT count(triggerid) +FROM triggers +WHERE description ~* 'disk space.*less.*20%' AND NOT description ~* '{#'; diff --git a/delete-lld-triggers.my.sql b/delete-lld-triggers.my.sql new file mode 100644 index 0000000..d2f70e4 --- /dev/null +++ b/delete-lld-triggers.my.sql @@ -0,0 +1,4 @@ +DELETE FROM triggers +WHERE description LIKE 'Free disk space is less than 20|%%' +ESCAPE '|' +AND NOT description LIKE '%{#FSNAME}%'; diff --git a/delete-lld-triggers.pg.sql b/delete-lld-triggers.pg.sql new file mode 100644 index 0000000..7b37ebe --- /dev/null +++ b/delete-lld-triggers.pg.sql @@ -0,0 +1,3 @@ +DELETE +FROM triggers +WHERE description ~* 'disk space.*less.*20%' AND NOT description ~* '{#'; From f05237094d962b3dc7a4f9ba2ddfc5efbd9d8451 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 3 Aug 2015 14:56:16 +0000 Subject: [PATCH 34/40] add reference to the original --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2981078..dbc3bb1 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ This deletes all history for disabled items. May come in handy when you disable #### LLD triggers -Zabbix will create automatically create "Mounted Filesystem Discovery" triggers, which you can't delete or remove from the web interface. These queries will allow you to delete them all at once (disk space and inodes). +Zabbix will create automatically create "Mounted Filesystem Discovery" triggers, which you can't delete or remove from the web interface. These queries will allow you to delete them all at once (disk space and inodes). [Inspired by.](#https://www.zabbix.com/forum/showthread.php?t=44028) + mysql zabbix < check-lld-triggers.my.sql psql -A -R ' : ' -P 'footer=off' zabbix < check-lld-triggers.pg.sql From f9d38ae4eff1d18a915d303e5581dbd8add40f82 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 3 Aug 2015 14:57:27 +0000 Subject: [PATCH 35/40] fix wording --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dbc3bb1..2b0fdaa 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ This deletes all history for disabled items. May come in handy when you disable #### LLD triggers -Zabbix will create automatically create "Mounted Filesystem Discovery" triggers, which you can't delete or remove from the web interface. These queries will allow you to delete them all at once (disk space and inodes). [Inspired by.](#https://www.zabbix.com/forum/showthread.php?t=44028) +Zabbix will create automatically create "Mounted Filesystem Discovery" triggers, which you can't disable or delete from the web interface. These queries will allow you to delete them all at once (disk space and inodes). [Inspired by.](#https://www.zabbix.com/forum/showthread.php?t=44028) mysql zabbix < check-lld-triggers.my.sql psql -A -R ' : ' -P 'footer=off' zabbix < check-lld-triggers.pg.sql From 01f828aab824e44402747840277b4f0baec0d8d7 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Sun, 13 Dec 2015 22:28:56 +0700 Subject: [PATCH 36/40] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b0fdaa..c3a923e 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ This deletes all history for disabled items. May come in handy when you disable #### LLD triggers -Zabbix will create automatically create "Mounted Filesystem Discovery" triggers, which you can't disable or delete from the web interface. These queries will allow you to delete them all at once (disk space and inodes). [Inspired by.](#https://www.zabbix.com/forum/showthread.php?t=44028) +Zabbix will create automatically create "Mounted Filesystem Discovery" triggers, which you can't disable or delete from the web interface. These queries will allow you to delete them all at once (disk space and inodes). [Inspired by](https://www.zabbix.com/forum/showthread.php?t=44028). mysql zabbix < check-lld-triggers.my.sql psql -A -R ' : ' -P 'footer=off' zabbix < check-lld-triggers.pg.sql From b4402264705d7660bc6ba8c39ddcc25f5f5f7e51 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Mon, 30 May 2016 21:00:25 +0700 Subject: [PATCH 37/40] tested some queries against 3.0, reflect in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 521d174..8411709 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup o ### Important notes * If you have a large database please note that these can take a while (a few hours is normal). -* Use the queries on your own risk. Take **backups** first. The queries were (mostly) tested against Zabbix 1.8-2.2. +* Use the queries on your own risk. Take **backups** first. The queries were (mostly) tested against Zabbix 1.8-3.0. * Some scripts are Mysql or Postgresql specific, they're named *.my.sql and *.pg.sql, respectively. Some are also Zabbix version specific. Filenames are self-explaining. * Patches are welcome. From afcdb2972470c3344867825234bfef0ec60e21d4 Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Tue, 11 Apr 2017 21:57:36 +0900 Subject: [PATCH 38/40] fix links --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 386cc3c..e92891a 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ This repo contains a few useful queries for Zabbix database, mostly to cleanup old and/or orphaned data. -- [Important notes] (#important-notes) -- [Usage] (#usage) - * [Orphaned data] (#orphaned-data) - * [Old data] (#old-data) - * [Unused data] (#unused-data) - * [Stop email flood] (#stop-email-flood) - * [LLD triggers] (#lld-triggers) +- [Important notes](#important-notes) +- [Usage](#usage) + * [Orphaned data](#orphaned-data) + * [Old data](#old-data) + * [Unused data](#unused-data) + * [Stop email flood](#stop-email-flood) + * [LLD triggers](#lld-triggers) ### Important notes From c62565d22402fef2de60f79432db3a19cb3c4a3e Mon Sep 17 00:00:00 2001 From: Ilya Ivanov Date: Tue, 18 Apr 2017 21:49:37 +0700 Subject: [PATCH 39/40] add note about bloating --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e92891a..6a9bf12 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ This set of queries allows you to delete all data older than a specified period. mysql zabbix < delete-old-data.my.sql psql -A -R ' : ' -P 'footer=off' zabbix < delete-old-data.pg.sql +**NOTE**: due to the way the databases work (Mysql in particular), running these scripts won't reduce Zabbix db size if it's already bloated. You will have to dump and reload the db after that. What the scripts do is keep its size more or less constant if you run them regularly. + #### Unused data This deletes all history for disabled items. May come in handy when you disable a significant number of items and no longer need the collected data. From e1b98b5271f58af21154b07671a42c4a3c8c9a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Murtaza=20Sar=C4=B1altun?= Date: Thu, 7 Jun 2018 01:15:50 +0300 Subject: [PATCH 40/40] add orphaned data delete and check for zabbix v3 --- check-orphaned-data.zbx3.sql | 77 +++++++++++++++++++++++++++++++++++ delete-orphaned-data.zbx3.sql | 61 +++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 check-orphaned-data.zbx3.sql create mode 100644 delete-orphaned-data.zbx3.sql diff --git a/check-orphaned-data.zbx3.sql b/check-orphaned-data.zbx3.sql new file mode 100644 index 0000000..5f89465 --- /dev/null +++ b/check-orphaned-data.zbx3.sql @@ -0,0 +1,77 @@ +-- Count the amount of rows that would be deleted for all orphaned acknowledge entries +SELECT COUNT(*) AS acknowedges_missing_user FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS acknowedges_missing_event FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); + +-- Count the amount of rows that would be deleted for orphaned alerts entries +SELECT COUNT(*) AS alerts_missing_action FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); +SELECT COUNT(*) AS alerts_missing_event FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); +SELECT COUNT(*) AS alerts_missing_user FROM alerts WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS alerts_missing_mediatype FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + +-- Count the amount of rows that would be deleted for orphaned application entries that no longer map back to a host +SELECT COUNT(*) AS applications_missing_host FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); + +-- Count the amount of rows that would be deleted for orphaned auditlog details (such as logins) +-- SELECT COUNT(*) AS auditlog_missing_details FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog); -- This takes exponential time, disabled +SELECT COUNT(*) AS auditlog_missing_user FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); + +-- Count the amount of rows that would be deleted for orphaned conditions +SELECT COUNT(*) AS conditions_missing_acction FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); + +-- Count the amount of rows that would be deleted for orphaned functions +SELECT COUNT(*) AS functions_missing_item FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS functions_missing_trigger FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); + +-- Count the amount of rows that would be deleted for orphaned graph items +SELECT COUNT(*) AS graph_items_missing_graphs FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); +SELECT COUNT(*) AS graph_items_missing_items FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); + +-- Count the amount of records in the history/trends table for items that no longer exist +SELECT COUNT(itemid) AS history_missing_item FROM history WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_uint_missing_item FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_log_missing_item FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_str_missing_item FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS history_text_missing_item FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned host macro's +SELECT COUNT(*) AS host_macros_missing_hosts FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); + +-- Count the amount of rows that would be deleted for orphaned item data +SELECT COUNT(itemid) AS items_missing_hosts FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS item_apps_missing_apps FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); +SELECT COUNT(*) AS item_apps_missing_items FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned HTTP check data +SELECT COUNT(*) AS http_step_missing_tests FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); +SELECT COUNT(*) AS http_step_items_missing_steps FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); +SELECT COUNT(*) AS http_step_items_missing_items FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); +SELECT COUNT(*) AS http_test_missing_apps FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); + +-- Count the amount of rows that would be deleted for orphaned maintenance data +SELECT COUNT(*) AS maintenance_group_missing_maintenances FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenance_group_missing_groups FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups); +SELECT COUNT(*) AS maintenances_hosts_missing_maintenances FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenances_hosts_missing_hosts FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts); +SELECT COUNT(*) AS maintenances_windows_missing_maintenances FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +SELECT COUNT(*) AS maintenances_windows_missing_windows FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods); + +-- Count the amount of rows that would be deleted for orphaned mappings +SELECT COUNT(*) AS mappings_missing_valuemap FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps); + +-- Count the amount of rows that would be deleted for orphaned media / user items +SELECT COUNT(*) AS media_missing_user FROM media WHERE NOT userid IN (SELECT userid FROM users); +SELECT COUNT(*) AS media_missing_type FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); +SELECT COUNT(*) AS rights_missing_user FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp); +SELECT COUNT(*) AS rights_missing_group FROM rights WHERE NOT id IN (SELECT groupid FROM groups); + +-- Count the amount of rows that would be deleted for orphaned screens +SELECT COUNT(*) AS screens_items_missing_screen FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens); + +SELECT COUNT(*) AS sessions_missing_user FROM sessions WHERE NOT userid IN (SELECT userid FROM users); + +SELECT COUNT(itemid) AS trends_missing_item FROM trends WHERE itemid NOT IN (SELECT itemid FROM items); +SELECT COUNT(itemid) AS trends_uint_missing_item FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items); + +-- Count the amount of rows that would be deleted for orphaned events & triggers +SELECT COUNT(*) AS trigger_down_dependency_missing_trigger FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); +SELECT COUNT(*) AS trigger_up_dependency_missing_trigger FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers); diff --git a/delete-orphaned-data.zbx3.sql b/delete-orphaned-data.zbx3.sql new file mode 100644 index 0000000..630579b --- /dev/null +++ b/delete-orphaned-data.zbx3.sql @@ -0,0 +1,61 @@ +DELETE FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users); +DELETE FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events); + +DELETE FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions); +DELETE FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events); +DELETE FROM alerts WHERE NOT userid IN (SELECT userid FROM users); +DELETE FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + +DELETE FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts); + +DELETE FROM auditlog WHERE NOT userid IN (SELECT userid FROM users); + +DELETE FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions); + +DELETE FROM functions WHERE NOT itemid IN (SELECT itemid FROM items); +DELETE FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers); + +DELETE FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs); +DELETE FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items); + +DELETE FROM history WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items); + +DELETE FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts); + +DELETE FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts); +DELETE FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications); +DELETE FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items); + +DELETE FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest); +DELETE FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep); +DELETE FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items); +DELETE FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications); + +DELETE FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +DELETE FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups); +DELETE FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +DELETE FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts); +DELETE FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances); +DELETE FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods); + +DELETE FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps); + +DELETE FROM media WHERE NOT userid IN (SELECT userid FROM users); +DELETE FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type); + +DELETE FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp); +DELETE FROM rights WHERE NOT id IN (SELECT groupid FROM groups); + +DELETE FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens); + +DELETE FROM sessions WHERE NOT userid IN (SELECT userid FROM users); + +DELETE FROM trends WHERE itemid NOT IN (SELECT itemid FROM items); +DELETE FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items); + +DELETE FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers); +DELETE FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers);