PHP did not return expected result / File not found

Post general support questions here that do not specifically fall into the Linux or Windows categories.

Moderators: Moderators, Developers

Author
Message
Comenius
Posts: 15
Joined: Sun Jul 24, 2016 11:28 am

Re: PHP did not return expected result / File not found

#16 Post by Comenius » Sun Jan 13, 2019 12:06 pm

I just ran the modified cli_test.php from the command line and it's generating an error:

Code: Select all

$ /usr/local/bin/php -q /home/blah/public_html/cacti/install/cli_test.php 7
PHP Parse error:  syntax error, unexpected '{' in /home/blah/public_html/cacti/install/cli_test.php on line 29
The unmodified cli_test.php still runs without error.

Comenius
Posts: 15
Joined: Sun Jul 24, 2016 11:28 am

Re: PHP did not return expected result / File not found

#17 Post by Comenius » Sun Jan 13, 2019 12:12 pm

I corrected the syntax errors and now get the following result:

Code: Select all

Input: 37
Output: 
ERROR 2 - This script is only meant to run at the command line.
Status: Invalid
Command: /usr/local/bin/php -q /home/blah/public_html/cacti/install/cli_test.php 37
Updated cli_test.php, which now runs without error from the command line:

Code: Select all

#!/usr/bin/php -q
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2018 The Cacti Group                                 |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | Cacti: The Complete RRDtool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
*/

/* do NOT run this script through a web browser */
if (!isset($_SERVER['argv'][0])) {
        die('<br><strong>ERROR 1 - This script is only meant to run at the command line.</strong>');
} elseif (isset($_SERVER['REQUEST_METHOD'])) {
        die('<br><strong>ERROR 2 - This script is only meant to run at the command line.</strong>');
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
        die('<br><strong>ERROR 3 - This script is only meant to run at the command line.</strong>');
}

if ($argv !== false && sizeof($argv)) {
	$value = intval($argv[1]);
	print $value * $value;
}

Comenius
Posts: 15
Joined: Sun Jul 24, 2016 11:28 am

Re: PHP did not return expected result / File not found

#18 Post by Comenius » Sun Jan 13, 2019 12:18 pm

Just out of curiosity, I reversed the #2 and #3 tests and the script is also dying at #3:

Code: Select all

Input: 60
Output: 
ERROR 3 - This script is only meant to run at the command line.
Status: Invalid
Command: /usr/local/bin/php -q /home/blah/public_html/cacti/install/cli_test.php 60

Comenius
Posts: 15
Joined: Sun Jul 24, 2016 11:28 am

Re: PHP did not return expected result / File not found

#19 Post by Comenius » Sun Jan 13, 2019 12:31 pm

If I delete all 3 of those tests, the file executes successfully:

Code: Select all

Input: 58
Output: 3364
Status: Valid
Command: /usr/local/bin/php -q /home/blah/public_html/cacti/install/cli_test.php 58
Updated, working cli_test.php file:

Code: Select all

#!/usr/bin/php -q
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2018 The Cacti Group                                 |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | Cacti: The Complete RRDtool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
*/

/* do NOT run this script through a web browser */

if ($argv !== false && sizeof($argv)) {
	$value = intval($argv[1]);
	print $value * $value;
}
With those tests removed, the PHP check no longer fails and I can proceed with the upgrade installation.

netniV
Cacti Guru User
Posts: 2680
Joined: Sun Aug 27, 2017 12:05 am

Re: PHP did not return expected result / File not found

#20 Post by netniV » Sun Jan 13, 2019 9:09 pm

OK that is strange, you are running a new instance of php with the -q option so you should not have request headers.

netniV
Cacti Guru User
Posts: 2680
Joined: Sun Aug 27, 2017 12:05 am

Re: PHP did not return expected result / File not found

#21 Post by netniV » Mon Jan 14, 2019 6:20 am

Can you update the cli_test.php to the following so I can see what's occurring:

Code: Select all

/* do NOT run this script through a web browser */
if (php_sapi_name() != 'cli') {
        die('<br><strong>This script is only meant to run at the command line.</strong>');
}

if ($argv !== false && sizeof($argv)) {
        $value = intval($argv[1]);
        print $value * $value . ' - ' . php_sapi_name();
}

print "\n";
print htmlspecialchars(var_export($_SERVER, true));
Because this will contain sensitive info and I need to see everything, please email a copy of that content to [email protected]

I think this is boiling down to, when is PHP CLI not PHP CLI? Also, can you remind me what OS and PHP version you are on?

Comenius
Posts: 15
Joined: Sun Jul 24, 2016 11:28 am

Re: PHP did not return expected result / File not found

#22 Post by Comenius » Mon Jan 14, 2019 11:37 am

I just ran the modified script and emailed you the results.

My setup is as follows:

Code: Select all

Apache version: Apache/2.4.37
PHP version: 5.6.39
MySQL version: 10.1.37-MariaDB
Distro Name: CentOS Linux release 7.6.1810 (Core)
Kernel Version: 3.10.0-957.1.3.el7.x86_64
Also, for what it's worth, I seem to be stuck in a loop during the final installation process. The following lines keep getting repeated every few seconds in the cacti.log:

Code: Select all

2019/01/13 09:56:18 - INSTALL-JSON: notice: Start: {"Step":"97","Eula":"1"}
2019/01/13 09:56:18 - INSTALL-STEP: notice: Install Parameters: array ( 'Runtime' => 'Web', 'Step' => '97', 'Eula' => '1', )
2019/01/13 09:56:18 - INSTALL-STEP: notice: Initial: '97'
2019/01/13 09:56:18 - INSTALL-STEP: notice: Previously complete: NULL
2019/01/13 09:56:18 - INSTALL-STEP: notice: After: '97'
2019/01/13 09:56:18 - INSTALL-STEP: debug: setStep(): '97'
2019/01/13 09:56:18 - INSTALL-STEP: notice: setStep(): stepError array ( ) < '97'
2019/01/13 09:56:18 - INSTALL-LANGUAGE: info: getLanguage(): en-US
2019/01/13 09:56:18 - INSTALL-LANGUAGE: debug: setLanguage(): en-US
2019/01/13 09:56:18 - INSTALL-THEME: info: getTheme(): modern
2019/01/13 09:56:18 - INSTALL-THEME: info: Checking theme: modern
2019/01/13 09:56:18 - INSTALL-THEME: debug: setTheme(): modern
2019/01/13 09:56:18 - INSTALL-STEP: debug: Error: false
2019/01/13 09:56:18 - INSTALL-STEP: debug: Done: '97'
2019/01/13 09:56:18 - INSTALL-BACKGROUND: debug: backgroundTime = 1547401153.5883
2019/01/13 09:56:18 - INSTALL-BACKGROUND: debug: backgroundNeeded =
2019/01/13 09:56:18 - INSTALL-BACKGROUND: debug: ---------------- Check Expire ----------------
2019/01/13 09:56:18 - INSTALL-BACKGROUND: debug: backgroundDateStarted = 2019-01-13 17:39:13
2019/01/13 09:56:18 - INSTALL-BACKGROUND: debug: backgroundLast = 1547401153.5883
2019/01/13 09:56:18 - INSTALL-BACKGROUND: notice: backgroundLast = 1547401153.5883 (Replaced)
2019/01/13 09:56:18 - INSTALL-BACKGROUND: debug: backgroundExpire = 1547400678
2019/01/13 09:56:19 - INSTALL-JSON: notice:   End: {"Mode":null,"Step":"97","Errors":[],"Eula":"1","Prev":{"Text":"Previous","Step":96,"Enabled":true,"Visible":false},"Next":{"Text":"Next","Step":98,"Enabled":true,"Visible":false},"Test":{"Text":"Test Connection","Step":-4,"Enabled":true,"Visible":false},"Theme":"modern","StepData":{"Current":0,"Total":100}}
The final install screen shows the progress bar heart beat ticking on the left but the completion status indicator on the right never goes past zero percent.
Last edited by Comenius on Mon Jan 14, 2019 12:38 pm, edited 1 time in total.

netniV
Cacti Guru User
Posts: 2680
Joined: Sun Aug 27, 2017 12:05 am

Re: PHP did not return expected result / File not found

#23 Post by netniV » Mon Jan 14, 2019 12:37 pm

So it would appear that your version of PHP is doing something crazy.... It's populating a CLI program with environment variables which should only be applicable to the web.  We check for web-only keys such as REMOTE_ADDR and REQUEST_METHOD but these are populated in your case.

The good news is that the "cli" response to the php_sapi_name() function is likely to help with your scenario and is something we are looking at for a fix but it needs to be tested on multiple platforms since it could be quite a breaking change.

Later tonight, if I get a change, I'll post the fix that I intend to you so you can try it out.  It may be adapted in the future if we find it causes a conflict with other users but it should be enough to get your past the post.

Comenius
Posts: 15
Joined: Sun Jul 24, 2016 11:28 am

Re: PHP did not return expected result / File not found

#24 Post by Comenius » Mon Jan 14, 2019 12:47 pm

OK, cool, sounds good. I'll wait for a code update from you that will hopefully get me past the looping final step of the installation / upgrade. Fingers crossed. :)

Really appreciate all your help!

netniV
Cacti Guru User
Posts: 2680
Joined: Sun Aug 27, 2017 12:05 am

Re: PHP did not return expected result / File not found

#25 Post by netniV » Tue Jan 15, 2019 4:32 am

Can you do me a favour and open a GitHub issue for this so I can track it in the CHANGELOG?


netniV
Cacti Guru User
Posts: 2680
Joined: Sun Aug 27, 2017 12:05 am

Re: PHP did not return expected result / File not found

#27 Post by netniV » Tue Jan 15, 2019 9:15 am

Thanks, I'll be posting any further updates there. I have committed a fix because we needed to have it commited to test across platforms. There are a number of issues outstanding that will be corrected by the 1.2.1 patch which will need to happen as soon as possible things like this.

User avatar
Osiris
Cacti Pro User
Posts: 835
Joined: Mon Jan 05, 2015 10:10 am

Re: PHP did not return expected result / File not found

#28 Post by Osiris » Tue Jan 15, 2019 10:15 pm

Please ensure mod_security is not installed too. Let us know if you find it installed. It would definitely block the install from completing.
Before history, there was a paradise, now dust.

rick89
Posts: 4
Joined: Sat Aug 03, 2019 4:41 am

Re: PHP did not return expected result / File not found

#29 Post by rick89 » Sat Aug 10, 2019 7:25 am

Sorry to post to an old thread, but its the only forum post I found with this issue.

I am on 1.1.0 and trying to upgrade to 1.2.0 and I'm getting 'php did not return the expected result' on step 5 of the upgrade.
I am ultimately trying to upgrade to 1.2.5..

I also tried upgrading to 1.2.1 1.24 and 1.25 and they give a 200 ok response to last action was unexpected on the first step, which I guess is part of the code change to try and resolve the issue I get in 1.2.0.

which php returns /usr/local/bin/php

the installer had /usr/bin/php in there but both generate the same error.

I'm not sure where to go from here?

ModSec isn't in use. When I upgraded from 0.8.8 to 1.1.0 the server passed all the requirements checks (I did this all on the same day, as I originally tried to go from 0.8.8 to 1.2.5 and hit issues so figured I'd try a stepped upgrade in case I was missing any db changes etc because of the big difference in version numbers).

I can upgrade to 1.1.9 but then get these errors in cacti.log:
20190810 14:10:02 - CMDPHP PHP ERROR WARNING Backtrace: (/cmd.php: 62 include)(/include/global.php: 262 session_start)(CactiErrorHandler)(/lib/functions.php: 4359 cacti_debug_backtrace)
20190810 14:10:02 - ERROR PHP : session_start(): Cannot send session cookie - headers already sent in file: /home/cacti/public_html/include/global.php on line: 262
2019/08/10 14:10:01 - SYSTEM STATS: Time:299.5994 Method:cmd.php Processes:1 Threads:N/A Hosts:1 HostsPerProcess:1 DataSources:26 RRDsProcessed:0
2019/08/10 14:10:01 - POLLER: Poller[1] Maximum runtime of 298 seconds exceeded. Exiting.

netniV
Cacti Guru User
Posts: 2680
Joined: Sun Aug 27, 2017 12:05 am

Re: PHP did not return expected result / File not found

#30 Post by netniV » Mon Aug 12, 2019 1:00 pm

Basically, that error means that PHP didn't run the correct file to produce the correct result without any errors or warnings. It basically runs a file called either cli_test.php or cli_check.php (I forget which) that should return nothing but a single number which is the square of the number passed in.

Post Reply