Skip to content

Commit

Permalink
SRE-48 Storage space audit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitris Spachos committed Nov 9, 2020
1 parent a911677 commit 64b867f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Policies/storage_space.policy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
title: "Storage Space"
class: \Drutiny\algm\Audit\StorageSpace
name: algm:StorageSpace
tags:
- Space
- Storage
description: |
A policy to notify when disk usage goes below a certain percentage threshold
success: |
{{ status }}
failure: |
{{ status }}
warning: |
{{ warning_message }}
parameters:
threshold_percent:
default: 10
98 changes: 98 additions & 0 deletions src/Audit/StorageSpace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Drutiny\algm\Audit;

use Drutiny\Audit;
use Drutiny\Sandbox\Sandbox;
use Drutiny\Annotation\Param;
use Drutiny\Annotation\Token;
use Drutiny\Target\DrushTarget;

/**
* Storage space notifier.
*
* @Token(
* name = "status",
* type = "string",
* description = "Status message"
* )
* @Token(
* name = "warning_message",
* type = "string",
* description = "Warning message"
* )
* @Param(
* name = "threshold_percent",
* type = "integer",
* description = "Storage space threshold to notify if disk space is under",
* )
*/
class StorageSpace extends Audit {
const MAX_STRING_LENGTH = 60;

/**
* Check that target is actually a DrushTarget
*
* @param Sandbox $sandbox
* @return void
*/
protected function requireDrushTarget(Sandbox $sandbox){
return $sandbox->getTarget() instanceof DrushTarget;
}

/**
* @inheritdoc
*/
public function audit(Sandbox $sandbox) {

$threshold = (int) $sandbox->getParameter('threshold_percent');
$status = $sandbox->drush(['format' => 'json'])->status();

if ($status === null) {
return AUDIT::ERROR;
}

try {
$output = $sandbox->exec("df -H");
}
catch (Exception $e) {
return Audit::ERROR;
}

$disk = array_map(function($line) {
$elements = preg_split('/\s+/', $line);
return([
'filesystem' => isset($elements[0]) ? $elements[0] : '',
'size' => isset($elements[1]) ? $elements[1] : '',
'used' => isset($elements[2]) ? $elements[2] : '',
'available' => isset($elements[3]) ? $elements[3] : '',
'use%' => isset($elements[4]) ? $elements[4] : '',
'mounted' => isset($elements[5]) ? $elements[5] : '',
]);
}, explode("\n",$output));

array_shift($disk);
$storage_warnings = [];
foreach ($disk as $item) {
if (!empty($item['use%'])) {
$use = (int) str_replace('%', '', $item['use%']);
if ((100-$use) <= $threshold) {
$free_space = 100 - (int) str_replace('%', '', $item['use%']) . '%';
$storage_warnings[] = "⚠️\t" .$item['filesystem'] . ' free space: ' . $free_space;
}
} //end if
} //end for

if (count($storage_warnings)) {
$msg = "The following filesystems are bellow or euqal to the threshold ({$threshold}%)." . PHP_EOL;
foreach ($storage_warnings as $item) {
$msg .= $item . PHP_EOL;
}
$sandbox->setParameter('warning_message', $msg);
return Audit::WARNING;
}

$sandbox->setParameter('status', "All filestystems have free space above threshhold ({$threshold}%).");
return Audit::SUCCESS;
}
}

0 comments on commit 64b867f

Please sign in to comment.