From f204ceeb71bcfeee6daaea9c3c4c119fe501174d Mon Sep 17 00:00:00 2001 From: alban Date: Thu, 11 Dec 2014 21:18:51 +0100 Subject: [PATCH] [enh] diagnostic near alpha --- lib/Alternc/Diagnostic/Data.php | 43 ++++- lib/Alternc/Diagnostic/Diff.php | 93 +++++++-- lib/Alternc/Diagnostic/Directory.php | 43 ++++- lib/Alternc/Diagnostic/Format/Abstract.php | 34 ++++ lib/Alternc/Diagnostic/Format/Interface.php | 17 +- lib/Alternc/Diagnostic/Format/Json.php | 20 +- lib/Alternc/Diagnostic/Format/Txt.php | 61 ++++++ lib/Alternc/Diagnostic/Manager.php | 97 ++++++++-- src/diagnostic.php | 200 +++++--------------- 9 files changed, 395 insertions(+), 213 deletions(-) create mode 100644 lib/Alternc/Diagnostic/Format/Txt.php diff --git a/lib/Alternc/Diagnostic/Data.php b/lib/Alternc/Diagnostic/Data.php index 1ee1e819..1e9078d0 100644 --- a/lib/Alternc/Diagnostic/Data.php +++ b/lib/Alternc/Diagnostic/Data.php @@ -24,7 +24,46 @@ class Alternc_Diagnostic_Data { } } - + + // recursive rebuild + function buildFromArray( $content ){ + + if( ! $this->isValidArrayData( $content ) ) { + return $content; + } + $type = $content["type"]; + $newInstance = new Alternc_Diagnostic_Data( $type ); + $newInstance->index = $content["index"]; + $newInstance->metadata = $content["metadata"]; + $data = $content["data"]; + // The content is raw + if( $type === self::TYPE_SECTION){ + $newInstance->data = $data; + } + // The content is made of services or sections + else foreach( $content["data"] as $section_name => $sectionData ){ + $sectionContent = $this->buildFromArray( $sectionData ); + $newInstance->addData( $section_name, $sectionContent ); + } + return $newInstance; + + } + + // Make sure we have a valid format result + function isValidArrayData( $content ){ + if( + !is_array($content) + || !array_key_exists( "type", $content ) + || !array_key_exists("metadata",$content) + || !array_key_exists("index",$content) + || !array_key_exists("data",$content) + || ! in_array( $content["type"], array(self::TYPE_ROOT,self::TYPE_DOMAIN,self::TYPE_SECTION) ) ){ + + return false; + + } + return true; + } /** * Sets @@ -115,4 +154,4 @@ class Alternc_Diagnostic_Data { } -} \ No newline at end of file +} diff --git a/lib/Alternc/Diagnostic/Diff.php b/lib/Alternc/Diagnostic/Diff.php index 788c2edd..f79b77ef 100644 --- a/lib/Alternc/Diagnostic/Diff.php +++ b/lib/Alternc/Diagnostic/Diff.php @@ -1,28 +1,83 @@ -getIndex(); + $targetIndex = $target->getIndex(); + // Check diagnostics are same level + $source_type = $source->getType(); + $target_type = $target->getType(); + if( $source_type != $target_type){ + throw new \Exception("Invalid type comparison requested: $source_type vs $target_type"); + } + $diffInstance = new Alternc_Diagnostic_Data( $source_type ); + +#echo "type $source_type\n"; + // Compare general data + if( $source->getMetadata() != $target->getMetadata() ){ + $diffInstance->setMetadata( array_diff( $source->getMetadata(), $target->getMetadata() ) ); + } + + if( $source->getIndex() != $target->getIndex() ){ + $diffInstance->setIndex( array_diff( $source->getIndex(), $target->getIndex() ) ); + } + + // If section content ie. no subsections + if( $source_type == Alternc_Diagnostic_Data::TYPE_SECTION ){ +#echo "Real section\n"; + if( is_array( $source->getData() ) && is_array( $target->getData() ) ){ + + $diff = array_diff( $source->getData(), $target->getData() ) ; + if( $diff ){ + $diffInstance->setData( array_diff( $source->getData(), $target->getData()) ); + } + }else{ + if( $source->getData() != $target->getData() ){ + $diffInstance->setData( array("source" => $source->getData(),"target" => $target->getData() ) ); + } + } + }else{ + + $sourceData = $source->getData(); + $targetData = $target->getData(); + $seenSections = array(); + foreach( $sourceData as $section_name => $sectionData ){ + +#echo "section_name $section_name\n"; + $section_data_type = $sectionData->getType(); + if( ! isset( $targetData[$section_name] ) ) { +#echo "section_name not in target\n"; + $tempDataInstance = new Alternc_Diagnostic_Data($section_data_type); + $tempDataInstance->setMetadata( array("Not in target") ); + }else{ +#echo "section_name for diff\n"; + $tempDataInstance = $this->compare($sectionData, $targetData[$section_name] ); + } + if( ! is_null( $tempDataInstance ) ){ + $diffInstance->addData( $section_name, $tempDataInstance); + } + + + } + + } + if( count( $diffInstance->getData() ) + || count( $diffInstance->getIndex() ) + || count( $diffInstance->getMetadata() ) + ){ + return $diffInstance; + } + + return null; + } - /** - * Finds a file by reference or name - * - * @param string $file_reference - * @return Alternc_Diagnostic_Data Resulting data - */ - function resolve( $file_reference){ - - } } diff --git a/lib/Alternc/Diagnostic/Directory.php b/lib/Alternc/Diagnostic/Directory.php index c89c7e84..7b6048a0 100644 --- a/lib/Alternc/Diagnostic/Directory.php +++ b/lib/Alternc/Diagnostic/Directory.php @@ -25,7 +25,17 @@ class Alternc_Diagnostic_Directory { function getList( $max = null){ $dir = new DirectoryIterator($this->file_path); - + $fileList = array(); + while($dir->valid()) { + if( $dir->isDot() ){ + $dir->next(); + continue; + } + $file_name = $dir->getFilename(); + $fileList[] = $file_name; + $dir->next(); + } + return $fileList; } /** @@ -36,6 +46,35 @@ class Alternc_Diagnostic_Directory { return $this; } + + // @todo Confirm usefulness + public function getFileContent( $id ) { + $fileList = $this->getList(); + if( array_key_exists( $id, $fileList ) ){ + return file_get_contents( $this->file_path."/".$fileList[$id]); + } + if( in_array($id, $fileList)){ + $key = array_search( $id, $fileList ); + return file_get_contents( $this->file_path."/".$fileList[$key]); + } + throw new \Exception("Could not find diagnostic for id : $id"); + } + + function getFileInfo($id){ + + $fileList = $this->getList(); + + if( array_key_exists( $id, $fileList ) ){ + $file_path = $this->file_path."/".$fileList[$id]; + } + if( in_array($id, $fileList)){ + $key = array_search( $id, $fileList ); + $file_path = $this->file_path."/".$fileList[$key]; + } + $fileInfos = pathinfo( $file_path ); + return ($fileInfos); + } + /** * @return string */ @@ -47,4 +86,4 @@ class Alternc_Diagnostic_Directory { } -} \ No newline at end of file +} diff --git a/lib/Alternc/Diagnostic/Format/Abstract.php b/lib/Alternc/Diagnostic/Format/Abstract.php index 58001499..2557409e 100644 --- a/lib/Alternc/Diagnostic/Format/Abstract.php +++ b/lib/Alternc/Diagnostic/Format/Abstract.php @@ -83,6 +83,40 @@ class Alternc_Diagnostic_Format_Abstract { return $this->directory; } + + /** + * Writes a Data object to file + * + * @return boolean + */ + public function write( Alternc_Diagnostic_Data $data = null ){ + if( null == $data ){ + if( ! $this->data ){ + throw new \Exception( "A format cannot be written without a Data"); + } + $data = $this->data; + } + $content = $this->dataToContent( $data ); + $filename = $this->getFilename(); + if( ! file_put_contents($filename, $content) ){ + throw new \Exception("Failed to write in json format to file $filename" ); + } + return true; + + } + + /** + * + * @param string file_name + * @return Alternc_Diagnostic_Data A diagnostic structure + */ + function read( $file_name ){ + + $content = $this->directory->getFileContent( $file_name ); + return $this->contentToData( $content ); + + } + } diff --git a/lib/Alternc/Diagnostic/Format/Interface.php b/lib/Alternc/Diagnostic/Format/Interface.php index 662bcb15..77231a46 100644 --- a/lib/Alternc/Diagnostic/Format/Interface.php +++ b/lib/Alternc/Diagnostic/Format/Interface.php @@ -2,20 +2,9 @@ interface Alternc_Diagnostic_Format_Interface{ - /** - * - * @param mixed $file_reference - * Either a number or a string refering to the file - * @return Alternc_Diagnostic_Data A diagnostic file - */ - function read( $file_reference ); + function contentToData( $content ); - /** - * Writes a Data object to file - * - * @return boolean - */ - function write(); + function dataToContent(); -} \ No newline at end of file +} diff --git a/lib/Alternc/Diagnostic/Format/Json.php b/lib/Alternc/Diagnostic/Format/Json.php index ccd77039..cca03791 100644 --- a/lib/Alternc/Diagnostic/Format/Json.php +++ b/lib/Alternc/Diagnostic/Format/Json.php @@ -19,29 +19,31 @@ class Alternc_Diagnostic_Format_Json /** * @inherit */ - function read( $file_reference ){ - + function contentToData( $content ){ + + $arrayData = json_decode( $content , true); + $dataInstance = new Alternc_Diagnostic_Data(Alternc_Diagnostic_Data::TYPE_ROOT); + $this->data = $dataInstance->buildFromArray( $arrayData ); + return $this->data; + } /** * @inherit */ - function write(Alternc_Diagnostic_Data $data = null ){ + function dataToContent(Alternc_Diagnostic_Data $data = null ){ if( $data ){ $this->setData($data); } - $file_content = json_encode($this->getData()); + $content = json_encode($this->getData()); $filename = $this->getFilename(); if(json_last_error()){ throw new \Exception("Json conversion failed with error #".json_last_error()."for data".serialize($this->getData())); } - if( ! file_put_contents($filename, $file_content) ){ - throw new \Exception("Failed to write in json format to file $filename for data".serialize($this->getData())); - } - return true; + return $content; } -} \ No newline at end of file +} diff --git a/lib/Alternc/Diagnostic/Format/Txt.php b/lib/Alternc/Diagnostic/Format/Txt.php new file mode 100644 index 00000000..407f633b --- /dev/null +++ b/lib/Alternc/Diagnostic/Format/Txt.php @@ -0,0 +1,61 @@ +setExtension("txt"); + } + + /** + * @inherit + */ + function contentToData( $content ){ + + // @todo or skip ? Quite a fragile storage + + } + + + /** + * @inherit + */ + function dataToContent(Alternc_Diagnostic_Data $data = null, $depth = 0 ){ + $d = $this->space_depth($depth); + echo $d."Type: ".$data->type."\n"; + $d .= " "; + if( $data->type == Alternc_Diagnostic_Data::TYPE_SECTION ){ + foreach( $data->data as $key => $value ){ + if( is_int( $key) ){ + echo $d.json_encode( $value, true )."\n"; + }else{ + echo $d.$key." => ".json_encode( $value, true )."\n"; + } + } + return; + } + foreach( $data->data as $section_name => $sectionData ){ + echo $d."Section: $section_name\n"; + $this->dataToContent( $sectionData, $depth+1); + } + } + + function space_depth( $depth){ + $buf = ""; + for( $i=0; $i < $depth; $i++){ + $buf .= " "; + } + return $buf; + } + + +} diff --git a/lib/Alternc/Diagnostic/Manager.php b/lib/Alternc/Diagnostic/Manager.php index e75c03d8..4a41c94d 100644 --- a/lib/Alternc/Diagnostic/Manager.php +++ b/lib/Alternc/Diagnostic/Manager.php @@ -55,32 +55,101 @@ class Alternc_Diagnostic_Manager{ * @param Console_CommandLine_Result $options * @throws \Exception */ - function create(Console_CommandLine_Result $options){ + function c_create(Console_CommandLine_Result $options){ $args = $options->args; $options = $options->options; - $diagnosticData = new Alternc_Diagnostic_Data(Alternc_Diagnostic_Data::TYPE_ROOT); + $diagnosticdata = new Alternc_Diagnostic_Data(Alternc_Diagnostic_Data::TYPE_ROOT); - $serviceList = explode(',',$options["services"]); - foreach ($serviceList as $service) { + $servicelist = explode(',',$options["services"]); + foreach ($servicelist as $service) { $class_name = "Alternc_Diagnostic_Service_".trim(ucfirst($service)); if(!class_exists($class_name)){ - throw new \Exception("Invalid service $service"); + throw new \exception("invalid service $service"); } - /** @var Alternc_Diagnostic_Service_Interface */ - $serviceAgent = new $class_name( array("service" => $this) ); + /** @var alternc_diagnostic_service_interface */ + $serviceagent = new $class_name( array("service" => $this) ); - // Runs the service agent and store the results - $diagnosticData->addData($serviceAgent->name, $serviceAgent->run()); + // runs the service agent and store the results + $diagnosticdata->addData($serviceagent->name, $serviceagent->run()); } - $this->formatInstance->setData($diagnosticData)->write(); + $this->formatInstance->setData($diagnosticdata)->write(); } - function compare( $options ){} - function index( $options ){} - function show( $options ){} - function delete( $options ){} + function c_diff( $options ){ + $args = $options->args; + $options = $options->options; + $source = $options["source"]; + $target = $options["target"]; + $format = $options['format']; + $sourceDiagnostic = $this->getDiagnosticFromId($source); + $targetDiagnostic = $this->getDiagnosticFromId($target); + $diff = new Alternc_Diagnostic_Diff(); + $diffData = $diff->compare($sourceDiagnostic,$targetDiagnostic); + $formatInstance = $this->getFormatInstance( $format ); + echo $formatInstance->dataToContent( $diffData); + } + + function c_list( $options ){ + + $args = $options->args; + $options = $options->options; + $fileList = $this->directoryInstance->getList(); + foreach( $fileList as $number => $file ){ + echo "$number\t$file\n"; + } + + + + } + function c_show( $options ){ + + + $args = $options->args; + $options = $options->options; + $id = $options['id']; + $format = $options['format']; + $data = $this->getDiagnosticFromId( $id); + $formatInstance = $this->getFormatInstance( $format ); + echo $formatInstance->dataToContent( $data ); + + + } + function c_delete( $options ){} + + /** + * Finds a file by reference or name + * + * @param string $file_reference + * @return Alternc_Diagnostic_Data Resulting data + * @todo add the ability to resolve by filename + */ + protected function getDiagnosticFromId ( $id ) { + + $fileInfo = $this->directoryInstance->getFileInfo( $id ) ; + $extension = $fileInfo["extension"]; + $formatInstance = $this->getFormatInstance( $extension); + $formatInstance->read( $fileInfo["basename"] ); + $data = $formatInstance->getData(); + return $data; + + } + + protected function getFormatInstance ( $format ){ + switch( $format ){ + case "json": + $instance = new Alternc_Diagnostic_Format_Json( $this->directoryInstance ); + break; + case "txt": + $instance = new Alternc_Diagnostic_Format_Txt( $this->directoryInstance ); + break; + default: + throw new \Exception("Invalid format : $format"); + break; + } + return $instance; + } } diff --git a/src/diagnostic.php b/src/diagnostic.php index 69ea41f9..00936055 100644 --- a/src/diagnostic.php +++ b/src/diagnostic.php @@ -126,7 +126,7 @@ if(is_file("/usr/share/alternc/panel/class/config_nochk.php")){ -$directoryInstance = new Alternc_Diagnostic_Directory("/tmp/diagnostic"); +$directoryInstance = new Alternc_Diagnostic_Directory("/var/log/alternc/diagnostic"); // instanciation of the diagnosticManager service @@ -168,9 +168,49 @@ $createCommmand->addOption('format', array( 'help_name' => 'format' )); -$indexCommmand = $consoleParser->addCommand('index', array('multiple'=>false,"alias"=>"i","description" => "Shows all available diagnostics")); -$compareCommmand = $consoleParser->addCommand('compare', array('multiple'=>false,"alias"=>"x","description" => "Removes one or more diagnotics")); -$compareCommmand = $consoleParser->addCommand('show', array('multiple'=>false,"alias"=>"s","description" => "Prints a diagnotic content")); +$listCommmand = $consoleParser->addCommand('list', array('multiple'=>false,"alias"=>"i","description" => "Shows all available diagnostics")); +$diffCommmand = $consoleParser->addCommand('diff', array('multiple'=>false,"alias"=>"x","description" => "Removes one or more diagnotics")); +$diffCommmand->addOption('source', array( + 'short_name' => '-s', + 'long_name' => '--source', + 'action' => 'StoreString', + 'default' => '0', + 'description' => 'First file to diff, id or basename. Default: 0', + 'help_name' => 'source' + )); +$diffCommmand->addOption('target', array( + 'short_name' => '-t', + 'long_name' => '--target', + 'action' => 'StoreString', + 'default' => '1', + 'description' => 'First file to diff, id or basename. Default: 1', + 'help_name' => 'target' + )); +$diffCommmand->addOption('format', array( + 'short_name' => '-f', + 'long_name' => '--format', + 'action' => 'StoreString', + 'default' => 'txt', + 'description' => 'Sets the format of the diagnostic diff. default: txt', + 'help_name' => 'format' + )); +$showCommand = $consoleParser->addCommand('show', array('multiple'=>false,"alias"=>"s","description" => "Prints a diagnotic content")); +$showCommand->addOption('id', array( + 'short_name' => '-i', + 'long_name' => '--id', + 'action' => 'StoreString', + 'default' => '0', + 'description' => 'Provides the id or name of a diagnostic to show. Default: 0', + 'help_name' => 'id' + )); +$showCommand->addOption('format', array( + 'short_name' => '-f', + 'long_name' => '--format', + 'action' => 'StoreString', + 'default' => 'txt', + 'description' => 'Sets the format of the output. Default: txt. Other choices : array', + 'help_name' => 'format' + )); $deleteCommmand = $consoleParser->addCommand('delete', array('multiple'=>false,"alias"=>"d","description" => "Deletes diagnostic files")); @@ -179,161 +219,15 @@ $deleteCommmand = $consoleParser->addCommand('delete', a try { $result = $consoleParser->parse(); if ($result->command_name){ - $command_name = $result->command_name; + $command_name = "c_".$result->command_name; $command = $result->command; }else{ - throw new \Exception("Command missing, use -h to learn about available commands."); + throw new \Exception("Welcome to AlternC Diagnostics! Use -h to learn about available commands."); } if( !method_exists($diagnosticManager, $command_name)){ throw new \Exception("Invalid command : $command"); } - $diagnosticManager->$command_name($command); + $result = $diagnosticManager->$command_name($command); } catch (\Exception $exc) { $consoleParser->displayError($exc->getMessage()); } -/* -// Put this var to 1 if you want to enable debug prints - - -$admin->stop_if_jobs_locked(); - -$LOCK_FIL E= '/var/run/alternc/do_actions_cron.lock'; -$SCRIP T= '/usr/bin/php do_actions.php'; -$MY_PI D= getmypid(); -$FIXPER M= '/usr/lib/alternc/fixperms.sh'; - -// Check if script isn't already running -if (file_exists($LOCK_FILE) !== false){ - d("Lock file already exists. "); - // Check if file is in process list - $PI D= file_get_contents($LOCK_FILE); - d("My PID is $MY_PID, PID in the lock file is $PID"); - if ($PID == exec("pidof $SCRIPT | tr ' ' '\n' | grep -v $MY_PID")){ - // Previous cron is not finished yet, just exit - d("Previous cron is already running, we just exit and let it finish :-)"); - exit(0); - }else{ - // Previous cron failed! - $error_raise .= "Lock file already exists. No process with PID $PID found! Previous cron failed...\n"; - d("Removing lock file and trying to process the failed action..."); - // Delete the lock and continue to the next action - unlink($LOCK_FILE); - - // Lock with the current script's PID - if (file_put_contents($LOCK_FILE,$MY_PID) === false){ - $error_raise .= "Cannot open/write $LOCK_FILE\n"; - mail_it(); - exit(1); - } - - // Get the action(s) that was processing when previous script failed - // (Normally, there will be at most 1 job pending... but who know?) - while($cc=$action->get_job()){ - $ c= $cc[0]; - $param s= unserialize($c["parameters"]); - // We can resume these types of action, so we reset the job to process it later - d("Previous job was the n°".$c["id"]." : '".$c["type"]."'"); - if($c["type"] == "CREATE_FILE" && is_dir(dirname($params["file"])) || $c["type"] == "CREATE_DIR" || $c["type"] == "DELETE" || $c["type"] == "FIXDIR" || $c["type"] == "FIXFILE"){ - d("Reset of the job! So it will be resumed..."); - $action->reset_job($c["id"]); - }else{ - // We can't resume the others types, notify the fail and finish this action - $error_raise .= "Can't resume the job n°".$c["id"]." action '".$c["type"]."', finishing it with a fail status.\n"; - if(!$action->finish($c["id"],"Fail: Previous script crashed while processing this action, cannot resume it.")){ - $error_raise .= "Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\n"; - break; // Else we go into an infinite loop... AAAAHHHHHH - } - } - } - } -}else{ - // Lock with the current script's PID - if (file_put_contents($LOCK_FILE,$MY_PID) === false){ - $error_raise .= "Cannot open/write $LOCK_FILE\n"; - mail_it(); - exit(1); - } -} - -//We get the next action to do -while ($rr=$action->get_action()){ - $ r= $rr[0]; - $retur n= "OK"; - // Do we have to do this action with a specific user? - if($r["user"] != "root") - $S U= "su ".$r["user"]." 2>&1 ;"; - else - $S U= ""; - unset($output); - // We lock the action - d("-----------\nBeginning action n°".$r["id"]); - $action->begin($r["id"]); - // We process it - $param s= @unserialize($r["parameters"]); - // We exec with the specified user - d("Executing action '".$r["type"]."' with user '".$r["user"]."'"); - switch ($r["type"]){ - case "FIX_USER" : - // Create the directory and make parent directories as needed - @exec("$FIXPERM -u ".$params["uid"]." 2>&1", $trash, $code); - break; - case "CREATE_FILE" : - if(!file_exists($params["file"])) - @exec("$SU touch ".$params["file"]." 2>&1 ; echo '".$params["content"]."' > '".$params["file"]."' 2>&1", $output); - else - $outpu t= array("Fail: file already exists"); - break; - case "CREATE_DIR" : - // Create the directory and make parent directories as needed - @exec("$SU mkdir -p ".$params["dir"]." 2>&1",$output); - break; - case "DELETE" : - // Delete file/directory and its contents recursively - @exec("$SU rm -rf ".$params["dir"]." 2>&1", $output); - break; - case "MOVE" : - // If destination dir does not exists, create it - if(!is_dir($params["dst"])) - @exec("$SU mkdir -p ".$params["dst"]." 2>&1",$output); - if(!isset($output[0])) - @exec("$SU mv -f ".$params["src"]." ".$params["dst"]." 2>&1", $output); - break; - case "FIXDIR" : - @exec("$FIXPERM -d ".$params["dir"]." 2>&1", $trash, $code); - if($code!=0) - $output[0]="Fixperms.sh failed, returned error code : $code"; - break; - case "FIXFILE" : - @exec("$FIXPERM -f ".$params["file"]." 2>&1", $trash, $code); - if($code!=0) - $output[0]="Fixperms.sh failed, returned error code : $code"; - break; - default : - $outpu t= array("Fail: Sorry dude, i do not know this type of action"); - break; - } - // Get the error (if exists). - if(isset($output[0])){ - $retur n= $output[0]; - $error_raise .= "Action n°".$r["id"]." '".$r["type"]."' failed! With user: ".$r["user"]."\nHere is the complete output:\n".print_r($output); - } - // We finished the action, notify the DB. - d("Finishing... return value is : $return\n"); - if(!$action->finish($r["id"],addslashes($return))){ - $error_raise .= "Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\nReturn value: ".addslashes($return)."\n"; - break; // Else we go into an infinite loop... AAAAHHHHHH - } -} - -// If something have failed, notify it to the admin -if($error_raise !== '') - mail_it(); - -// Unlock the script -unlink($LOCK_FILE); - -// Exit this script -exit(0); -?> - -*/