summaryrefslogtreecommitdiffstats
path: root/src/zabbix_sucker/housekeeper.c
blob: 9d29fa1af417dd7ae3fef67c30b6c420a4e5b569 (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* 
** Zabbix
** Copyright (C) 2000,2001,2002,2003,2004 Alexei Vladishev
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <sys/wait.h>

#include <string.h>

#ifdef HAVE_NETDB_H
	#include <netdb.h>
#endif

/* Required for getpwuid */
#include <pwd.h>

#include <signal.h>
#include <errno.h>

#include <time.h>

#include "common.h"
#include "cfg.h"
#include "db.h"
#include "log.h"

#include "housekeeper.h"

/* Remove items having status 'deleted' */
#ifdef ZABBIX_THREADS
void *housekeeping_items(MYSQL *database)
#else
int housekeeping_items(void)
#endif
{
	char		sql[MAX_STRING_LEN];
	DB_RESULT	*result;
	int		i,itemid;

	snprintf(sql,sizeof(sql)-1,"select itemid from items where status=%d", ITEM_STATUS_DELETED);
#ifdef ZABBIX_THREADS
	result = DBselect_thread(database, sql);
#else
	result = DBselect(sql);
#endif
	for(i=0;i<DBnum_rows(result);i++)
	{
		itemid=atoi(DBget_field(result,i,0));
#ifdef ZABBIX_THREADS
		DBdelete_item_thread(database, itemid);
#else
		DBdelete_item(itemid);
#endif
	}
	DBfree_result(result);
	return SUCCEED;
}

/* Remove hosts having status 'deleted' */
#ifdef ZABBIX_THREADS
int housekeeping_hosts(MYSQL *database)
#else
int housekeeping_hosts(void)
#endif
{
	char		sql[MAX_STRING_LEN];
	DB_RESULT	*result;
	int		i,hostid;

	snprintf(sql,sizeof(sql)-1,"select hostid from hosts where status=%d", HOST_STATUS_DELETED);
#ifdef ZABBIX_THREADS
	result = DBselect_thread(database, sql);
#else
	result = DBselect(sql);
#endif
	for(i=0;i<DBnum_rows(result);i++)
	{
		hostid=atoi(DBget_field(result,i,0));
#ifdef ZABBIX_THREADS
		DBdelete_host_thread(database, hostid);
#else
		DBdelete_host(hostid);
#endif
	}
	DBfree_result(result);
	return SUCCEED;
}

#ifdef ZABBIX_THREADS
int housekeeping_history_and_trends(MYSQL *database, int now)
#else
int housekeeping_history_and_trends(int now)
#endif
{
	char		sql[MAX_STRING_LEN];
	DB_ITEM		item;

	DB_RESULT	*result;

	int		i;

/* How lastdelete is used ??? */
	snprintf(sql,sizeof(sql)-1,"select itemid,lastdelete,history,delay,trends from items where lastdelete<=%d", now);
#ifdef ZABBIX_THREADS
	result = DBselect_thread(database, sql);
#else
	result = DBselect(sql);
#endif

	for(i=0;i<DBnum_rows(result);i++)
	{
		item.itemid=atoi(DBget_field(result,i,0));
		item.lastdelete=atoi(DBget_field(result,i,1));
		item.history=atoi(DBget_field(result,i,2));
		item.delay=atoi(DBget_field(result,i,3));
		item.trends=atoi(DBget_field(result,i,4));

		if(item.delay==0)
		{
			item.delay=1;
		}

/* Delete HISTORY */
#ifdef HAVE_MYSQL
		snprintf(sql,sizeof(sql)-1,"delete from history where itemid=%d and clock<%d limit %d",item.itemid,now-24*3600*item.history,2*CONFIG_HOUSEKEEPING_FREQUENCY*3600/item.delay);
#else
		snprintf(sql,sizeof(sql)-1,"delete from history where itemid=%d and clock<%d",item.itemid,now-24*3600*item.history);
#endif
#ifdef ZABBIX_THREADS
		DBexecute_thread(database, sql);
#else
		DBexecute(sql);
#endif

/* Delete HISTORY_STR */
#ifdef HAVE_MYSQL
		snprintf(sql,sizeof(sql)-1,"delete from history_str where itemid=%d and clock<%d limit %d",item.itemid,now-24*3600*item.history,2*CONFIG_HOUSEKEEPING_FREQUENCY*3600/item.delay);
#else
		snprintf(sql,sizeof(sql)-1,"delete from history_str where itemid=%d and clock<%d",item.itemid,now-24*3600*item.history);
#endif
#ifdef ZABBIX_THREADS
		DBexecute_thread(database, sql);
#else
		DBexecute(sql);
#endif

/* Delete HISTORY_TRENDS */
#ifdef HAVE_MYSQL
		snprintf(sql,sizeof(sql)-1,"delete from trends where itemid=%d and clock<%d limit %d",item.itemid,now-24*3600*item.trends,2*CONFIG_HOUSEKEEPING_FREQUENCY*3600/item.delay);
#else
		snprintf(sql,sizeof(sql)-1,"delete from trends where itemid=%d and clock<%d",item.itemid,now-24*3600*item.trends);
#endif
#ifdef ZABBIX_THREADS
		DBexecute_thread(database,sql);
#else
		DBexecute(sql);
#endif
		snprintf(sql,sizeof(sql)-1,"update items set lastdelete=%d where itemid=%d",now,item.itemid);
#ifdef ZABBIX_THREADS
		DBexecute_thread(database,sql);
#else
		DBexecute(sql);
#endif
	}
	DBfree_result(result);
	return SUCCEED;
}

#ifdef ZABBIX_THREADS
int housekeeping_sessions(MYSQL *database, int now)
#else
int housekeeping_sessions(int now)
#endif
{
	char	sql[MAX_STRING_LEN];

	snprintf(sql,sizeof(sql)-1,"delete from sessions where lastaccess<%d",now-24*3600);
#ifdef ZABBIX_THREADS
	DBexecute_thread(database,sql);
#else
	DBexecute(sql);
#endif

	return SUCCEED;
}

#ifdef ZABBIX_THREADS
int housekeeping_alerts(MYSQL *database, int now)
#else
int housekeeping_alerts(int now)
#endif
{
	char		sql[MAX_STRING_LEN];
	int		alert_history;
	DB_RESULT	*result;
	int		res = SUCCEED;

	snprintf(sql,sizeof(sql)-1,"select alert_history from config");
#ifdef ZABBIX_THREADS
	result = DBselect_thread(database, sql);
#else
	result = DBselect(sql);
#endif

	if(DBnum_rows(result) == 0)
	{
		zabbix_log( LOG_LEVEL_ERR, "No records in table 'config'.");
		res = FAIL;
	}
	else
	{
		alert_history=atoi(DBget_field(result,0,0));

		snprintf(sql,sizeof(sql)-1,"delete from alerts where clock<%d",now-24*3600*alert_history);
#ifdef ZABBIX_THREADS
		DBexecute_thread(database,sql);
#else
		DBexecute(sql);
#endif
	}

	DBfree_result(result);
	return res;
}

#ifdef ZABBIX_THREADS
int housekeeping_alarms(MYSQL *database, int now)
#else
int housekeeping_alarms(int now)
#endif
{
	char		sql[MAX_STRING_LEN];
	int		alarm_history;
	DB_RESULT	*result;
	int		res = SUCCEED;

	snprintf(sql,sizeof(sql)-1,"select alarm_history from config");
#ifdef ZABBIX_THREADS
	result = DBselect_thread(database, sql);
#else
	result = DBselect(sql);
#endif
	if(DBnum_rows(result) == 0)
	{
		zabbix_log( LOG_LEVEL_ERR, "No records in table 'config'.");
		res = FAIL;
	}
	else
	{
		alarm_history=atoi(DBget_field(result,0,0));

		snprintf(sql,sizeof(sql)-1,"delete from alarms where clock<%d",now-24*3600*alarm_history);
#ifdef ZABBIX_THREADS
		DBexecute_thread(database,sql);
#else
		DBexecute(sql);
#endif
	}
	
	DBfree_result(result);
	return res;
}

#ifdef ZABBIX_THREADS
void *main_housekeeper_loop()
#else
int main_housekeeper_loop()
#endif
{
	int	now;

#ifdef ZABBIX_THREADS
	DB_HANDLE	database;
#endif

#ifdef ZABBIX_THREADS
	my_thread_init();
#endif

	if(CONFIG_DISABLE_HOUSEKEEPING == 1)
	{
		for(;;)
		{
/* Do nothing */
#ifdef HAVE_FUNCTION_SETPROCTITLE
			setproctitle("do nothing");
#endif
			sleep(3600);
		}
	}

	for(;;)
	{
		now = time(NULL);
#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("connecting to the database");
#endif
#ifdef ZABBIX_THREADS
		DBconnect_thread(&database);
#else
		DBconnect();
#endif

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [removing deleted hosts]");
#endif
#ifdef ZABBIX_THREADS
		housekeeping_hosts(&database);
#else
		housekeeping_hosts();
#endif

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [removing deleted items]");
#endif

#ifdef ZABBIX_THREADS
		housekeeping_items(&database);
#else
		housekeeping_items();
#endif

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [removing old values]");
#endif

#ifdef ZABBIX_THREADS
		housekeeping_history_and_trends(&database, now);
#else
		housekeeping_history_and_trends(now);
#endif

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [removing old alarms]");
#endif

#ifdef ZABBIX_THREADS
		housekeeping_alarms(&database, now);
#else
		housekeeping_alarms(now);
#endif

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [removing old alerts]");
#endif

#ifdef ZABBIX_THREADS
		housekeeping_alerts(&database, now);
#else
		housekeeping_alerts(now);
#endif

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [removing old sessions]");
#endif

#ifdef ZABBIX_THREADS
		housekeeping_sessions(&database, now);
#else
		housekeeping_sessions(now);
#endif

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [vacuuming database]");
#endif
#ifdef ZABBIX_THREADS
		DBvacuum_thread(&database);
#else
		DBvacuum();
#endif

		zabbix_log( LOG_LEVEL_DEBUG, "Sleeping for %d hours", CONFIG_HOUSEKEEPING_FREQUENCY);

#ifdef HAVE_FUNCTION_SETPROCTITLE
		setproctitle("housekeeper [sleeping for %d hour(s)]", CONFIG_HOUSEKEEPING_FREQUENCY);
#endif

#ifdef ZABBIX_THREADS
		DBclose_thread(&database);
#else
		DBclose();
#endif
		sleep(3660*CONFIG_HOUSEKEEPING_FREQUENCY);
	}
}