Skip to content

Commit

Permalink
Revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin22 committed Sep 5, 2022
1 parent ff7d89e commit d386733
Show file tree
Hide file tree
Showing 188 changed files with 2,693 additions and 1,397 deletions.
74 changes: 49 additions & 25 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PhpCsFixer\Finder;

$rules = [
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'default' => 'single_space',
Expand All @@ -17,12 +18,22 @@
'braces' => true,
'cast_spaces' => true,
'class_attributes_separation' => [
'elements' => ['method' => 'one'],
'elements' => [
// 'const' => 'one',
'method' => 'one',
// 'property' => 'one',
'trait_import' => 'none',
],
],
'class_definition' => [
'multi_line_extends_each_single_line' => true,
'single_item_single_line' => true,
// 'single_line' => true,
],
'class_definition' => true,
'concat_space' => [
'spacing' => 'one',
'spacing' => 'one', // upstream none
],
'constant_case' => ['case' => 'lower'],
'declare_equal_normalize' => true,
'elseif' => true,
'encoding' => true,
Expand All @@ -38,20 +49,23 @@
'linebreak_after_opening_tag' => true,
'line_ending' => true,
'lowercase_cast' => true,
'constant_case' => true,
'lowercase_keywords' => true,
'lowercase_static_reference' => true, // added from Symfony
'magic_method_casing' => true, // added from Symfony
'magic_constant_casing' => true,
'method_argument_space' => true,
'method_argument_space' => [
'on_multiline' => 'ignore',
],
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
],
'native_function_casing' => true,
'no_alias_functions' => false, // TODO: Enable?
'no_alias_functions' => true,
'no_extra_blank_lines' => [
'tokens' => [
'extra',
'throw',
'use',
'use_trait',
],
],
'no_blank_lines_after_class_opening' => true,
Expand All @@ -65,27 +79,29 @@
'use' => 'echo',
],
'no_multiline_whitespace_around_double_arrow' => true,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
],
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_around_offset' => true,
'no_spaces_around_offset' => [
'positions' => ['inside', 'outside'],
],
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
'no_unreachable_default_argument_value' => false, // TODO: Enable?
'no_unneeded_control_parentheses' => [
'statements' => ['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield'],
],
'no_unreachable_default_argument_value' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'not_operator_with_successor_space' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'psr_autoloading' => true,
'phpdoc_indent' => true,
'phpdoc_inline_tag_normalizer' => true,
'phpdoc_no_access' => true,
Expand All @@ -94,18 +110,19 @@
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => false,
'phpdoc_to_comment' => false, // override to preserve user preference
'phpdoc_tag_type' => true,
'phpdoc_to_comment' => false, // TODO: Enable?
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'psr_autoloading' => false, // TODO: Enable?
'self_accessor' => false, // TODO: Enable?
'short_scalar_cast' => true,
'simplified_null_return' => false, // disabled by Shift
'simplified_null_return' => false, // disabled as "risky"
'single_blank_line_at_eof' => true,
'single_blank_line_before_namespace' => true,
'single_class_element_per_statement' => true,
'single_class_element_per_statement' => [
'elements' => ['const', 'property'],
],
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_line_comment_style' => [
Expand All @@ -117,26 +134,33 @@
'switch_case_semicolon_to_colon' => true,
'switch_case_space' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => ['property', 'method', 'const'],
'elements' => ['method', 'property'],
],
'whitespace_after_comma_in_array' => true,
];


$finder = Finder::create()
->in(__DIR__)
->exclude('storage')
->exclude('bootstrap/cache')
->exclude('vendor')
->in([
__DIR__ . '/app',
__DIR__ . '/config',
__DIR__ . '/database',
__DIR__ . '/resources',
__DIR__ . '/routes',
__DIR__ . '/tests',
__DIR__ . '/LibreNMS',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new Config)
//->setRiskyAllowed(true)
->setFinder($finder)
->setRules($rules);
->setRules($rules)
->setRiskyAllowed(true)
->setUsingCache(true);
13 changes: 7 additions & 6 deletions LibreNMS/Alert/RunAlerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public function describeAlert($alert)
foreach ($extra['rule'] as $incident) {
$i++;
$obj['faults'][$i] = $incident;
$obj['faults'][$i]['string'] = '';
foreach ($incident as $k => $v) {
if (! empty($v) && $k != 'device_id' && (stristr($k, 'id') || stristr($k, 'desc') || stristr($k, 'msg')) && substr_count($k, '_') <= 1) {
$obj['faults'][$i]['string'] .= $k . ' => ' . $v . '; ';
Expand Down Expand Up @@ -206,11 +207,11 @@ public function describeAlert($alert)
public function timeFormat($secs)
{
$bit = [
'y' => $secs / 31556926 % 12,
'w' => $secs / 604800 % 52,
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
'y' => round($secs / 31556926) % 12,
'w' => round($secs / 604800) % 52,
'd' => round($secs / 86400) % 7,
'h' => round($secs / 3600) % 24,
'm' => round($secs / 60) % 60,
's' => $secs % 60,
];
$ret = [];
Expand Down Expand Up @@ -535,7 +536,7 @@ public function extTransports($obj)
c_echo(" :: $transport_title => ");
try {
$instance = new $class($item['transport_id']);
$tmp = $instance->deliverAlert($obj, $item['opts']);
$tmp = $instance->deliverAlert($obj, $item['opts'] ?? []);
$this->alertLog($tmp, $obj, $obj['transport']);
} catch (\Exception $e) {
$this->alertLog($e, $obj, $obj['transport']);
Expand Down
12 changes: 11 additions & 1 deletion LibreNMS/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Config
public static function load()
{
// don't reload the config if it is already loaded, reload() should be used for that
if (! is_null(self::$config)) {
if (self::isLoaded()) {
return self::$config;
}

Expand Down Expand Up @@ -566,4 +566,14 @@ public static function populateLegacyDbCredentials()
self::set('db_port', config("database.connections.$db.port", 3306));
self::set('db_socket', config("database.connections.$db.unix_socket"));
}

/**
* Check if the config has been loaded yet
*
* @return bool
*/
public static function isLoaded(): bool
{
return ! is_null(self::$config);
}
}
30 changes: 25 additions & 5 deletions LibreNMS/Data/Source/NetSnmpQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class NetSnmpQuery implements SnmpQueryInterface
* @var \App\Models\Device
*/
private $device;
/**
* @var bool
*/
private $abort = false;

public function __construct()
{
Expand Down Expand Up @@ -159,6 +163,17 @@ public function mibDir(?string $dir): SnmpQueryInterface
return $this;
}

/**
* When walking multiple OIDs, stop if one fails. Used when the first OID indicates if the rest are supported.
* OIDs will be walked in order, so you may want to put your OIDs in a specific order.
*/
public function abortOnFailure(): SnmpQueryInterface
{
$this->abort = true;

return $this;
}

/**
* Do not error on out of order indexes.
* Use with caution as we could get stuck in an infinite loop.
Expand Down Expand Up @@ -330,17 +345,22 @@ private function buildAuth(array &$cmd): void
}
}

private function execMultiple(string $command, array $oids): ?SnmpResponse
private function execMultiple(string $command, array $oids): SnmpResponse
{
$combined = null;
$response = new SnmpResponse('');

foreach ($oids as $oid) {
$response = $this->exec($command, [$oid]);
$response = $response->append($this->exec($command, [$oid]));

$combined = $combined ? $combined->append($response) : $response;
// if abort on failure is set, return after first failure
if ($this->abort && ! $response->isValid()) {
Log::debug("SNMP failed walking $oid of " . implode(',', $oids) . ' aborting.');

return $response;
}
}

return $combined;
return $response;
}

private function exec(string $command, array $oids): SnmpResponse
Expand Down
6 changes: 6 additions & 0 deletions LibreNMS/Data/Source/SnmpQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public function context(string $context): SnmpQueryInterface;
*/
public function mibDir(?string $dir): SnmpQueryInterface;

/**
* When walking multiple OIDs, stop if one fails. Used when the first OID indicates if the rest are supported.
* OIDs will be walked in order, so you may want to put your OIDs in a specific order.
*/
public function abortOnFailure(): SnmpQueryInterface;

/**
* Do not error on out of order indexes.
* Use with caution as we could get stuck in an infinite loop.
Expand Down
6 changes: 3 additions & 3 deletions LibreNMS/Data/Store/Graphite.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public function put($device, $measurement, $tags, $fields)
}

foreach ($fields as $k => $v) {
// Send zero for fields without values
if (empty($v)) {
$v = 0;
// Skip fields without values
if (is_null($v)) {
continue;
}
$metric = implode('.', array_filter([$this->prefix, $hostname, $measurement, $ms_name, $k]));
$this->writeData($metric, $v, $timestamp);
Expand Down
3 changes: 2 additions & 1 deletion LibreNMS/Device/Availability.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace LibreNMS\Device;

use App\Models\DeviceOutage;
use LibreNMS\Util\Number;

class Availability
{
Expand Down Expand Up @@ -126,6 +127,6 @@ public static function availability($device, $duration, $precision = 3, $now = n

$outage_summary = self::outageSummary($found_outages, $duration, $now);

return round(100 * ($duration - $outage_summary) / $duration, $precision);
return Number::calculatePercent($duration - $outage_summary, $duration, $precision);
}
}
6 changes: 4 additions & 2 deletions LibreNMS/Device/YamlDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ public static function getValueFromData($name, $index, $discovery_data, $pre_cac
$sub_index = 0;
$sub_index_end = null;
if (preg_match('/^(.+):(\d+)(?:-(\d+))?$/', $name, $matches)) {
[,$name, $sub_index, $sub_index_end] = $matches;
$name = $matches[1] ?? null;
$sub_index = $matches[2] ?? null;
$sub_index_end = $matches[3] ?? null;
}

if (isset($pre_cache[$name]) && ! is_numeric($name)) {
Expand Down Expand Up @@ -291,7 +293,7 @@ public static function preCache(OS $os)
$saved_nobulk = Config::getOsSetting($os->getName(), 'snmp_bulk', true);

foreach ($data_array as $data) {
foreach ((array) $data['oid'] as $oid) {
foreach (Arr::wrap($data['oid'] ?? []) as $oid) {
if (! array_key_exists($oid, $pre_cache)) {
if (isset($data['snmp_flags'])) {
$snmp_flag = Arr::wrap($data['snmp_flags']);
Expand Down
3 changes: 2 additions & 1 deletion LibreNMS/Modules/PrinterSupplies.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use LibreNMS\Interfaces\Module;
use LibreNMS\OS;
use LibreNMS\RRD\RrdDefinition;
use LibreNMS\Util\Number;
use Log;

class PrinterSupplies implements Module
Expand Down Expand Up @@ -276,7 +277,7 @@ private static function getTonerLevel($device, $raw_value, $capacity)
}
}

return round($raw_value / $capacity * 100);
return Number::calculatePercent($raw_value, $capacity, 0);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions LibreNMS/OS.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,7 @@ public static function make(&$device)
if (isset($device['os'])) {
// load os definition and populate os_group
\LibreNMS\Util\OS::loadDefinition($device['os']);
if ($os_group = Config::get("os.{$device['os']}.group")) {
$device['os_group'] = $os_group;
} else {
unset($device['os_group']);
}
$device['os_group'] = Config::get("os.{$device['os']}.group");

$class = StringHelpers::toClass($device['os'], 'LibreNMS\\OS\\');
d_echo('Attempting to initialize OS: ' . $device['os'] . PHP_EOL);
Expand Down
Loading

0 comments on commit d386733

Please sign in to comment.