Files

188 lines
5.6 KiB
PHP
Raw Permalink Normal View History

<?php
2018-08-21 08:06:49 -05:00
use App\Models\Ipv4Address;
use App\Models\OspfArea;
use App\Models\OspfInstance;
use App\Models\OspfNbr;
use App\Models\OspfPort;
use LibreNMS\RRD\RrdDefinition;
2020-10-19 09:35:41 -05:00
$device_model = DeviceCache::getPrimary();
$vrfs_lite_cisco = empty($device['vrf_lite_cisco'])
? [['context_name' => null]]
: $device['vrf_lite_cisco'];
2016-01-20 15:13:53 +01:00
foreach ($vrfs_lite_cisco as $vrf_lite) {
$device['context_name'] = $vrf_lite['context_name'];
2018-08-21 08:06:49 -05:00
echo ' Processes: ';
2016-01-20 15:13:53 +01:00
// Pull data from device
2020-09-21 15:43:38 +02:00
$ospf_instances_poll = snmpwalk_cache_oid($device, 'OSPF-MIB::ospfGeneralGroup', [], 'OSPF-MIB');
2018-08-21 08:06:49 -05:00
d_echo($ospf_instances_poll);
$ospf_instances = collect();
2016-01-20 15:13:53 +01:00
foreach ($ospf_instances_poll as $ospf_instance_id => $ospf_entry) {
2018-08-21 08:06:49 -05:00
// TODO add model listener from wireless polling PR #8607 for improved output
$instance = OspfInstance::updateOrCreate([
'device_id' => $device['device_id'],
'ospf_instance_id' => $ospf_instance_id,
'context_name' => $device['context_name'],
], $ospf_entry);
$ospf_instances->push($instance);
2016-01-20 15:13:53 +01:00
}
2011-04-27 14:59:50 +00:00
2018-08-21 08:06:49 -05:00
// cleanup
OspfInstance::query()
->where(['device_id' => $device['device_id'], 'context_name' => $device['context_name']])
->whereNotIn('id', $ospf_instances->pluck('id'))->delete();
2016-01-20 15:13:53 +01:00
2018-08-25 03:57:28 -05:00
$instance_count = $ospf_instances->count();
echo $instance_count;
if ($instance_count == 0) {
// if there are no instances, don't check for areas, neighbors, and ports
return;
}
2016-01-20 15:13:53 +01:00
2018-08-21 08:06:49 -05:00
echo ' Areas: ';
2016-01-20 15:13:53 +01:00
// Pull data from device
2020-09-21 15:43:38 +02:00
$ospf_areas_poll = snmpwalk_cache_oid($device, 'OSPF-MIB::ospfAreaEntry', [], 'OSPF-MIB');
2018-08-21 08:06:49 -05:00
d_echo($ospf_areas_poll);
2016-01-20 15:13:53 +01:00
2018-08-21 08:06:49 -05:00
$ospf_areas = collect();
2016-01-20 15:13:53 +01:00
foreach ($ospf_areas_poll as $ospf_area_id => $ospf_area) {
2018-08-21 08:06:49 -05:00
$area = OspfArea::updateOrCreate([
'device_id' => $device['device_id'],
'ospfAreaId' => $ospf_area_id,
'context_name' => $device['context_name'],
], $ospf_area);
$ospf_areas->push($area);
2016-01-20 15:13:53 +01:00
}
2018-08-21 08:06:49 -05:00
// cleanup
OspfArea::query()
->where(['device_id' => $device['device_id'], 'context_name' => $device['context_name']])
->whereNotIn('id', $ospf_areas->pluck('id'))->delete();
2016-01-20 15:13:53 +01:00
2018-08-21 08:06:49 -05:00
echo $ospf_areas->count();
echo ' Ports: ';
2016-01-20 15:13:53 +01:00
// Pull data from device
2020-09-21 15:43:38 +02:00
$ospf_ports_poll = snmpwalk_cache_oid($device, 'OSPF-MIB::ospfIfEntry', [], 'OSPF-MIB');
2018-08-21 08:06:49 -05:00
d_echo($ospf_ports_poll);
2016-01-20 15:13:53 +01:00
2018-08-21 08:06:49 -05:00
$ospf_ports = collect();
2016-01-20 15:13:53 +01:00
foreach ($ospf_ports_poll as $ospf_port_id => $ospf_port) {
2018-08-21 08:06:49 -05:00
// find port_id
if ($ospf_port['ospfAddressLessIf']) {
2020-09-21 15:43:38 +02:00
$ospf_port['port_id'] = (int) $device_model->ports()->where('ifIndex', $ospf_port['ospfAddressLessIf'])->value('port_id');
2018-08-21 08:06:49 -05:00
} else {
// FIXME force same device ?
2020-09-21 15:43:38 +02:00
$ospf_port['port_id'] = (int) Ipv4Address::query()
2018-08-21 08:06:49 -05:00
->where('ipv4_address', $ospf_port['ospfIfIpAddress'])
->where('context_name', $device['context_name'])
->value('port_id');
}
2018-08-21 08:06:49 -05:00
$port = OspfPort::updateOrCreate([
'device_id' => $device['device_id'],
'ospf_port_id' => $ospf_port_id,
'context_name' => $device['context_name'],
], $ospf_port);
$ospf_ports->push($port);
2016-01-20 15:13:53 +01:00
}
2018-08-21 08:06:49 -05:00
// cleanup
OspfPort::query()
->where(['device_id' => $device['device_id'], 'context_name' => $device['context_name']])
->whereNotIn('id', $ospf_ports->pluck('id'))->delete();
2018-08-21 08:06:49 -05:00
echo $ospf_ports->count();
2018-08-21 08:06:49 -05:00
echo ' Neighbours: ';
2016-01-20 15:13:53 +01:00
// Pull data from device
2020-09-21 15:43:38 +02:00
$ospf_nbrs_poll = snmpwalk_cache_oid($device, 'OSPF-MIB::ospfNbrEntry', [], 'OSPF-MIB');
2018-08-21 08:06:49 -05:00
d_echo($ospf_nbrs_poll);
2018-08-21 08:06:49 -05:00
$ospf_neighbours = collect();
2016-01-20 15:13:53 +01:00
foreach ($ospf_nbrs_poll as $ospf_nbr_id => $ospf_nbr) {
2018-08-21 08:06:49 -05:00
// get neighbor port_id
$ospf_nbr['port_id'] = Ipv4Address::query()
->where('ipv4_address', $ospf_nbr['ospfNbrIpAddr'])
->where('context_name', $device['context_name'])
->value('port_id');
$ospf_nbr['ospf_nbr_id'] = $ospf_nbr_id;
$neighbour = OspfNbr::updateOrCreate([
'device_id' => $device['device_id'],
'ospf_nbr_id' => $ospf_nbr_id,
'context_name' => $device['context_name'],
], $ospf_nbr);
$ospf_neighbours->push($neighbour);
}
2018-08-21 08:06:49 -05:00
// cleanup
OspfNbr::query()
->where(['device_id' => $device['device_id'], 'context_name' => $device['context_name']])
->whereNotIn('id', $ospf_neighbours->pluck('id'))->delete();
echo $ospf_neighbours->count();
2016-01-20 15:13:53 +01:00
}
2018-08-21 08:06:49 -05:00
unset($device['context_name'], $vrfs_lite_cisco, $vrf_lite);
2018-08-21 08:06:49 -05:00
if ($instance_count) {
// Create device-wide statistics RRD
$rrd_def = RrdDefinition::make()
->addDataset('instances', 'GAUGE', 0, 1000000)
->addDataset('areas', 'GAUGE', 0, 1000000)
->addDataset('ports', 'GAUGE', 0, 1000000)
->addDataset('neighbours', 'GAUGE', 0, 1000000);
$fields = [
'instances' => $instance_count,
'areas' => $ospf_areas->count(),
'ports' => $ospf_ports->count(),
'neighbours' => $ospf_neighbours->count(),
];
$tags = compact('rrd_def');
data_update($device, 'ospf-statistics', $tags, $fields);
}
2015-08-19 20:58:02 +00:00
2018-08-21 08:06:49 -05:00
echo PHP_EOL;
unset(
2018-08-21 08:06:49 -05:00
$ospf_instances,
2018-08-25 03:57:28 -05:00
$instance_count,
2018-08-21 08:06:49 -05:00
$ospf_areas,
$ospf_ports,
$ospf_neighbours,
$ospf_instances_poll,
$ospf_areas_poll,
$ospf_ports_poll,
$ospf_nbrs_poll,
$ospf_entry,
$instance,
$ospf_instance_id,
$ospf_area,
$area,
$ospf_area_id,
$ospf_port,
$port,
$ospf_port_id,
$ospf_nbr,
$neighbour,
$ospf_nbr_id,
$rrd_def,
$fields,
$tags
);