Skip to content
Snippets Groups Projects
Commit 88a1f5f9 authored by Tibo's avatar Tibo
Browse files

Initial version of PHP client

parent d98d84a6
No related branches found
No related tags found
No related merge requests found
Showing with 1215 additions and 4 deletions
/client/target/
/web/nbproject/private/
\ No newline at end of file
/web/nbproject/private/
/php-client/nbproject/private/
\ No newline at end of file
File added
{
"chmod": "0755",
"directories": [
"src"
],
"files": [
"vendor/herrera-io/phar-update/res/schema.json",
"version"
],
"finder": [
{
"name": "*.php",
"exclude": [
"Tests",
"tests",
"herrera-io/annotations",
"herrera-io/box"
],
"in": "vendor"
}
],
"main": "src/Main.php",
"output": "bin/monitor.phar",
"stub": true
}
......@@ -4,7 +4,9 @@
"type": "project",
"require": {
"symfony/finder": "3.4.*",
"symfony/console": "3.4.*"
"symfony/console": "3.4.*",
"symfony/process": "3.4.*",
"kherge/box": "^2.7"
},
"require-dev": {
"phpunit/phpunit": "^7.2",
......@@ -16,5 +18,11 @@
"name": "Thibault Debatty",
"email": "thibault.debatty@gmail.com"
}
]
],
"autoload" : {
"psr-4" : {
"Monitor\\" : "src"
}
}
}
This diff is collapsed.
include.path=${php.global.include.path}
php.version=PHP_70
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=false
web.root=.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>php-client</name>
</data>
</configuration>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
#!/bin/sh
DATE=`date '+%Y%m%d.%H%M%S'`
echo $DATE > version
./vendor/bin/box build
echo "@dev" > version
<?php
namespace Monitor;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
/**
* Description of LoadAvg
*
* @author tibo
*/
class LoadAvg implements SensorInterface {
public function run() {
$process = new Process('cat /proc/loadavg');
$process->run();
return $process->getOutput();
}
}
#!/usr/bin/env php
<?php
/**
* Command line entry point.
*/
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/autoload.php';
use Symfony\Component\Console\Application;
$version = date(file_get_contents(__DIR__ . "/../version"));
$application = new Application("PHP Monitor Client", $version);
$application->add(new Monitor\PingCommand());
$application->run();
<?php
namespace Monitor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Description of PingCommand
*
* @author tibo
*/
class PingCommand extends Command {
protected function configure()
{
$this
->setName('ping')
->setDescription('Send ping to monitor server')
->addOption('server', 's', InputOption::VALUE_OPTIONAL, "Monitor address")
->addOption('id', 'i', InputOption::VALUE_OPTIONAL, "ID of this server")
->addOption('token', 't', InputOption::VALUE_OPTIONAL, "Token of this server");
}
/**
* Runs the command analyze:directory
* {@inheritDoc}
*
* @param InputInterface $input stdin reader
* @param OutputInterface $output stdout writer
*
* @see \Symfony\Component\Console\Command\Command::execute()
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$server = $input->getOption('server');
if ($server === null) {
$server = "https://monitor.web-d.be";
}
$myid = $input->getOption('id');
$mytoken = $input->getOption('token');
$version = file_get_contents(__DIR__ . "/../version");
$url = $server ."/api/record/$myid";
$data = array(
'token' => $mytoken,
'version' => $version);
$sensors = [
"loadavg" => LoadAvg::class,
"reboot" => Reboot::class,
"updates" => Updates::class
];
foreach ($sensors as $key => $sensor_class) {
$sensor = new $sensor_class();
$data[$key] = $sensor->run();
}
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result . "\n";
// var_dump($result);
}
}
<?php
namespace Monitor;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
/**
* Description of LoadAvg
*
* @author tibo
*/
class Reboot implements SensorInterface {
public function run() {
return file_exists("/var/run/reboot-required");
}
}
<?php
namespace Monitor;
/**
* Description of SensorInterface
*
* @author tibo
*/
interface SensorInterface {
public function run();
}
<?php
namespace Monitor;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
/**
* Description of LoadAvg
*
* @author tibo
*/
class Updates implements SensorInterface {
public function run() {
$process = new Process('cat /var/lib/update-notifier/updates-available');
$process->run();
return $process->getOutput();
}
}
<?php
spl_autoload_register(
function ($class) {
// project-specific namespace prefix
$prefix = 'Monitor';
// base directory for the namespace prefix
$base_dir = __DIR__;
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
include $file;
}
}
);
@dev
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment