summaryrefslogtreecommitdiffstats
path: root/src/zabbix_server/trapper/autoregister.c
blob: e40da759ff098e123382d23006ccb55f9269910d (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
/* 
** 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 "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include "cfg.h"
#include "pid.h"
#include "db.h"
#include "log.h"
#include "zlog.h"

#include <sys/types.h>
#include <regex.h>

#include "common.h"


int	autoregister(char *server)
{
	DB_RESULT	*result;

	int	ret=SUCCEED;
	char	sql[MAX_STRING_LEN];
	char	*pattern;
	int	i;
	int	len;
	int	hostid;
	
	zabbix_log( LOG_LEVEL_WARNING, "In autoregister(%s)",server);

	if(DBhost_exists(server) == SUCCEED)
	{
		zabbix_log( LOG_LEVEL_WARNING, "Host [%s] already exists. Do nothing.", server);
		return FAIL;
	}

	snprintf(sql,sizeof(sql)-1,"select id,pattern,hostid from autoreg order by priority");

	result = DBselect(sql);

	for(i=0;i<DBnum_rows(result);i++)
	{
		pattern=DBget_field(result,i,1);
		hostid=atoi(DBget_field(result,i,2));

		if(zbx_regexp_match(server, pattern, &len) != 0)
		{
			zabbix_log( LOG_LEVEL_WARNING, "Matched [%s] [%s]",server,pattern);
			register_new_host(server, hostid);
			break;
		}
		else
		{
			zabbix_log( LOG_LEVEL_WARNING, "No match [%s] [%s]",server,pattern);
		}
	}

	DBfree_result(result);

	return ret;
}

void	register_new_host(char *server, int host_templateid)
{
	int	hostid;

	zabbix_log( LOG_LEVEL_WARNING, "In register_new_host(%s,%d)", server, host_templateid);

	hostid = DBadd_host(server, 10050, HOST_STATUS_MONITORED, 0, "", 0, HOST_AVAILABLE_UNKNOWN);

	zabbix_log( LOG_LEVEL_WARNING, "Added new host with hostid [%d]", hostid);

	/* Use hostid as a template */
	if( (hostid>0) && (host_templateid!=0))
	{
		zabbix_log( LOG_LEVEL_WARNING, "Using hostid [%d] as a template", host_templateid);

		DBadd_templates_to_host(hostid,host_templateid);
		DBsync_host_with_templates(hostid);

	}
}