ℹ️ Your data is safe here... unless you make the SNIP gods angry. And let's just say they have a really bad sense of humor.

From Dziadziumil, 6 Months ago, written in Plain Text.
Embed
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. /**
  5.  * LibreNMS
  6.  *
  7.  *   This file is part of LibreNMS.
  8.  *
  9.  * @copyright  (C) 2006 - 2012 Adam Armstrong
  10.  */
  11.  
  12. use LibreNMS\Util\Debug;
  13.  
  14. $init_modules = ['discovery'];
  15. require __DIR__ . '/includes/init.php';
  16.  
  17. $start = microtime(true);
  18. Log::setDefaultDriver('console');
  19. $sqlparams = [];
  20. $options = getopt('h:m:i:n:d::v::a::q', ['os:', 'type:']);
  21.  
  22. if (! isset($options['q'])) {
  23.     echo \LibreNMS\Config::get('project_name') . " Discovery\n";
  24. }
  25.  
  26. $where = '';
  27. if (isset($options['h'])) {
  28.     if ($options['h'] == 'odd') {
  29.         $options['n'] = '1';
  30.         $options['i'] = '2';
  31.     } elseif ($options['h'] == 'even') {
  32.         $options['n'] = '0';
  33.         $options['i'] = '2';
  34.     } elseif ($options['h'] == 'all') {
  35.         $where = ' ';
  36.         $doing = 'all';
  37.     } elseif ($options['h'] == 'new') {
  38.         $new_discovery_lock = Cache::lock('new-discovery', 300);
  39.         $where = 'AND `last_discovered` IS NULL';
  40.         $doing = 'new';
  41.     } elseif ($options['h']) {
  42.         if (is_numeric($options['h'])) {
  43.             $where = "AND `device_id` = '" . $options['h'] . "'";
  44.             $doing = $options['h'];
  45.         } else {
  46.             $where = "AND `hostname` LIKE '" . str_replace('*', '%', $options['h']) . "'";
  47.             $doing = $options['h'];
  48.         }
  49.     }//end if
  50. }//end if
  51.  
  52. if (isset($options['os'])) {
  53.     $where .= ' AND os = ?';
  54.     $sqlparams[] = $options['os'];
  55. }
  56.  
  57. if (isset($options['type'])) {
  58.     $where .= ' AND type = ?';
  59.     $sqlparams[] = $options['type'];
  60. }
  61.  
  62. if (isset($options['i']) && $options['i'] && isset($options['n'])) {
  63.     $where .= ' AND MOD(device_id,' . $options['i'] . ") = '" . $options['n'] . "'";
  64.     $doing = $options['n'] . '/' . $options['i'];
  65. }
  66.  
  67. if (Debug::set(isset($options['d']), false) || isset($options['v'])) {
  68.     echo \LibreNMS\Util\Version::get()->header();
  69.  
  70.     echo "DEBUG!\n";
  71.     Debug::setVerbose(isset($options['v']));
  72.     \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
  73. }
  74.  
  75. if (! $where) {
  76.     echo "-h <device> | <device>  Poll single device\n";
  77.     echo "-h odd             Poll odd numbered devices  (same as -i 2 -n 0)\n";
  78.     echo "-h even            Poll even numbered devices (same as -i 2 -n 1)\n";
  79.     echo "-h all             Poll all devices\n";
  80.     echo "-h new             Poll all devices that have not had a discovery run before\n";
  81.     echo "--os <os>     Poll devices only with specified operating system\n";
  82.     echo "--type <type>      Poll devices only with specified type\n";
  83.     echo "-i <instances> -n <number>                   Poll as instance <number> of <instances>\n";
  84.     echo "                   Instances start at 0. 0-3 for -n 4\n";
  85.     echo "\n";
  86.     echo "Debugging and testing options:\n";
  87.     echo "-d                 Enable debugging output\n";
  88.     echo "-v                 Enable verbose debugging output\n";
  89.     echo "-m                 Specify single module to be run. Comma separate modules, submodules may be added with /\n";
  90.     echo "\n";
  91.     echo "Invalid arguments!\n";
  92.     exit;
  93. }
  94.  
  95. // If we've specified modules with -m, use them
  96. $module_override = parse_modules('discovery', $options);
  97.  
  98. $discovered_devices = 0;
  99.  
  100. if (! empty(\LibreNMS\Config::get('distributed_poller_group'))) {
  101.     $where .= ' AND poller_group IN(' . \LibreNMS\Config::get('distributed_poller_group') . ')';
  102. }
  103.  
  104. global $device;
  105. foreach (dbFetchRows("SELECT * FROM `devices` WHERE disabled = 0 $where ORDER BY device_id DESC", $sqlparams) as $device) {
  106.     $device_start = microtime(true);
  107.     DeviceCache::setPrimary($device['device_id']);
  108.  
  109.     if (discover_device($device, $module_override)) {
  110.         $discovered_devices++;
  111.  
  112.         $device_time = round(microtime(true) - $device_start, 3);
  113.         DB::table('devices')->where('device_id', $device['device_id'])->update([
  114.             'last_discovered_timetaken' => $device_time,
  115.             'last_discovered' => DB::raw('NOW()'),
  116.         ]);
  117.  
  118.         echo "Discovered in $device_time seconds\n\n";
  119.     }
  120. }
  121.  
  122. $end = microtime(true);
  123. $run = ($end - $start);
  124. $proctime = substr($run, 0, 5);
  125.  
  126. if (isset($new_discovery_lock)) {
  127.     $new_discovery_lock->release();
  128. }
  129.  
  130. $string = $argv[0] . " $doing " . date(\LibreNMS\Config::get('dateformat.compact')) . " - $discovered_devices devices discovered in $proctime secs";
  131. d_echo("$string\n");
  132.  
  133. if (! isset($options['q'])) {
  134.     echo PHP_EOL;
  135.     app(\App\Polling\Measure\MeasurementManager::class)->printStats();
  136. }
  137.  
  138. logfile&#40;$string&#41;;
  139.  
  140. if ($doing !== 'new' && $discovered_devices == 0) {
  141.     // No discoverable devices, either down or disabled
  142.     exit(5);
  143. }
  144.  
captcha