Skip to content

Instantly share code, notes, and snippets.

@BlueSkyDetector
Created May 11, 2011 20:49
Show Gist options
  • Save BlueSkyDetector/967312 to your computer and use it in GitHub Desktop.
Save BlueSkyDetector/967312 to your computer and use it in GitHub Desktop.
CREATE TABLE events_history_relation (
eventid bigint unsigned DEFAULT '0' NOT NULL,
history_log_id bigint unsigned DEFAULT '0' NOT NULL,
history_text_id bigint unsigned DEFAULT '0' NOT NULL,
PRIMARY KEY (eventid)
) type=InnoDB;
diff -uNrp zabbix-1.8.5.orig/include/db.h zabbix-1.8.5/include/db.h
--- zabbix-1.8.5.orig/include/db.h 2011-04-16 04:17:28.000000000 +0900
+++ zabbix-1.8.5/include/db.h 2011-05-12 03:19:11.000000000 +0900
@@ -548,7 +548,7 @@ int DBstop_escalation(zbx_uint64_t actio
int DBremove_escalation(zbx_uint64_t escalationid);
void DBupdate_triggers_status_after_restart();
int DBupdate_trigger_value(zbx_uint64_t triggerid, int type, int value,
- const char *trigger_error, int new_value, int now, const char *reason);
+ const char *trigger_error, int new_value, int now, const char *reason, unsigned char history_value_type, zbx_uint64_t history_id);
int DBget_row_count(const char *table_name);
int DBget_items_unsupported_count();
diff -uNrp zabbix-1.8.5.orig/src/libs/zbxdbcache/dbcache.c zabbix-1.8.5/src/libs/zbxdbcache/dbcache.c
--- zabbix-1.8.5.orig/src/libs/zbxdbcache/dbcache.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/libs/zbxdbcache/dbcache.c 2011-05-12 05:20:36.000000000 +0900
@@ -104,6 +104,7 @@ ZBX_DC_HISTORY
unsigned char value_null;
unsigned char keep_history;
unsigned char keep_trends;
+ zbx_uint64_t id;
};
ZBX_DC_TREND
@@ -863,6 +864,8 @@ static void DCmass_update_triggers(ZBX_D
int clock;
unsigned char type;
unsigned char value;
+ unsigned char history_value_type;
+ zbx_uint64_t history_id;
} zbx_trigger_t;
zbx_trigger_t *tr = NULL, *tr_last = NULL;
@@ -940,7 +943,11 @@ static void DCmass_update_triggers(ZBX_D
if (itemid == history[i].itemid)
{
if (tr_last->clock < history[i].clock)
+ {
tr_last->clock = history[i].clock;
+ tr_last->history_value_type = history[i].value_type;
+ tr_last->history_id = history[i].id;
+ }
break;
}
}
@@ -962,11 +969,11 @@ static void DCmass_update_triggers(ZBX_D
zabbix_log(LOG_LEVEL_WARNING, "Expression [%s] cannot be evaluated: %s", tr[i].exp, error);
DBupdate_trigger_value(tr[i].triggerid, tr[i].type, tr[i].value,
- tr[i].error, TRIGGER_VALUE_UNKNOWN, tr[i].clock, error);
+ tr[i].error, TRIGGER_VALUE_UNKNOWN, tr[i].clock, error, ITEM_VALUE_TYPE_FLOAT, 0);
}
else
DBupdate_trigger_value(tr[i].triggerid, tr[i].type, tr[i].value,
- tr[i].error, exp_value, tr[i].clock, NULL);
+ tr[i].error, exp_value, tr[i].clock, NULL, tr[i].history_value_type ,tr[i].history_id);
zbx_free(tr[i].error);
zbx_free(tr[i].exp);
@@ -1738,6 +1745,7 @@ static void DCmass_add_history(ZBX_DC_HI
history[i].clock,
value_esc);
#endif
+ history[i].id = id;
zbx_free(value_esc);
id++;
}
@@ -1803,6 +1811,7 @@ static void DCmass_add_history(ZBX_DC_HI
value_esc,
history[i].logeventid);
#endif
+ history[i].id = id;
zbx_free(value_esc);
zbx_free(source_esc);
id++;
diff -uNrp zabbix-1.8.5.orig/src/libs/zbxdbhigh/db.c zabbix-1.8.5/src/libs/zbxdbhigh/db.c
--- zabbix-1.8.5.orig/src/libs/zbxdbhigh/db.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/libs/zbxdbhigh/db.c 2011-05-12 03:25:44.000000000 +0900
@@ -479,7 +479,7 @@ static int trigger_dependent(zbx_uint64_
}
int DBupdate_trigger_value(zbx_uint64_t triggerid, int trigger_type, int trigger_value,
- const char *trigger_error, int new_value, int now, const char *reason)
+ const char *trigger_error, int new_value, int now, const char *reason, unsigned char history_value_type, zbx_uint64_t history_id)
{
const char *__function_name = "update_trigger_value";
int ret = SUCCEED;
@@ -544,7 +544,7 @@ int DBupdate_trigger_value(zbx_uint64_t
event.value = new_value;
/* Processing event */
- if (FAIL == (ret = process_event(&event, 0)))
+ if (FAIL == (ret = process_event(&event, 0, history_value_type, history_id)))
{
zabbix_log(LOG_LEVEL_DEBUG, "Event not added for triggerid [" ZBX_FS_UI64 "]",
triggerid);
@@ -695,7 +695,7 @@ void DBupdate_triggers_status_after_rest
continue;
DBupdate_trigger_value(triggerid, trigger_type, trigger_value, trigger_error,
- TRIGGER_VALUE_UNKNOWN, min_nextcheck, "Zabbix was restarted.");
+ TRIGGER_VALUE_UNKNOWN, min_nextcheck, "Zabbix was restarted.", ITEM_VALUE_TYPE_FLOAT, 0);
}
DBfree_result(result);
@@ -1976,7 +1976,7 @@ void DBregister_host(zbx_uint64_t proxy_
event.value = TRIGGER_VALUE_TRUE;
/* Processing event */
- process_event(&event, 0);
+ process_event(&event, 0, ITEM_VALUE_TYPE_FLOAT, 0);
}
zbx_free(host_esc);
diff -uNrp zabbix-1.8.5.orig/src/libs/zbxdbhigh/discovery.c zabbix-1.8.5/src/libs/zbxdbhigh/discovery.c
--- zabbix-1.8.5.orig/src/libs/zbxdbhigh/discovery.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/libs/zbxdbhigh/discovery.c 2011-05-12 03:26:32.000000000 +0900
@@ -49,7 +49,7 @@ static void discovery_add_event(int obje
event.clock = now;
event.value = value;
- process_event(&event, 0);
+ process_event(&event, 0, ITEM_VALUE_TYPE_FLOAT, 0);
}
static DB_RESULT discovery_get_dhost_by_value(zbx_uint64_t dcheckid, const char *value)
diff -uNrp zabbix-1.8.5.orig/src/libs/zbxserver/expression.c zabbix-1.8.5/src/libs/zbxserver/expression.c
--- zabbix-1.8.5.orig/src/libs/zbxserver/expression.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/libs/zbxserver/expression.c 2011-05-12 05:44:26.000000000 +0900
@@ -1096,7 +1096,7 @@ fail:
* Comments: *
* *
******************************************************************************/
-static int DBget_item_value(DB_TRIGGER *trigger, char **value, int N_functionid, int clock)
+static int DBget_item_value(DB_TRIGGER *trigger, char **value, int N_functionid, int clock, zbx_uint64_t eventid)
{
const char *__function_name = "DBget_item_value";
DB_RESULT result;
@@ -1106,6 +1106,7 @@ static int DBget_item_value(DB_TRIGGER *
zbx_uint64_t functionid, valuemapid;
int value_type, ret = FAIL;
char tmp[MAX_STRING_LEN];
+ char tmp2[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
@@ -1124,11 +1125,28 @@ static int DBget_item_value(DB_TRIGGER *
value_type = atoi(row[1]);
ZBX_STR2UINT64(valuemapid, row[2]);
- zbx_snprintf(tmp, sizeof(tmp), "select value from %s where itemid=%s and clock<=%d order by itemid,clock desc",
- get_table_by_value_type(value_type), row[0], clock);
-
- h_result = DBselectN(tmp, 1);
- if (NULL != (h_row = DBfetch(h_result)))
+ if(eventid)
+ {
+ if (ITEM_VALUE_TYPE_LOG == value_type)
+ {
+ zbx_snprintf(tmp2, sizeof(tmp2), "select value from %s , events_history_relation where itemid=%s and eventid=" ZBX_FS_UI64 " and history_log_id=id order by itemid,clock desc",
+ get_table_by_value_type(value_type), row[0], eventid);
+ }
+ if (ITEM_VALUE_TYPE_TEXT == value_type)
+ {
+ zbx_snprintf(tmp2, sizeof(tmp2), "select value from %s , events_history_relation where itemid=%s and eventid=" ZBX_FS_UI64 " and history_text_id=id order by itemid,clock desc",
+ get_table_by_value_type(value_type), row[0], eventid);
+ }
+ h_result = DBselectN(tmp2, 1);
+ }
+
+ if(NULL == (h_row = DBfetch(h_result))){
+ DBfree_result(h_result);
+ zbx_snprintf(tmp, sizeof(tmp), "select value from %s where itemid=%s and clock<=%d order by itemid,clock desc",
+ get_table_by_value_type(value_type), row[0], clock);
+ h_result = DBselectN(tmp, 1);
+ }
+ else
{
switch (value_type)
{
@@ -1770,7 +1788,7 @@ int substitute_simple_macros(DB_EVENT *e
else if (0 == strcmp(m, MVAR_ITEM_LASTVALUE))
ret = DBget_item_lastvalue(&event->trigger, &replace_to, N_functionid);
else if (0 == strcmp(m, MVAR_ITEM_VALUE))
- ret = DBget_item_value(&event->trigger, &replace_to, N_functionid, event->clock);
+ ret = DBget_item_value(&event->trigger, &replace_to, N_functionid, event->clock, event->eventid);
else if (0 == strcmp(m, MVAR_ITEM_LOG_DATE))
{
if (SUCCEED == (ret = DBget_history_log_value(&event->trigger, &replace_to,
@@ -1988,7 +2006,7 @@ int substitute_simple_macros(DB_EVENT *e
else if (0 == strcmp(m, MVAR_ITEM_LASTVALUE))
ret = DBget_item_lastvalue(&event->trigger, &replace_to, N_functionid);
else if (0 == strcmp(m, MVAR_ITEM_VALUE))
- ret = DBget_item_value(&event->trigger, &replace_to, N_functionid, event->clock);
+ ret = DBget_item_value(&event->trigger, &replace_to, N_functionid, event->clock, event->eventid);
else if (0 == strncmp(m, "{$", 2)) /* user defined macros */
zbxmacros_get_value_by_triggerid(macros, event->objectid, m, &replace_to);
}
diff -uNrp zabbix-1.8.5.orig/src/zabbix_server/events.c zabbix-1.8.5/src/zabbix_server/events.c
--- zabbix-1.8.5.orig/src/zabbix_server/events.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/zabbix_server/events.c 2011-05-12 03:47:04.000000000 +0900
@@ -285,7 +285,7 @@ out:
* Comments: *
* *
******************************************************************************/
-int process_event(DB_EVENT *event, int force_actions)
+int process_event(DB_EVENT *event, int force_actions, unsigned char history_value_type, zbx_uint64_t history_id)
{
const char *__function_name = "process_event";
int ret = FAIL;
@@ -307,6 +307,23 @@ int process_event(DB_EVENT *event, int f
event->objectid,
event->clock,
event->value);
+ if ((history_value_type == ITEM_VALUE_TYPE_LOG) && (history_id != 0))
+ {
+ DBexecute("insert into events_history_relation (eventid,history_log_id,history_text_id)"
+ " values (" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ")",
+ event->eventid,
+ history_id,
+ 0);
+ }
+ if ((history_value_type == ITEM_VALUE_TYPE_TEXT) && (history_id != 0))
+ {
+ DBexecute("insert into events_history_relation (eventid,history_log_id,history_text_id)"
+ " values (" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ")",
+ event->eventid,
+ 0,
+ history_id);
+ }
+
if (0 != event->ack_eventid)
copy_acknowledges(event->ack_eventid, event->eventid);
diff -uNrp zabbix-1.8.5.orig/src/zabbix_server/events.h zabbix-1.8.5/src/zabbix_server/events.h
--- zabbix-1.8.5.orig/src/zabbix_server/events.h 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/zabbix_server/events.h 2011-05-12 03:22:12.000000000 +0900
@@ -23,6 +23,6 @@
#include "common.h"
#include "db.h"
-int process_event(DB_EVENT *event, int force_actions);
+int process_event(DB_EVENT *event, int force_actions, unsigned char history_value_type, zbx_uint64_t history_id);
#endif
diff -uNrp zabbix-1.8.5.orig/src/zabbix_server/poller/poller.c zabbix-1.8.5/src/zabbix_server/poller/poller.c
--- zabbix-1.8.5.orig/src/zabbix_server/poller/poller.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/zabbix_server/poller/poller.c 2011-05-12 03:28:45.000000000 +0900
@@ -209,7 +209,7 @@ static void update_triggers_status_to_un
trigger_error = row[3];
DBupdate_trigger_value(triggerid, trigger_type, trigger_value,
- trigger_error, TRIGGER_VALUE_UNKNOWN, now, reason);
+ trigger_error, TRIGGER_VALUE_UNKNOWN, now, reason, ITEM_VALUE_TYPE_FLOAT, 0);
}
DBfree_result(result);
diff -uNrp zabbix-1.8.5.orig/src/zabbix_server/timer/timer.c zabbix-1.8.5/src/zabbix_server/timer/timer.c
--- zabbix-1.8.5.orig/src/zabbix_server/timer/timer.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/zabbix_server/timer/timer.c 2011-05-12 03:29:40.000000000 +0900
@@ -97,11 +97,11 @@ static void process_time_functions()
zabbix_syslog("Expression [%s] cannot be evaluated: %s", exp, error);
DBupdate_trigger_value(triggerid, trigger_type, trigger_value,
- trigger_error, TRIGGER_VALUE_UNKNOWN, time(NULL), error);
+ trigger_error, TRIGGER_VALUE_UNKNOWN, time(NULL), error, ITEM_VALUE_TYPE_FLOAT, 0);
}
else
DBupdate_trigger_value(triggerid, trigger_type, trigger_value,
- trigger_error, exp_value, time(NULL), NULL);
+ trigger_error, exp_value, time(NULL), NULL, ITEM_VALUE_TYPE_FLOAT, 0);
zbx_free(exp);
}
@@ -444,7 +444,7 @@ static void generate_events(zbx_uint64_t
event.clock = maintenance_to;
event.value = value_after;
- process_event(&event, 1);
+ process_event(&event, 1, ITEM_VALUE_TYPE_FLOAT, 0);
}
DBfree_result(result);
}
diff -uNrp zabbix-1.8.5.orig/src/zabbix_server/trapper/nodehistory.c zabbix-1.8.5/src/zabbix_server/trapper/nodehistory.c
--- zabbix-1.8.5.orig/src/zabbix_server/trapper/nodehistory.c 2011-04-16 04:17:29.000000000 +0900
+++ zabbix-1.8.5/src/zabbix_server/trapper/nodehistory.c 2011-05-12 03:30:56.000000000 +0900
@@ -206,7 +206,7 @@ static int process_record_event(int send
}
}
- return process_event(&event, 0);
+ return process_event(&event, 0, ITEM_VALUE_TYPE_FLOAT, 0);
error:
zabbix_log(LOG_LEVEL_ERR, "NODE %d: Received invalid record from node %d for node %d [%s]",
CONFIG_NODEID,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment