codice:
<?php
/**
* Copyright (c) 2010 Daniele Salvatore Albano.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY ALBANO DANIELE SALVATORE AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
class Phunin
{
const COMMAND_VERSION = 'version';
const COMMAND_NODES = 'nodes';
const COMMAND_LIST = 'list';
const COMMAND_CONFIG = 'config';
const COMMAND_FETCH = 'fetch';
private $socket;
public function __construct($Hostname, $Port)
{
// Try to connect to munin node
$this->ConnectTo($Hostname, $Port);
}
public function Version()
{
return $this->SendCommand(Phunin::COMMAND_VERSION);
}
public function Nodes()
{
return $this->SendCommand(Phunin::COMMAND_NODES);
}
public function ListPlugins()
{
return $this->SendCommand(Phunin::COMMAND_LIST);
}
public function Config($PluginName)
{
return $this->SendCommand(Phunin::COMMAND_CONFIG, trim($PluginName));
}
public function Fetch($PluginName)
{
return $this->SendCommand(Phunin::COMMAND_FETCH, trim($PluginName));
}
private function ConnectTo($Hostname, $Port)
{
// Prepare the socket to connect to remote munin node
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Do connect
if (socket_connect($this->socket, $Hostname, $Port) === false)
{
throw new Exception(sprintf("Unable to connect to %s:%s", $Hostname, $Port));
}
// Initialize buffer
$data = '';
// Read data from socket
while(($buffer = socket_read($this->socket, 8 * 1024)) !== false)
{
// Acquire buffer
$data .= $buffer;
// Check for \n
if (substr($data, 0, 16) === '# munin node at ')
{
if (strpos("\n", $data) === false)
{
return true;
}
else
{
if (strlen($data) > 192)
{
throw new Exception(sprintf("Wrong answer from remote munin node"));
}
}
}
else
{
throw new Exception(sprintf("Wrong answer from remote munin node"));
}
}
}
private function SendCommand($Command, $Argument = false)
{
// Check command
switch($Command)
{
case Phunin::COMMAND_VERSION:
case Phunin::COMMAND_NODES:
case Phunin::COMMAND_LIST:
break;
case Phunin::COMMAND_CONFIG:
case Phunin::COMMAND_FETCH:
if ($Argument == false)
{
throw new Exception(sprintf("Missing argument for command %s", $Command));
}
break;
default:
throw new Exception(sprintf("Unknown command %s", $Command));
break;
}
// Try to send command
if (socket_write($this->socket, $Command . ($Argument !== false ? ' ' . $Argument : '') . "\n") == 0)
{
throw new Exception(sprintf("Unable to send command %s", $Command));
}
// Wait for answer
return $this->ReadAnswer($Command);
}
private function ReadAnswer($Command)
{
// Check command
switch($Command)
{
case Phunin::COMMAND_VERSION:
case Phunin::COMMAND_LIST:
$endsWithDot = false;
$charsToStrip = 1;
break;
case Phunin::COMMAND_NODES:
case Phunin::COMMAND_CONFIG:
case Phunin::COMMAND_FETCH:
$endsWithDot = true;
$charsToStrip = 3;
break;
}
// Initialize data
$data = '';
// Read data from socket
while(($buffer = socket_read($this->socket, 8 * 1024)) !== false)
{
// Acquire buffer
$data .= $buffer;
if (substr($data, -$charsToStrip) == ($endsWithDot == true ? "\n." : '') . "\n")
{
$data = substr($data, 0, -$charsToStrip);
break;
}
else if (strlen($data) > 16 * 1024)
{
throw new Exception(sprintf("Wrong answer from remote munin node"));
}
}
// Split data
switch($Command)
{
case Phunin::COMMAND_VERSION:
return $data;
break;
case Phunin::COMMAND_LIST:
echo $data;
return explode(' ', $data);
break;
case Phunin::COMMAND_NODES:
case Phunin::COMMAND_CONFIG:
case Phunin::COMMAND_FETCH:
return explode("\n", $data);
break;
}
}
}
$phunin = new Phunin('192.168.1.15', 4949);
var_dump($phunin->ListPlugins());
echo "
";
var_dump($phunin->Config('df'));
echo "
";
var_dump($phunin->Fetch('df'));
echo "
";
?>
come puoi vedere è estremamente semplice