Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
44a1b85
Migrate to Laravel Zero
jbrooksuk May 5, 2022
b88b6e3
Merge branch 'master' into laravel-zero
jbrooksuk May 5, 2022
33638ab
wip
jbrooksuk May 5, 2022
87b50ef
Add workflow to build application
jbrooksuk May 5, 2022
0615989
wip
jbrooksuk May 5, 2022
7c80880
wip
jbrooksuk May 5, 2022
1399ede
wip
jbrooksuk May 5, 2022
7375d7f
Add sudo
jbrooksuk May 5, 2022
4a5f8b9
wip
jbrooksuk May 5, 2022
56a18b4
Use HTTPS
jbrooksuk May 5, 2022
46ba377
wip
jbrooksuk May 5, 2022
e9ab497
wip
jbrooksuk May 5, 2022
fa52bd2
wip
jbrooksuk May 5, 2022
63bb892
wip
jbrooksuk May 6, 2022
82863b5
wip
jbrooksuk May 6, 2022
858dc62
wip
jbrooksuk May 6, 2022
a7086d2
anonymous migrations
jbrooksuk May 9, 2022
73f90d9
wip
jbrooksuk May 9, 2022
b21e9d7
wip
jbrooksuk May 9, 2022
696a818
wip
jbrooksuk May 9, 2022
820f3ee
wip
jbrooksuk May 9, 2022
ee8fe0f
wip
jbrooksuk May 9, 2022
79e0161
wip
jbrooksuk May 9, 2022
550f0df
wip
jbrooksuk May 9, 2022
8a56d8a
Fix permissions
jbrooksuk May 9, 2022
bbe50b6
wip
jbrooksuk May 9, 2022
50f4aba
wip
jbrooksuk May 9, 2022
fa1042b
wip
jbrooksuk May 9, 2022
9194c77
wip
jbrooksuk May 9, 2022
6cc70f8
wip
jbrooksuk May 9, 2022
5c93b39
wip
jbrooksuk May 9, 2022
bc86294
Fix code style
jbrooksuk Jun 23, 2022
8a77864
Upload binary
jbrooksuk Jul 27, 2022
f55d4a1
wip
jbrooksuk Jul 27, 2022
a40f819
Run through Pint
jbrooksuk Jul 27, 2022
d13800c
wip
jbrooksuk Jul 27, 2022
be0dc67
wip
jbrooksuk Jul 27, 2022
791c078
wip
jbrooksuk Jul 27, 2022
4181149
Use ref
jbrooksuk Jul 27, 2022
c5a2135
Merge branch 'master' into laravel-zero
jbrooksuk Jul 27, 2022
7fc7ba2
update deps
jbrooksuk Jul 27, 2022
134ec50
Use actions/checkout
jbrooksuk Aug 5, 2022
6b10a0b
Fix repo
jbrooksuk Aug 5, 2022
485a450
Cache micro-sfx
jbrooksuk Aug 5, 2022
a731532
wip
jbrooksuk Aug 5, 2022
0f16752
wip
jbrooksuk Aug 5, 2022
3306386
wip
jbrooksuk Aug 5, 2022
63fe292
wip
jbrooksuk Aug 5, 2022
42cb15f
wip
jbrooksuk Aug 5, 2022
f889985
enable dom
jbrooksuk Aug 5, 2022
c9bdff7
wip
jbrooksuk Aug 5, 2022
6b05586
wip
jbrooksuk Aug 5, 2022
b56210f
wip
jbrooksuk Aug 5, 2022
3aecd3c
Update environment support
jbrooksuk Aug 25, 2022
ef05de6
Add timezone config
jbrooksuk Aug 25, 2022
7b42664
Fix bug
jbrooksuk Aug 25, 2022
0a2d805
wip
jbrooksuk Aug 25, 2022
5350b36
change build version
jbrooksuk Aug 31, 2022
fb36070
wip
jbrooksuk Aug 31, 2022
6b345d1
Update readme
jbrooksuk Sep 2, 2022
1ceade3
Build on Ubuntu 18.04
jbrooksuk Sep 8, 2022
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
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ trim_trailing_whitespace = true
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_style = space
indent_size = 2
13 changes: 1 addition & 12 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

MONITOR_ENDPOINT=

LOG_CHANNEL=stack

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
DB_DATABASE=database.sqlite
8 changes: 5 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
* text=auto
*.css linguist-vendored
*.scss linguist-vendored
*.js linguist-vendored
/.github export-ignore
.styleci.yml export-ignore
.scrutinizer.yml export-ignore
BACKERS.md export-ignore
CONTRIBUTING.md export-ignore
CHANGELOG.md export-ignore
115 changes: 115 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: Build

on: [push, pull_request]

jobs:
prepare:
name: Compile PHP with Micro
runs-on: ubuntu-18.04

steps:
- name: Install Dependencies
run: sudo apt-get -y install flex bison re2c libonig-dev libsqlite3-dev

- name: Checkout PHP
uses: actions/checkout@v3
with:
repository: php/php-src
ref: PHP-8.0.19
path: php-src

- name: Checkout PHPMicro
uses: actions/checkout@v3
with:
repository: dixyes/phpmicro
ref: master
path: php-src/sapi/micro

- name: Apply PHPMicro Patch
run: |
cd php-src
patch -p1 < sapi/micro/patches/cli_checks_80.patch

- name: Build
run: |
cd php-src
./buildconf --force

- name: Cache micro.sfx
id: cache-micro-sfx
uses: actions/cache@v3
with:
path: php-src/sapi/micro/micro.sfx
key: ${{ runner.os }}-micro-sfx-libxmldom

- name: Configure
if: steps.cache-micro-sfx-libxmldom.outputs.cache-hit != 'true'
run: |
cd php-src
./configure --disable-phpdbg --disable-cgi --disable-cli --disable-all --enable-micro --enable-phar --enable-pdo --enable-dom --with-libxml --with-zlib --with-iconv --enable-filter --with-pdo-sqlite --with-sqlite3

- name: Make Micro
if: steps.cache-micro-sfx-libxmldom.outputs.cache-hit != 'true'
run: |
cd php-src
make micro

- uses: actions/upload-artifact@v2
if: steps.cache-micro-sfx-libxmldom.outputs.cache-hit != 'true'
with:
name: micro
path: php-src/sapi/micro/micro.sfx

build-forge-monitor:
needs: prepare

name: Build Forge Monitor
runs-on: ubuntu-latest

steps:
- name: Extract the tag version
id: tag
run: echo ::set-output name=tag::${GITHUB_REF##*v}

- name: Checkout the code
uses: actions/checkout@v3

- name: Obtain Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.0"
extensions: json, dom, curl, libxml, mbstring, zip
coverage: none
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

- name: Build Forge Monitor
run: ./application app:build --build-version=${{ github.ref_name }}

- uses: actions/cache@v3
with:
path: php-src/sapi/micro/micro.sfx
key: ${{ runner.os }}-micro-sfx-libxmldom

- name: Compile Forge Monitor
run: cat php-src/sapi/micro/micro.sfx ./builds/application > monitor

- name: Upload the binary to release
uses: actions/upload-artifact@v3
with:
name: monitor
path: monitor
7 changes: 7 additions & 0 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: fix code styling

on: [push]

jobs:
compile:
uses: laravel/.github/.github/workflows/coding-standards.yml@main
18 changes: 7 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
/.idea
/.vscode
/.vagrant
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.monitor
/builds
/database/database.sqlite
micro.sfx
.env
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
# Forge Monitor

Laravel Forge monitoring built with Laravel.
Laravel Forge monitoring built with [Laravel Zero](https://laravel-zero.com).

## Building & Compiling

The monitoring agent is built using Laravel Zero, a CLI framework for console applications. We then take the generated PHAR archive and compile this with [phpmicro](https://github.com/easysoft/phpmicro).

You can check the `build.yml` file to see the steps required to compile the application.

## Installation

The monitoring agent is expected to be installed to `/root/forge-monitor`. You'll also need to `chmod +x monitor` so that it can be executed.

You'll then need to create a `.env` file in the same directory:

```env
DB_DATABASE=/root/forge-monitor/database.sqlite
```

## Running

You can run Forge Monitor with:

```bash
php artisan stat:mem
./monitor stat:mem
```

The available stat commands are:
Expand All @@ -31,7 +47,7 @@ For the `disk` monitors, this threshold will only need to be met once at any giv

## Data Storage

Forge Monitor writes to a local SQLite database at `./database/database.db`, giving us a history of recent stat points.
Forge Monitor writes to a local SQLite database, giving us a history of recent stat points.

After a sample of the stat has been taken, we will check whether each configured monitor of the stat type has been met. This can be done with SQL queries executed against the DB.

Expand Down
4 changes: 2 additions & 2 deletions app/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Alert extends Model
/**
* Create an alert for a monitor's state.
*
* @param \App\Monitors\Monitor $monitor
* @param string $state
* @param \App\Monitors\Monitor $monitor
* @param string $state
* @return $this
*/
public static function createForMonitor(Monitor $monitor, $state)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<?php

namespace App\Console\Commands;
namespace App\Commands;

use App\Monitors\MonitorConfig;
use Exception;
use Illuminate\Console\Command;
use LaravelZero\Framework\Commands\Command;

abstract class AbstractStatCommand extends Command
{
/**
* The monitor config instance.
*
* @var \App\Monitors\MonitorConfig
*/
protected $monitorConfig;

/**
* The configured monitors for the stat.
*
Expand All @@ -32,10 +39,11 @@ public function __construct(MonitorConfig $monitorConfig)
{
parent::__construct();

if (!$this->statType) {
if (! $this->statType) {
throw new Exception('No statType defined.');
}

$this->monitorConfig = $monitorConfig;
$this->monitors = $monitorConfig->forType($this->statType);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace App\Console\Commands;
namespace App\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
use LaravelZero\Framework\Commands\Command;

class CleanupCommand extends Command
{
Expand All @@ -12,7 +13,7 @@ class CleanupCommand extends Command
*
* @var string
*/
protected $signature = 'app:clean-up';
protected $signature = 'stat:clean-up';

/**
* The console command description.
Expand All @@ -32,4 +33,19 @@ public function handle()
DB::delete('DELETE FROM disk_usages WHERE created_at <= DATETIME("NOW", "-INTERVAL 1 WEEK")');
DB::delete('DELETE FROM memory_usages WHERE created_at <= DATETIME("NOW", "-INTERVAL 1 WEEK")');
}

/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule)
{
if (config('app.env') === 'production') {
$schedule->exec('/root/forge-monitor/monitor stat:clean-up')->everyMinute();
} else {
$schedule->command(static::class)->everyMinute();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace App\Console\Commands;
namespace App\Commands;

use Illuminate\Console\Scheduling\Schedule;

class DiskStatCommand extends AbstractStatCommand
{
Expand Down Expand Up @@ -42,15 +44,19 @@ class DiskStatCommand extends AbstractStatCommand
public function handle()
{
// Don't run when no monitors are configured.
if ($this->monitors->isEmpty()) {
$this->verboseInfo("No disk monitors configured...");
// if ($this->monitors->isEmpty()) {
// $this->verboseInfo('No disk monitors configured...');
//
// return;
// }

return;
}
$this->verboseInfo(sprintf('Monitor Config: %s...', $this->monitorConfig->getConfigPath()));

return;

$this->monitors->each(function ($monitor) {
// Take the sample if we haven't done so already.
if (!$this->sampleTaken) {
if (! $this->sampleTaken) {
$this->sampleTaken = true;

return $monitor->stat()->sample();
Expand All @@ -61,4 +67,19 @@ public function handle()
$monitor->stat()->test();
});
}

/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule)
{
if (config('app.env') === 'production') {
$schedule->exec('/root/forge-monitor/monitor stat:disk')->everyMinute();
} else {
$schedule->command(static::class)->everyMinute();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace App\Console\Commands;
namespace App\Commands;

trait InteractsWithCli
{
/**
* Write a string as information output if verbose enough.
*
* @param string $line
* @param string $line
* @return void
*/
protected function verboseInfo($line)
Expand Down
Loading