[enh] diagnostic apache2 working

This commit is contained in:
alban 2014-06-30 00:38:24 +02:00
parent 581162d28f
commit 7c1ce762c1
7 changed files with 207 additions and 35 deletions

View File

@ -3,5 +3,20 @@
/**
* Console aware class, encapsulates the Console CommandLine class
*/
class Alternc_Diagnostic_Console extends Console_CommandLine{}
class Alternc_Diagnostic_Console extends Console_CommandLine{
const DESCRIPTION = "Handles diagnostics of an alternc server.";
const VERSION = "0.1";
function __construct(array $params = array()) {
$params = array(
'description' => self::DESCRIPTION,
'version' => self::VERSION
);
parent::__construct($params);
}
}

View File

@ -27,13 +27,15 @@ class Alternc_Diagnostic_Data {
/**
* Sets
*
* @param array $options a module name => data
* @param string $sectionname
* @param Alternc_Diagnostic_Data $data
* @return boolean
*/
function addData( $name, Alternc_Diagnostic_Data $data){
$this->index[] = $name;
$this->data[$name] = $data;
function addData( $sectionname, Alternc_Diagnostic_Data $data){
$this->index[] = $sectionname;
$this->data[$sectionname] = $data;
return true;
}
@ -97,4 +99,20 @@ class Alternc_Diagnostic_Data {
return $this->metadata;
}
/**
* Retrieves a given section of the data
*
*
* @param string $section_name
* @return boolean
*/
public function getSection( $section_name ){
if( !in_array($section_name, $this->index)){
return FALSE;
}
return $this->data[$section_name];
}
}

View File

@ -22,6 +22,7 @@ class Alternc_Diagnostic_Manager{
* @throws \Exception
*/
public function __construct($options) {
// Attempts to retrieve formatInstance
if (isset($options["formatInstance"]) && ! is_null($options["formatInstance"])) {
$this->formatInstance = $options["formatInstance"];
@ -35,6 +36,7 @@ class Alternc_Diagnostic_Manager{
} else {
throw new \Exception("Missing parameter directoryInstance");
}
}
/**

View File

@ -8,7 +8,7 @@ abstract class Alternc_Diagnostic_Service_Abstract{
/** @var Alternc_Diagnostic_Data*/
protected $data;
/** @var m_mysql */
/** @var DB_Sql*/
public $db;
/** @var m_mysql */
@ -92,8 +92,14 @@ abstract class Alternc_Diagnostic_Service_Abstract{
return $output;
}
protected function filterRegexp($pattern,$result){
/**
* Filters lines of a result to only include the matching lines
*
* @param string $pattern
* @param array $result
* @return type
*/
protected function filterRegexp($result,$pattern){
$returnArray = array();
foreach ($result as $line) {
$captures_count = preg_match($pattern, $line, $matches);
@ -128,7 +134,7 @@ abstract class Alternc_Diagnostic_Service_Abstract{
* @param mixed $content
* @return boolean
*/
function addDataSection( $name, $content){
function writeSectionData( $name, $content){
$section = new Alternc_Diagnostic_Data(Alternc_Diagnostic_Data::TYPE_SECTION,$content);
$this->data->addData($name, $section);

View File

@ -11,16 +11,131 @@ class Alternc_Diagnostic_Service_Apache2
implements Alternc_Diagnostic_Service_Interface
{
public $name = "web";
const SECTION_APACHE2_VHOSTS = "apache2 vhosts";
const SECTION_APACHE2_MODULES = "apache2 modules";
public $name = "apache2";
const SECTION_VHOSTS = "vhosts";
const SECTION_MODULES = "modules";
const SECTION_REDIRECTIONS = "redirections";
const SECTION_RESPONSES = "responses";
function run(){
$this->addDataSection (self::SECTION_APACHE2_VHOSTS,$this->filterRegexp("/^[\D]*(\d{2,4}).* (.*) \(\/etc.*$/u", $this->execCmd("apache2ctl -S")));
$this->addDataSection (self::SECTION_APACHE2_MODULES,$this->filterRegexp("/^[\W]*(\w+).*\(.*$/u", $this->execCmd("apache2ctl -M")));
$this->addDataSection (self::SECTION_APACHE2_REDIRECTION,$this->mysql->query("SELECT domaine, valeur from sub_domaines where type='url';"));
// Writes the modules
$this->writeSectionData (self::SECTION_MODULES,$this->filterRegexp ($this->execCmd("apache2ctl -M"), "/^[\W]*(\w+).*\(.*$/u" ));
// Writes the vhosts in the form "port servername"
$this->writeSectionData (self::SECTION_VHOSTS,$this->getVhosts());
// Writes the redirects
$this->writeSectionData (self::SECTION_REDIRECTIONS, $this->getRedirects());
// Writes the tests
$this->writeSectionData (self::SECTION_RESPONSES,$this->testServers());
return $this->data;
}
function getVhosts(){
$list = $this->filterRegexp( $this->execCmd("apache2ctl -S"), "/^[\D]*(\d{2,4}).* (.*) \(\/etc.*$/u");
$returnArray = array();
foreach( $list as $vhost){
$returnArray[] = explode(" ",$vhost);
}
return $returnArray;
}
function getRedirects(){
$mysqlResource = $this->db->query("SELECT domaine as domain, valeur as url from sub_domaines where type='url';");
$resultArray = array();
if ($this->db->num_rows()) {
while(($resultArray[] = mysql_fetch_assoc($mysqlResource)) || array_pop($resultArray));
}
return $resultArray;
}
/**
* Reads an array of URL and returns the CURL results
*
* @param array $urlList
* @param array $fieldsList curlInfo array keys
* @param int $sockets_max
* @return array
*/
function curlRequest($urlList,$fieldsList = array("http_code","url"),$sockets_max = 8){
$returnArray = array();
// Attempts to retrive a multi connection curl handle
$multiCurlHandle = curl_multi_init();
for ($index = 0; $index < $sockets_max; $index++) {
$ch = "ch".$index;
$$ch = curl_init();
curl_setopt($$ch, CURLOPT_HEADER, 1);
curl_setopt($$ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($$ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($$ch, CURLOPT_TIMEOUT, 3);
curl_setopt($$ch, CURLOPT_NOBODY, 1);
curl_multi_add_handle($multiCurlHandle,$$ch);
}
$url_count = count($urlList);
$url_pointer = 0;
while( $url_pointer < $url_count){
$sockets = $url_count - $url_pointer > $sockets_max ? $sockets_max : $url_count - $url_pointer ;
$loopUrlList = array();
for ($index2 = 0; $index2 < $sockets; $index2++) {
$ch = "ch".$index2;
$url = $urlList[$url_pointer];
$loopUrlList[$index2] = $url;
curl_setopt($$ch, CURLOPT_URL, $url);
$url_pointer++;
}
do {
curl_multi_exec($multiCurlHandle, $running);
curl_multi_select($multiCurlHandle);
} while ($running > 0);
for ($index3 = 0; $index3 < $sockets; $index3++) {
$ch = "ch".$index3;
$url = $loopUrlList[$index3];
$curlInfo = curl_getinfo($$ch);
$urlInfo = array();
foreach ($fieldsList as $field) {
$urlInfo[$field] = $curlInfo[$field];
}
$returnArray[] = $urlInfo;
}
}
//close the handles
curl_multi_close($multiCurlHandle);
for ($index = 0; $index < $sockets_max; $index++) {
$ch = "ch".$index;
curl_close($$ch);
}
return $returnArray;
}
function testServers(){
$sockets_max = 8;
$fieldsList = array("http_code","url");
$vhostUrlList = array();
// Retrieves and tests local vhosts
$vhostList = $this->data->getSection(self::SECTION_VHOSTS)->getData();
foreach( $vhostList as $vhostInfo){
$protocol = $vhostInfo[0] == 443 ? "https://":"http://";
$vhostUrlList[] = "{$protocol}{$vhostInfo[1]}";
}
$vhostResult = $this->curlRequest($vhostUrlList,$fieldsList,$sockets_max);
// Retrieves and tests local redirs
$redirList = $this->data->getSection(self::SECTION_REDIRECTIONS)->getData();
foreach( $redirList as $redirInfo){
$redirUrlList[] = $redirInfo["url"];
}
$redirResult = $this->curlRequest($redirUrlList,$fieldsList,$sockets_max);
return array_merge($vhostResult,$redirResult);
}
}

View File

@ -11,7 +11,7 @@ class Alternc_Diagnostic_Service_System
public $name = "system";
function run(){
$this->addDataSection("ip list", $this->execCmd("ip a"));
$this->writeSectionData("ip list", $this->execCmd("ip a"));
return $this->data;
}
}

View File

@ -40,9 +40,14 @@
*/
//include "Console.php";
/**
* Attempts to load a class in multiple path, the PSR-0 or old style way
*
* @staticvar array $srcPathList
* @staticvar boolean $init
* @param string $class_name
* @return boolean
*/
function __autoload($class_name)
{
// Contains (Namespace) => directory
@ -95,11 +100,31 @@ function __autoload($class_name)
return true;
}
}
// Failed to find file
return false;
}
// ==================================================================
// ==================================================================
// alternc config
// ==================================================================
// ==================================================================
require_once("/usr/share/alternc/panel/class/config_nochk.php");
$directoryInstance = new Alternc_Diagnostic_Directory("/tmp/diagnostic");
// instanciation of the diagnosticManager service
$diagnosticManager = new Alternc_Diagnostic_Manager( array(
"directoryInstance" => $directoryInstance,
"formatInstance" => new Alternc_Diagnostic_Format_Json($directoryInstance)
));
// ==================================================================
@ -108,22 +133,21 @@ function __autoload($class_name)
// ==================================================================
// ==================================================================
$consoleParser = new Alternc_Diagnostic_Console(array(
'description' => "Handles diagnostics of an alternc server.",
'version' => '0.0.1',
));
$consoleParser = new Alternc_Diagnostic_Console();
$createCommmand = $consoleParser->addCommand('create', array('multiple'=>true,"alias"=>"c","description" => "Creates a new diagnostic"));
$createCommmand->addOption('services', array(
'short_name' => '-s',
'long_name' => '--services',
'action' => 'StoreString',
'default' => 'apache2,dns,email,system,mailman,mysql,panel,ftp',
'default' => 'apache2,dns,mail,system,mailman,mysql,panel,ftp',
'description' => 'Sets the services to use for diagnostics separated by comma
ex: -d apache2,dns,email',
ex: -d apache2,dns,mail',
'help_name' => 'services'
));
$createCommmand->addOption('format', array(
'short_name' => '-f',
'long_name' => '--format',
@ -139,16 +163,8 @@ $compareCommmand = $consoleParser->addCommand('show', arr
$deleteCommmand = $consoleParser->addCommand('delete', array('multiple'=>false,"alias"=>"d","description" => "Deletes diagnostic files"));
$directoryInstance = new Alternc_Diagnostic_Directory("/tmp/diagnostic");
$diagnosticManager = new Alternc_Diagnostic_Manager( array(
"directoryInstance" => $directoryInstance,
"formatInstance" => new Alternc_Diagnostic_Format_Json($directoryInstance)
));
//require_once("/usr/share/alternc/panel/class/config_nochk.php");
require_once("../bureau/class/config_nochk.php");
// Attempts to parse command line
try {
$result = $consoleParser->parse();
if ($result->command_name){