Issues (2963)

includes/polling/ntp/awplus.inc.php (1 issue)

Labels
Severity
1
<?php
2
/*
3
 * LibreNMS module to capture statistics from the AT-NTP-MIB
4
 *
5
 * Copyright (c) 2018 Matt Read <[email protected]>
6
 *
7
 * This program is free software: you can redistribute it and/or modify it
8
 * under the terms of the GNU General Public License as published by the
9
 * Free Software Foundation, either version 3 of the License, or (at your
10
 * option) any later version.  Please see LICENSE.txt at the top level of
11
 * the source code distribution for details.
12
 */
13
14
use LibreNMS\RRD\RrdDefinition;
15
16
$tmp_module = 'ntp';
17
18
$component = new LibreNMS\Component();
19
$options = [];
20
$options['filter']['type'] = ['=', $tmp_module];
21
$options['filter']['disabled'] = ['=', 0];
22
$options['filter']['ignore'] = ['=', 0];
23
$components = $component->getComponents($device['device_id'], $options);
24
25
// We only care about our device id.
26
$components = $components[$device['device_id']];
27
28
// Only collect SNMP data if we have enabled components
29
if (count($components > 0)) {
0 ignored issues
show
$components > 0 of type boolean is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
if (count(/** @scrutinizer ignore-type */ $components > 0)) {
Loading history...
30
    // Let's gather the stats..
31
    $atNtpAssociationEntry = snmpwalk_group($device, 'atNtpAssociationEntry', 'AT-NTP-MIB');
32
33
    // Loop through the components and extract the data.
34
    foreach ($components as $key => &$array) {
35
        $peer = $array['peer'];
36
37
        // Let's make sure the rrd is setup for this class.
38
        $rrd_name = ['ntp', $peer];
39
        $rrd_def = RrdDefinition::make()
40
            ->addDataset('stratum', 'GAUGE', 0, 16)
41
            ->addDataset('offset', 'GAUGE', -1000)
42
            ->addDataset('delay', 'GAUGE', -1000)
43
            ->addDataset('dispersion', 'GAUGE', -1000);
44
45
        $array['stratum'] = $atNtpAssociationEntry[$array['UID']]['atNtpAssociationStratum'];
46
        // Set the status, 16 = Bad
47
        if ($array['stratum'] == 16) {
48
            $array['status'] = 2;
49
            $array['error'] = 'NTP is not in sync';
50
        } else {
51
            $array['status'] = 0;
52
            $array['error'] = '';
53
        }
54
55
        // Extract the statistics and update rrd
56
        $rrd['stratum'] = $array['stratum'];
57
        $rrd['offset'] = $atNtpAssociationEntry[$array['UID']]['atNtpAssociationOffset'];
58
        $rrd['offset'] = str_replace(' milliseconds', '', $rrd['offset']);
59
        $rrd['offset'] = $rrd['offset'] / 1000; // Convert to seconds
60
        $rrd['delay'] = $atNtpAssociationEntry[$array['UID']]['atNtpAssociationDelay'];
61
        $rrd['delay'] = str_replace(' milliseconds', '', $rrd['delay']);
62
        $rrd['delay'] = $rrd['delay'] / 1000; // Convert to seconds
63
        $rrd['dispersion'] = $atNtpAssociationEntry[$array['UID']]['atNtpAssociationDisp'];
64
        $tags = compact('ntp', 'rrd_name', 'rrd_def', 'peer');
65
        data_update($device, 'ntp', $tags, $rrd);
66
67
        // Let's print some debugging info.
68
        d_echo("\n\nComponent: " . $key . "\n");
69
        d_echo('    Index:      ' . $array['UID'] . "\n");
70
        d_echo('    Peer:       ' . $array['peer'] . ':' . $array['port'] . "\n");
71
        d_echo('    Stratum:    atNtpAssociationStratum.' . $array['UID'] . '  = ' . $rrd['stratum'] . "\n");
72
        d_echo('    Offset:     atNtpAssociationOffset.' . $array['UID'] . ' = ' . $rrd['offset'] . "\n");
73
        d_echo('    Delay:      atNtpAssociationDelay.' . $array['UID'] . ' = ' . $rrd['delay'] . "\n");
74
        d_echo('    Dispersion: atNtpAssociationDisp.' . $array['UID'] . ' = ' . $rrd['dispersion'] . "\n");
75
76
        // Clean-up after yourself!
77
        unset($filename, $rrd_filename, $rrd);
78
    } // End foreach components
79
80
    // Write the Components back to the DB.
81
    $component->setComponentPrefs($device['device_id'], $components);
82
} // end if count components
83
84
// Clean-up after yourself!
85
unset($type, $components, $component, $options, $tmp_module);
86