Files
librenms-librenms/app/Polling/Measure/MeasurementManager.php
Tony Murray c79b187d72 Poller rewrite (Try 2) (#13525)
* core WIP

* try to finish up

* trim space too
and a couple of cleanups

* update test data

* put escapes back

* another net-snmp difference

* correct copy paste error

* WIP

* Use new code YAY

* a tiny bit more

* Kind of working

* Handle manual modules correctly

* convert core to modern module

* Only save metrics if modules is not overridden

* correct module exists check

* database error handling

* debug handling

* restore bad changes

* Introduce Actions
 RunAlertRulesAction
 UpdateDeviceGroupsAction

* tweaks to output

* Fix some issues in outside code

* Style fixes

* fixes to module status checks

* typehints!

* Use logger only and DI

* OS module not named correctly

* Work on quiet output a bit more

* generically don't change output when disabling debug if the driver is already stack

* Fix missing $device variable for legacy os polling
Fix missing dbFacile functions when no legacy modules polled in RunAlertRulesAction

* restore legacy os module shim

* use the new poller code for tests

* PollingDevice event

* Fix some issues and enable/disable error reporting around legacy modules

* typehints

* fully update baseline

* Use Process for version commands so we don't leak debug output.

* don't detect rrdtool version in ci every time

* style fixes

* Warning fixes

* more fixes

* re-update baseline

* remove diff noise

* fix up alerts

* Catch exceptions in device ip lookup

* Revert accidental snmp.inc.php poller target change
(should have been ?: not ??)
2021-11-17 19:23:55 -06:00

161 lines
4.8 KiB
PHP

<?php
/**
* MeasurementManager.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2021 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Polling\Measure;
use DB;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Collection;
use Log;
class MeasurementManager
{
const SNMP_COLOR = "\e[0;36m";
const DB_COLOR = "\e[1;33m";
const DATASTORE_COLOR = "\e[0;32m";
const NO_COLOR = "\e[0m";
/**
* @var \Illuminate\Support\Collection<MeasurementCollection>
*/
private static $categories;
public function __construct()
{
if (self::$categories === null) {
self::$categories = new Collection;
self::$categories->put('snmp', new MeasurementCollection());
self::$categories->put('db', new MeasurementCollection());
}
}
/**
* Register DB listener to record sql query stats
*/
public function listenDb(): void
{
DB::listen(function (QueryExecuted $event) {
$type = strtolower(substr($event->sql, 0, strpos($event->sql, ' ')));
$this->recordDb(Measurement::make($type, $event->time ? $event->time / 100 : 0));
});
}
/**
* Update statistics for the given category
*/
public function record(string $category, Measurement $measurement): void
{
$this->getCategory($category)->record($measurement);
}
/**
* Update statistics for db operations
*/
public function recordDb(Measurement $measurement): void
{
$this->record('db', $measurement);
}
/**
* Print out the stats totals since the last checkpoint
*/
public function printChangedStats(): void
{
$dsStats = app('Datastore')->getStats()->map(function (MeasurementCollection $stats, $datastore) {
return sprintf('%s%s%s: [%d/%.2fs]', self::DATASTORE_COLOR, $datastore, self::NO_COLOR, $stats->getCountDiff(), $stats->getDurationDiff());
});
Log::info(sprintf(
'>> %sSNMP%s: [%d/%.2fs] %sMySQL%s: [%d/%.2fs] %s',
self::SNMP_COLOR,
self::NO_COLOR,
$this->getCategory('snmp')->getCountDiff(),
$this->getCategory('snmp')->getDurationDiff(),
self::DB_COLOR,
self::NO_COLOR,
$this->getCategory('db')->getCountDiff(),
$this->getCategory('db')->getDurationDiff(),
$dsStats->implode(' ')
));
$this->checkpoint();
}
/**
* Make a new checkpoint so to compare against
*/
public function checkpoint(): void
{
self::$categories->each->checkpoint();
app('Datastore')->getStats()->each->checkpoint();
}
/**
* Record a measurement for snmp
*/
public function recordSnmp(Measurement $measurement): void
{
$this->record('snmp', $measurement);
}
/**
* Print global stat arrays
*/
public function printStats(): void
{
$this->printSummary('SNMP', $this->getCategory('snmp'), self::SNMP_COLOR);
$this->printSummary('SQL', $this->getCategory('db'), self::DB_COLOR);
app('Datastore')->getStats()->each(function (MeasurementCollection $stats, string $datastore) {
$this->printSummary($datastore, $stats, self::DATASTORE_COLOR);
});
}
public function getCategory(string $category): MeasurementCollection
{
if (! self::$categories->has($category)) {
self::$categories->put($category, new MeasurementCollection());
}
return self::$categories->get($category);
}
public function printSummary(string $name, MeasurementCollection $collection, string $color = ''): void
{
$summaries = $collection->map(function (MeasurementSummary $stat) {
return sprintf('%s[%d/%.2fs]', ucfirst($stat->getType()), $stat->getCount(), $stat->getDuration());
});
Log::info(sprintf('%s%s%s [%d/%.2fs]: %s',
$color,
$name,
$color ? self::NO_COLOR : '',
$collection->getTotalCount(),
$collection->getTotalDuration(),
$summaries->implode(' ')
));
}
}