int DBnum_rows(MYSQL_RES *result) { int rows; if(result == NULL) { return 0; } /* Order is important ! */ rows = mysql_num_rows(result); if(rows == 0) { return 0; } /* This is necessary to exclude situations like * atoi(DBget_field(result,0,0). This leads to coredump. */ /* This is required for empty results for count(*), etc */ if(DBget_field(result,0,0) == 0) { return 0; } return rows; }
/****************************************************************************** * * * Function: process_updated_records * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void process_updated_records(int nodeid, char *data) { const char *__function_name = "process_updated_records"; char *r, *lf, *value_esc; int op, dnum; const ZBX_TABLE *table = NULL; const ZBX_FIELD *field = NULL; zbx_uint64_t recid; char *dsql = NULL, *isql = NULL, *ifld = NULL, *ival = NULL, *usql = NULL, *ufld = NULL; size_t dsql_alloc = 4 * ZBX_KIBIBYTE, dsql_offset = 0, dtmp_offset = 0, isql_alloc = 4 * ZBX_KIBIBYTE, isql_offset = 0, ifld_alloc = 4 * ZBX_KIBIBYTE, ifld_offset = 0, ival_alloc = 4 * ZBX_KIBIBYTE, ival_offset = 0, usql_alloc = 4 * ZBX_KIBIBYTE, usql_offset = 0, ufld_alloc = 4 * ZBX_KIBIBYTE, ufld_offset = 0; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); dsql = zbx_malloc(dsql, dsql_alloc); isql = zbx_malloc(isql, isql_alloc); ifld = zbx_malloc(ifld, ifld_alloc); ival = zbx_malloc(ival, ival_alloc); usql = zbx_malloc(usql, usql_alloc); ufld = zbx_malloc(ufld, ufld_alloc); #ifdef HAVE_ORACLE DBbegin_multiple_update(&dsql, &dsql_alloc, &dsql_offset); DBbegin_multiple_update(&isql, &isql_alloc, &isql_offset); DBbegin_multiple_update(&usql, &usql_alloc, &usql_offset); #endif for (r = data; '\0' != *r;) { if (NULL != (lf = strchr(r, '\n'))) *lf = '\0'; /* table name */ zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); if (NULL == table || 0 != strcmp(table->table, buf)) { if (NULL == (table = DBget_table(buf))) { zabbix_log(LOG_LEVEL_DEBUG, "%s(): cannot find table [%s]", __function_name, buf); goto next; } } /* record id */ zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); ZBX_STR2UINT64(recid, buf); if (NULL == r) { zabbix_log(LOG_LEVEL_DEBUG, "%s(): invalid record", __function_name); goto next; } if ('0' == *r) /* NODE_CONFIGLOG_OP_UPDATE */ { result = DBselect("select 0 from %s where %s=" ZBX_FS_UI64, table->table, table->recid, recid); if (NULL == (row = DBfetch(result))) op = NODE_CONFIGLOG_OP_ADD; else op = NODE_CONFIGLOG_OP_UPDATE; DBfree_result(result); zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); ifld_offset = 0; ival_offset = 0; ufld_offset = 0; dtmp_offset = dsql_offset; dnum = 0; if (op == NODE_CONFIGLOG_OP_ADD && NULL != table->uniq) { zbx_snprintf_alloc(&dsql, &dsql_alloc, &dsql_offset, "delete from %s where ", table->table); } while (NULL != r) { /* field name */ zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); if (NULL == (field = DBget_field(table, buf))) { zabbix_log(LOG_LEVEL_DEBUG, "%s(): cannot find field [%s.%s]", __function_name, table->table, buf); goto next; } if (NODE_CONFIGLOG_OP_UPDATE == op) zbx_snprintf_alloc(&ufld, &ufld_alloc, &ufld_offset, "%s=", buf); else /* NODE_CONFIGLOG_OP_ADD */ zbx_snprintf_alloc(&ifld, &ifld_alloc, &ifld_offset, "%s,", buf); /* value type (ignored) */ zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); if (0 == strcmp(buf, "NULL")) { if (NODE_CONFIGLOG_OP_UPDATE == op) zbx_strcpy_alloc(&ufld, &ufld_alloc, &ufld_offset, "NULL,"); else /* NODE_CONFIGLOG_OP_ADD */ zbx_strcpy_alloc(&ival, &ival_alloc, &ival_offset, "NULL,"); continue; } switch (field->type) { case ZBX_TYPE_ID: /* if the field relates the same table * for example: host.proxy_hostid relates with host.hostid */ if (NODE_CONFIGLOG_OP_ADD == op && NULL != field->fk_table && 0 == strcmp(table->table, field->fk_table)) { zbx_snprintf_alloc(&ufld, &ufld_alloc, &ufld_offset, "%s=%s,", field->name, buf); zbx_strcpy_alloc(&ival, &ival_alloc, &ival_offset, "NULL,"); break; } case ZBX_TYPE_INT: case ZBX_TYPE_UINT: case ZBX_TYPE_FLOAT: if (NODE_CONFIGLOG_OP_UPDATE == op) zbx_snprintf_alloc(&ufld, &ufld_alloc, &ufld_offset, "%s,", buf); else /* NODE_CONFIGLOG_OP_ADD */ { zbx_snprintf_alloc(&ival, &ival_alloc, &ival_offset, "%s,", buf); if (NULL != table->uniq && SUCCEED == str_in_list(table->uniq, field->name, ',')) { zbx_snprintf_alloc(&dsql, &dsql_alloc, &dsql_offset, "%s=%s and ", field->name, buf); dnum++; } } break; case ZBX_TYPE_BLOB: if ('\0' == *buf) { if (NODE_CONFIGLOG_OP_UPDATE == op) zbx_strcpy_alloc(&ufld, &ufld_alloc, &ufld_offset, "'',"); else /* NODE_CONFIGLOG_OP_ADD */ zbx_strcpy_alloc(&ival, &ival_alloc, &ival_offset, "'',"); } else { #if defined(HAVE_POSTGRESQL) size_t len; len = zbx_hex2binary(buf); DBbytea_escape((u_char *)buf, len, &tmp, &tmp_alloc); if (NODE_CONFIGLOG_OP_UPDATE == op) zbx_snprintf_alloc(&ufld, &ufld_alloc, &ufld_offset, "'%s',", tmp); else /* NODE_CONFIGLOG_OP_ADD */ zbx_snprintf_alloc(&ival, &ival_alloc, &ival_offset, "'%s',", tmp); #else if (NODE_CONFIGLOG_OP_UPDATE == op) zbx_snprintf_alloc(&ufld, &ufld_alloc, &ufld_offset, "0x%s,", buf); else /* NODE_CONFIGLOG_OP_ADD */ zbx_snprintf_alloc(&ival, &ival_alloc, &ival_offset, "0x%s,", buf); #endif } break; default: /* ZBX_TYPE_TEXT, ZBX_TYPE_CHAR */ zbx_hex2binary(buf); value_esc = DBdyn_escape_string(buf); if (NODE_CONFIGLOG_OP_UPDATE == op) zbx_snprintf_alloc(&ufld, &ufld_alloc, &ufld_offset, "'%s',", value_esc); else /* NODE_CONFIGLOG_OP_ADD */ { zbx_snprintf_alloc(&ival, &ival_alloc, &ival_offset, "'%s',", value_esc); if (NULL != table->uniq && SUCCEED == str_in_list(table->uniq, field->name, ',')) { zbx_snprintf_alloc(&dsql, &dsql_alloc, &dsql_offset, "%s='%s' and ", field->name, value_esc); dnum++; } } zbx_free(value_esc); } } if (dsql_offset != dtmp_offset) { if (dnum != num_param(table->uniq)) { zabbix_log(LOG_LEVEL_DEBUG, "%s(): missing required fields [%s][%s]", __function_name, table->table, table->uniq); dsql_offset = dtmp_offset; goto next; } dsql_offset -= 5; zbx_strcpy_alloc(&dsql, &dsql_alloc, &dsql_offset, ";\n"); } if (0 != ifld_offset) { ifld[--ifld_offset] = '\0'; ival[--ival_offset] = '\0'; zbx_snprintf_alloc(&isql, &isql_alloc, &isql_offset, "insert into %s (%s,%s) values (" ZBX_FS_UI64 ",%s);\n", table->table, table->recid, ifld, recid, ival); } if (0 != ufld_offset) { ufld[--ufld_offset] = '\0'; zbx_snprintf_alloc(&usql, &usql_alloc, &usql_offset, "update %s set %s where %s=" ZBX_FS_UI64 ";\n", table->table, ufld, table->recid, recid); } if (dsql_offset > ZBX_MAX_SQL_SIZE || isql_offset > ZBX_MAX_SQL_SIZE || usql_offset > ZBX_MAX_SQL_SIZE) { DBend_multiple_update(&dsql, &dsql_alloc, &dsql_offset); DBend_multiple_update(&isql, &isql_alloc, &isql_offset); DBend_multiple_update(&usql, &usql_alloc, &usql_offset); if (dsql_offset > 16) DBexecute("%s", dsql); if (isql_offset > 16) DBexecute("%s", isql); if (usql_offset > 16) DBexecute("%s", usql); dsql_offset = 0; isql_offset = 0; usql_offset = 0; #ifdef HAVE_ORACLE DBbegin_multiple_update(&dsql, &dsql_alloc, &dsql_offset); DBbegin_multiple_update(&isql, &isql_alloc, &isql_offset); DBbegin_multiple_update(&usql, &usql_alloc, &usql_offset); #endif } } next: if (lf != NULL) { *lf++ = '\n'; r = lf; } else break; } DBend_multiple_update(&dsql, &dsql_alloc, &dsql_offset); DBend_multiple_update(&isql, &isql_alloc, &isql_offset); DBend_multiple_update(&usql, &usql_alloc, &usql_offset); if (dsql_offset > 16) DBexecute("%s", dsql); if (isql_offset > 16) DBexecute("%s", isql); if (usql_offset > 16) DBexecute("%s", usql); zbx_free(ufld); zbx_free(usql); zbx_free(ival); zbx_free(ifld); zbx_free(isql); zbx_free(dsql); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
void load_oids(void) { char sql[1024]; char *hostname; char *community; char *oid; MYSQL_RES *result; int i; struct host *h; struct oid *o; zbx_snprintf(sql, sizeof(sql), "select h.ip,i.snmp_community,i.snmp_oid from hosts h,items i where i.hostid=h.hostid and i.type=1 and i.status=0 and h.status=0 and h.useip=1"); result=DBselect(sql); for(i=0;i<DBnum_rows(result);i++) { /* printf("[%s] [%s] [%s]\n",DBget_field(result,i,0),DBget_field(result,i,1),DBget_field(result,i,2)); */ hostname = DBget_field(result,i,0); community = DBget_field(result,i,1); oid = DBget_field(result,i,2); h = hosts; while(h->hostname) { if(strcmp(h->hostname,hostname)==0) break; h++; } if(!h->hostname) { h->hostname=strdup(hostname); h->community=strdup(community); h++; h->hostname = NULL; } o = oids; while(o->hostname) { o++; } o->hostname=strdup(hostname); o->Name=strdup(oid); o++; o->hostname = NULL; } h = hosts; i = 0; while(h->hostname) { i++; h++; } printf("Loaded [%d] hosts\n", i); o = oids; i = 0; while(o->hostname) { i++; o++; } printf("Loaded [%d] OIDs\n", i); }