View Full Version : PATCH: ItemID Macro
James Wells
01-09-2010, 20:46
Greetings,
Here is a simple patch to add the ItemID as a macro for notifications. I recently worked on a project where I needed to manufacture URL's to get to the latest data graph any time a certain set of alerts came in. Rather than script the URL creation outside of the Zabbix server, I wrote this patch to provide a new macro: {ITEM.ID}.
Please note that this macro will only return the ItemID of one item, so if you are using a complex trigger, this will not give you the second, third, etc ItemIDs.
mm. might be neat to polish it to provide additional functionality as other macros do for more advanced triggers ({ITEM.ID1}, {ITEM.ID2} etc).
and frontend support (in network maps and maybe elsewhere) would also be interesting... then the patch could be submitted on the issue tracker ;)
James Wells
02-09-2010, 01:29
mm. might be neat to polish it to provide additional functionality as other macros do for more advanced triggers ({ITEM.ID1}, {ITEM.ID2} etc).
I think I can manage that. Prolly have it submitted over the weekend.
I'm trying to use your patch and I have to say sorry, because my knowledge in C code and so on pointers has been degraded to its minimum..
I'm trying to get the itemid for a trigger ID.. this's the call to your function
get_itemid_status = DBget_item_id_by_triggerid(event->objectid, itemid, 0);
event->objectid has the triggerID..
But when this peace of code gets executed, zabbix_server crashes, obviously by a bad usage with that pointer..
By the definition of your function, itemid should be a pointer to a pointer??
is that right??
I mean, itemid could be a simple zbx_uint64_t.. or am I not understanding this??
Thanks for any help.
I forgot to mention that I'm trying to use your function in src/zabbix_server/actions.c.. not in expression.c
I want to check for an action with a KNOWN name, search trigger ID for the event and check which host caused the trigger to fire. Depending on this, I'll let the action continue, or I'll change event->skip_actions = 1 in order to skip this...
Edited:
if (SUCCEED == check_action_conditions(event, &action))
{
zabbix_log(LOG_LEVEL_DEBUG, "Conditions match our event. Execute operations.");
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int get_itemid_status;
char *itemid = NULL;
char query_rt [120];
result = DBselect("select name from actions where actionid=%s",row[0]);
if (NULL != (row = DBfetch(result)))
{
if (row[0] == "TICKET RT")
{
get_itemid_status = DBget_item_id_by_triggerid(event->objectid, &itemid, 1);
if (get_itemid_status == SUCCEED)
{
result = DBselect("select host from hosts inner join items on hosts.hostid=items.hostid where itemid=%s",*itemid);
if (NULL != (row = DBfetch(result)))
{
mysql_init(&mysql);
//connect to request tracker
connection = mysql_real_connect(&mysql,"host","user","pass","rt3",0,NULL,0);
if (connection != NULL)
{
zbx_snprintf(query_rt, sizeof(query_rt), "select id from Tickets where queue=6 and Subject like '%s Equipo caido' and (Status='new' or Status='open')", row[0]);
query_state = mysql_query(connection, query_rt);
if (query_state == 0)
{
result = mysql_store_result(connection);
if (mysql_num_rows(result)>0)
event->skip_actions = 1;
}
}
}
}
}
}
if (!event->skip_actions)
{
DBstart_escalation(action.actionid, event->source == EVENT_SOURCE_TRIGGERS ? event->objectid : 0, event->eventid);
if (event->source == EVENT_SOURCE_DISCOVERY || event->source == EVENT_SOURCE_AUTO_REGISTRATION)
execute_operations(event, &action);
DBfree_result(result);
}
}
James Wells
07-09-2010, 18:48
I'm trying to get the itemid for a trigger ID.. this's the call to your function
get_itemid_status = DBget_item_id_by_triggerid(event->objectid, itemid, 0);
IIRC, itemid would needs to be &itemid as it is an indirect pointer to the value. Unfortunately, pointers are my absolute weakest area programming. :(
int get_itemid_status;
char *itemid = NULL;
Not sure, but I suspect the itemid here is the culprit. Looking at the actual function declaration, I see that the itemid is indeed an indirect pointer, but you are declaring it here as a direct pointer. Try changing it to;
int get_itemid_status;
char **itemid;
EDIT: BTW, I didn't get a chance to work on this over the weekend afterall. :( Hopefully this evening.
Sorry, I put the wrong code, I realized about those mistakes, but the most important mistake was a string comparison like this...
if (row[0] == "TEXT") :eek: instead of using strcmp
Sorry, I need to stay apart of PHP programming for a while ;)
Thanks again for your help.
James.. I used your function in a patch that I've submitted in another thread on forums (http://www.zabbix.com/forum/showthread.php?p=71676#post71676).. sorry for didn't ask you before.. I didn't touch your comments over the function DBget_item_id_by_triggerid..
Hope that you don't mind..