Skip to content

Instantly share code, notes, and snippets.

@gbirke
Created April 23, 2010 13:51
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 gbirke/376559 to your computer and use it in GitHub Desktop.
Save gbirke/376559 to your computer and use it in GitHub Desktop.
Asterisk callfilter
#!/usr/bin/php
<?php
/**
* Error code when file was not found
*/
define('EXIT_NOTFOUND', 11);
require_once 'Console/CommandLine.php';
function getOptionsAndArguments() {
$parser = new Console_CommandLine(array(
'description' => 'Show all application log file lines that contain a specific channel',
'version' => '0.0.1', // the version of your program
));
$parser->addOption('after_context', array(
'short_name' => '-A',
'long_name' => '--after-context',
'description' => 'Show NUM lines after lines with matching channel',
'action' => 'StoreInt',
'default' => 0
));
$parser->addOption('extended_regexp', array(
'short_name' => '-E',
'long_name' => '--extended-regexp',
'description' => 'Use regexp syntax for pattern instead of searching for it as a plain string',
'action' => 'StoreTrue',
'default' => false
));
$parser->addArgument('pattern', array(
'description' => 'String or Pattern to look for. The channel from lines that contain this pattern will be used for the subseqent searches.'
));
$parser->addArgument('inputfile', array(
'description' => 'File to search. Defaults to STDIN',
'optional' => true,
'help_name' => '[inputfile]'
));
try {
return $parser->parse();
} catch (Exception $exc) {
$parser->displayError($exc->getMessage());
}
}
function getInputFile($inputfileName) {
if($inputfileName) {
if(is_readable($inputfileName))
{
$inputfile = fopen($inputfileName, 'r');
}
else {
if(file_exists($inputfileName))
echo "File $inputfileName is not readable.\n";
else
echo "File $inputfileName not found.\n";
exit(EXIT_NOTFOUND);
}
}
else {
$inputfile = STDIN;
}
return $inputfile;
}
$options = getOptionsAndArguments();
$inputfile = getInputFile($options->args['inputfile']);
if($options->options['extended_regexp'])
$pattern = str_replace('/', '\\/', $options->args['pattern']);
else
$pattern = preg_quote($options->args['pattern'], '/');
$channel = '(SIP\/[-a-zA-Z0-9_.]+-[a-f0-9]+)';
$searchpattern = '/('.$channel.'.*'.$pattern.'|'.$pattern.'.*'.$channel.')/';
$afterContext = $options->options['after_context'];
$channelpattern = '';
$linecount = 1;
$lastLinematch = 0;
while(!feof($inputfile)) {
$line = fgets($inputfile);
if(preg_match($searchpattern, $line, $matches)) {
if(count($matches) == 4)
$channelpattern = "/".preg_quote($matches[3], '/').'/';
else
$channelpattern = "/".preg_quote($matches[2], '/').'/';
//echo "pat=$pat\n";
}
if($channelpattern && preg_match($channelpattern, $line)) {
$lastLinematch = $linecount;
echo $line;
}
// Handle -A output
elseif($lastLinematch && $lastLinematch + $afterContext >= $linecount) {
echo $line;
}
$linecount++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment