forked from mattiasgeniar/zabbix-orphaned-data-cleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.sh
executable file
·225 lines (157 loc) · 8.64 KB
/
cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
export CONF ORPHANED_ITEMIDs=() FILES=() CPU_COUNT
CPU_COUNT=$(nproc)
# Let's free some space for other threads on machine
((CPU_COUNT > 1)) && CPU_COUNT=$((CPU_COUNT-1))
CONF="$(mktemp)"
FILES+=("$CONF")
cleanup_conf(){ rm -f "${FILES[@]}"; stty echo; exit;}
trap cleanup_conf SIGINT SIGTERM EXIT
if [ ! -z "$1" ] && [ -f "$1" ]; then
echo "Use cnf file: $1"
for val in user password database; do
grep -q "$val" "$1" || {
echo "Missing $val field in $1"
exit 1
}
done
cp "$1" "$CONF"
else
echo -n "MySQL username: " ; read -r username
echo -n "MySQL password: " ; stty -echo ; read -r password ; stty echo ; echo
echo -n "MySQL database: " ; read -r database
{
echo [client]
echo user = "$username"
echo password = "$password"
echo database = "$database"
} > "$CONF"
fi
mysql_w(){
[ ! -f "$CONF" ] && exit 1
mysql --defaults-file="$CONF" -NBe "${*} SELECT ROW_COUNT();"
}
echo "Delete orphaned alerts entries"
echo -n "Table: alerts orphaned actions: "
mysql_w "DELETE FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions);"
echo -n "Table: alerts orphaned events: "
mysql_w "DELETE FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events);"
echo -n "Table: alerts orphaned users: "
mysql_w "DELETE FROM alerts WHERE NOT userid IN (SELECT userid FROM users);"
echo -n "Table: alerts orphaned media_types: "
mysql_w "DELETE FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);"
echo "Delete orphaned application entries that no longer map back to a host"
echo -n "Table: applications orphaned hosts: "
mysql_w "DELETE FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts);"
echo "Delete orphaned auditlog details (such as logins)"
echo -n "Table: auditlog_details orphaned audits: "
mysql_w "DELETE FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog);"
echo -n "Table: auditlog orphaned users: "
mysql_w "DELETE FROM auditlog WHERE NOT userid IN (SELECT userid FROM users);"
echo "Delete orphaned conditions"
echo -n "Table: conditions orphaned actions: "
mysql_w "DELETE FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions);"
echo "Delete orphaned functions"
echo -n "Table: functions orphaned items: "
mysql_w "DELETE FROM functions WHERE NOT itemid IN (SELECT itemid FROM items);"
echo -n "Table: functions orphaned triggers: "
mysql_w "DELETE FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers);"
echo "Delete orphaned graph items"
echo -n "Table: graphs_items orphaned graphs: "
mysql_w "DELETE FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs);"
echo -n "Table: graphs_items orphaned items: "
mysql_w "DELETE FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items);"
echo "Delete orphaned host macro's"
echo -n "Table: hostmacro orphaned hosts: "
mysql_w "DELETE FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts);"
echo "Delete orphaned item data"
echo -n "Table: items orphaned hosts: "
mysql_w "DELETE FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts);"
echo -n "Table: items_applications orphaned applications: "
mysql_w "DELETE FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications);"
echo -n "Table: items_applications orphaned items: "
mysql_w "DELETE FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items);"
echo "Delete orphaned HTTP check data"
echo -n "Table: httpstep orphaned httptests: "
mysql_w "DELETE FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest);"
echo -n "Table: httpstepitem orphaned httpsteps: "
mysql_w "DELETE FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep);"
echo -n "Table: httpstepitem orphaned items: "
mysql_w "DELETE FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items);"
echo -n "Table: httptest orphaned applications: "
mysql_w "DELETE FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications);"
echo "Delete orphaned maintenance data"
echo -n "Table: maintenances_groups orphaned maintenances: "
mysql_w "DELETE FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);"
echo -n "Table: maintenances_groups orphaned groups: "
mysql_w "DELETE FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups);"
echo -n "Table: maintenances_hosts orphaned maintenances: "
mysql_w "DELETE FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);"
echo -n "Table: maintenances_hosts orphaned hosts: "
mysql_w "DELETE FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts);"
echo -n "Table: maintenances_windows orphaned maintenances: "
mysql_w "DELETE FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);"
echo -n "Table: maintenances_windows orphaned timeperiods: "
mysql_w "DELETE FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods);"
echo "Delete orphaned mappings"
echo -n "Table: mappings orphaned valuemaps: "
mysql_w "DELETE FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps);"
echo "Delete orphaned media items"
echo -n "Table: media orphaned users: "
mysql_w "DELETE FROM media WHERE NOT userid IN (SELECT userid FROM users);"
echo -n "Table: media orphaned media_types: "
mysql_w "DELETE FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);"
echo -n "Table: rights orphaned user groups: "
mysql_w "DELETE FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp);"
echo -n "Table: rights orphaned groups: "
mysql_w "DELETE FROM rights WHERE NOT id IN (SELECT groupid FROM groups);"
echo -n "Table: sessions orphaned users: "
mysql_w "DELETE FROM sessions WHERE NOT userid IN (SELECT userid FROM users);"
echo "Cleanup screens"
echo -n "Table: screens_items orphaned screens: "
mysql_w "DELETE FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens);"
echo "Cleanup Events & triggers"
echo -n "Table: trigger_depends orphaned triggers(triggerid_down): "
mysql_w "DELETE FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers);"
echo -n "Table: trigger_depends orphaned triggers(triggerid_up): "
mysql_w "DELETE FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers);"
mysql_w_l(){
[ ! -f "$CONF" ] && exit 1
mysql --defaults-file="$CONF" -NBe "${*}"
}
# xargs can only run external script/binary
TMP_SCRIPT=$(mktemp)
chmod +x "$TMP_SCRIPT"
FILES+=("$TMP_SCRIPT")
echo "Delete records in the history/trends table where items that no longer exist"
TABLES=(history history_uint history_log history_str history_text trends trends_uint)
for table in "${TABLES[@]}"; do
ORPHANED_ITEMIDs=(
$(mysql_w_l "SELECT DISTINCT(itemid) FROM $table WHERE itemid NOT IN (SELECT itemid FROM items);")
)
echo "Table: $table orphaned items: ${#ORPHANED_ITEMIDs[@]}"
{
echo "#!/bin/bash"
echo mysql --defaults-file="$CONF" -NBe \"DELETE FROM $table WHERE itemid = \$1\;\"
} > "$TMP_SCRIPT"
# Use xargs for parallel deletion
if ((${#ORPHANED_ITEMIDs[@]} > 0)); then
echo "${ORPHANED_ITEMIDs[@]}" | xargs -n 1 -P $CPU_COUNT $TMP_SCRIPT
fi
done
echo "Delete records in the events table where triggers/items no longer exist"
echo -n "Table: events orphaned triggers(src=0, obj=0): "
mysql_w "DELETE FROM events WHERE source = 0 AND object = 0 AND objectid NOT IN (SELECT triggerid FROM triggers);"
echo -n "Table: events orphaned triggers(src=3, obj=0): "
mysql_w "DELETE FROM events WHERE source = 3 AND object = 0 AND objectid NOT IN (SELECT triggerid FROM triggers);"
echo -n "Table: events orphaned items(src=3, obj=4): "
mysql_w "DELETE FROM events WHERE source = 3 AND object = 4 AND objectid NOT IN (SELECT itemid FROM items);"
echo "Delete all orphaned acknowledge entries"
echo -n "Table: acknowledges orphaned events: "
mysql_w "DELETE FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events);"
echo -n "Table: acknowledges orphaned users: "
mysql_w "DELETE FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users);"
echo -n "Table: acknowledges orphaned events(src=0|3): "
mysql_w "DELETE FROM acknowledges WHERE eventid IN (SELECT eventid FROM events WHERE (source = 0 OR source=3) AND object = 0 AND objectid NOT IN (SELECT triggerid FROM triggers));"
echo -n "Table: acknowledges orphaned events(src=3, obj=4): "
mysql_w "DELETE FROM acknowledges WHERE eventid IN (SELECT eventid FROM events WHERE source=3 AND object = 4 AND objectid NOT IN (SELECT itemid FROM items));"