Files
librenms-librenms/includes/callback.php

139 lines
7.8 KiB
PHP
Raw Normal View History

2015-04-06 09:23:13 +01:00
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
2015-04-06 20:41:50 +01:00
$enabled = dbFetchCell("SELECT `value` FROM `callback` WHERE `name` = 'enabled'");
if ($enabled == 1) {
if (dbFetchCell("SELECT `value` FROM `callback` WHERE `name` = 'uuid'") == '') {
2020-09-21 15:40:17 +02:00
dbInsert(['name' => 'uuid', 'value' => guidv4(openssl_random_pseudo_bytes(16))], 'callback');
2015-04-06 20:41:50 +01:00
}
2015-07-13 20:10:26 +02:00
2015-04-06 20:41:50 +01:00
$uuid = dbFetchCell("SELECT `value` FROM `callback` WHERE `name` = 'uuid'");
2015-04-06 09:23:13 +01:00
$version = version_info();
2020-09-21 15:40:17 +02:00
$queries = [
'alert_rules' => 'SELECT COUNT(*) AS `total`,`severity` FROM `alert_rules` WHERE `disabled`=0 GROUP BY `severity`',
'alert_templates' => 'SELECT COUNT(*) AS `total` FROM `alert_templates`',
'api_tokens' => 'SELECT COUNT(*) AS `total` FROM `api_tokens` WHERE `disabled`=0',
'applications' => 'SELECT COUNT(*) AS `total`,`app_type` FROM `applications` GROUP BY `app_type`',
'bgppeer_state' => 'SELECT COUNT(*) AS `total`,`bgpPeerState` FROM `bgpPeers` GROUP BY `bgpPeerState`',
'bgppeer_status' => 'SELECT COUNT(*) AS `total`,`bgpPeerAdminStatus` FROM `bgpPeers` GROUP BY `bgpPeerAdminStatus`',
'bills' => 'SELECT COUNT(*) AS `total`,`bill_type` FROM `bills` GROUP BY `bill_type`',
'cef' => 'SELECT COUNT(*) AS `total` FROM `cef_switching`',
'cisco_asa' => 'SELECT COUNT(*) AS `total`,`oid` FROM `ciscoASA` WHERE `disabled` = 0 GROUP BY `oid`',
'mempool' => 'SELECT COUNT(*) AS `total`,`mempool_descr` FROM `mempools` GROUP BY `mempool_descr`',
'dbschema' => 'SELECT COUNT(*) AS `total`, COUNT(*) AS `version` FROM `migrations`',
'snmp_version' => 'SELECT COUNT(*) AS `total`,`snmpver` FROM `devices` GROUP BY `snmpver`',
'os' => 'SELECT COUNT(*) AS `total`,`os` FROM `devices` GROUP BY `os`',
'type' => 'SELECT COUNT(*) AS `total`,`type` FROM `devices` GROUP BY `type`',
'hardware' => 'SELECT COUNT(*) AS `total`, `hardware` FROM `devices` GROUP BY `hardware`',
'ipsec' => 'SELECT COUNT(*) AS `total` FROM `ipsec_tunnels`',
'ipv4_addresses' => 'SELECT COUNT(*) AS `total` FROM `ipv4_addresses`',
'ipv4_macaddress' => 'SELECT COUNT(*) AS `total` FROM ipv4_mac',
'ipv4_networks' => 'SELECT COUNT(*) AS `total` FROM ipv4_networks',
'ipv6_addresses' => 'SELECT COUNT(*) AS `total` FROM `ipv6_addresses`',
'ipv6_networks' => 'SELECT COUNT(*) AS `total` FROM `ipv6_networks`',
'xdp' => 'SELECT COUNT(*) AS `total`,`protocol` FROM `links` GROUP BY `protocol`',
'ospf' => 'SELECT COUNT(*) AS `total`,`ospfVersionNumber` FROM `ospf_instances` GROUP BY `ospfVersionNumber`',
'ospf_links' => 'SELECT COUNT(*) AS `total`,`ospfIfType` FROM `ospf_ports` GROUP BY `ospfIfType`',
'arch' => 'SELECT COUNT(*) AS `total`,`arch` FROM `packages` GROUP BY `arch`',
'pollers' => 'SELECT COUNT(*) AS `total` FROM `pollers`',
'port_type' => 'SELECT COUNT(*) AS `total`,`ifType` FROM `ports` GROUP BY `ifType`',
'port_ifspeed' => 'SELECT COUNT(*) AS `total`,ROUND(`ifSpeed`/1000/1000) FROM `ports` GROUP BY `ifSpeed`',
'port_vlans' => 'SELECT COUNT(*) AS `total`,`state` FROM `ports_vlans` GROUP BY `state`',
'processes' => 'SELECT COUNT(*) AS `total` FROM `processes`',
'processors' => 'SELECT COUNT(*) AS `total`,`processor_type` FROM `processors` GROUP BY `processor_type`',
'pseudowires' => 'SELECT COUNT(*) AS `total` FROM `pseudowires`',
'sensors' => 'SELECT COUNT(*) AS `total`,`sensor_class` FROM `sensors` GROUP BY `sensor_class`',
New module add juniper rpm support + reimplementation of cisco-sla module (#12799) * Definition of a new SLA module to migrate from old cisco-sla and support other devices : Juniper RPM and Huawei * Change wrong description name of module SLA * Re implementing Juniper RPM support in a new module Slas (Cisco SLA to be added) * Added Graphs for Juniper RPM types * Tried to make discover and poller generic : to handle Cisco SLA and Juniper RPM and others * Reimplementing Cisco SLA in the new Sla module and adding condition to manage both SLA and RPM * Definition of a new SLA module to migrate from old cisco-sla and support other devices : Juniper RPM and Huawei * Change wrong description name of module SLA * Re implementing Juniper RPM support in a new module Slas (Cisco SLA to be added) * Added Graphs for Juniper RPM types * Tried to make discover and poller generic : to handle Cisco SLA and Juniper RPM and others * Reimplementing Cisco SLA in the new Sla module and adding condition to manage both SLA and RPM * Fix some error due to Elequent object replacing SQL request * Fixing indentation * Fixed continuous integrations errors * Fixed continuous integrations errors part 2 * Fixed continuous integrations errors part 3 : missing last new lines * Fixed PHP Static Analysis checks * Fixed continuous integrations errors part 4 * Fixed PHP Static Analysis checks part 2 * Trying to convert existing cisco-sla to new slas one * Trying to add slas to fix test failure (I am pretty sure this is not enough but I don't know how to do it) * Switching old cisco-sla module declaration to new slas module in concerned OS files * Fixing wrong sla module name instead of slas in concerned OS files * Fixing wrong module name sla changed to slas (as is it in power-supplies reimplementation) * Fixing some warnings shown in the Inspection CI but skipping some that are already present * Adding tests files from virtual Juniper VMX and adding slas modules in /tests/module_tables * Adding missing SNMP data files * Module Slas modifications to use inheritance * Fix CI errors * Fix CI errors part 2 * Fix CI errors part 3 * Fixing no value on RTT graphs * Fixing only last SLA to be polled : problem with loops * Fix CI errors part 4 * Optimizing a little the Juniper RPM poller part I wanted to query only the required values but it is complicated to query a specific OID as Cisco-SLA does because the owner and tag are hardcoded in ASCII decimal with strange values. So I use the function snmp_walk which gets the whole OID values in text. And I index the Juniper MIB (because we want multiple values). But I changed the DISMAN MIB requests to get only what we want. * Fix CI errors * Fix CI errors part 2 * Fix CI errors part 3 * Fixing PHP Static Analysis error 2/4 * Fix CI errors * Implementing Interfaces to fix some errors that occurs with inheritance * Adding missing Interfaces files * Moving generic Sla discovery code into Junos Class * Fix CI errors * Fix CI errors part 2 * Fixing missing definition of SlaDiscovery in Cisco shared Class * Fixing missing import for Illuminate\Database\Eloquent\Collection * Fix CI errors part 3 * Fix PHP Static Analysis error * change type strings to use translations instead of config * IOSXR test data * remove test data garbage * simplify discovery * Fix style * Updating test files to adapt mock sla_nr ID starting from 0 and not 1 : CI tests should pass * clean up polling add rtt field in the db cisco icmpjitter seems to have confused a lot of values * rtt on discovery * use crc32 for consistent sla_nr values * revert snmprec * Fix style * correct schema Co-authored-by: Tony Murray <murraytony@gmail.com>
2021-06-11 20:48:33 +02:00
'sla' => 'SELECT COUNT(*) AS `total`,`rtt_type` FROM `slas` GROUP BY `rtt_type`',
'wireless' => 'SELECT COUNT(*) AS `total`,`sensor_class` FROM `wireless_sensors` GROUP BY `sensor_class`',
'storage' => 'SELECT COUNT(*) AS `total`,`storage_type` FROM `storage` GROUP BY `storage_type`',
'toner' => 'SELECT COUNT(*) AS `total`,`supply_type` FROM `printer_supplies` GROUP BY `supply_type`',
'vlans' => 'SELECT COUNT(*) AS `total`,`vlan_type` FROM `vlans` GROUP BY `vlan_type`',
'vminfo' => 'SELECT COUNT(*) AS `total`,`vm_type` FROM `vminfo` GROUP BY `vm_type`',
'vmware' => 'SELECT COUNT(*) AS `total` FROM `vminfo`',
'vrfs' => 'SELECT COUNT(*) AS `total` FROM `vrfs`',
2015-07-13 20:10:26 +02:00
'mysql_version' => 'SELECT 1 AS `total`, @@version AS `version`',
2020-09-21 15:40:17 +02:00
];
2015-04-06 09:23:13 +01:00
foreach ($queries as $name => $query) {
2020-09-21 15:40:17 +02:00
$data = dbFetchRows($query);
2015-04-06 09:23:13 +01:00
$response[$name] = $data;
}
2020-09-21 15:40:17 +02:00
$response['php_version'][] = ['total' => 1, 'version' => $version['php_ver']];
$response['python_version'][] = ['total' => 1, 'version' => $version['python_ver']];
$response['rrdtool_version'][] = ['total' => 1, 'version' => $version['rrdtool_ver']];
$response['netsnmp_version'][] = ['total' => 1, 'version' => $version['netsnmp_ver']];
2015-07-13 20:10:26 +02:00
// collect sysDescr and sysObjectID for submission
$device_info = dbFetchRows('SELECT COUNT(*) AS `count`,`os`, `sysDescr`, `sysObjectID` FROM `devices`
WHERE `sysDescr` IS NOT NULL AND `sysObjectID` IS NOT NULL GROUP BY `os`, `sysDescr`, `sysObjectID`');
// sanitize sysDescr
$device_info = array_map(function ($entry) {
// remove hostnames from linux, macosx, and SunOS
$entry['sysDescr'] = preg_replace_callback('/^(Linux |Darwin |FreeBSD |SunOS )[A-Za-z0-9._\-]+ ([0-9.]{3,9})/', function ($matches) {
2020-09-21 15:40:17 +02:00
return $matches[1] . 'hostname ' . $matches[2];
}, $entry['sysDescr']);
// wipe serial numbers, preserve the format
2020-09-21 15:40:17 +02:00
$sn_patterns = ['/[A-Z]/', '/[a-z]/', '/[0-9]/'];
$sn_replacements = ['A', 'a', '0'];
$entry['sysDescr'] = preg_replace_callback(
'/((s\/?n|serial num(ber)?)[:=]? ?)([a-z0-9.\-]{4,16})/i',
function ($matches) use ($sn_patterns, $sn_replacements) {
return $matches[1] . preg_replace($sn_patterns, $sn_replacements, $matches[4]);
},
$entry['sysDescr']
);
return $entry;
}, $device_info);
2020-09-21 15:40:17 +02:00
$output = [
2015-07-13 20:10:26 +02:00
'uuid' => $uuid,
'data' => $response,
'info' => $device_info,
2020-09-21 15:40:17 +02:00
];
$data = json_encode($output);
$submit = ['data' => $data];
2015-04-06 09:23:13 +01:00
$fields = '';
2015-04-13 19:35:25 +10:00
foreach ($submit as $key => $value) {
2020-09-21 15:40:17 +02:00
$fields .= $key . '=' . $value . '&';
2015-04-06 09:23:13 +01:00
}
2015-07-13 20:10:26 +02:00
2015-04-06 09:23:13 +01:00
rtrim($fields, '&');
$post = curl_init();
2015-04-13 19:51:16 +10:00
set_curl_proxy($post);
curl_setopt($post, CURLOPT_URL, \LibreNMS\Config::get('callback_post'));
2015-04-06 09:23:13 +01:00
curl_setopt($post, CURLOPT_POST, count($submit));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
} elseif ($enabled == 2) {
2020-09-21 15:40:17 +02:00
$uuid = dbFetchCell("SELECT `value` FROM `callback` WHERE `name` = 'uuid'");
2015-04-06 20:41:50 +01:00
$fields = "uuid=$uuid";
$clear = curl_init();
2015-04-13 20:04:41 +10:00
set_curl_proxy($clear);
curl_setopt($clear, CURLOPT_URL, \LibreNMS\Config::get('callback_clear'));
2015-04-06 20:41:50 +01:00
curl_setopt($clear, CURLOPT_POST, count($clear));
curl_setopt($clear, CURLOPT_POSTFIELDS, $fields);
curl_setopt($clear, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($clear);
2020-09-21 15:40:17 +02:00
dbDelete('callback', '`name`="uuid"', []);
dbUpdate(['value' => '0'], 'callback', '`name` = "enabled"', []);
2015-04-06 09:23:13 +01:00
}