Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When removing devices via the CLI, undefined variable errors may be seen #3782

Closed
bmfmancini opened this issue Sep 1, 2020 · 9 comments
Closed
Labels
bug Undesired behaviour resolved A fixed issue
Milestone

Comments

@bmfmancini
Copy link
Member

Hey Guys

I came across the following issue

When using the remove_device.php CLI script if you remove a device by name in my case test the script comes back with multiple results and goes on the delete the results see below

php remove_device.php --description=test
PHP Notice: Undefined variable: debug in /var/www/html/cacti/cli/remove_device.php on line 106
id | host | description
276 | IP | test
277 | IP | test10

Removing devices with ids: 276, 277
Success - removed device-ids: 276, 277

Now test and test10 did have the same IP but I wouldn't think that would be a factor since we are doing a check based on the description

I also got the following backtrace in the log

2020-09-01 14:54:27 - CMDPHP PHP ERROR NOTICE Backtrace: (/cli/remove_device.php[106]:CactiErrorHandler())
2020-09-01 14:54:27 - ERROR PHP NOTICE: Undefined variable: debug in file: /var/www/html/cacti/cli/remove_device.php on line: 106

I am going to test this on 1.2.14 but based on the commit history on this script I believe it will be the same result

@bmfmancini bmfmancini added bug Undesired behaviour unverified Some days we don't have a clue labels Sep 1, 2020
@bmfmancini
Copy link
Member Author

bmfmancini commented Sep 1, 2020

ok so in 1.2.14 bit of a different issue

I have these 3 devices

image

When I run the cli script

By description

root@cact1:/var/www/html/cacti/cli# php remove_device.php --description=test1
PHP Notice: Undefined variable: debug in /var/www/html/cacti/cli/remove_device.php on line 106
ERROR: Unable to find host in the database matching desciption (test1)
root@cact1:/var/www/html/cacti/cli#

By ip

root@cact1:/var/www/html/cacti/cli# php remove_device.php --ip=1.1.1.1
PHP Notice: Undefined variable: debug in /var/www/html/cacti/cli/remove_device.php on line 118
id | host | description
23 | 1.1.1.1 | test10

Please use --confirm to remove these devices
root@cact1:/var/www/html/cacti/cli#

MariaDB [cacti]> select description,hostname,id from host where description LIKE 'test%';
+-------------+----------+----+
| description | hostname | id |
+-------------+----------+----+
| test1 | 1.1.1.1 | 21 |
| test2 | 1.1.1.1 | 22 |
| test10 | 1.1.1.1 | 23 |
+-------------+----------+----+
3 rows in set (0.01 sec)

3 rows in set (0.00 sec)

MariaDB [cacti]>

Whats more strange is this result from 1.2.14

root@cact1:/var/www/html/cacti/cli# php remove_device.php --ip=1.1.1.1
id | host | description
23 | 1.1.1.1 | test10

Please use --confirm to remove these devices
root@cact1:/var/www/html/cacti/cli# php remove_device.php --description=test10
ERROR: Unable to find host in the database matching desciption (test10)
root@cact1:/var/www/html/cacti/cli#

@bmfmancini
Copy link
Member Author

Same error occurred as well

2020/09/01 15:08:09 - CMDPHP PHP ERROR NOTICE Backtrace: (/cli/remove_device.php[118]:CactiErrorHandler())
2020/09/01 15:08:09 - ERROR PHP NOTICE: Undefined variable: debug in file: /var/www/html/cacti/cli/remove_device.php on line: 118

@bmfmancini
Copy link
Member Author

bmfmancini commented Sep 1, 2020

No change between 1.2.12 release and 1.2.14

root@cact1:~/cacti-release-1.2.12/cli# diff remove_device.php /var/www/html/cacti/cli/remove_device.php
root@cact1:~/cacti-release-1.2.12/cli#
root@cact1:~/cacti-release-1.2.12/cli# md5sum remove_device.php
73183f208dbdcd65d957560c15c49da0  remove_device.php
root@cact1:~/cacti-release-1.2.12/cli# md5sum /var/www/html/cacti/cli/remove_device.php
73183f208dbdcd65d957560c15c49da0  /var/www/html/cacti/cli/remove_device.php
root@cact1:~/cacti-release-1.2.12/cli#

root@cact1:~/cacti-release-1.2.12/cli# cat /var/www/html/cacti/include/cacti_version
1.2.14
root@cact1:~/cacti-release-1.2.12/cli#

@bmfmancini
Copy link
Member Author

Here is the relevant code

/* process host description */
        if ($description > '') {
                if ($debug) {
                        print "Searching hosts by description...\n";
                }

@bmfmancini
Copy link
Member Author

Need to change

if (cacti_sizeof($parms)) {
        /* setup defaults */
        $description   = '';
        $ip            = '';
        $host_id       = '';
        $quietMode     = false;
        $confirm       = false;
        $quiet         = false;

to

if (cacti_sizeof($parms)) {
        /* setup defaults */
        $description   = '';
        $ip            = '';
        $host_id       = '';
        $debug         = false;
        $quietMode     = false;
        $confirm       = false;
        $quiet         = false;

that fixes the error but not the device search

@bmfmancini
Copy link
Member Author

OK found the issue

If you have a device that is set to template none this script fails

See below
test 2 and test 10 are set to host_template_id 0 which is none
test 1 has now been set to cisco router

MariaDB [cacti]> select hostname,description,host_template_id from host where description LIKE 'test%';
+----------+-------------+------------------+
| hostname | description | host_template_id |
+----------+-------------+------------------+
| 1.1.1.1 | test1 | 1 |
| 1.1.1.1 | test2 | 0 |
| 1.1.1.1 | test10 | 0 |
+----------+-------------+------------------+
3 rows in set (0.00 sec)

MariaDB [cacti]>

See result now

root@cact1:/var/www/html/cacti/cli# php remove_device.php --description=test
id | host | description
21 | 1.1.1.1 | test1

Please use --confirm to remove these devices

After changing all of the devices to have a template

MariaDB [cacti]> select hostname,description,host_template_id from host where description LIKE 'test%';
+----------+-------------+------------------+
| hostname | description | host_template_id |
+----------+-------------+------------------+
| 1.1.1.1 | test1 | 1 |
| 1.1.1.1 | test2 | 1 |
| 1.1.1.1 | test10 | 1 |
+----------+-------------+------------------+
3 rows in set (0.00 sec)

MariaDB [cacti]>

root@cact1:/var/www/html/cacti/cli# php remove_device.php --description=test
id | host | description
21 | 1.1.1.1 | test1
23 | 1.1.1.1 | test10
22 | 1.1.1.1 | test2

Please use --confirm to remove these devices
root@cact1:/var/www/html/cacti/cli#

@bmfmancini
Copy link
Member Author

bmfmancini commented Sep 1, 2020

now it appears that the script is performing alike search instead of a equals to search which in my mind should not be the way because you are entering a specific hostname so cacti should be looking for that specific hostname

@TheWitness
Copy link
Member

I closed the pull request. You opened it on the develop branch and should have on the 1.2.x branch.

netniV added a commit that referenced this issue Oct 6, 2020
@netniV
Copy link
Member

netniV commented Oct 6, 2020

I've applied the debug fix.

@TheWitness TheWitness added resolved A fixed issue and removed unverified Some days we don't have a clue labels Oct 9, 2020
@TheWitness TheWitness added this to the 1.2.15 milestone Oct 9, 2020
@netniV netniV changed the title Cacti V1.2.12 remove_device.php removes multiple devices with similar name When removing devices via the CLI, undefined variable errors may be seen Nov 2, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Feb 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Undesired behaviour resolved A fixed issue
Projects
None yet
Development

No branches or pull requests

3 participants