Skip to content

Commit

Permalink
Merge branch 'master' into poller-deprecate
Browse files Browse the repository at this point in the history
  • Loading branch information
murrant authored Oct 4, 2023
2 parents 74d724d + c125832 commit 75e5d21
Show file tree
Hide file tree
Showing 138 changed files with 27,131 additions and 1,536 deletions.
32 changes: 10 additions & 22 deletions LibreNMS/Alert/AlertUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
use App\Models\Device;
use App\Models\User;
use DeviceCache;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use LibreNMS\Config;
use PHPMailer\PHPMailer\PHPMailer;

Expand Down Expand Up @@ -168,28 +168,16 @@ public static function findContactsSysContact(array $results): array

public static function findContactsOwners(array $results): array
{
return User::whereNot('email', '')->whereIn('user_id', function (\Illuminate\Database\Query\Builder $query) use ($results) {
$tables = [
'bill_id' => 'bill_perms',
'port_id' => 'ports_perms',
'device_id' => 'devices_perms',
];

$first = true;
foreach ($tables as $column => $table) {
$ids = array_filter(Arr::pluck($results, $column)); // find IDs for this type

if (! empty($ids)) {
if ($first) {
$query->select('user_id')->from($table)->whereIn($column, $ids);
$first = false;
} else {
$query->union(DB::table($table)->select('user_id')->whereIn($column, $ids));
}
}
return User::whereNot('email', '')->where(function (Builder $query) use ($results) {
if ($device_ids = array_filter(Arr::pluck($results, 'device_id'))) {
$query->orWhereHas('devicesOwned', fn ($q) => $q->whereIn('devices_perms.device_id', $device_ids));
}
if ($port_ids = array_filter(Arr::pluck($results, 'port_id'))) {
$query->orWhereHas('portsOwned', fn ($q) => $q->whereIn('ports_perms.port_id', $port_ids));
}
if ($bill_ids = array_filter(Arr::pluck($results, 'bill_id'))) {
$query->orWhereHas('bills', fn ($q) => $q->whereIn('bill_perms.bill_id', $bill_ids));
}

return $query;
})->pluck('realname', 'email')->all();
}

Expand Down
3 changes: 1 addition & 2 deletions LibreNMS/Alert/Transport/Linemessagingapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
namespace LibreNMS\Alert\Transport;

use LibreNMS\Alert\Transport;
use LibreNMS\Config;
use LibreNMS\Exceptions\AlertTransportDeliveryException;
use LibreNMS\Util\Http;

class LineMessagingAPI extends Transport
class Linemessagingapi extends Transport
{
protected string $name = 'LINE Messaging API';

Expand Down
3 changes: 2 additions & 1 deletion LibreNMS/Data/Store/Datastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@

use Illuminate\Support\Collection;
use LibreNMS\Config;
use LibreNMS\Interfaces\Data\DataStorageInterface;
use LibreNMS\Interfaces\Data\Datastore as DatastoreContract;

class Datastore
class Datastore implements DataStorageInterface
{
protected $stores;

Expand Down
42 changes: 20 additions & 22 deletions LibreNMS/Device/Availability.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

namespace LibreNMS\Device;

use App\Models\Device;
use App\Models\DeviceOutage;
use Illuminate\Support\Collection;
use LibreNMS\Util\Number;

class Availability
Expand All @@ -37,28 +39,28 @@ class Availability
* 1 year 365 * 24 * 60 * 60 = 31536000
*/

public static function day($device, $precision = 3)
public static function day(Device $device, int $precision = 3): float
{
$duration = 86400;

return self::availability($device, $duration, $precision);
}

public static function week($device, $precision = 3)
public static function week(Device $device, int $precision = 3): float
{
$duration = 604800;

return self::availability($device, $duration, $precision);
}

public static function month($device, $precision = 3)
public static function month(Device $device, int $precision = 3): float
{
$duration = 2592000;

return self::availability($device, $duration, $precision);
}

public static function year($device, $precision = 3)
public static function year(Device $device, int $precision = 3): float
{
$duration = 31536000;

Expand All @@ -68,17 +70,13 @@ public static function year($device, $precision = 3)
/**
* addition of all recorded outages in seconds
*
* @param object $found_outages filtered database object with all recorded outages
* @param Collection<DeviceOutage> $found_outages filtered database object with all recorded outages
* @param int $duration time period to calculate for
* @param int $now timestamp for 'now'
* @return int sum of all matching outages in seconds
*/
protected static function outageSummary($found_outages, $duration, $now = null)
protected static function outageSummary(Collection $found_outages, int $duration, int $now): int
{
if (! is_numeric($now)) {
$now = time();
}

// sum up time period of all outages
$outage_sum = 0;
foreach ($found_outages as $outage) {
Expand All @@ -103,26 +101,26 @@ protected static function outageSummary($found_outages, $duration, $now = null)
* means, starting with 100% as default
* substracts recorded outages
*
* @param array $device device to be looked at
* @param Device $device device to be looked at
* @param int $duration time period to calculate for
* @param int $precision float precision for calculated availability
* @return float calculated availability
*/
public static function availability($device, $duration, $precision = 3, $now = null)
public static function availability(Device $device, int $duration, int $precision = 3): float
{
if (! is_numeric($now)) {
$now = time();
}
$now = time();

$query = DeviceOutage::where('device_id', '=', $device['device_id'])
->where('up_again', '>=', $now - $duration)
->orderBy('going_down');
$found_outages = $device->outages()->where('up_again', '>=', $now - $duration)
->orderBy('going_down')->get();

$found_outages = $query->get();
// no recorded outages found, so use current status
if ($found_outages->isEmpty()) {
return 100 * $device->status;
}

// no recorded outages found, so use current uptime
if (! count($found_outages)) {
return 100 * 1;
// don't calculate for time when the device didn't exist
if ($device->inserted) {
$duration = min($duration, $device->inserted->diffInSeconds());
}

$outage_summary = self::outageSummary($found_outages, $duration, $now);
Expand Down
9 changes: 5 additions & 4 deletions LibreNMS/Device/Sensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use LibreNMS\Interfaces\Polling\PollerModule;
use LibreNMS\OS;
use LibreNMS\RRD\RrdDefinition;
use LibreNMS\Util\StringHelpers;

class Sensor implements DiscoveryModule, PollerModule
{
Expand Down Expand Up @@ -510,22 +511,22 @@ private function getUniqueId()

protected static function getDiscoveryInterface($type)
{
return str_to_class($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\') . 'Discovery';
return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\') . 'Discovery';
}

protected static function getDiscoveryMethod($type)
{
return 'discover' . str_to_class($type);
return 'discover' . StringHelpers::toClass($type, null);
}

protected static function getPollingInterface($type)
{
return str_to_class($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\') . 'Polling';
return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\') . 'Polling';
}

protected static function getPollingMethod($type)
{
return 'poll' . str_to_class($type);
return 'poll' . StringHelpers::toClass($type, null);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions LibreNMS/Device/WirelessSensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use LibreNMS\Config;
use LibreNMS\OS;
use LibreNMS\Util\StringHelpers;

class WirelessSensor extends Sensor
{
Expand Down Expand Up @@ -229,22 +230,22 @@ public static function getTypes($valid = false, $device_id = null)

protected static function getDiscoveryInterface($type)
{
return str_to_class($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\Wireless') . 'Discovery';
return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\Wireless') . 'Discovery';
}

protected static function getDiscoveryMethod($type)
{
return 'discoverWireless' . str_to_class($type);
return 'discoverWireless' . StringHelpers::toClass($type, null);
}

protected static function getPollingInterface($type)
{
return str_to_class($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\Wireless') . 'Polling';
return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\Wireless') . 'Polling';
}

protected static function getPollingMethod($type)
{
return 'pollWireless' . str_to_class($type);
return 'pollWireless' . StringHelpers::toClass($type, null);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion LibreNMS/IRCBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use App\Models\User;
use LibreNMS\DB\Eloquent;
use LibreNMS\Enum\AlertState;
use LibreNMS\Util\Mail;
use LibreNMS\Util\Number;
use LibreNMS\Util\Time;
use LibreNMS\Util\Version;
Expand Down Expand Up @@ -706,7 +707,7 @@ private function _auth($params)
$this->log("Auth for '" . $params[0] . "', ID: '" . $user->user_id . "', Token: '" . $token . "', Mail: '" . $user->email . "'");
}

if (send_mail($user->email, 'LibreNMS IRC-Bot Authtoken', "Your Authtoken for the IRC-Bot:\r\n\r\n" . $token . "\r\n\r\n") === true) {
if (Mail::send($user->email, 'LibreNMS IRC-Bot Authtoken', "Your Authtoken for the IRC-Bot:\r\n\r\n" . $token . "\r\n\r\n", false) === true) {
return $this->respond('Token sent!');
} else {
return $this->respond('Sorry, seems like mail doesnt like us.');
Expand Down
46 changes: 46 additions & 0 deletions LibreNMS/Interfaces/Data/DataStorageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* DataStorageInterface.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 2023 Tony Murray
* @author Tony Murray <[email protected]>
*/

namespace LibreNMS\Interfaces\Data;

interface DataStorageInterface
{
/**
* Datastore-independent function which should be used for all polled metrics.
*
* RRD Tags:
* rrd_def RrdDefinition
* rrd_name array|string: the rrd filename, will be processed with rrd_name()
* rrd_oldname array|string: old rrd filename to rename, will be processed with rrd_name()
* rrd_step int: rrd step, defaults to 300
*
* @param array $device
* @param string $measurement Name of this measurement
* @param array $tags tags for the data (or to control rrdtool)
* @param array|mixed $fields The data to update in an associative array, the order must be consistent with rrd_def,
* single values are allowed and will be paired with $measurement
*/
public function put($device, $measurement, $tags, $fields);
}
19 changes: 1 addition & 18 deletions LibreNMS/Interfaces/Data/Datastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace LibreNMS\Interfaces\Data;

interface Datastore
interface Datastore extends DataStorageInterface
{
/**
* Check if this is enabled by the configuration
Expand Down Expand Up @@ -54,21 +54,4 @@ public function getName();
* @return array
*/
public function getStats();

/**
* Datastore-independent function which should be used for all polled metrics.
*
* RRD Tags:
* rrd_def RrdDefinition
* rrd_name array|string: the rrd filename, will be processed with rrd_name()
* rrd_oldname array|string: old rrd filename to rename, will be processed with rrd_name()
* rrd_step int: rrd step, defaults to 300
*
* @param array $device
* @param string $measurement Name of this measurement
* @param array $tags tags for the data (or to control rrdtool)
* @param array|mixed $fields The data to update in an associative array, the order must be consistent with rrd_def,
* single values are allowed and will be paired with $measurement
*/
public function put($device, $measurement, $tags, $fields);
}
15 changes: 14 additions & 1 deletion LibreNMS/Interfaces/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
namespace LibreNMS\Interfaces;

use App\Models\Device;
use LibreNMS\Interfaces\Data\DataStorageInterface;
use LibreNMS\OS;
use LibreNMS\Polling\ModuleStatus;

interface Module
{
Expand All @@ -35,6 +37,16 @@ interface Module
*/
public function dependencies(): array;

/**
* Should this module be run?
*/
public function shouldDiscover(OS $os, ModuleStatus $status): bool;

/**
* Should polling run for this device?
*/
public function shouldPoll(OS $os, ModuleStatus $status): bool;

/**
* Discover this module. Heavier processes can be run here
* Run infrequently (default 4 times a day)
Expand All @@ -49,8 +61,9 @@ public function discover(OS $os): void;
* Run frequently (default every 5 minutes)
*
* @param \LibreNMS\OS $os
* @param \LibreNMS\Interfaces\Data\DataStorageInterface $datastore
*/
public function poll(OS $os): void;
public function poll(OS $os, DataStorageInterface $datastore): void;

/**
* Remove all DB data for this module.
Expand Down
4 changes: 3 additions & 1 deletion LibreNMS/Interfaces/Polling/OSPolling.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

namespace LibreNMS\Interfaces\Polling;

use LibreNMS\Interfaces\Data\DataStorageInterface;

interface OSPolling
{
/**
* Poll additional OS data.
* Data must be manually saved within this method.
*/
public function pollOS(): void;
public function pollOS(DataStorageInterface $datastore): void;
}
Loading

0 comments on commit 75e5d21

Please sign in to comment.