Skip to content

Instantly share code, notes, and snippets.

@zsprackett
Created March 26, 2014 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsprackett/9786457 to your computer and use it in GitHub Desktop.
Save zsprackett/9786457 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
require(dirname(dirname(__FILE__)) . '/include/bootstrap.php');
require(dirname(dirname(__FILE__)) . '/lib/InstanceInfo.php');
function usage() {
error_log("Usage: $0 -d /var/www/od2/sugarondemand.com -s SQL\n");
error_log(" -e include eval instances");
error_log(" -t include test instances");
exit(1);
}
$config = Array();
$opt_parser = new IonGetopt;
$opts = $opt_parser->fetch_opts('hd:es:t');
if (!$opts || array_key_exists('h', $opts)) usage();
$config['eval'] = array_key_exists('e', $opts);
$config['test'] = array_key_exists('t', $opts);
$config['directory'] = array_key_exists('d', $opts) ? $opts['d'][0] : false;
$config['sql'] = array_key_exists('s', $opts) ? $opts['s'][0] : false;
if ($config['directory'] === false || !is_dir($config['directory'])) {
error_log("Directory not set or not a directory");
usage();
}
if ($config['sql'] == false) {
error_log("Required argument -s was missing");
usage();
}
$header_sent = false;
function callback($instance) {
global $config;
global $header_sent;
if (!$config['test'] && $instance->isTest()) {
return;
}
if (!$config['eval'] && $instance->isEval()) {
return;
}
$db = $instance->getDbConnection();
if (empty($db))
return;
$result = $db->query($config['sql']);
if (!$result) {
return;
}
if ($row = $db->fetch_assoc($result)) {
$keys = array_keys($row);
if (!$header_sent) {
print("instance,flavor,licensed_users,enabled_users");
foreach ($keys as $k) {
print(",$k");
}
print("\n");
$header_sent = true;
}
printf("%s,%s,%d,%d", $instance->getName(), $instance->getFlavor(), $instance->getLicensedUsers(), $instance->getEnabledUsersCount());
foreach ($keys as $k) {
print("," . $row[$k]);
}
print("\n");
}
}
// Open a known directory, and proceed to read its contents
InstanceInfo::foreachInstance($config['directory'], "callback");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment