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

add_graphs.php with multiples --snmp-field and --snmp-value #1306

Closed
gadzet21 opened this issue Feb 5, 2018 · 12 comments
Closed

add_graphs.php with multiples --snmp-field and --snmp-value #1306

gadzet21 opened this issue Feb 5, 2018 · 12 comments
Labels
bug Undesired behaviour CLI CLI related issue

Comments

@gadzet21
Copy link
Contributor

gadzet21 commented Feb 5, 2018

Hello,
I would like to use multiples --snmp-field:

php -q /usr/share/cacti/cli/add_graphs.php --host-id=393 --graph-type=ds --graph-template-id=13 --snmp-query-id=3 --snmp-query-type-id=20 --reindex-method=3 --snmp-field=ifType --snmp-value=128 --snmp-field=ifOperStatus --snmp-value=Up
PHP Warning:  PDO::quote() expects parameter 1 to be string, array given in /usr/share/cacti/lib/database.php on line 1029
PHP Warning:  PDO::quote() expects parameter 1 to be string, array given in /usr/share/cacti/lib/database.php on line 1029
PHP Notice:  Array to string conversion in /usr/share/cacti/cli/add_graphs.php on line 686
PHP Notice:  Array to string conversion in /usr/share/cacti/cli/add_graphs.php on line 689
ERROR: Could not find snmp-field Array (Array) for host-id 393 (172.18.121.10)
Try --host-id=393 --list-snmp-fields

or

php -q /usr/share/cacti/cli/add_graphs.php --host-id=393 --graph-type=ds --graph-template-id=13 --snmp-query-id=3 --snmp-query-type-id=20 --reindex-method=3 --snmp-field=ifDescr --snmp-value-regex=Down --snmp-field=ifOperStatus --snmp-value=Up
PHP Warning:  PDO::quote() expects parameter 1 to be string, array given in /usr/share/cacti/lib/database.php on line 1029
PHP Notice:  Array to string conversion in /usr/share/cacti/cli/add_graphs.php on line 686
ERROR: Could not find snmp-field Array (Up) for host-id 393 (172.18.121.10)
Try --host-id=393 --list-snmp-fields

or

php -q /usr/share/cacti/cli/add_graphs.php --host-id=393 --graph-type=ds --graph-template-id=13 --snmp-query-id=3 --snmp-query-type-id=20 --snmp-field=ifType --snmp-value-regex=128 --snmp-field=ifOperStatus --snmp-value-regex=Up
PHP Warning:  PDO::quote() expects parameter 1 to be string, array given in /usr/share/cacti/lib/database.php on line 1029
PHP Warning:  addslashes() expects parameter 1 to be string, array given in /usr/share/cacti/cli/add_graphs.php on line 608
PHP Notice:  Array to string conversion in /usr/share/cacti/cli/add_graphs.php on line 686
PHP Notice:  Array to string conversion in /usr/share/cacti/cli/add_graphs.php on line 691
ERROR: Could not find snmp-field Array (Array) for host-id 393 (172.18.121.10)
Try --host-id=393 --list-snmp-fields

snmp-fields is fine:

php /usr/share/cacti/cli/add_graphs.php --host-id=393 --list-snmp-fields
Known SNMP Fields for host-id 393: (name)
ifAlias
ifDescr
ifHwAddr
ifIndex
ifName
ifOperStatus
ifSpeed
ifType

Single one --snmp-field is OK:

php -q /usr/share/cacti/cli/add_graphs.php --host-id=393 --graph-type=ds --graph-template-id=13 --snmp-query-id=3 --snmp-query-type-id=20 --snmp-field=ifDescr --snmp-value-regex=Down
Graph Added - graph-id: (1023) - data-source-ids: (1007, 1007)
Graph Added - graph-id: (1024) - data-source-ids: (1008, 1008)
...

Jacek

@netniV
Copy link
Member

netniV commented Feb 5, 2018

Looking at the code, snmp-field, snmp-value-regex and snmp-value are all designed to be multi value

                case 'snmp-field':
                        $dsGraph['snmpField'][] = $value;

                        break;
                case 'snmp-value-regex':
                        $dsGraph['snmpValueRegex'][] = $value;

                        break;
                case 'snmp-value':
                        $dsGraph['snmpValue'][] = $value;

                        break;

The problem here is not that you are trying to add multiples but more that it doesn't return that field when queried. There is a problem with the output of the error though in that it doesn't take into account it's an array. Hence you are seeing 'Array (value)' rather than 'Name (Value)'

What I would do is try and grab a copy of the SQL actually being used at around line 615 of add_graphs.php:

file_put_contents('/tmp/add_graphs.sql',$req,FILE_APPEND);         <-- Add this
$snmp_indexes = db_fetch_assoc($req);

Once you have the SQL in question, copy and paste that into MySQL and you'll find it is returning nothing. Paste it here so I can compare what you input vs what was being looked up. Don't forget to take the above line back out once you're done testing or it'll keep writing to that file.

@cigamit cigamit added the CLI CLI related issue label Feb 6, 2018
@gadzet21
Copy link
Contributor Author

gadzet21 commented Feb 6, 2018

Ok, I used this code.
It looks like:
php -q /usr/share/cacti/cli/add_graphs.php --host-id=393 --graph-type=ds --graph-template-id=13 --snmp-query-id=3 --snmp-query-type-id=20 --snmp-field=ifType --snmp-value-regex=128 --snmp-field=ifOperStatus --snmp-value-regex=Up
Command line log:

PHP Warning:  PDO::quote() expects parameter 1 to be string, array given in /usr/share/cacti/lib/database.php on line 1029
PHP Warning:  addslashes() expects parameter 1 to be string, array given in /usr/share/cacti/cli/add_graphs.php on line 608
PHP Notice:  Array to string conversion in /usr/share/cacti/cli/add_graphs.php on line 686
PHP Notice:  Array to string conversion in /usr/share/cacti/cli/add_graphs.php on line 691
ERROR: Could not find snmp-field Array (Array) for host-id 393 (172.18.121.10)
Try --host-id=393 --list-snmp-fields

In add_graph.sql:

SELECT distinct snmp_index
			FROM host_snmp_cache
			WHERE host_id=393
			AND snmp_query_id=3 AND snmp_index IN (
					SELECT DISTINCT snmp_index FROM host_snmp_cache WHERE host_id=393 AND field_name =  AND field_value LIKE "%%")

I used code in at around line 605 of add_graphs.php
print_r ($dsGraph);
Result:

Array
(
    [snmpFieldSpec] =>
    [snmpQueryId] => 3
    [snmpQueryType] => 20
    [snmpField] => Array
        (
            [0] => Array
                (
                    [0] => ifType
                    [1] => ifOperStatus
                )

        )

    [snmpValue] => Array
        (
        )

    [snmpValueRegex] => Array
        (
            [0] => Array
                (
                    [0] => 128
                    [1] => Up
                )

        )

    [reindex_method] => 1
)

Array in the Array ?

@gadzet21
Copy link
Contributor Author

gadzet21 commented Feb 6, 2018

Is:

		case 'snmp-field':
			$dsGraph['snmpField'][] = $value;

			break;
		case 'snmp-value-regex':
			$dsGraph['snmpValueRegex'][] = $value;

			break;
		case 'snmp-value':
			$dsGraph['snmpValue'][] = $value;

Should:

		case 'snmp-field':
			$dsGraph['snmpField'] = $value;

			break;
		case 'snmp-value-regex':
			$dsGraph['snmpValueRegex'] = $value;

			break;
		case 'snmp-value':
			$dsGraph['snmpValue'] = $value;

@netniV
Copy link
Member

netniV commented Feb 6, 2018

What that original bit of coding should be doing is getting the value $dsGraph['index'] and then [] means to add an element to the array. What I am curious about here, what is the value of $value when it is adding it.

If you change it the way you suggest, that would only have a single value for all instances.

@gadzet21
Copy link
Contributor Author

gadzet21 commented Feb 6, 2018

I've improved the code a bit :
Code between 600-613

		if (sizeof($dsGraph['snmpField'],1) == 1) {
			$req  .= ' AND snmp_index IN (
					SELECT DISTINCT snmp_index FROM host_snmp_cache WHERE host_id=' . $host_id . ' AND field_name = ' . db_qstr($dsGraph['snmpField'][$index_snmp_filter]);

				if (sizeof($dsGraph['snmpValue'])) {
					$req .= ' AND field_value = ' . db_qstr($dsGraph['snmpValue'][$index_snmp_filter]). ')';
				} else {
					$req .= ' AND field_value LIKE "%' . addslashes($dsGraph['snmpValueRegex'][$index_snmp_filter]) . '%")';
				}
			
		} else {
			foreach ($dsGraph['snmpField'][0] as $snmpField) {
				$req  .= ' AND snmp_index IN (
					SELECT DISTINCT snmp_index FROM host_snmp_cache WHERE host_id=' . $host_id . ' AND field_name = ' . db_qstr($snmpField);

				if (sizeof($dsGraph['snmpValue'])) {
					$req .= ' AND field_value = ' . db_qstr($dsGraph['snmpValue'][0][$index_snmp_filter]). ')';
				} else {
					$req .= ' AND field_value LIKE "%' . addslashes($dsGraph['snmpValueRegex'][0][$index_snmp_filter]) . '%")';
				}

				$index_snmp_filter++;
			}
		}

and 686-693:

			if (sizeof($dsGraph['snmpField'],1) == 1) {
				$err_msg = 'ERROR: Could not find snmp-field: ' . implode(",", $dsGraph['snmpField']) . ' (';

				if (sizeof($dsGraph['snmpValue'])) {
					$err_msg .= implode(",", $dsGraph['snmpValue']);
				} else {
					$err_msg .= implode(",", $dsGraph['snmpValueRegex']);
				}
				$err_msg .= ') for host-id ' . $host_id . ' (' . $hosts[$host_id]['hostname'] . ")\n";
			} else {
				$err_msg = 'ERROR: Could not find snmp-field: ' . implode(",", $dsGraph['snmpField'][0]) . ' (';

				if (sizeof($dsGraph['snmpValue'])) {
					$err_msg .= implode(",", $dsGraph['snmpValue'][0]);
				} else {
					$err_msg .= implode(",", $dsGraph['snmpValueRegex'][0]);
				}
				$err_msg .= ') for host-id ' . $host_id . ' (' . $hosts[$host_id]['hostname'] . ")\n";
			}
				echo $err_msg;
				echo 'Try --host-id=' . $host_id . " --list-snmp-fields\n";
				exit(1);

Result:

SELECT distinct snmp_index
			FROM host_snmp_cache
			WHERE host_id=393
			AND snmp_query_id=3 AND snmp_index IN (
					SELECT DISTINCT snmp_index FROM host_snmp_cache WHERE host_id=393 AND field_name = 'ifType' AND field_value LIKE "%128%") AND snmp_index IN (
					SELECT DISTINCT snmp_index FROM host_snmp_cache WHERE host_id=393 AND field_name = 'ifOperStatus' AND field_value LIKE "%Up%")

I know, not a pretty code, but graphs is added to Cacti.

@netniV
Copy link
Member

netniV commented Feb 6, 2018

Can you create a diff between the original and your current version of the code? I can use that to generate a pull request then.

@netniV
Copy link
Member

netniV commented Feb 6, 2018

OK, thanks for your change. However, I think the problem is actually solved by a more simple change. On any field that can be an array, we should check if the parameter is an array and if not, make it an array. Then we remove the [] on the left hand side of the assignment.

The issue is that getopts returns a single value as a singe value and multiple values as an array.

@netniV
Copy link
Member

netniV commented Feb 6, 2018

Can you test the PR I have pushed out to see if it works for you.

@cigamit
Copy link
Member

cigamit commented Feb 7, 2018

I saw a few issues as well, that may be unrelated. I'll merge one of the two and then take a second look.

cigamit pushed a commit that referenced this issue Feb 7, 2018
…#1313)

* Fix issue #1306 where multi-value args are not parsing correctly

* Fix up error reporting of multi-values
@cigamit cigamit added the bug Undesired behaviour label Feb 7, 2018
@gadzet21
Copy link
Contributor Author

gadzet21 commented Feb 7, 2018

I tested - Works. Thanks.

@gadzet21 gadzet21 closed this as completed Feb 7, 2018
@infinitynet2011
Copy link

Hello,

I have this issue when i try to use reg with another snmp-field

php -q /var/www/cacti-1.1.38/cli/add_graphs.php --host-id=9 --graph-type=ds --graph-template-id=2 --snmp-query-id=1 --snmp-query-type-id=6 --snmp-field=ifSpeed --snmp-value-regex='1' --snmp-field=ifOperStatus --snmp-value=Up

ERROR: Could not find one of more snmp-fields ifSpeed,ifOperStatus with values (Up) for host-id 9

BUT

php -q /var/www/cacti-1.1.38/cli/add_graphs.php --host-id=9 --graph-type=ds --graph-template-id=2 --snmp-query-id=1 --snmp-query-type-id=6 --snmp-field=ifSpeed --snmp-value=1000000000 --snmp-field=ifOperStatus --snmp-value=Up

Graph Added - graph-id: (1782) - data-source-ids: (1778, 1778)

--
Thank you,
Gabriel

@infinitynet2011
Copy link

My mistake, both field and value must be regex.

--
Thank you,
Gabriel

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Undesired behaviour CLI CLI related issue
Projects
None yet
Development

No branches or pull requests

4 participants