File: events.cpp

package info (click to toggle)
netmrg 0.20-2.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,820 kB
  • ctags: 2,009
  • sloc: php: 9,401; sh: 4,597; cpp: 2,898; perl: 621; ansic: 381; makefile: 331; xml: 92; sql: 71; sed: 16
file content (186 lines) | stat: -rw-r--r-- 5,132 bytes parent folder | download | duplicates (4)
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
/********************************************
* NetMRG Integrator
*
* events.cpp
* NetMRG Gatherer Events Library
*
* see doc/LICENSE for copyright information
********************************************/

#include "events.h"
#include "utils.h"
#include "settings.h"
#include "monitors.h"

string situations[4] = { "Disabled", "Normal", "Warning", "Critical" };

uint process_events(DeviceInfo info, MYSQL *mysql)
{
	MYSQL_RES 	*mysql_res;
	MYSQL_ROW 	mysql_row;
	uint		status = 0;

	string query = "SELECT id, trigger_type, last_status, situation, last_triggered, name FROM events WHERE mon_id=" + inttostr(info.monitor_id) + " AND trigger_type = 1";
	mysql_res = db_query(mysql, &info, query);

	for (uint i = 0; i < mysql_num_rows(mysql_res); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);
		info.event_id = strtoint(mysql_row[0]);
		if (process_event(info, mysql, strtoint(mysql_row[1]), strtoint(mysql_row[2]), strtoint(mysql_row[3]), strtoint(mysql_row[4]), mysql_row[5]))
		{
			status = worstof(status, strtoint(mysql_row[3]));
		}
	}

	mysql_free_result(mysql_res);

	return status;
} // end process_events()


uint process_event(DeviceInfo info, MYSQL *mysql, int trigger_type, int last_status, int situation, long int last_triggered, string name)
{
	MYSQL_RES	*mysql_res;
	MYSQL_ROW	mysql_row;
	uint		status = 0;

	string query = "SELECT `value`, `value_type`, `condition`, `logic_condition` FROM `conditions` WHERE `event_id` = '" + inttostr(info.event_id) + "' ORDER BY `id`";
	mysql_res = db_query(mysql, &info, query);

	for (uint i = 0; i < mysql_num_rows(mysql_res); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);

		if (i == 0)
		{
			status = process_condition(info, strtoint(mysql_row[0]), strtoint(mysql_row[1]), strtoint(mysql_row[2]));
		}
		else
		{
			switch (strtoint(mysql_row[3]))
			{
				case 0:	status = status && process_condition(info, strtoint(mysql_row[0]), strtoint(mysql_row[1]), strtoint(mysql_row[2]));
					break;
				case 1:	status = status || process_condition(info, strtoint(mysql_row[0]), strtoint(mysql_row[1]), strtoint(mysql_row[2]));
					break;
			}
		}
	}

	mysql_free_result(mysql_res);

	if (status == 0)
	{
		debuglogger(DEBUG_EVENT, LEVEL_INFO, &info, "Not Triggered.");
		db_update(mysql, &info, "UPDATE events SET last_status=0 WHERE id=" + inttostr(info.event_id));
		return 0;
	}
	else
	{
		debuglogger(DEBUG_EVENT, LEVEL_INFO, &info, "Triggered.");
		
		// setup parameters for the response to use.
		info.parameters.push_front(ValuePair("event_name", name));
		info.parameters.push_front(ValuePair("situation", situations[situation]));

		if ((uint) last_status != status)
		{
			db_update(mysql, &info, "UPDATE events SET last_triggered=UNIX_TIMESTAMP(NOW()), last_status=1 WHERE id=" + inttostr(info.event_id));
			db_update(mysql, &info, "INSERT INTO event_log SET date=UNIX_TIMESTAMP(NOW()), time_since_last_change=UNIX_TIMESTAMP(NOW())-" + inttostr(last_triggered) + ", event_id=" + inttostr(info.event_id));

			process_responses(info, mysql);
		}

		return 1;
	}
} // end process_event()

uint process_condition(DeviceInfo info, long long int compare_value, int value_type, int condition)
{
	long long int actual_value = 0;

	switch (value_type)
	{
		case 0:
			if (info.curr_val == "U")
				return 0;
			actual_value = strtoint(info.curr_val);
			break;

		case 1: 
			if (info.delta_val == "U")
				return 0;
			actual_value = strtoint(info.delta_val);
			break;

		case 2: 
			if (info.rate_val == "U")
				return 0;
			actual_value = strtoint(info.rate_val);
			break;
		
		case 3:
			if (info.last_val == "U")
				return 0;
			actual_value = strtoint(info.last_val);
			break;
	}

	switch (condition)
	{
		case 0:	if (actual_value < compare_value)
				return 1;
			break;

		case 1: if (actual_value == compare_value)
				return 1;
			break;

		case 2: if (actual_value > compare_value)
				return 1;
			break;

		case 3: if (actual_value <= compare_value)
				return 1;
			break;

		case 4: if (actual_value != compare_value)
				return 1;
			break;

		case 5: if (actual_value >= compare_value)
				return 1;
			break;
	}

	return 0;
} // end process_condition()

void process_responses(DeviceInfo info, MYSQL *mysql)
{
	MYSQL_RES	*mysql_res;
	MYSQL_ROW	mysql_row;
	int			notification_res;

	string query = 	string("SELECT	notifications.command, responses.parameters, responses.id ") 	+
			string("FROM	responses, notifications ")			+
			string("WHERE	responses.event_id=") + inttostr(info.event_id) + " " +
			string("AND	responses.notification_id=notifications.id AND notifications.disabled = 0");

	mysql_res = db_query(mysql, &info, query);

	for (uint i = 0; i < mysql_num_rows(mysql_res); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);
		info.response_id = strtoint(mysql_row[2]);
		string command = string(mysql_row[0]) + " " + string(mysql_row[1]);
		command = expand_parameters(info, command);
		if (command[0] != '/')
			command = get_setting(setPathLibexec) + "/" + command;
		debuglogger(DEBUG_RESPONSE, LEVEL_INFO, &info, "Running Response: " + command);
		notification_res = system(command.c_str());
	}

} // end process_responses()