summaryrefslogtreecommitdiffstats
path: root/src/zabbix_server/zlog.c
blob: dac1e760e5437d77a104e831ceb60044b52192d0 (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
/* 
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** 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 <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <time.h>

#include "common.h"
#include "log.h"
#include "zbxserver.h"

#include "zlog.h"

/******************************************************************************
 *                                                                            *
 * Function: zabbix_syslog                                                    *
 *                                                                            *
 * Purpose: save internal warning or error message in item zabbix[log]        *
 *                                                                            *
 * Parameters: va_list arguments                                              *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments: do nothing if no zabbix[log] items                               *
 *                                                                            *
 ******************************************************************************/
void __zbx_zabbix_syslog(const char *fmt, ...)
{ 
	va_list		ap;
	char		value_str[MAX_STRING_LEN];

	DB_ITEM		item;
	DB_RESULT	result;
	DB_ROW		row;
	AGENT_RESULT	agent;
	time_t		now;

	zabbix_log(LOG_LEVEL_DEBUG, "In zabbix_log()");

	/* This is made to disable writing to database for watchdog */
	if(CONFIG_ENABLE_LOG == 0)	return;

	result = DBselect("select %s where h.hostid=i.hostid and h.status=%d and i.status=%d"
			" and h.proxy_hostid=0 and i.key_='%s' and i.value_type=%d" DB_NODE,
			ZBX_SQL_ITEM_SELECT,
			ITEM_STATUS_ACTIVE,
			HOST_STATUS_MONITORED,
			SERVER_ZABBIXLOG_KEY,
			ITEM_VALUE_TYPE_STR,
			DBnode_local("h.hostid"));

	now = time(NULL);

	while((row=DBfetch(result)))
	{
		DBget_item_from_db(&item,row);

		va_start(ap,fmt);
		vsnprintf(value_str,sizeof(value_str),fmt,ap);
		value_str[MAX_STRING_LEN-1]=0;
		va_end(ap);

		init_result(&agent);
		SET_STR_RESULT(&agent, strdup(value_str));

		if (0 == CONFIG_DBSYNCER_FORKS)
		{
			process_new_value(&item, &agent, now);
			update_triggers(item.itemid);
		}
		else
			process_new_value(&item, &agent, now);

		free_result(&agent);
	}

	DBfree_result(result);
}