Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 4 additions & 65 deletions bin/jmx.php
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
#!/usr/bin/env php
<?php

require_once __DIR__.'/../vendor/autoload.php';

use CentralDesktop\JMX\Client;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

require_once("vendor/autoload.php");


class ReadCommand extends Command {
protected
function configure() {
$this
->setName('read')
->setDescription("Get's JMX attributes")
->addArgument(
'uri',
InputArgument::REQUIRED,
'URL for the JMX Jolokia endpoint'
)
->addArgument(
'object',
InputArgument::REQUIRED,
'Name of the object you are fetching'
)
->addOption('user', '', InputOption::VALUE_REQUIRED, "Username")
->addOption('password', '', InputOption::VALUE_REQUIRED, "Password");
}

protected
function execute(InputInterface $input, OutputInterface $output) {
$client = new Client($input->getArgument("uri"),
$input->getOption("user"),
$input->getOption("password"));
$bean = $client->bean($input->getArgument("object"));

$bean->read();

$attributes = $bean->getAttributes();
$keys = array_keys($attributes);

foreach ($keys as $key) {
$value = $attributes[$key];

if (is_array($value)) {
$output->writeln("$key : array(" . count($value) . ")");
foreach ($value as $el) {
$output->writeln(" --> $el ");
}

}
else {
$output->writeln("$key : ");
}

}

}
}


$application = new Application();
$application->add(new ReadCommand());
$application = new Symfony\Component\Console\Application();
$application->add(new CentralDesktop\JMX\Command\ListCommand());
$application->add(new CentralDesktop\JMX\Command\ReadCommand());
$application->run();
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@
"symfony/console": "~2.3"
},
"require-dev": {
"phpunit/phpunit": ">=3.7.0",
"phpmd/phpmd": "1.5.*",
"pdepend/pdepend": "1.1.*",
"squizlabs/php_codesniffer": "1.4.*",
"mockery/mockery": "0.8.0"
"phpunit/phpunit": "^5.7",
"phpmd/phpmd": "^2.6",
"pdepend/pdepend": "^2.5",
"squizlabs/php_codesniffer": "^2.8",
"mockery/mockery": "^0.9.7"
},


"autoload": {
"psr-0": {
"CentralDesktop": "src/"
}
},
"autoload-dev": {
"psr-4": {
"CentralDesktop\\JMX\\Tests\\": "tests/"
}
},

"config": {
"platform": {
"php": "5.6"
}
}
}
8 changes: 2 additions & 6 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="test/bootstrap.php" colors="true">
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="JMX Test Suite">
<directory>test/src/CentralDesktop/JMX</directory>
<directory>tests/</directory>
</testsuite>
</testsuites>

Expand All @@ -12,8 +12,4 @@
<directory suffix=".php">src/CentralDesktop/</directory>
</whitelist>
</filter>

<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
</listeners>
</phpunit>
59 changes: 32 additions & 27 deletions src/CentralDesktop/JMX/Client.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php


namespace CentralDesktop\JMX;


use Guzzle\Http;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\NullLogger;

class Client implements \Psr\Log\LoggerAwareInterface {

class Client implements LoggerAwareInterface
{
private $logger;

private $uri;
Expand All @@ -23,61 +23,66 @@ class Client implements \Psr\Log\LoggerAwareInterface {
* @param $username
* @param $password
*/
public
function __construct($uri, $username = "", $password = "") {
error_log("$username $password");
public function __construct($uri, $username = '', $password = '') {
$this->uri = $uri;
$this->username = $username;
$this->password = $password;

$this->logger = new \Psr\Log\NullLogger();


$this->logger = new NullLogger();

$client = new Http\Client($uri);

$client->setDefaultOption('auth',
array($username, $password, 'Basic'));

$client->setDefaultOption('auth', array($username, $password, 'Basic'));
$client->setDefaultOption('timeout', 5);
$client->setDefaultOption('connect_timeout', 1);

$this->setClient($client);
}


public function setClient(\Guzzle\Http\Client $client){
$this->g = $client;
}


public
function bean($name) {
public function bean($name) {
$bean = new Bean($this, $name);
$bean->setLogger($this->logger);

return $bean;
}


/**
* Sets a logger instance on the object
*
* @param LoggerInterface $logger
*
* @return null
*/
public
function setLogger(LoggerInterface $logger) {
public function setLogger(LoggerInterface $logger) {
$this->logger = $logger;
}

public function read($name)
{
return $this->sendRequest("read/{$name}");
}

public function list()
{
$response = $this->sendRequest('list');

$beans = [];
foreach($response['value'] as $domain => $list) {
foreach($list as $name => $metadata) {
$beans[] = $this->bean($domain.':'.$name);
}
}

return $beans;
}

public
function read($name) {
$request = $this->g->get("read/{$name}");
$response = $request->send();
protected function sendRequest($path)
{
$request = $this->g->get($path);

return $response->json();
return $request->send()->json();
}
}
}
32 changes: 32 additions & 0 deletions src/CentralDesktop/JMX/Command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace CentralDesktop\JMX\Command;

use CentralDesktop\JMX\Client;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

abstract class Command extends BaseCommand
{
protected function configure()
{
$this
->addArgument('uri', InputArgument::REQUIRED, 'URL for the JMX Jolokia endpoint')
->addOption('user', '', InputOption::VALUE_REQUIRED, 'Username')
->addOption('password', '', InputOption::VALUE_REQUIRED, 'Password')
;
}

protected function buildClient(InputInterface $input)
{
return new Client(
$input->getArgument('uri'),
$input->getOption('user'),
$input->getOption('password')
);
}
}
34 changes: 34 additions & 0 deletions src/CentralDesktop/JMX/Command/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CentralDesktop\JMX\Command;

use CentralDesktop\JMX\Client;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListCommand extends Command
{
protected function configure()
{
parent::configure();

$this
->setName('beans')
->setDescription('Lists MBeans')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $this->buildClient($input);

$beans = $client->list();

foreach ($beans as $bean) {
$output->writeln($bean->getName());
}
}
}
54 changes: 54 additions & 0 deletions src/CentralDesktop/JMX/Command/ReadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace CentralDesktop\JMX\Command;

use CentralDesktop\JMX\Client;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ReadCommand extends Command
{
protected function configure()
{
parent::configure();

$this
->setName('read')
->setDescription('Displays MBean attributes')
->addArgument(
'object',
InputArgument::REQUIRED,
'Name of the object you are fetching'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $this->buildClient($input);

$bean = $client->bean($input->getArgument('object'));

$attributes = $bean->getAttributes();
$keys = array_keys($attributes);

foreach ($keys as $key) {
$value = $attributes[$key];

if (is_array($value)) {
$output->writeln("$key : array(" . count($value) . ")");
foreach ($value as $el) {
$output->writeln(" --> $el ");
}

}
else {
$output->writeln("$key : ");
}

}
}
}
6 changes: 0 additions & 6 deletions test/bootstrap.php

This file was deleted.

Loading