Skip to content
Snippets Groups Projects
MemInfo.php 1.97 KiB
Newer Older
Tibo's avatar
Tibo committed
<?php

namespace App\Sensor;

use \App\AbstractSensor;

/**
 * Description of MemInfo
 *
 * @author tibo
 */
class MemInfo extends AbstractSensor {

    public function report() {
Tibo's avatar
Tibo committed

        return view("agent.meminfo", ["server" => $this->getServer()]);
    }

    public function usedMemoryPoints() {
Tibo's avatar
Tibo committed
        $records = $this->getLastRecords("memory", 288);

        $used = [];
        foreach ($records as $record) {
            $meminfo = $this->parseMeminfo($record->memory);
            $used[] = new Point(
Tibo's avatar
Tibo committed
                    $record->time * 1000, $meminfo->used() / 1000);
Tibo's avatar
Tibo committed
        }

Tibo's avatar
Tibo committed
        return $used;
    }

    public function cachedMemoryPoints() {
        $records = $this->getLastRecords("memory", 288);

        $points = [];
        foreach ($records as $record) {
            $meminfo = $this->parseMeminfo($record->memory);
            $points[] = new Point(
                    $record->time * 1000, $meminfo->cached / 1000);
        }

        return $points;
Tibo's avatar
Tibo committed
    }

    public function status() {
        return self::STATUS_OK;
    }

    // used = total - free - cached
    const MEMTOTAL = "/^MemTotal:\\s+([0-9]+) kB$/m";
    const MEMFREE = "/^MemFree:\\s+([0-9]+) kB$/m";
    const MEMCACHED = "/^Cached:\\s+([0-9]+) kB$/m";

    public function parseMeminfo($string) {
        return new Memory(
                $this->pregMatchOne(self::MEMTOTAL, $string),
                $this->pregMatchOne(self::MEMFREE, $string),
                $this->pregMatchOne(self::MEMCACHED, $string));
    }

    public function pregMatchOne($pattern, $string) {
        $matches = array();
        preg_match($pattern, $string, $matches);
        return $matches[1];
    }
}

class Memory {
    public $total;
    public $free;
    public $cached;

    public function __construct($total, $free, $cached) {
        $this->total = $total;
        $this->free = $free;
        $this->cached = $cached;
    }

    public function used() {
        return $this->total - $this->free - $this->cached;
    }
}