PageRenderTime 75ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/discovery/functions.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 486 lines | 425 code | 47 blank | 14 comment | 84 complexity | b828e16e924d416c89d7e120e0ef2e12 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. /* Observium Network Management and Monitoring System
  3. * Copyright (C) 2006-2011, Observium Developers - http://www.observium.org
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * See COPYING for more details.
  11. */
  12. function discover_new_device($hostname)
  13. {
  14. global $config;
  15. if ($config['autodiscovery']['xdp']) {
  16. if ( isDomainResolves($hostname . "." . $config['mydomain']) ) {
  17. $dst_host = $hostname . "." . $config['mydomain'];
  18. } else {
  19. $dst_host = $hostname;
  20. }
  21. $ip = gethostbyname($dst_host);
  22. if ( match_network($config['nets'], $ip) )
  23. {
  24. $remote_device_id = addHost ($dst_host);
  25. if ($remote_device_id) {
  26. $remote_device = device_by_id_cache($remote_device_id, 1);
  27. echo("+[".$remote_device['hostname']."(".$remote_device['device_id'].")]");
  28. discover_device($remote_device);
  29. $remote_device = device_by_id_cache($remote_device_id, 1);
  30. return $remote_device_id;
  31. }
  32. }
  33. } else {
  34. return FALSE;
  35. }
  36. }
  37. function discover_device($device, $options = NULL)
  38. {
  39. global $config, $valid;
  40. $valid = array(); ## Reset $valid array
  41. $attribs = get_dev_attribs($device['device_id']);
  42. $device_start = utime(); // Start counting device poll time
  43. echo($device['hostname'] . " ".$device['device_id']." ".$device['os']." ");
  44. if ($device['os'] == 'generic') // verify if OS has changed from generic
  45. {
  46. $device['os']= getHostOS($device);
  47. if ($device['os'] != 'generic')
  48. {
  49. echo "Device os was updated to ".$device['os']."!";
  50. dbUpdate(array('os' => $device['os']), 'devices', '`device_id` = ?', array($device['device_id']));
  51. }
  52. }
  53. if ($config['os'][$device['os']]['group'])
  54. {
  55. $device['os_group'] = $config['os'][$device['os']]['group'];
  56. echo(" (".$device['os_group'].")");
  57. }
  58. echo("\n");
  59. ### If we've specified a module, use that, else walk the modules array
  60. if ($options['m'])
  61. {
  62. if (is_file("includes/discovery/".$options['m'].".inc.php"))
  63. {
  64. include("includes/discovery/".$options['m'].".inc.php");
  65. }
  66. } else {
  67. foreach ($config['discovery_modules'] as $module => $module_status)
  68. {
  69. if ($attribs['discover_'.$module] || ( $module_status && !isset($attribs['discover_'.$module])))
  70. {
  71. include('includes/discovery/'.$module.'.inc.php');
  72. } elseif (isset($attribs['discover_'.$module]) && $attribs['discover_'.$module] == "0") {
  73. echo("Module [ $module ] disabled on host.\n");
  74. } else {
  75. echo("Module [ $module ] disabled globally.\n");
  76. }
  77. }
  78. }
  79. ### Set type to a predefined type for the OS if it's not already set
  80. if ($device['type'] == "unknown" || $device['type'] == "")
  81. {
  82. if ($config['os'][$device['os']]['type'])
  83. {
  84. $device['type'] = $config['os'][$device['os']]['type'];
  85. }
  86. }
  87. $device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5);
  88. dbUpdate(array('last_discovered' => array('NOW()'), 'type' => $device['type'], 'last_discovered_timetaken' => $device_time), 'devices', '`device_id` = ?', array($device['device_id']));
  89. echo("Discovered in $device_time seconds\n");
  90. global $discovered_devices;
  91. echo("\n"); $discovered_devices++;
  92. }
  93. ### Discover sensors
  94. function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor = '1', $multiplier = '1', $low_limit = NULL, $low_warn_limit = NULL, $warn_limit = NULL, $high_limit = NULL, $current = NULL, $poller_type = 'snmp', $entPhysicalIndex = NULL, $entPhysicalIndex_measured = NULL)
  95. {
  96. global $config, $debug;
  97. if ($debug) { echo("Discover sensor: $oid, $index, $type, $descr, $precision, $entPhysicalIndex\n"); }
  98. if (dbFetchCell("SELECT COUNT(sensor_id) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND sensor_type = ? AND `sensor_index` = ?", array($poller_type, $class, $device['device_id'], $type, $index)) == '0')
  99. {
  100. if (!$high_limit) { $high_limit = sensor_limit($class, $current); }
  101. if (!$low_limit) { $low_limit = sensor_low_limit($class, $current); }
  102. $insert = array('poller_type' => $poller_type, 'sensor_class' => $class, 'device_id' => $device['device_id'], 'sensor_oid' => $oid, 'sensor_index' => $index, 'sensor_type' => $type, 'sensor_descr' => $descr,
  103. 'sensor_divisor' => $divisor, 'sensor_multiplier' => $multiplier, 'sensor_limit' => $high_limit, 'sensor_limit_warn' => $warn_limit, 'sensor_limit_low' => $low_limit,
  104. 'sensor_limit_low_warn' => $low_warn_limit, 'sensor_current' => $current, 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured );
  105. $inserted = dbInsert($insert, 'sensors');
  106. if ($debug) { echo("( $inserted inserted )\n"); }
  107. echo("+");
  108. log_event("Sensor Added: ".mres($class)." ".mres($type)." ". mres($index)." ".mres($descr), $device, 'sensor', mysql_insert_id());
  109. }
  110. else
  111. {
  112. $sensor_entry = dbFetchRow("SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?", array($class, $device['device_id'], $type, $index));
  113. if (!isset($high_limit))
  114. {
  115. if (!$sensor_entry['sensor_limit'])
  116. {
  117. $high_limit = sensor_limit($class, $current);
  118. } else {
  119. $high_limit = $sensor_entry['sensor_limit'];
  120. }
  121. }
  122. if ($high_limit != $sensor_entry['sensor_limit'])
  123. {
  124. $update = array('sensor_limit' => ($high_limit == NULL ? array('NULL') : $high_limit));
  125. $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
  126. if ($debug) { echo("( $updated updated )\n"); }
  127. echo("H");
  128. log_event("Sensor High Limit Updated: ".mres($class)." ".mres($type)." ". mres($index)." ".mres($descr)." (".$high_limit.")", $device, 'sensor', $sensor_id);
  129. }
  130. if (!isset($low_limit))
  131. {
  132. if (!$sensor_entry['sensor_limit_low'])
  133. {
  134. $low_limit = sensor_low_limit($class, $current);
  135. } else {
  136. $low_limit = $sensor_entry['sensor_limit_low'];
  137. }
  138. }
  139. if ($sensor_entry['sensor_limit_low'] != $low_limit)
  140. {
  141. $update = array('sensor_limit_low' => ($low_limit == NULL ? array('NULL') : $low_limit));
  142. $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
  143. if ($debug) { echo("( $updated updated )\n"); }
  144. echo("L");
  145. log_event("Sensor Low Limit Updated: ".mres($class)." ".mres($type)." ". mres($index)." ".mres($descr)." (".$low_limit.")", $device, 'sensor', $sensor_id);
  146. }
  147. if ($oid == $sensor_entry['sensor_oid'] && $descr == $sensor_entry['sensor_descr'] && $multiplier == $sensor_entry['sensor_multiplier'] && $divisor == $sensor_entry['sensor_divisor'] && $entPhysicalIndex_measured == $sensor_entry['entPhysicalIndex_measured'] && $entPhysicalIndex == $sensor_entry['entPhysicalIndex'])
  148. {
  149. echo(".");
  150. }
  151. else
  152. {
  153. $update = array('sensor_oid' => $oid, 'sensor_descr' => $descr, 'sensor_multiplier' => $multiplier, 'sensor_divisor' => $divisor,
  154. 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured);
  155. $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
  156. echo("U");
  157. log_event("Sensor Updated: ".mres($class)." ".mres($type)." ". mres($index)." ".mres($descr), $device, 'sensor', $sensor_id);
  158. if ($debug) { echo("( $updated updated )\n"); }
  159. }
  160. }
  161. $valid[$class][$type][$index] = 1;
  162. }
  163. function sensor_low_limit($class, $current)
  164. {
  165. $limit = NULL;
  166. switch($class)
  167. {
  168. case 'temperature':
  169. $limit = $current * 0.7;
  170. break;
  171. case 'voltage':
  172. $limit = $current * (1 - (sgn($current) * 0.15));
  173. break;
  174. case 'humidity':
  175. $limit = "70";
  176. break;
  177. case 'frequency':
  178. $limit = $current * 0.95;
  179. break;
  180. case 'current':
  181. $limit = NULL;
  182. break;
  183. case 'fanspeed':
  184. $limit = $current * 0.80;
  185. break;
  186. case 'power':
  187. $limit = NULL;
  188. break;
  189. }
  190. return $limit;
  191. }
  192. function sensor_limit($class, $current)
  193. {
  194. $limit = NULL;
  195. switch($class)
  196. {
  197. case 'temperature':
  198. $limit = $current * 1.60;
  199. break;
  200. case 'voltage':
  201. $limit = $current * (1 + (sgn($current) * 0.15));
  202. break;
  203. case 'humidity':
  204. $limit = "70";
  205. break;
  206. case 'frequency':
  207. $limit = $current * 1.05;
  208. break;
  209. case 'current':
  210. $limit = $current * 1.50;
  211. break;
  212. case 'fanspeed':
  213. $limit = $current * 1.30;
  214. break;
  215. case 'power':
  216. $limit = $current * 1.50;
  217. break;
  218. }
  219. return $limit;
  220. }
  221. function check_valid_sensors($device, $class, $valid)
  222. {
  223. $entries = dbFetchRows("SELECT * FROM sensors AS S, devices AS D WHERE S.sensor_class=? AND S.device_id = D.device_id AND D.device_id = ?", array($class, $device['device_id']));
  224. if (count($entries))
  225. {
  226. foreach ($entries as $entry)
  227. {
  228. $index = $entry['sensor_index'];
  229. $type = $entry['sensor_type'];
  230. if ($debug) { echo($index . " -> " . $type . "\n"); }
  231. if (!$valid[$class][$type][$index])
  232. {
  233. echo("-");
  234. dbDelete('sensors', "`sensor_id` = ?", array($entry['sensor_id']));
  235. log_event("Sensor Deleted: ".$entry['sensor_class']." ".$entry['sensor_type']." ". $entry['sensor_index']." ".$entry['sensor_descr'], $device, 'sensor', $sensor_id);
  236. }
  237. unset($oid); unset($type);
  238. }
  239. }
  240. }
  241. function discover_juniAtmVp(&$valid, $interface_id, $vp_id, $vp_descr)
  242. {
  243. global $config, $debug;
  244. if (dbFetchCell("SELECT COUNT(*) FROM `juniAtmVp` WHERE `interface_id` = ? AND `vp_id` = ?", array($interface_id, $vp_id)) == "0")
  245. {
  246. $inserted = dbInsert(array('interface_id' => $interface_id,'vp_id' => $vp_id,'vp_descr' => $vp_descr), 'juniAtmVp');
  247. if ($debug) { echo("( $inserted inserted )\n"); }
  248. #FIXME vv no $device!
  249. log_event("Juniper ATM VP Added: port ".mres($interface_id)." vp ".mres($vp_id)." descr". mres($vp_descr), 'juniAtmVp', mysql_insert_id());
  250. }
  251. else
  252. {
  253. echo(".");
  254. }
  255. $valid[$interface_id][$vp_id] = 1;
  256. }
  257. function discover_link($local_interface_id, $protocol, $remote_interface_id, $remote_hostname, $remote_port, $remote_platform, $remote_version)
  258. {
  259. global $config, $debug, $link_exists;
  260. if (dbFetchCell("SELECT COUNT(*) FROM `links` WHERE `remote_hostname` = ? AND `local_interface_id` = ? AND `protocol` = ? AND `remote_port` = ?",
  261. array($remote_hostname, $local_interface_id, $protocol, $remote_port)) == "0")
  262. {
  263. $inserted = dbInsert(array('local_interface_id' => $local_interface_id,'protocol' => $protocol,'remote_interface_id' => $remote_interface_id,'remote_hostname' => $remote_hostname,
  264. 'remote_port' => $remote_port,'remote_platform' => $remote_platform,'remote_version' => $remote_version), 'links');
  265. echo("+"); if ($debug) { echo("( $inserted inserted )"); }
  266. }
  267. else
  268. {
  269. $data = dbFetchRow("SELECT * FROM `links` WHERE `remote_hostname` = ? AND `local_interface_id` = ? AND `protocol` = ? AND `remote_port` = ?", array($remote_hostname, $local_interface_id, $protocol, $remote_port));
  270. if ($data['remote_interface_id'] == $remote_interface_id && $data['remote_platform'] == $remote_platform && $remote_version == $remote_version)
  271. {
  272. echo(".");
  273. }
  274. else
  275. {
  276. $updated = dbUpdate(array('remote_interface_id' => $remote_interface_id, 'remote_platform' => $remote_platform, 'remote_version' => $remote_version), 'links', '`id` = ?', array($data['id']));
  277. echo("U"); if ($debug) { echo("( $updated updated )"); }
  278. }
  279. }
  280. $link_exists[$local_interface_id][$remote_hostname][$remote_port] = 1;
  281. }
  282. function discover_storage(&$valid, $device, $index, $type, $mib, $descr, $size, $units, $used = NULL)
  283. {
  284. global $config, $debug;
  285. if ($debug) { echo("$device, $index, $type, $mib, $descr, $units, $used, $size\n"); }
  286. if ($descr && $size > "0")
  287. {
  288. $storage = dbFetchRow("SELECT * FROM `storage` WHERE `storage_index` = ? AND `device_id` = ? AND `storage_mib` = ?", array($index, $device['device_id'], $mib));
  289. if ($storage === FALSE || !count($storage))
  290. {
  291. $insert = dbInsert(array('device_id' => $device['device_id'], 'storage_descr' => $descr, 'storage_index' => $index, 'storage_mib' => $mib, 'storage_type' => $type,
  292. 'storage_units' => $units, 'storage_size' => $size, 'storage_used' => $used), 'storage');
  293. if ($debug) { mysql_error(); }
  294. echo("+");
  295. }
  296. else
  297. {
  298. $updated = dbUpdate(array('storage_descr' => $descr, 'storage_type' => $type, 'storage_units' => $units, 'storage_size' => $size), 'storage', '`device_id` = ? AND `storage_index` = ? AND `storage_mib` = ?', array($device['device_id'], $index, $mib));
  299. if ($updated) { echo("U"); } else { echo("."); }
  300. }
  301. $valid[$mib][$index] = 1;
  302. }
  303. }
  304. function discover_processor(&$valid, $device, $oid, $index, $type, $descr, $precision = "1", $current = NULL, $entPhysicalIndex = NULL, $hrDeviceIndex = NULL)
  305. {
  306. global $config, $debug;
  307. if ($debug) { echo("$device, $oid, $index, $type, $descr, $precision, $current, $entPhysicalIndex, $hrDeviceIndex\n"); }
  308. if ($descr)
  309. {
  310. $descr = str_replace("\"", "", $descr);
  311. if (mysql_result(mysql_query("SELECT count(processor_id) FROM `processors` WHERE `processor_index` = '$index' AND `device_id` = '".$device['device_id']."' AND `processor_type` = '$type'"),0) == '0')
  312. {
  313. $query = "INSERT INTO processors (`entPhysicalIndex`, `hrDeviceIndex`, `device_id`, `processor_descr`, `processor_index`, `processor_oid`, `processor_usage`, `processor_type`, `processor_precision`)
  314. values ('$entPhysicalIndex', '$hrDeviceIndex', '".$device['device_id']."', '$descr', '$index', '$oid', '$current', '$type','$precision')";
  315. mysql_query($query);
  316. if ($debug) { print $query . "\n"; }
  317. echo("+");
  318. log_event("Processor added: type ".mres($type)." index ".mres($index)." descr ". mres($descr), $device, 'processor', mysql_insert_id());
  319. }
  320. else
  321. {
  322. echo(".");
  323. $query = "UPDATE `processors` SET `processor_descr` = '".$descr."', `processor_oid` = '".$oid."', `processor_precision` = '".$precision."'
  324. WHERE `device_id` = '".$device['device_id']."' AND `processor_index` = '".$index."' AND `processor_type` = '".$type."'";
  325. mysql_query($query);
  326. if ($debug) { print $query . "\n"; }
  327. }
  328. $valid[$type][$index] = 1;
  329. }
  330. }
  331. function discover_mempool(&$valid, $device, $index, $type, $descr, $precision = "1", $entPhysicalIndex = NULL, $hrDeviceIndex = NULL)
  332. {
  333. global $config, $debug;
  334. if ($debug) { echo("$device, $oid, $index, $type, $descr, $precision, $current, $entPhysicalIndex, $hrDeviceIndex\n"); }
  335. if ($descr)
  336. {
  337. if (mysql_result(mysql_query("SELECT count(mempool_id) FROM `mempools` WHERE `mempool_index` = '$index' AND `device_id` = '".$device['device_id']."' AND `mempool_type` = '$type'"),0) == '0')
  338. {
  339. $query = "INSERT INTO mempools (`entPhysicalIndex`, `hrDeviceIndex`, `device_id`, `mempool_descr`, `mempool_index`, `mempool_type`, `mempool_precision`)
  340. values ('$entPhysicalIndex', '$hrDeviceIndex', '".$device['device_id']."', '$descr', '$index', '$type','$precision')";
  341. mysql_query($query);
  342. if ($debug) { print $query . "\n"; }
  343. echo("+");
  344. log_event("Memory pool added: type ".mres($type)." index ".mres($index)." descr ". mres($descr), $device, 'mempool', mysql_insert_id());
  345. }
  346. else
  347. {
  348. echo(".");
  349. $query = "UPDATE `mempools` SET `mempool_descr` = '".$descr."', `entPhysicalIndex` = '".$entPhysicalIndex."'";
  350. $query .= ", `hrDeviceIndex` = '$hrDeviceIndex' ";
  351. $query .= "WHERE `device_id` = '".$device['device_id']."' AND `mempool_index` = '".$index."' AND `mempool_type` = '".$type."'";
  352. mysql_query($query);
  353. if ($debug) { print $query . "\n"; }
  354. }
  355. $valid[$type][$index] = 1;
  356. }
  357. }
  358. function discover_toner(&$valid, $device, $oid, $index, $type, $descr, $capacity = NULL, $current = NULL)
  359. {
  360. global $config, $debug;
  361. if ($debug) { echo("$oid, $index, $type, $descr, $capacity\n"); }
  362. if (mysql_result(mysql_query("SELECT count(toner_id) FROM `toner` WHERE device_id = '".$device['device_id']."' AND toner_type = '$type' AND `toner_index` = '$index'"),0) == '0')
  363. {
  364. $query = "INSERT INTO toner (`device_id`, `toner_oid`, `toner_index`, `toner_type`, `toner_descr`, `toner_capacity`, `toner_current`) ";
  365. $query .= " VALUES ('".$device['device_id']."', '$oid', '$index', '$type', '$descr', '$capacity', '$current')";
  366. mysql_query($query);
  367. echo("+");
  368. log_event("Toner added: type ".mres($type)." index ".mres($index)." descr ". mres($descr), $device, 'toner', mysql_insert_id());
  369. }
  370. else
  371. {
  372. $toner_entry = mysql_fetch_assoc(mysql_query("SELECT * FROM `toner` WHERE device_id = '".$device['device_id']."' AND toner_type = '$type' AND `toner_index` = '$index'"));
  373. if ($oid == $toner_entry['toner_oid'] && $descr == $toner_entry['toner_descr'] && $capacity == $toner_entry['toner_capacity'])
  374. {
  375. echo(".");
  376. }
  377. else
  378. {
  379. mysql_query("UPDATE toner SET `toner_descr` = '$descr', `toner_oid` = '$oid', `toner_capacity` = '$capacity' WHERE `device_id` = '".$device['device_id']."' AND toner_type = '$type' AND `toner_index` = '$index' ");
  380. echo("U");
  381. }
  382. }
  383. $valid[$type][$index] = 1;
  384. }
  385. function discover_process_ipv6(&$valid, $ifIndex,$ipv6_address,$ipv6_prefixlen,$ipv6_origin)
  386. {
  387. global $device,$config;
  388. $ipv6_network = Net_IPv6::getNetmask("$ipv6_address/$ipv6_prefixlen") . '/' . $ipv6_prefixlen;
  389. $ipv6_compressed = Net_IPv6::compress($ipv6_address);
  390. if (Net_IPv6::getAddressType($ipv6_address) == NET_IPV6_LOCAL_LINK)
  391. {
  392. # ignore link-locals (coming from IPV6-MIB)
  393. return;
  394. }
  395. if (mysql_result(mysql_query("SELECT count(*) FROM `ports`
  396. WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1')
  397. {
  398. $i_query = "SELECT interface_id FROM `ports` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'";
  399. $interface_id = mysql_result(mysql_query($i_query), 0);
  400. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = '$ipv6_network'"), 0) < '1')
  401. {
  402. mysql_query("INSERT INTO `ipv6_networks` (`ipv6_network`) VALUES ('$ipv6_network')");
  403. echo("N");
  404. }
  405. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = '$ipv6_network'"), 0) < '1')
  406. {
  407. mysql_query("INSERT INTO `ipv6_networks` (`ipv6_network`) VALUES ('$ipv6_network')");
  408. echo("N");
  409. }
  410. $ipv6_network_id = @mysql_result(mysql_query("SELECT `ipv6_network_id` from `ipv6_networks` WHERE `ipv6_network` = '$ipv6_network'"), 0);
  411. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_addresses` WHERE `ipv6_address` = '$ipv6_address' AND `ipv6_prefixlen` = '$ipv6_prefixlen' AND `interface_id` = '$interface_id'"), 0) == '0')
  412. {
  413. mysql_query("INSERT INTO `ipv6_addresses` (`ipv6_address`, `ipv6_compressed`, `ipv6_prefixlen`, `ipv6_origin`, `ipv6_network_id`, `interface_id`)
  414. VALUES ('$ipv6_address', '$ipv6_compressed', '$ipv6_prefixlen', '$ipv6_origin', '$ipv6_network_id', '$interface_id')");
  415. echo("+");
  416. }
  417. else
  418. {
  419. echo(".");
  420. }
  421. $full_address = "$ipv6_address/$ipv6_prefixlen";
  422. $valid_address = $full_address . "-" . $interface_id;
  423. $valid['ipv6'][$valid_address] = 1;
  424. }
  425. }
  426. ?>