Extension of the host DB not saved

General discussion about Plugins for Cacti

Moderators: Developers, Moderators

Post Reply
Rno
Cacti Pro User
Posts: 704
Joined: Wed Dec 07, 2011 9:19 am

Extension of the host DB not saved

Post by Rno »

I have a plugin that make an extension of the host DB with 3 fields (serial_no, type of device, and sysobjID)

At the beginning the value are filed with an automated process, but then sometime I need to change it manually.

But the value are not updated, since the api_device_save take only the standard value, and I have in now way the possibility to get what the user enter into the form, since it's lost during the call of api_device_save

How can I correct this ?
Beside changing this function to make it work with form that has more value than just the default!
Test
Almalinux
php 8.2.14
mariadb 10.6.16
Cacti 1.2.27
Spine 1.2.27
RRD 1.7.2
thold 1.8
monitor 2.5
syslog 3.2
flowview: 3.3
weathermap 1.0 Beta
cigamit
Developer
Posts: 3369
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Extension of the host DB not saved

Post by cigamit »

There is a plugin hook for that, if you want an example, the monitor plugin uses it.

Code: Select all

	api_plugin_register_hook('monitor', 'api_device_save', 'monitor_api_device_save', 'setup.php');
Rno
Cacti Pro User
Posts: 704
Joined: Wed Dec 07, 2011 9:19 am

Re: Extension of the host DB not saved

Post by Rno »

I'm using it, but the call is done with the $save taken from api_device_save, and this one containe only the default value taken either from the DB ofr from the web form, if you extend the DB of a device, the value you add are never send to api_device_save.

As you know it's call from form_save from host.php, and the call include only the default field.

Thold dosen't allow you to add information on a device, on the web form I mean; it only create trigger data based on the device, and store it on another table. But not an editable field on the host form.

And the hook config_setting and config_form allow you to add field to a host form, and then to the DB, but no other explanation to handle it!
Test
Almalinux
php 8.2.14
mariadb 10.6.16
Cacti 1.2.27
Spine 1.2.27
RRD 1.7.2
thold 1.8
monitor 2.5
syslog 3.2
flowview: 3.3
weathermap 1.0 Beta
cigamit
Developer
Posts: 3369
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Extension of the host DB not saved

Post by cigamit »

Can you post your plugins function that is called when using the 'api_device_save' hook. You should be able to get all the data from the POST just like monitor does.
Rno
Cacti Pro User
Posts: 704
Joined: Wed Dec 07, 2011 9:19 am

Re: Extension of the host DB not saved

Post by Rno »

I didn't check monitor, I was checking thold.
But lokking into monitor give me some hint to look into it.

Here is my plugin, but I'm gona look like monitor did it

function extenddb_api_device_new($hostrecord_array) {

$snmpsysobjid = ".1.3.6.1.2.1.1.2.0"; // return Cisco OID type

// don't do it for disabled
if( $hostrecord_array['disabled'] == 'on' ) {
return $hostrecord_array;
}

// host record_array just contain the basic information, need to be pooled for extenddb value
$host_extend_record = db_fetch_row_prepared('SELECT serial_no, type, isPhone, SysObjId
FROM host
WHERE id ='.
$hostrecord_array['id']);

// look for the equipement type
$searchtype = cacti_snmp_get( $hostrecord_array['hostname'], $hostrecord_array['snmp_community'], $snmpsysobjid,
$hostrecord_array['snmp_version'], $hostrecord_array['snmp_username'], $hostrecord_array['snmp_password'],
$hostrecord_array['snmp_auth_protocol'], $hostrecord_array['snmp_priv_passphrase'],
$hostrecord_array['snmp_priv_protocol'], $hostrecord_array['snmp_context'] );

if( strcmp( $searchtype, 'U' ) == 0 ) {
return $hostrecord_array;
}

// find and store device SysObjId
$host_extend_record['SysObjId'] = trim( substr($searchtype, strpos( $searchtype, ':' )+1) );
db_execute("UPDATE host SET SysObjId='".$host_extend_record['SysObjId']. "' WHERE id=" . $hostrecord_array['id'] );

// find and store Serial Number
$host_extend_record['serial_no'] = getSN( $hostrecord_array, $host_extend_record['SysObjId'] );
db_execute("UPDATE host SET serial_no='".$host_extend_record['serial_no']. "' WHERE id=" . $hostrecord_array['id'] );

return $hostrecord_array;
}
Test
Almalinux
php 8.2.14
mariadb 10.6.16
Cacti 1.2.27
Spine 1.2.27
RRD 1.7.2
thold 1.8
monitor 2.5
syslog 3.2
flowview: 3.3
weathermap 1.0 Beta
User avatar
Osiris
Cacti Guru User
Posts: 1424
Joined: Mon Jan 05, 2015 10:10 am

Re: Extension of the host DB not saved

Post by Osiris »

That looks correct, make sure you always return the modified array, or you will break other plugins using it.
Before history, there was a paradise, now dust.
Rno
Cacti Pro User
Posts: 704
Joined: Wed Dec 07, 2011 9:19 am

Re: Extension of the host DB not saved

Post by Rno »

Ok so I change my code, based on Monitor, and it's works well.

I didn't knew that the http data was forwarded to the plugin !

Thanks for the help
Test
Almalinux
php 8.2.14
mariadb 10.6.16
Cacti 1.2.27
Spine 1.2.27
RRD 1.7.2
thold 1.8
monitor 2.5
syslog 3.2
flowview: 3.3
weathermap 1.0 Beta
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests