Exemplo n.º 1
0
function main()
{
    // Check Env
    CommandLineUtils::checkRequiredExtentions();
    // Options
    $helpOpt = new Option(null, 'help');
    $helpOpt->setDescription('Print usage information.');
    $zookeeper_connectOpt = new Option(null, 'zookeeper_connect', Getopt::REQUIRED_ARGUMENT);
    $zookeeper_connectOpt->setDescription('REQUIRED: 
                          The connection string for the zookeeper connection in the form
                          "host1:port1,host2:port2,host3:port3/chroot/path", The "/chroot/path" of connection string 
                          *MUST* be the same as of the kafka server. Multiple URLS can be given to allow fail-over.');
    $createOpt = new Option(null, 'create');
    $createOpt->setDescription('Create a new config.');
    $deleteOpt = new Option(null, 'delete');
    $deleteOpt->setDescription('Delete a config');
    $listOpt = new Option(null, 'list');
    $listOpt->setDescription('List all available configs.');
    $monitorOpt = new Option(null, 'monitor');
    $monitorOpt->setDescription('Monitor all available configs.');
    $logkafka_idOpt = new Option(null, 'logkafka_id', Getopt::REQUIRED_ARGUMENT);
    $logkafka_idOpt->setDescription('The logkafka_id which holds log files');
    $logkafka_idOpt->setValidation(function ($value) {
        $ret = AdminUtils::isLogkafkaIdValid($value);
        return $ret['valid'];
    });
    $log_pathOpt = new Option(null, 'log_path', Getopt::REQUIRED_ARGUMENT);
    $log_pathOpt->setDescription('The log file path, like "/usr/local/apache2/logs/access_log.%Y%m%d"');
    $log_pathOpt->setValidation(function ($value) {
        $ret = AdminUtils::isFilePathValid($value);
        return $ret['valid'];
    });
    $topicOpt = new Option(null, 'topic', Getopt::REQUIRED_ARGUMENT);
    $topicOpt->setDescription('The topic of messages to be sent.');
    $topicOpt->setValidation(function ($value) {
        return AdminUtils::isTopicValid($value);
    });
    $partitionOpt = new Option(null, 'partition', Getopt::REQUIRED_ARGUMENT);
    $partitionOpt->setDescription('The partition of messages to be sent.' . '-1 : random' . 'n(>=0): partition n');
    $partitionOpt->setDefaultValue('-1');
    $partitionOpt->setValidation(function ($value) {
        return is_numeric($value);
    });
    $keyOpt = new Option(null, 'key', Getopt::REQUIRED_ARGUMENT);
    $keyOpt->setDescription('The key of messages to be sent.');
    $keyOpt->setDefaultValue('');
    $keyOpt->setValidation(function ($value) {
        return is_string($value);
    });
    $requiredAcksOpt = new Option(null, 'required_acks', Getopt::REQUIRED_ARGUMENT);
    $requiredAcksOpt->setDescription('Required ack number');
    $requiredAcksOpt->setDefaultValue('1');
    $requiredAcksOpt->setValidation(function ($value) {
        return is_numeric($value);
    });
    $compression_codecOpt = new Option(null, 'compression_codec', Getopt::REQUIRED_ARGUMENT);
    $compression_codecOpt->setDescription("Optional compression method of messages: " . implode(", ", AdminUtils::$COMPRESSION_CODECS));
    $compression_codecOpt->setDefaultValue('none');
    $compression_codecOpt->setValidation(function ($value) {
        return AdminUtils::isCompressionCodecValid($value);
    });
    $batchsizeOpt = new Option(null, 'batchsize', Getopt::REQUIRED_ARGUMENT);
    $batchsizeOpt->setDescription('The batch size of messages to be sent');
    $batchsizeOpt->setDefaultValue('1000');
    $batchsizeOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value > 0;
    });
    $follow_lastOpt = new Option(null, 'follow_last', Getopt::REQUIRED_ARGUMENT);
    $follow_lastOpt->setDescription('If set to "false", when restarting logkafka process, 
                          the log_path formatted with current time will be collect;
                          If set to "true", when restarting logkafka process, the last 
                          collecting file will be collected continually');
    $follow_lastOpt->setDefaultValue('true');
    $follow_lastOpt->setValidation(function ($value) {
        return in_array($value, array('true', 'false'));
    });
    $message_timeout_msOpt = new Option(null, 'message_timeout_ms', Getopt::REQUIRED_ARGUMENT);
    $message_timeout_msOpt->setDescription('Local message timeout. This value is only enforced locally 
                          and limits the time a produced message waits for successful delivery. 
                          A time of 0 is infinite.');
    $message_timeout_msOpt->setDefaultValue('0');
    $message_timeout_msOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value >= 0;
    });
    $regex_filter_patternOpt = new Option(null, 'regex_filter_pattern', Getopt::REQUIRED_ARGUMENT);
    $regex_filter_patternOpt->setDescription("Optional regex filter pattern, the messages matching this pattern will be dropped");
    $regex_filter_patternOpt->setDefaultValue('');
    $regex_filter_patternOpt->setValidation(function ($value) {
        return AdminUtils::isRegexFilterPatternValid($value);
    });
    $lagging_max_bytesOpt = new Option(null, 'lagging_max_bytes', Getopt::REQUIRED_ARGUMENT);
    $lagging_max_bytesOpt->setDescription("log lagging max bytes, the monitor will alarm according to this setting");
    $lagging_max_bytesOpt->setDefaultValue('');
    $lagging_max_bytesOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value >= 0;
    });
    $rotate_lagging_max_secOpt = new Option(null, 'rotate_lagging_max_sec', Getopt::REQUIRED_ARGUMENT);
    $rotate_lagging_max_secOpt->setDescription("log rotatiion lagging max seconds, the monitor will alarm according to this setting");
    $rotate_lagging_max_secOpt->setDefaultValue('');
    $rotate_lagging_max_secOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value >= 0;
    });
    $monitorNameOpt = new Option(null, 'monitor_name', Getopt::REQUIRED_ARGUMENT);
    $monitorNameOpt->setDescription("the monitor name");
    $monitorNameOpt->setDefaultValue('');
    $monitorNameOpt->setValidation(function ($value) {
        return AdminUtils::isMonitorNameValid($value);
    });
    $monitor_interval_msOpt = new Option(null, 'monitor_interval_ms', Getopt::REQUIRED_ARGUMENT);
    $monitor_interval_msOpt->setDescription("the monitor checking interval");
    $monitor_interval_msOpt->setDefaultValue('');
    $monitor_interval_msOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value >= 1000;
    });
    $monitor_max_countOpt = new Option(null, 'monitor_max_count', Getopt::REQUIRED_ARGUMENT);
    $monitor_max_countOpt->setDescription("the monitor checking max count, if setting it to 0, the monitor will keep checking");
    $monitor_max_countOpt->setDefaultValue('');
    $monitor_max_countOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value >= 0;
    });
    $validOpt = new Option(null, 'valid', Getopt::REQUIRED_ARGUMENT);
    $validOpt->setDescription('Enable now or not');
    $validOpt->setDefaultValue('true');
    $validOpt->setValidation(function ($value) {
        return in_array($value, array('true', 'false'));
    });
    $parser = new Getopt(array($zookeeper_connectOpt, $createOpt, $deleteOpt, $listOpt, $monitorOpt, $logkafka_idOpt, $log_pathOpt, $topicOpt, $partitionOpt, $keyOpt, $requiredAcksOpt, $compression_codecOpt, $batchsizeOpt, $follow_lastOpt, $message_timeout_msOpt, $regex_filter_patternOpt, $lagging_max_bytesOpt, $rotate_lagging_max_secOpt, $monitorNameOpt, $monitor_interval_msOpt, $monitor_max_countOpt, $validOpt));
    try {
        $parser->parse();
        // Error handling and --help functionality omitted for brevity
        $createOptVal = 0;
        $deleteOptVal = 0;
        $listOptVal = 0;
        $monitorOptVal = 0;
        if ($parser["create"]) {
            $createOptVal = 1;
        }
        if ($parser["delete"]) {
            $deleteOptVal = 1;
        }
        if ($parser["list"]) {
            $listOptVal = 1;
        }
        if ($parser["monitor"]) {
            $monitorOptVal = 1;
        }
    } catch (UnexpectedValueException $e) {
        echo "Error: " . $e->getMessage() . "\n";
        echo $parser->getHelpText();
        exit(1);
    }
    // actions
    $actions = $createOptVal + $deleteOptVal + $listOptVal + $monitorOptVal;
    if ($actions != 1) {
        CommandLineUtils::printUsageAndDie($parser, "Command must include exactly one action: --create, --delete, --list or --monitor");
    }
    // check args
    checkArgs($parser);
    // create admin utils
    $adminUtils = new AdminUtils($parser['zookeeper_connect']);
    $adminUtils->init();
    $adminUtils->checkZkState();
    // create monitor
    if ($monitorOptVal) {
        try {
            $monitor = createMonitor($parser['monitor_name']);
        } catch (Exception $e) {
            echo "Caught Exception ('{$e->getMessage()}')\n{$e}\n";
            exit(1);
        }
    } else {
        $monitor = NULL;
    }
    $adminUtils->setMonitor($monitor);
    try {
        if ($parser['create']) {
            createConfig($adminUtils, $parser);
        } else {
            if ($parser['delete']) {
                deleteConfig($adminUtils, $parser);
            } else {
                if ($parser['list']) {
                    listConfig($adminUtils, $parser);
                } else {
                    if ($parser['monitor']) {
                        monitorConfig($adminUtils, $parser);
                    }
                }
            }
        }
    } catch (LogConfException $e) {
        echo "Caught LogConfException ('{$e->getMessage()}')\n{$e}\n";
    } catch (Exception $e) {
        echo "Caught Exception ('{$e->getMessage()}')\n{$e}\n";
    }
}
Exemplo n.º 2
0
function main()
{
    // Check Env
    CommandLineUtils::checkRequiredExtentions();
    // Options
    $helpOpt = new Option(null, 'help');
    $helpOpt->setDescription('Print usage information.');
    $zookeeperOpt = new Option(null, 'zookeeper', Getopt::REQUIRED_ARGUMENT);
    $zookeeperOpt->setDescription('REQUIRED: The connection string for the zookeeper connection in the form host:port.' . 'Multiple URLS can be given to allow fail-over.');
    $createOpt = new Option(null, 'create');
    $createOpt->setDescription('Create a new config.');
    $deleteOpt = new Option(null, 'delete');
    $deleteOpt->setDescription('Delete a config');
    $listOpt = new Option(null, 'list');
    $listOpt->setDescription('List all available configs.');
    $hostnameOpt = new Option(null, 'hostname', Getopt::REQUIRED_ARGUMENT);
    $hostnameOpt->setDescription('The hostname of machine which holds log files');
    $log_pathOpt = new Option(null, 'log_path', Getopt::REQUIRED_ARGUMENT);
    $log_pathOpt->setDescription('The log file path, like "/usr/local/apache2/logs/access_log.%Y%m%d"');
    $log_pathOpt->setValidation(function ($value) {
        $ret = AdminUtils::isFilePathValid($value);
        return $ret['valid'];
    });
    $topicOpt = new Option(null, 'topic', Getopt::REQUIRED_ARGUMENT);
    $topicOpt->setDescription('The topic of messages to be sent.');
    $topicOpt->setValidation(function ($value) {
        return AdminUtils::isTopicValid($value);
    });
    $partitionOpt = new Option(null, 'partition', Getopt::REQUIRED_ARGUMENT);
    $partitionOpt->setDescription('The partition of messages to be sent.' . '-1 : random' . 'n(>=0): partition n');
    $partitionOpt->setDefaultValue(-1);
    $partitionOpt->setValidation(function ($value) {
        return is_numeric($value);
    });
    $keyOpt = new Option(null, 'key', Getopt::REQUIRED_ARGUMENT);
    $keyOpt->setDescription('The key of messages to be sent.');
    $keyOpt->setDefaultValue('');
    $keyOpt->setValidation(function ($value) {
        return is_string($value);
    });
    $requiredAcksOpt = new Option(null, 'required_acks', Getopt::REQUIRED_ARGUMENT);
    $requiredAcksOpt->setDescription('Required ack number');
    $requiredAcksOpt->setDefaultValue(1);
    $requiredAcksOpt->setValidation(function ($value) {
        return is_numeric($value);
    });
    $compression_codecOpt = new Option(null, 'compression_codec', Getopt::REQUIRED_ARGUMENT);
    $compression_codecOpt->setDescription("Optional compression method of messages: " . implode(", ", AdminUtils::$COMPRESSION_CODECS));
    $compression_codecOpt->setDefaultValue('none');
    $compression_codecOpt->setValidation(function ($value) {
        return AdminUtils::isCompressionCodecValid($value);
    });
    $batchsizeOpt = new Option(null, 'batchsize', Getopt::REQUIRED_ARGUMENT);
    $batchsizeOpt->setDescription('The batch size of messages to be sent');
    $batchsizeOpt->setDefaultValue(1000);
    $batchsizeOpt->setValidation(function ($value) {
        return is_numeric($value) && $value > 0;
    });
    $follow_lastOpt = new Option(null, 'follow_last', Getopt::REQUIRED_ARGUMENT);
    $follow_lastOpt->setDescription('If set to "false", when restarting logkafka process, 
                          the log_path formatted with current time will be collect;
                          If set to "true", when restarting logkafka process, the last 
                          collecting file will be collected continually');
    $follow_lastOpt->setDefaultValue('true');
    $follow_lastOpt->setValidation(function ($value) {
        return in_array($value, array('true', 'false'));
    });
    $message_timeout_msOpt = new Option(null, 'message_timeout_ms', Getopt::REQUIRED_ARGUMENT);
    $message_timeout_msOpt->setDescription('Local message timeout. This value is only enforced locally 
                          and limits the time a produced message waits for successful delivery. 
                          A time of 0 is infinite.');
    $message_timeout_msOpt->setDefaultValue(0);
    $message_timeout_msOpt->setValidation(function ($value) {
        return is_numeric($value) && $value >= 0;
    });
    $validOpt = new Option(null, 'valid', Getopt::REQUIRED_ARGUMENT);
    $validOpt->setDescription('Enable now or not');
    $validOpt->setDefaultValue('true');
    $validOpt->setValidation(function ($value) {
        return in_array($value, array('true', 'false'));
    });
    $parser = new Getopt(array($zookeeperOpt, $createOpt, $deleteOpt, $listOpt, $hostnameOpt, $log_pathOpt, $topicOpt, $partitionOpt, $keyOpt, $requiredAcksOpt, $compression_codecOpt, $batchsizeOpt, $follow_lastOpt, $message_timeout_msOpt, $validOpt));
    try {
        $parser->parse();
        // Error handling and --help functionality omitted for brevity
        $listOptVal = 0;
        $createOptVal = 0;
        $deleteOptVal = 0;
        if ($parser["list"]) {
            $listOptVal = 1;
        }
        if ($parser["create"]) {
            $createOptVal = 1;
        }
        if ($parser["delete"]) {
            $deleteOptVal = 1;
        }
    } catch (UnexpectedValueException $e) {
        echo "Error: " . $e->getMessage() . "\n";
        echo $parser->getHelpText();
        exit(1);
    }
    // actions
    $actions = $listOptVal + $createOptVal + $deleteOptVal;
    if ($actions != 1) {
        CommandLineUtils::printUsageAndDie($parser, "Command must include exactly one action: --list, --create, or --delete");
    }
    // check args
    checkArgs($parser);
    $zkClient = new Zookeeper($parser['zookeeper']);
    AdminUtils::checkZkState($zkClient);
    try {
        if ($parser['create']) {
            createConfig($zkClient, $parser);
        } else {
            if ($parser['delete']) {
                deleteConfig($zkClient, $parser);
            } else {
                if ($parser['list']) {
                    listConfig($zkClient, $parser);
                }
            }
        }
    } catch (LogConfException $e) {
        echo "Caught LogConfException ('{$e->getMessage()}')\n{$e}\n";
    } catch (Exception $e) {
        echo "Caught Exception ('{$e->getMessage()}')\n{$e}\n";
    }
}