Short version;
Function returns string correctly in executable version of module seems to return integer (or memory pointer?) when the file is a shared object (for the server). Code is below.
Long Version;
This may be a basic C/C++ Coding issue - I'm definitely not an experienced developer. I apologize in advance that my c and c++ coding does not comply with the zabbix programming guidelines.
I am attempting to develop a loadable module to pull some data out of IP Cameras. During testing I had written my testing code in c++ and successfully pulled data using zabbix's external checks scripting.
I've used 'Extern "C"' to make the C++ function that I'll be calling work with C. I've created the shared Library and everything is linked together. When I call the function (the only one I'm testing so far) it should return the name of the device. If I run the code from Main() it works fine and returns the name i.e. "IP-CAM-1". However, if that same code runs via the (key) function than it returns an integer (usually 87 in my case).
If someone could explain why I'm seeing what I'm seeing I would be extremely grateful.
Thanks!
---Snippit of C++ Code (shared object). This is the only function that is directly called by zabbix module. ---
-- Snippit 1 of Zabbix Module ----
This I put in for testing purposes. When it is compiled as an executable and not a shared object this code prints out the desired info (camera Name).
-- Snippit 2 of Zabbix Module ----
The demo module is the base of this, I've just modified it as needed. Comments in the code should explain the issue part (as far as I can tell)
I only know how to test this by creating it as a shared object and loading it into the zabbix server.
Thanks again!
Steve
Function returns string correctly in executable version of module seems to return integer (or memory pointer?) when the file is a shared object (for the server). Code is below.
Long Version;
This may be a basic C/C++ Coding issue - I'm definitely not an experienced developer. I apologize in advance that my c and c++ coding does not comply with the zabbix programming guidelines.
I am attempting to develop a loadable module to pull some data out of IP Cameras. During testing I had written my testing code in c++ and successfully pulled data using zabbix's external checks scripting.
I've used 'Extern "C"' to make the C++ function that I'll be calling work with C. I've created the shared Library and everything is linked together. When I call the function (the only one I'm testing so far) it should return the name of the device. If I run the code from Main() it works fine and returns the name i.e. "IP-CAM-1". However, if that same code runs via the (key) function than it returns an integer (usually 87 in my case).
If someone could explain why I'm seeing what I'm seeing I would be extremely grateful.
Thanks!
---Snippit of C++ Code (shared object). This is the only function that is directly called by zabbix module. ---
Code:
extern "C" const char *VNConnect (const char *ipAddr, const char *OutputOption)
{
IF2SetupPacket packet;
GloDstIP = ipAddr; //Global Var - Destination IP Address
GloOutput = OutputOption; //Global Var - Sets what will be returned (Name, Mac, HW Info, Etc)
IF2PacketSend(&packet); //Function Fills packet data, Creates socket, transmits, etc
return ReturnVal.c_str(); //ReturnVal is a Global Var
}
-- Snippit 1 of Zabbix Module ----
This I put in for testing purposes. When it is compiled as an executable and not a shared object this code prints out the desired info (camera Name).
Code:
int main()
{
extern char *VNConnect(char *IPADDRESS, char *OUTPUT);
const char *VNName = VNConnect("172.17.43.143","Name");
printf(VNName);
return 0;
}
The demo module is the base of this, I've just modified it as needed. Comments in the code should explain the issue part (as far as I can tell)
I only know how to test this by creating it as a shared object and loading it into the zabbix server.
Code:
int zbx_module_Camera_DevName(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char *param;
if (1 != request->nparam)
{
/* set optional error message */
SET_MSG_RESULT(result, strdup("Invalid number of parameters."));
return SYSINFO_RET_FAIL;
}
param = get_rparam(request, 0); //Gets IP Address
extern char *VNConnect(char *IPADDRESS, char *OUTPUT); //Defines External Function
const char *VNName = VNConnect(param,"Name"); //calls external function and returns name (maybe)
zabbix_log(LOG_LEVEL_ERR,"VNSetup - %s - %s",param,"Name"); //First logging line
zabbix_log(LOG_LEVEL_ERR,"VNSetup - Name - %i",VNName); //Log the result - the %i [I]should[/I] be %s, but that causes zabbix server (or at least the logger) to lock up. With %i it always prints 87.
SET_STR_RESULT(result, strdup(param)); //ultimatly param will be replaced with VNName
return SYSINFO_RET_OK;
}
Thanks again!
Steve