This commit is contained in:
2023-03-28 10:34:44 +02:00
parent f803effbbd
commit f7e0f37528
428 changed files with 9982 additions and 100 deletions

View File

@@ -0,0 +1,362 @@
<?php
if (!defined('PICONTROL')) exit();
class LogStatistic
{
private $file, $stream, $limit = -1, $length = 100;
function __destruct()
{
if (is_resource($this->stream))
fclose($this->stream);
}
public function setFile($pFile = '')
{
$this->file = $pFile;
}
public function setLimit($pLimit = -1)
{
$this->limit = $pLimit;
}
public function setLength($pLength = 100)
{
$this->length = $pLength;
}
public function add($entry, $moreThanOne = false)
{
if (!is_array($entry) || empty($entry))
return false;
if (!is_resource($this->stream))
$this->open();
if ($this->limit > -1)
$this->shortLog();
fseek($this->stream, 0, SEEK_END);
if ($moreThanOne === false)
fputcsv($this->stream, $entry);
else {
foreach ($entry as $item)
fputcsv($this->stream, $item);
}
return true;
}
public function getAll()
{
if (!is_resource($this->stream))
$this->open();
$entries = array();
fseek($this->stream, 0);
while (($entry = fgetcsv($this->stream, $this->length)) !== false)
$entries[] = $entry;
return $entries;
}
public function getLast()
{
if (!is_resource($this->stream))
$this->open();
$entries = $this->getAll();
if (is_array($entries))
return end($entries);
}
private function shortLog()
{
if ($this->limit == -1)
return false;
if (!is_array(($entries = $this->getAll())))
return false;
if (count($entries) >= $this->limit) {
$unsetLineCount = count($entries) - $this->limit;
for ($i = 0; $i <= $unsetLineCount; $i++)
unset($entries[$i]);
fseek($this->stream, 0);
ftruncate($this->stream, 0);
foreach ($entries as $entry)
fputcsv($this->stream, $entry);
}
return true;
}
public function deleteLog()
{
if (is_file($this->file)) {
if (unlink($this->file) or exit(_t('Konnte Log-Datei nicht l&ouml;schen: %s', $this->file)))
return true;
} else
return false;
}
public function clearLog()
{
if (!is_resource($this->stream))
$this->open();
fseek($this->stream, 0);
ftruncate($this->stream, 0);
}
public function close()
{
fclose($this->stream);
}
private function open()
{
if (!file_exists($this->file) || !is_file($this->file))
touch($this->file);
$this->stream = fopen($this->file, 'r+');# || exit(_t('Konnte Log-Datei nicht &ouml;ffnen: %s', $this->file));
}
}
class StatisticController
{
private $statistics = array();
public function __construct($tpl = NULL)
{
if (!isset($tpl))
return;
$jsTranslations = array();
$jsTranslations[] = 'Es sind noch keine Werte verf&uuml;gbar. Werte werden alle %%s Minuten eingetragen.';
$jsTranslations[] = 'Es ist ein Fehler aufgetreten! Fehler: %%s';
$jsTranslations[] = 'Zeit';
$jsTranslations[] = 'Es ist ein Fehler aufgetreten! Fehlercode: %%s';
$tpl->assign('jsTranslations', $jsTranslations, true);
}
public function loadStatistics($folder = NULL)
{
if ($folder == NULL)
$possibleFolders = array(LOG_PATH);
else
$possibleFolders = array(LOG_PATH . $folder);
foreach ($possibleFolders as $folder) {
$this->statistics = array_merge($this->statistics, $this->loadStatisticsIterator($folder));
}
}
private function loadStatisticsIterator($folder)
{
$fileNames = array();
$files = array_diff(@scandir($folder), array('..', '.'));
foreach ($files as $file) {
$absolutePath = $folder . '/' . $file;
if (is_file($absolutePath) && substr($file, -4) == '.csv') {
$fileName = str_replace(LOG_PATH, '', $folder) . substr($file, 0, -4);
$fileNames[substr(md5($fileName), 0, 8)] = $fileName;
} elseif (is_dir($absolutePath))
$fileNames = array_merge($fileNames, $this->loadStatisticsIterator($folder . $file . '/'));
}
return $fileNames;
}
public function getStatisticID($name)
{
if (empty($this->statistics))
$this->loadStatistics();
if (!is_string($name))
return false;
if (($id = array_search($name, $this->statistics)) !== false)
return $id;
return false;
}
public function getStatisticName($id)
{
if (empty($this->statistics))
$this->loadStatistics();
if (!is_string($id))
return false;
if (isset($this->statistics[$id]))
return $this->statistics[$id];
return false;
}
public function checkStatistic($value, $isID = false)
{
if (!is_string($value))
return false;
if (!is_bool($isID))
return false;
if ($isID === true) {
if ($this->getStatisticName($value) !== false)
return true;
else
return false;
} elseif ($isID === false) {
if ($this->getStatisticID($value) !== false)
return true;
else
return false;
}
return false;
}
public function getStatistics()
{
if (empty($this->statistics))
$this->loadStatistics();
return $this->statistics;
}
}
class StatisticBuilder
{
private $name, $columns, $title, $prefix, $suffix, $raw, $label, $unit, $cycle, $limits;
public function loadFromFile($name, $plugin = NULL)
{
$source = LIBRARY_PATH . 'statistic/statistic.config.php';
if ($plugin != NULL && is_string($plugin))
$source = PLUGINS_PATH . $plugin . '/plugin.statistic.config.php';
if (!file_exists($source) || !is_file($source))
return false;
include $source;
if (!isset($statisticConfig) || !is_array($statisticConfig))
return false;
$this->raw = $name;
if (strpos($name, '/') !== false) {
$explodes = explode('/', strrev($name), 2);
$name = strrev($explodes[0]);
$prefix = strrev($explodes[1]) . '/';
}
if (!isset($statisticConfig[$name]) && isset($statisticConfig[substr($name, 0, strpos($name, '_'))])) {
if (strpos($name, '_') !== false) {
$explodes = explode('_', strrev($name), 2);
$suffix = strrev($explodes[0]);
$name = strrev($explodes[1]);
}
}
if (!isset($statisticConfig[$name]))
return false;
$statistic = $statisticConfig[$name];
$this->name = $name;
$this->title = _t($statistic['title']);
$this->prefix = isset($prefix) ? $prefix : '';
$this->suffix = isset($suffix) ? $suffix : NULL;
$this->label = _t($statistic['label']);
$this->unit = $statistic['unit'];
if ($statistic['unit'] == 'MB') {
$log = new LogStatistic();
$log->setFile(LOG_PATH . $this->raw . '.csv');
$lastValue = $log->getLast();
$log->close();
if ($lastValue && $lastValue[1] > 999999999) {
$this->unit = 'GB';
$this->label = 'GB';
}
}
$this->cycle = $statistic['cycle'];
$this->limits = $statistic['limits'];
foreach ($statistic['columns'] as $column) {
foreach ($column as &$values) {
if (is_string($values))
$values = _t($values);
}
$this->columns[] = $column;
}
if (substr_count($this->title, '%s') == 1) {
$this->title = sprintf($this->title, $this->suffix);
}
if ($this->title == '')
$this->title = $this->suffix;
return true;
}
public function setTitle($title)
{
if (!is_string($title))
return false;
$this->title = $title;
}
public function getId()
{
return substr(md5($this->raw), 0, 8);
}
public function getArray()
{
return array(
'id' => $this->getId(),
'name' => $this->name,
'title' => $this->title,
'prefix' => $this->prefix,
'suffix' => $this->suffix,
'raw' => $this->raw,
'label' => $this->label,
'unit' => $this->unit,
'cycle' => $this->cycle,
'columns' => $this->columns,
'limits' => $this->limits
);
}
public function getJSON()
{
$json = json_encode(
array(
'id' => $this->getId(),
'label' => $this->label,
'unit' => $this->unit,
'cycle' => $this->cycle,
'columns' => $this->columns
)
);
return $json;
}
}

View File

@@ -0,0 +1,241 @@
<?php
$statisticConfig = array(
'coretemp' => array(
'title' => 'CPU-Temperatur',
'label' => 'Grad Celsius',
'unit' => '°C',
'cycle' => 5,
'columns' => array(
array(
'label' => 'Zeit',
'type' => 'datetime',
'downloadTitle' => 'Datum'
),
array(
'label' => 'Temperatur',
'type' => 'number',
'downloadTitle' => 'Temperatur in Grad Celsius'
)
),
'limits' => array(
'min' => array(
'value' => 0.95,
'use' => 'multiply',
'fix' => true
),
'max' => array(
'value' => 1.05,
'use' => 'multiply',
'fix' => true
)
)
),
'cpuload' => array(
'title' => 'CPU-Auslastung',
'label' => 'Auslastung %%',
'unit' => '%',
'cycle' => 5,
'columns' => array(
array(
'label' => 'Zeit',
'type' => 'datetime',
'downloadTitle' => 'Datum'
),
array(
'label' => 'Auslastung',
'type' => 'number',
'downloadTitle' => 'Auslastung in Prozent'
)
),
'limits' => array(
'min' => array(
'value' => 0.01,
'use' => 'fix',
'fix' => true
),
'max' => array(
'value' => 100,
'use' => 'fix',
'fix' => true
)
)
),
'cpufrequency' => array(
'title' => 'CPU-Takt',
'label' => 'MHz',
'unit' => 'MHz',
'cycle' => 5,
'columns' => array(
array(
'label' => 'Zeit',
'type' => 'datetime',
'downloadTitle' => 'Datum'
),
array(
'label' => 'Takt',
'type' => 'number',
'downloadTitle' => 'Auslastung in MHz'
)
),
'limits' => array(
'min' => array(
'value' => 0.01,
'use' => 'fix',
'fix' => true
),
'max' => array(
'value' => 2100,
'use' => 'fix',
'fix' => true
)
)
),
'ram' => array(
'title' => 'RAM-Auslastung',
'label' => 'Auslastung %%',
'unit' => '%',
'cycle' => 5,
'columns' => array(
array(
'label' => 'Zeit',
'type' => 'datetime',
'downloadTitle' => 'Datum'
),
array(
'label' => 'Auslastung',
'type' => 'number',
'downloadTitle' => 'Auslastung in Prozent'
)
),
'limits' => array(
'min' => array(
'value' => 0.01,
'use' => 'fix',
'fix' => true
),
'max' => array(
'value' => 100,
'use' => 'fix',
'fix' => true
)
)
),
'network' => array(
'title' => 'Netzwerkdaten - %%s',
'label' => 'Daten (MB)',
'unit' => 'MB',
'cycle' => 5,
'columns' => array(
array(
'label' => 'Zeit',
'type' => 'datetime',
'downloadTitle' => 'Datum'
),
array(
'label' => 'Gesendet',
'type' => 'number',
'downloadTitle' => 'Gesendet in Byte',
'division' => 1048576
),
array(
'label' => 'Empfangen',
'type' => 'number',
'downloadTitle' => 'Empfangen in Byte',
'division' => 1048576
)
),
'limits' => array(
'min' => array(
'value' => 0.90,
'use' => 'multiply',
'fix' => false
),
'max' => array(
'value' => 1.10,
'use' => 'multiply',
'fix' => false
)
)
),
'network_packets' => array(
'title' => 'Netzwerkpakete - %%s',
'label' => 'Pakete',
'unit' => 'Pakete',
'cycle' => 5,
'columns' => array(
array(
'label' => 'Zeit',
'type' => 'datetime',
'downloadTitle' => 'Datum'
),
array(
'label' => 'Gesendet',
'type' => 'number',
'downloadTitle' => 'Gesendete Pakete'
),
array(
'label' => 'Empfangen',
'type' => 'number',
'downloadTitle' => 'Empfangene Pakete'
)
),
'limits' => array(
'min' => array(
'value' => 0.90,
'use' => 'multiply',
'fix' => false
),
'max' => array(
'value' => 1.10,
'use' => 'multiply',
'fix' => false
)
)
),
'memory' => array(
'title' => 'Speicherverbrauch',
'label' => 'MB',
'unit' => 'MB',
'cycle' => 5,
'columns' => array(
array(
'label' => 'Zeit',
'type' => 'datetime',
'downloadTitle' => 'Datum'
),
array(
'label' => 'Gesamt',
'type' => 'number',
'downloadTitle' => 'Gesamt in Byte',
'division' => 1048576,
'style' => array(
'color' => '#3366cc',
'type' => 'line',
'lineDashStyle' => 10
)
),
array(
'label' => 'Belegt',
'type' => 'number',
'downloadTitle' => 'Belegt in Byte',
'division' => 1048576,
'style' => array(
'color' => '#3366cc'
)
)
),
'limits' => array(
'min' => array(
'value' => 0.01,
'use' => 'fix',
'fix' => true
),
'max' => array(
'value' => 1.10,
'use' => 'multiply',
'fix' => false
)
)
)
);
?>

View File

@@ -0,0 +1,141 @@
<?php
if (!defined('PICONTROL')) exit();
function minNotNull($values)
{
return min(array_diff(array_map('intval', $values), array(0)));
}
function getValueRow($value)
{
return array('v' => floatval(str_replace(array("\n", ','), array('', '.'), $value)));
}
function calculatePeriods(&$info, $lastTime)
{
$info['periods']['seven'] = ($lastTime - 604800) * 1000;
$info['periods']['six'] = ($lastTime - 518400) * 1000;
$info['periods']['five'] = ($lastTime - 432000) * 1000;
$info['periods']['four'] = ($lastTime - 345600) * 1000;
$info['periods']['three'] = ($lastTime - 259200) * 1000;
$info['periods']['two'] = ($lastTime - 172800) * 1000;
$info['periods']['one'] = ($lastTime - 86400) * 1000;
}
function calculateEmptyRows(&$arr, $columns, $firstTime, $lastTime, $cycle)
{
$buffer = array();
if ($lastTime < (time() - ($cycle * 60))) {
for ($i = 0; $i < ceil(((time() - ($cycle * 60)) - $lastTime) / ($cycle * 60)); $i++) {
$dummy = array($lastTime + (($i + 1) * ($cycle * 60)));
for ($j = 1; $j < count($columns); $j++)
$dummy[] = 0;
$buffer[] = $dummy;
}
$arr['rows'] = array_merge($arr['rows'], $buffer);
}
$buffer = array();
if (isset($arr['rows']) && count($arr['rows']) < (604800 / ($cycle * 60))) {
for ($i = 0; $i < ((604800 / ($cycle * 60)) - count($arr['rows'])); $i++) {
$dummy = array($firstTime - (($i + 1) * ($cycle * 60)));
for ($j = 1; $j < count($columns); $j++)
$dummy[] = 0;
$buffer[] = $dummy;
}
$arr['rows'] = array_merge(array_reverse($buffer), $arr['rows']);
}
}
function getRowsFromLog(&$arr, &$info, $log, $columns, $cycle)
{
$lastTime = NULL;
$firstTime = 0;
$leapyear = -1;
$leapyearSkip = 0;
foreach ($log as $row) {
foreach ($row as &$data)
$data = trim($data);
if ($leapyearSkip != 0) {
$leapyearSkip--;
continue;
}
if ($leapyear != -1 && $leapyear > date('I', $row[0])) {
$leapyear = 0;
$leapyearSkip = (60 / $cycle) - 1;
continue;
}
$leapyear = date('I', $row[0]);
if ($lastTime !== NULL && ($lastTime + ($cycle * 60) + 100) < $row[0]) {
$skipped = round(($row[0] - ($lastTime + ($cycle * 60))) / ($cycle * 60));
for ($i = 0; $i < $skipped; $i++) {
$dummy = array($lastTime + (($i + 1) * ($cycle * 60)));
for ($j = 1; $j < count($columns); $j++)
$dummy[] = 0;
$arr['rows'][] = $dummy;
}
}
$dummy = array((int) $row[0]);
for ($i = 1; $i < count($columns); $i++) {
$rowFloat = 0;
if (isset($columns[$i]['division'])) {
if ((float)$log[count($log) - 1][1] > 999999999)
$rowFloat = (float) round(str_replace(array("\n", ','), array('', '.'), $row[$i]) / $columns[$i]['division'] / 1024, 2);
else
$rowFloat = (float) round(str_replace(array("\n", ','), array('', '.'), $row[$i]) / $columns[$i]['division'], 2);
} elseif (isset($columns[$i]['multiplication']))
$rowFloat = (float) round(str_replace(array("\n", ','), array('', '.'), $row[$i]) * $columns[$i]['multiplication'], 2);
else
$rowFloat = (float) $row[$i];
if (!isset($info['min']) || $rowFloat < $info['min'])
$info['min'] = $rowFloat;
if (!isset($info['max']) || $rowFloat > $info['max'])
$info['max'] = $rowFloat;
$dummy[] = $rowFloat;
}
$arr['rows'][] = $dummy;
if ($firstTime == 0)
$firstTime = $row[0];
$lastTime = $row[0];
}
calculateEmptyRows($arr, $columns, $firstTime, $lastTime, $cycle);
calculatePeriods($info, $lastTime);
}
function convertForGoogleChart($rows)
{
foreach ($rows as $index => $row) {
$dummy = array('c' => array(array('v' => 'Date(' . date('Y,' . (date('m', $row[0]) - 1) . ',d,H,i', $row[0]) . ')')));
unset($row[0]);
foreach ($row as $inde2 => $row2)
$dummy['c'][] = array('v' => $row2);
$rows[$index] = $dummy;
}
return $rows;
}