zabbix-agent2/src/libs/zbxdbupgrade/dbupgrade_4050.c
/******************************************************************************
 *                                                                            *
 * Function: DBpatch_if_load_data                                             *
 *                                                                            *
 * Purpose: loading all unlinked interfaces, snmp data and hostid of host     *
 *          prototype for discovered hosts                                    *
 *                                                                            *
 * Parameters: new_ifs      - [OUT] new interfaces to be created on master    *
 *                                  hosts                                     *
 *             snmp_new_ifs - [OUT] snmp data associated with new interfaces  *
 *             if_links     - [OUT] set of pairs for discovered host          *
 *                                  interfaceid and parent interfaceid of     *
 *                                  parent host                               *
 *                                                                            *
 * Comments: When host is created by lld the parent host interfaces are       *
 *           copied over to the discovered hosts. Previous patch could have   *
 *           created new SNMP interfaces on discovered hosts, which must be   *
 *           linked to the corresponding interfaces (created if necessary) to *
 *           the parent host.                                                 *
 *                                                                            *
 ******************************************************************************/
DBpatch_if_load_data

Purpose: loading all unlinked interfaces, snmp data and hostid of host
prototype for discovered hosts

Parameters: new_ifs      - [OUT] new interfaces to be created on master
hosts
snmp_new_ifs - [OUT] snmp data associated with new interfaces
if_links     - [OUT] set of pairs for discovered host
interfaceid and parent interfaceid of
parent host

Comments: When host is created by lld the parent host interfaces are
copied over to the discovered hosts. Previous patch could have
created new SNMP interfaces on discovered hosts, which must be
linked to the corresponding interfaces (created if necessary) to
the parent host.
DBpatch_if_load_data Purpose : loading all unlinked interfaces snmp data and hostid of host prototype for discovered hosts Parameters : new_ifs - [ OUT ] new interfaces to be created on master hosts snmp_new_ifs - [ OUT ] snmp data associated with new interfaces if_links - [ OUT ] set of pairs for discovered host interfaceid and parent interfaceid of parent host Comments : When host is created by lld the parent host interfaces are copied over to the discovered hosts . Previous patch could have created new SNMP interfaces on discovered hosts which must be linked to the corresponding interfaces ( created if necessary ) to the parent host .
static void	DBpatch_if_load_data(zbx_vector_dbu_interface_t *new_ifs, zbx_vector_dbu_snmp_if_t *snmp_new_ifs,
		zbx_vector_uint64_pair_t *if_links)
{
	DB_RESULT	result;
	DB_ROW		row;
	result = DBselect(
			"select hreal.hostid,"
				"i.interfaceid,"
				"i.main,"
				"i.type,"
				"i.useip,"
				"i.ip,"
				"i.dns,"
				"i.port,"
				"s.version,"
				"s.bulk,"
				"s.community,"
				"s.securityname,"
				"s.securitylevel,"
				"s.authpassphrase,"
				"s.privpassphrase,"
				"s.authprotocol,"
				"s.privprotocol,"
				"s.contextname"
			" from interface i"
			" left join interface_discovery d on i.interfaceid=d.interfaceid"
			" join interface_snmp s on i.interfaceid=s.interfaceid"
			" join hosts hdisc on i.hostid=hdisc.hostid"
			" join host_discovery hd on hdisc.hostid=hd.hostid"
			" join hosts hproto on hd.parent_hostid=hproto.hostid"
			" join host_discovery hdd on hd.parent_hostid=hdd.hostid"
			" join items drule on drule.itemid=hdd.parent_itemid"
			" join hosts hreal on drule.hostid=hreal.hostid"
			" where"
				" i.type=2 and"
				" hdisc.flags=4 and"
				" drule.flags=1 and"
				" hproto.flags=2 and"
				" hreal.status in (1,0) and"
				" d.interfaceid is null"
			" order by drule.hostid asc, i.interfaceid asc");
	while (NULL != (row = DBfetch(result)))
	{
		dbu_interface_t		interface;
		dbu_snmp_if_t		snmp;
		zbx_uint64_t		if_parentid;
		ZBX_DBROW2UINT64(interface.hostid, row[0]);
		ZBX_DBROW2UINT64(interface.interfaceid , row[1]);
		ZBX_STR2UCHAR(interface.main, row[2]);
		ZBX_STR2UCHAR(interface.type, row[3]);
		ZBX_STR2UCHAR(interface.useip, row[4]);
		interface.ip = zbx_strdup(NULL, row[5]);
		interface.dns = zbx_strdup(NULL, row[6]);
		interface.port = zbx_strdup(NULL, row[7]);
		ZBX_STR2UCHAR(snmp.version, row[8]);
		ZBX_STR2UCHAR(snmp.bulk, row[9]);
		snmp.community = zbx_strdup(NULL, row[10]);
		snmp.securityname = zbx_strdup(NULL, row[11]);
		ZBX_STR2UCHAR(snmp.securitylevel, row[12]);
		snmp.authpassphrase = zbx_strdup(NULL, row[13]);
		snmp.privpassphrase = zbx_strdup(NULL, row[14]);
		ZBX_STR2UCHAR(snmp.authprotocol, row[15]);
		ZBX_STR2UCHAR(snmp.privprotocol, row[16]);
		snmp.contextname = zbx_strdup(NULL, row[17]);
		snmp.item_port = NULL;
		snmp.skip = 0;
		snmp.item_interfaceid = 0;
		if (0 < new_ifs->values_num &&
				0 != (if_parentid = db_if_find(&interface, &snmp, new_ifs, snmp_new_ifs)))
		{
			db_if_link(interface.interfaceid, if_parentid, if_links);
			db_snmpinterface_free(snmp);
			db_interface_free(interface);
			continue;
		}
		if_parentid = DBget_maxid("interface");
		db_if_link(interface.interfaceid, if_parentid, if_links);
		interface.interfaceid = if_parentid;
		snmp.interfaceid = if_parentid;
		zbx_vector_dbu_interface_append(new_ifs, interface);
		zbx_vector_dbu_snmp_if_append(snmp_new_ifs, snmp);
	}
	DBfree_result(result);
}
static void DBpatch_if_load_data ( zbx_vector_dbu_interface_t * new_ifs , zbx_vector_dbu_snmp_if_t * snmp_new_ifs , zbx_vector_uint64_pair_t * if_links ) { DB_RESULT result ; DB_ROW row ; result = DBselect ( " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ) ; while ( NULL != ( row = DBfetch ( result ) ) ) { dbu_interface_t interface ; dbu_snmp_if_t snmp ; zbx_uint64_t if_parentid ; ZBX_DBROW2UINT64 ( interface . hostid , row [ 0 ] ) ; ZBX_DBROW2UINT64 ( interface . interfaceid , row [ 1 ] ) ; ZBX_STR2UCHAR ( interface . main , row [ 2 ] ) ; ZBX_STR2UCHAR ( interface . type , row [ 3 ] ) ; ZBX_STR2UCHAR ( interface . useip , row [ 4 ] ) ; interface . ip = zbx_strdup ( NULL , row [ 5 ] ) ; interface . dns = zbx_strdup ( NULL , row [ 6 ] ) ; interface . port = zbx_strdup ( NULL , row [ 7 ] ) ; ZBX_STR2UCHAR ( snmp . version , row [ 8 ] ) ; ZBX_STR2UCHAR ( snmp . bulk , row [ 9 ] ) ; snmp . community = zbx_strdup ( NULL , row [ 10 ] ) ; snmp . securityname = zbx_strdup ( NULL , row [ 11 ] ) ; ZBX_STR2UCHAR ( snmp . securitylevel , row [ 12 ] ) ; snmp . authpassphrase = zbx_strdup ( NULL , row [ 13 ] ) ; snmp . privpassphrase = zbx_strdup ( NULL , row [ 14 ] ) ; ZBX_STR2UCHAR ( snmp . authprotocol , row [ 15 ] ) ; ZBX_STR2UCHAR ( snmp . privprotocol , row [ 16 ] ) ; snmp . contextname = zbx_strdup ( NULL , row [ 17 ] ) ; snmp . item_port = NULL ; snmp . skip = 0 ; snmp . item_interfaceid = 0 ; if ( 0 < new_ifs -> values_num && 0 != ( if_parentid = db_if_find ( & interface , & snmp , new_ifs , snmp_new_ifs ) ) ) { db_if_link ( interface . interfaceid , if_parentid , if_links ) ; db_snmpinterface_free ( snmp ) ; db_interface_free ( interface ) ; continue ; } if_parentid = DBget_maxid ( " " ) ; db_if_link ( interface . interfaceid , if_parentid , if_links ) ; interface . interfaceid = if_parentid ; snmp . interfaceid = if_parentid ; zbx_vector_dbu_interface_append ( new_ifs , interface ) ; zbx_vector_dbu_snmp_if_append ( snmp_new_ifs , snmp ) ; } DBfree_result ( result ) ; }
Function: DBpatch_if_load_data                                             
                                                                            
 Purpose: loading all unlinked interfaces, snmp data and hostid of host     
          prototype for discovered hosts                                    
                                                                            
 Parameters: new_ifs      - [OUT] new interfaces to be created on master    
                                  hosts                                     
             snmp_new_ifs - [OUT] snmp data associated with new interfaces  
             if_links     - [OUT] set of pairs for discovered host          
                                  interfaceid and parent interfaceid of     
                                  parent host                               
                                                                            
 Comments: When host is created by lld the parent host interfaces are       
           copied over to the discovered hosts.
Function : DBpatch_if_load_data Purpose : loading all unlinked interfaces snmp data and hostid of host prototype for discovered hosts Parameters : new_ifs - [ OUT ] new interfaces to be created on master hosts snmp_new_ifs - [ OUT ] snmp data associated with new interfaces if_links - [ OUT ] set of pairs for discovered host interfaceid and parent interfaceid of parent host Comments : When host is created by lld the parent host interfaces are copied over to the discovered hosts .