I had a very specific requirement: All items received through the trapper interface are to be registered in Zabbix. This amounts to several hundreds of items per host and items were to be added regularly. In order to accomodate this demand, I wrote a simple patch to the zabbix trapper. I'm posting it here to receive some C&C and also because some of you might find it usefull as well..
This patch applies cleanly to the pristine Zabbix 1.1.4 sourcecode with this command:
Here is the patch:
This patch applies cleanly to the pristine Zabbix 1.1.4 sourcecode with this command:
Code:
# cd /usr/src/zabbix-1.1.4 # patch -p0 < ../item_autoregistration.patch
Code:
Index: src/zabbix_server/trapper/autoregister.h
===================================================================
--- src/zabbix_server/trapper/autoregister.h (revision 6114)
+++ src/zabbix_server/trapper/autoregister.h (working copy)
@@ -21,5 +21,6 @@
#define ZABBIX_AUTOREGISTER_H
int autoregister(char *server);
+int autoregister_item(char *server, char *key);
#endif
Index: src/zabbix_server/trapper/autoregister.c
===================================================================
--- src/zabbix_server/trapper/autoregister.c (revision 6114)
+++ src/zabbix_server/trapper/autoregister.c (working copy)
@@ -103,3 +103,64 @@
}
}
+
+int autoregister_item(char *server, char *key)
+{
+ DB_ITEM item;
+ DB_HOST host;
+
+ char dummy[] = "";
+
+ zabbix_log( LOG_LEVEL_DEBUG, "In autoregister_item(%s, %s)", server, key);
+
+ if(SUCCEED == DBget_host_by_name(server, &host))
+ {
+ item.hostid = host.hostid;
+ strscpy(item.key, key);
+ item.description = key;
+ item.delay = 30;
+ item.history = 90;
+ item.status = ITEM_STATUS_ACTIVE;
+ item.type = ITEM_TYPE_TRAPPER;
+ item.value_type = ITEM_VALUE_TYPE_FLOAT;
+ item.trends = 365;
+ item.formula = "1";
+
+ item.trapper_hosts = dummy;
+ item.units = dummy;
+ item.snmp_port = 161; // For some reason, this field is mandatory when editing an item in the frontend
+ item.snmp_community = dummy;
+ item.snmp_oid = dummy;
+ item.snmpv3_securityname = dummy;
+ item.snmpv3_securitylevel = 0;
+ item.snmpv3_authpassphrase = dummy;
+ item.snmpv3_privpassphrase = dummy;
+ item.logtimefmt = dummy;
+
+ return DBadd_item(
+ item.description,
+ item.key,
+ item.hostid,
+ item.delay,
+ item.history,
+ item.status,
+ item.type,
+ item.snmp_community,
+ item.snmp_oid,
+ item.value_type,
+ item.trapper_hosts,
+ item.snmp_port,
+ item.units,
+ item.multiplier,
+ item.delta,
+ item.snmpv3_securityname,
+ item.snmpv3_securitylevel,
+ item.snmpv3_authpassphrase,
+ item.snmpv3_privpassphrase,
+ item.formula,
+ item.trends,
+ item.logtimefmt);
+ } else {
+ return FAIL;
+ }
+}
Index: src/zabbix_server/trapper/trapper.c
===================================================================
--- src/zabbix_server/trapper/trapper.c (revision 6114)
+++ src/zabbix_server/trapper/trapper.c (working copy)
@@ -140,7 +140,14 @@
severity[0]=0;
}
- ret=process_data(sockfd,server,key,value_string,lastlogsize,timestamp,source,severity);
+ ret = process_data(sockfd,server,key,value_string,lastlogsize,timestamp,source,severity);
+
+ if(SUCCEED != ret) {
+ if(SUCCEED == autoregister_item(server, key)) {
+ ret = process_data(sockfd,server,key,value_string,lastlogsize,timestamp,source,severity);
+ }
+ }
+
if( SUCCEED == ret)
{
snprintf(result,sizeof(result)-1,"OK\n");
Index: src/libs/zbxdbhigh/host.c
===================================================================
--- src/libs/zbxdbhigh/host.c (revision 6114)
+++ src/libs/zbxdbhigh/host.c (working copy)
@@ -160,6 +160,35 @@
return SUCCEED;
}
+int DBget_host_by_name(char *server, DB_HOST *host)
+{
+ int hostid = 0;
+ int ret = FAIL;
+ char server_esc[MAX_STRING_LEN];
+ char sql[MAX_STRING_LEN];
+
+ DB_RESULT result;
+ DB_ROW row;
+
+ zabbix_log( LOG_LEVEL_DEBUG, "In DBget_host_by_name(%s)", server);
+
+ DBescape_string(server, server_esc, MAX_STRING_LEN);
+ snprintf(sql, sizeof(sql)-1, "select hostid from hosts where host='%s'", server_esc);
+
+ result = DBselect(sql);
+ row = DBfetch(result);
+
+ if(row) {
+ hostid = atoi(row[0]);
+
+ if(hostid != 0) {
+ ret = DBget_host_by_hostid(hostid, host);
+ }
+ }
+
+ return ret;
+}
+
int DBget_host_by_hostid(int hostid,DB_HOST *host)
{
DB_RESULT result;