Help - Search - Members - Calendar
Full Version: How-to: Remove old Urchin Profiles
The Planet Forums > Control Panels > cPanel/WHM > Cpanel/WHM HOWTOs
TheFreak
I posted this on cpanel's forums and I figure I might as well post it here as well:
I got a support ticket the other day asking as to why the client's urchin was not working. I was surprised myself because I knew that the server had a 300 domain/profile urchin license and that this server was only hosting about 100 domains / subdomains.

I found that cPanel was not removing old Urchin profiles when the domain accounts were being removed from the server. Thus, my urchin profile limit of 300 had been reached and I was given the error Unable to get profile id from configuration database when trying to view a new client's urchin stats.

I looked around a few different forums for a script to remove old urchin profiles but came up with nothing. So I created my own. I figure running it once a week or month via cron will do the job I need of removing those old urchin profiles:

[PHP]#!/usr/local/cpanel/3rdparty/bin/php


// path to urchin folder ~ no trailing slash
$urchin_path = "/usr/local/urchin";

// Your access hash key from WHM panel
$accesshash = 'INSERT YOUR ACCESS KEY HASH HERE';




/* Syntax we are using... You can find info on urchin and cpanel sites

List all urchin profiles - /usr/local/urchin/util/uconf-driver action=list table="profile"
Remove urchine profile $name - /usr/local/urchin/util/uconf-driver action=delete table="profile" name="$name"
List all cpanel accounts of $user - $accts = listaccts($host,$user,$accesshash,0);

*/

// cPanel variables
require '/usr/local/cpanel/Cpanel/Accounting.php.inc';
$host = "localhost";
$user = "root";



$accts = listaccts($host,$user,$accesshash,0);

// Cycle through array that is returned. The first of each account array contains domain
foreach($accts as $current_account)
$ALL_ACCOUNT_DOMAINS[] = $current_account[0];


// Get number of records in urchin db
$urchin_num = `{$urchin_path}/util/uconf-driver action=nrecords table="profile"`;

$urchin_command = "{$urchin_path}/util/uconf-driver action=list table=\"profile\" start=0 n=" . trim($urchin_num);

// Contains all return info
$urchin_id_strings = `$urchin_command`;

// Create array based on spaces
foreach(explode(" ", $urchin_id_strings) as $current_value)
{
// Look for format of profile names, store in regs
if (ereg("^(name=)\"([^\"]+)\"", $current_value, $regs))
{
// If current profile is not in cpanel accounts, assume it needs to be removed
if (array_search($regs[2], $ALL_ACCOUNT_DOMAINS) === false)
{
$IDS_TO_REMOVE[] = $regs[2];
}
}
}

if (!empty($IDS_TO_REMOVE))
{
// Yes, I know -- we could have done this command earlier. But lets just place it here
foreach($IDS_TO_REMOVE as $current_profile)
{
// Remove current_profile
$result = `{$urchin_path}/util/uconf-driver action=delete table="profile" name="$current_profile"`;
}
}

// Check new urchin number
$new_urchin_num = `{$urchin_path}/util/uconf-driver action=nrecords table="profile"`;

// Echo out how many records have been removed.
echo "Profiles Removed: " . (trim($urchin_num) - trim($new_urchin_num)) . "\n\n";[/PHP]

The main two variables that need to be edited are your urchin path and your WHM hash access key.

This script will pull the account domains on the server from cpanel, check them against the profiles in urchin, and remove any profiles that are old and do not have any corresponding domain account on the server.

Just place it in a file (something like remove-old-urchin.php).
# chmod 0755 remove-old-urchin.php
and execute: # ./remove-old-urchin.php

I have used this on my servers and it works great. As to why cpanel doesn't remove these in the first place, I do not know.
TheFreak
And then I ran across another problem: How do I add the profiles that were not added because of the urchin license limt?

This script will do that for you:
[PHP]#!/usr/local/cpanel/3rdparty/bin/php


// path to urchin folder ~ no trailing slash
$urchin_path = "/usr/local/urchin";

// Your access hash key from WHM panel
$accesshash = 'INSERT WHM HASH KEY HERE';




/* Syntax we are using... You can find info on urchin and cpanel sites

List all urchin profiles - /usr/local/urchin/util/uconf-driver action=list table="profile"
Remove urchine profile $name - /usr/local/urchin/util/uconf-driver action=delete table="profile" name="$name"
List all cpanel accounts of $user - $accts = listaccts($host,$user,$accesshash,0);

*/

// cPanel variables
require '/usr/local/cpanel/Cpanel/Accounting.php.inc';
$host = "localhost";
$user = "root";



$accts = listaccts($host,$user,$accesshash,0);

// Cycle through array that is returned. The first of each account array contains domain
foreach($accts as $current_account)
$ALL_ACCOUNT_DOMAINS[] = $current_account[0];


// Get number of records in urchin db
$urchin_num = `{$urchin_path}/util/uconf-driver action=nrecords table="profile"`;

$urchin_command = "{$urchin_path}/util/uconf-driver action=list table=\"profile\" start=0 n=" . trim($urchin_num);

// Contains all return info
$urchin_id_strings = `$urchin_command`;

// Create array based on spaces
foreach(explode(" ", $urchin_id_strings) as $current_value)
{
// Look for format of profile names, store in regs
if (ereg("^(name=)\"([^\"]+)\"", $current_value, $regs))
{
$IDS_FROM_URCHIN[] = $regs[2];
}
}

// Cycle through cpanel accounts and check if each exists in urchin
foreach($ALL_ACCOUNT_DOMAINS as $current_domain)
{
if (array_search($current_domain, $IDS_FROM_URCHIN) === false)
{
$logfile_format = "/usr/local/apache/domlogs/" . $current_domain;
$add_profile = `{$urchin_path}/util/uconf-driver action=add table=profile name="$current_domain" ct_name="$current_domain" ct_website="http:\//www.$current_domain" ct_reportdomains="$current_domain,www.$current_domain"`;

$add_logs = `{$urchin_path}/util/uconf-driver action=add table=logfile name="$current_domain" cr_action=2 ct_name="$current_domain" cr_type=local ct_loglocation="$logfile_format" cs_logformat=auto cs_rlist=$add_profile`;

$add_ref = `{$urchin_path}/util/uconf-driver action=set_parameter recnum=$add_profile cs_llist=$add_logs`;
echo "Added " . $current_domain . "\n";
}
}

// Check new urchin number
$new_urchin_num = `{$urchin_path}/util/uconf-driver action=nrecords table="profile"`;

// Echo out how many records have been added.
echo "Profiles Added: " . (trim($new_urchin_num) - trim($urchin_num)) . "\n\n";[/PHP]

It checks the cpanel account against the urchin accounts. If a cpanel account exists that is found not to have an urchin profile, the script creates the profile, the log source, and then links the profile and log source.

I have tested & executed this on my servers as well. It does the job. This one, however, should only need to be run once (after the first time you run the removal script) because cpanel should add profiles for all new domains created as long as you have not reached your urchin license limit.
trepid
QUOTE
Originally posted by TheFreak
......... cpanel should add profiles for all new domains created as long as you have not reached your urchin license limit.

Is there a way *not* to allow cpanel automatically adding a profile for a new domain?
PDM
QUOTE(trepid)
Is there a way *not* to allow cpanel automatically adding a profile for a new domain?


Did you find an answer to this issue?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.