Auto archive segfaults in 1.4

I still have an issue with archive.sh segfaulting on the first run. Is anyone else experiencing this? I’m on Ubuntu Server 10.04.2 LTS with the standard PHP and MySQL packages installed and updated.

I created a PHP based cron script (archive.php) that seems to be working just fine for me. I can’t attach it so you’ll need to copy and paste the following code into your own archive.php file in the piwik/misc/cron directory. If your having the same issues this may help. :slight_smile:

EDIT NOTE - I’ve updated the original file to use Piwik_API_Request class directly. Still no segfault errors occurring with the new script below.

Cheers,
~Dave

Here’s the new script.


<?php
/**
 * PHP based archive cron for Piwik
 * 
 *   To run hourly add the following cron entry:
 *   5 * * * * php /path/to/piwik/misc/cron/archive.php  >/dev/null
 *   
 *   For more information about auto archiving please review the
 *   documentation at http://piwik.org/docs/setup-auto-archiving/
 *   
 * @copyright Dave Lozier - May 1st, 2011 
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 * 
 */

// command line only
if(!isset($_SERVER['argc']))
{
	echo "Command line usage only!\n";
	exit(1);
}

define('PIWIK_INCLUDE_PATH', preg_replace('!^(.*)'.DIRECTORY_SEPARATOR.'misc'.DIRECTORY_SEPARATOR.'cron$!i', '$1', dirname(__FILE__)).DIRECTORY_SEPARATOR);
define('PIWIK_USER_PATH', PIWIK_INCLUDE_PATH);
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);

$ini_file = PIWIK_INCLUDE_PATH.'config'.DIRECTORY_SEPARATOR.'config.ini.php';
if (!file_exists($ini_file))
{
	echo "Piwik configuration file $ini_file not found!\n";
	exit(1);
}
$config = file_get_contents($ini_file);

preg_match('!\[superuser\].*login\s*=\s*"(.*)".*password\s*=\s*"(.*)"!sU', $config, $matches);

$PIWIK_SUPERUSER = trim($matches[1]);
$PIWIK_SUPERUSER_MD5_PASSWORD = trim($matches[2]);
if (empty($PIWIK_SUPERUSER) || empty($PIWIK_SUPERUSER_MD5_PASSWORD))
{
	echo "Could not extract PIWIK_SUPERUSER and/or PIWIK_SUPERUSER_MD5_PASSWORD!\n";
	exit(1);
}

require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";

Piwik_FrontController::getInstance()->init();
$request = new Piwik_API_Request("method=UsersManager.getTokenAuth&userLogin=$PIWIK_SUPERUSER&md5Password=$PIWIK_SUPERUSER_MD5_PASSWORD&format=php&serialize=0" );
$TOKEN_AUTH = trim($request->process());
if (empty($TOKEN_AUTH))
{
	echo "Could not get TOKEN_AUTH, unable to continue.\n";
	exit(1);
}

$request = new Piwik_API_Request("method=SitesManager.getAllSitesId&token_auth=$TOKEN_AUTH&format=php&serialize=1" );
$ID_SITES = unserialize($request->process());
if (!count($ID_SITES))
{
	echo "ID_SITES does not contain any sites to archive!\n";
	exit(1);
}

$request = new Piwik_API_Request("method=CoreAdminHome.getKnownSegmentsToArchive&token_auth=$TOKEN_AUTH&format=php&serialize=1" );
$SEGMENTS_TO_ARCHIVE = unserialize($request->process());

$periods = array('day', 'week', 'month', 'year');
foreach ($ID_SITES as $id_sites_key => $id_sites_value)
{
	$idsite = trim($id_sites_value[0]);
	if (!$idsite || !ctype_digit($idsite))
	{
		echo "Skipping idsite = $idsite (must be a number to process)\n";
		continue;
	}
	
	foreach ($periods as $period)
	{
		echo "\n";
		echo "Archiving period = $period for idsite = $idsite...\n";
		$request = new Piwik_API_Request("method=VisitsSummary.getVisits&idSite=$idsite&period=$period&date=last52&format=xml&token_auth=$TOKEN_AUTH" );
		echo $request->process();
		echo "\n";
		
		foreach ($SEGMENTS_TO_ARCHIVE as $segments_to_archive_key => $segments_to_archive_value)
		{
			$segment = $segments_to_archive_value[0];
			if ($segment != 'value')
			{
				echo "\n";
				echo " - Archiving for visitor segment $segment...\n";
				$request = new Piwik_API_Request("method=VisitsSummary.getVisits&idSite=$idsite&period=$period&date=last52&format=xml&token_auth=$TOKEN_AUTH&segment=$segment" );
				echo $request->process();
				echo "\n";
			}
		}
	}
	echo "Archiving for idsite = $idsite done!\n";
}

echo "Reports archiving finished.\n";
echo "---------------------------\n";
echo "Starting Scheduled tasks...\n";
echo "\n";

$request = new Piwik_API_Request("method=CoreAdminHome.runScheduledTasks&format=xml&token_auth=$TOKEN_AUTH" );
echo $request->process();
echo "\n";

echo "\n";
echo "Finished Scheduled tasks.\n";
echo "\n";

Cheers,
~Dave