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
|
/*
** 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 <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 "functions.h"
#include "log.h"
#include "zlog.h"
#include "email.h"
#include "escalator.h"
int process_escalation(DB_ESCALATION_LOG *escalation_log)
{
int i,now;
char sql[MAX_STRING_LEN];
int processed_level;
DB_RESULT *result;
DB_ESCALATION_RULE escalation_rule;
zabbix_log( LOG_LEVEL_WARNING, "In process_escalation()");
snprintf(sql,sizeof(sql)-1,"select escalationruleid, escalationid,level,period,delay,actiontype from escalation_rules where escalationid=%d and level>=%d order by level", escalation_log->escalationid, escalation_log->level);
zabbix_log( LOG_LEVEL_WARNING, "SQL [%s]", sql);
result = DBselect(sql);
processed_level=escalation_log->level;
for(i=0;i<DBnum_rows(result);i++)
{
escalation_rule.escalationruleid=atoi(DBget_field(result,i,0));
escalation_rule.escalationid=atoi(DBget_field(result,i,1));
escalation_rule.level=atoi(DBget_field(result,i,2));
escalation_rule.period=DBget_field(result,i,3);
escalation_rule.delay=atoi(DBget_field(result,i,4));
escalation_rule.actiontype=atoi(DBget_field(result,i,5));
if(processed_level > escalation_rule.level) break;
zabbix_log( LOG_LEVEL_WARNING, "Selected escalationrule ID [%d]", escalation_rule.escalationruleid);
now=time(NULL);
if(escalation_log->nextcheck <= now)
{
/* ADD check_period() !!! */
switch (escalation_rule.actiontype)
{
case ESCALATION_ACTION_NOTHING:
zabbix_log( LOG_LEVEL_WARNING, "ESCALATION_ACTION_NOTHING");
escalation_log->nextcheck = escalation_rule.delay+now;
escalation_log->level = escalation_rule.level;
snprintf(sql,sizeof(sql)-1,"update escalation_log set nextcheck=%d,level=%d where escalationlogid=%d", escalation_log->nextcheck, escalation_log->level, escalation_log->escalationlogid);
zabbix_log( LOG_LEVEL_WARNING, "SQL [%s]", sql);
DBexecute(sql);
break;
case ESCALATION_ACTION_EXEC_ACTION:
zabbix_log( LOG_LEVEL_WARNING, "ESCALATION_ACTION_EXEC_ACTION");
escalation_log->nextcheck = escalation_rule.delay+now;
escalation_log->level = escalation_rule.level;
snprintf(sql,sizeof(sql)-1,"update escalation_log set nextcheck=%d,level=%d where escalationlogid=%d", escalation_log->nextcheck, escalation_log->level, escalation_log->escalationlogid);
zabbix_log( LOG_LEVEL_WARNING, "SQL [%s]", sql);
DBexecute(sql);
break;
case ESCALATION_ACTION_INC_SEVERITY:
zabbix_log( LOG_LEVEL_WARNING, "ESCALATION_ACTION_INC_SEVERITY");
escalation_log->nextcheck = escalation_rule.delay+now;
escalation_log->level = escalation_rule.level;
snprintf(sql,sizeof(sql)-1,"update escalation_log set nextcheck=%d,level=%d where escalationlogid=%d", escalation_log->nextcheck, escalation_log->level, escalation_log->escalationlogid);
zabbix_log( LOG_LEVEL_WARNING, "SQL [%s]", sql);
DBexecute(sql);
break;
case ESCALATION_ACTION_INC_ADMIN:
zabbix_log( LOG_LEVEL_WARNING, "ESCALATION_ACTION_INC_ADMIN");
escalation_log->nextcheck = escalation_rule.delay+now;
escalation_log->level = escalation_rule.level;
snprintf(sql,sizeof(sql)-1,"update escalation_log set nextcheck=%d,level=%d where escalationlogid=%d", escalation_log->nextcheck, escalation_log->level, escalation_log->escalationlogid);
zabbix_log( LOG_LEVEL_WARNING, "SQL [%s]", sql);
DBexecute(sql);
break;
default:
zabbix_log( LOG_LEVEL_ERR, "Unknow escalation action type [%d]", escalation_rule.actiontype);
}
processed_level= escalation_rule.level;
}
}
if(DBnum_rows(result)==0)
{
zabbix_log( LOG_LEVEL_WARNING, "No more escalation levels");
snprintf(sql,sizeof(sql)-1,"update escalation_log set status=1 where escalationlogid=%d", escalation_log->escalationlogid);
zabbix_log( LOG_LEVEL_WARNING, "SQL [%s]", sql);
DBexecute(sql);
}
DBfree_result(result);
return SUCCEED;
}
int main_escalator_loop()
{
char sql[MAX_STRING_LEN];
int i,res;
int now;
DB_RESULT *result;
DB_ESCALATION_LOG escalation_log;
for(;;)
{
zabbix_log( LOG_LEVEL_WARNING, "Selecting data from escalation_log");
#ifdef HAVE_FUNCTION_SETPROCTITLE
setproctitle("connecting to the database");
#endif
DBconnect();
now=time(NULL);
snprintf(sql,sizeof(sql)-1,"select escalationlogid,triggerid, alarmid, escalationid, level, adminlevel, nextcheck, status from escalation_log where status=0 and nextcheck<=%d", now);
result = DBselect(sql);
for(i=0;i<DBnum_rows(result);i++)
{
escalation_log.escalationlogid=atoi(DBget_field(result,i,0));
escalation_log.triggerid=atoi(DBget_field(result,i,1));
escalation_log.alarmid=atoi(DBget_field(result,i,2));
escalation_log.escalationid=atoi(DBget_field(result,i,3));
escalation_log.level=atoi(DBget_field(result,i,4));
escalation_log.adminlevel=atoi(DBget_field(result,i,5));
escalation_log.nextcheck=atoi(DBget_field(result,i,6));
escalation_log.status=atoi(DBget_field(result,i,7));
res=process_escalation(&escalation_log);
if(res==SUCCEED)
{
zabbix_log( LOG_LEVEL_WARNING, "Processing escalation_log ID [%d]", escalation_log.escalationlogid);
}
else
{
zabbix_log( LOG_LEVEL_WARNING, "Processing escalation_log ID [%d] failed", escalation_log.escalationlogid);
}
}
DBfree_result(result);
DBclose();
#ifdef HAVE_FUNCTION_SETPROCTITLE
setproctitle("escalator [sleeping for %d seconds]", 10);
#endif
sleep(10);
}
}
|