PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/polling/ospf.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 346 lines | 265 code | 46 blank | 35 comment | 42 complexity | cf74e9e81786a5025eeef29e9b33bbf3 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. echo("OSPF: ");
  3. echo("Processes: ");
  4. $ospf_instance_count = 0;
  5. $ospf_port_count = 0;
  6. $ospf_area_count = 0;
  7. $ospf_neighbour_count = 0;
  8. $ospf_oids_db = array('ospfRouterId', 'ospfAdminStat', 'ospfVersionNumber', 'ospfAreaBdrRtrStatus', 'ospfASBdrRtrStatus',
  9. 'ospfExternLsaCount', 'ospfExternLsaCksumSum', 'ospfTOSSupport', 'ospfOriginateNewLsas', 'ospfRxNewLsas',
  10. 'ospfExtLsdbLimit', 'ospfMulticastExtensions', 'ospfExitOverflowInterval', 'ospfDemandExtensions');
  11. ### Build array of existing entries
  12. foreach (dbFetchRows("SELECT * FROM `ospf_instances` WHERE `device_id` = ?", array($device['device_id'])) as $entry)
  13. {
  14. $ospf_instances_db[$entry['ospf_instance_id']] = $entry;
  15. }
  16. ### Pull data from device
  17. $ospf_instances_poll = snmpwalk_cache_oid($device, "OSPF-MIB::ospfGeneralGroup", array(), "OSPF-MIB");
  18. foreach ($ospf_instances_poll as $ospf_instance_id => $ospf_entry)
  19. {
  20. ### If the entry doesn't already exist in the prebuilt array, insert into the database and put into the array
  21. if (!isset($ospf_instances_db[$ospf_instance_id]))
  22. {
  23. dbInsert(array('device_id' => $device['device_id'], 'ospf_instance_id' => $ospf_instance_id), 'ospf_instances');
  24. echo("+");
  25. $ospf_instances_db[$entry['ospf_instance_id']] = dbFetchRow("SELECT * FROM `ospf_instances` WHERE `device_id` = ? AND `ospf_instance_id` = ?", array($device['device_id'],$ospf_instance_id));
  26. $ospf_instances_db[$entry['ospf_instance_id']] = $entry;
  27. }
  28. }
  29. if ($debug)
  30. {
  31. echo("\nPolled: ");
  32. print_r($ospf_instances_poll);
  33. echo("Database: ");
  34. print_r($ospf_instances_db);
  35. echo("\n");
  36. }
  37. ### Loop array of entries and update
  38. if (is_array($ospf_instances_db))
  39. {
  40. foreach ($ospf_instances_db as $ospf_instance_db)
  41. {
  42. $ospf_instance_poll = $ospf_instances_poll[$ospf_instance_db['ospf_instance_id']];
  43. foreach ($ospf_oids_db as $oid)
  44. { // Loop the OIDs
  45. if ($ospf_instance_db[$oid] != $ospf_instance_poll[$oid])
  46. { // If data has changed, build a query
  47. $ospf_instance_update[$oid] = $ospf_instance_poll[$oid];
  48. #log_event("$oid -> ".$this_port[$oid], $device, 'ospf', $port['interface_id']); ## FIXME
  49. }
  50. }
  51. if ($ospf_instance_update)
  52. {
  53. dbUpdate($ospf_instance_update, 'ospf_instances', '`device_id` = ? AND `ospf_instance_id` = ?', array($device['device_id'], $ospf_instance_id));
  54. echo("U");
  55. unset($ospf_instance_update);
  56. } else {
  57. echo(".");
  58. }
  59. unset($ospf_instance_poll);
  60. unset($ospf_instance_db);
  61. $ospf_instance_count++;
  62. }
  63. }
  64. unset($ospf_instances_poll);
  65. unset($ospf_instances_db);
  66. echo(" Areas: ");
  67. $ospf_area_oids = array('ospfAuthType','ospfImportAsExtern','ospfSpfRuns','ospfAreaBdrRtrCount','ospfAsBdrRtrCount','ospfAreaLsaCount','ospfAreaLsaCksumSum','ospfAreaSummary','ospfAreaStatus');
  68. ### Build array of existing entries
  69. foreach (dbFetchRows("SELECT * FROM `ospf_areas` WHERE `device_id` = ?", array($device['device_id'])) as $entry)
  70. {
  71. $ospf_areas_db[$entry['ospfAreaId']] = $entry;
  72. }
  73. ### Pull data from device
  74. $ospf_areas_poll = snmpwalk_cache_oid($device, "OSPF-MIB::ospfAreaEntry", array(), "OSPF-MIB");
  75. foreach ($ospf_areas_poll as $ospf_area_id => $ospf_area)
  76. {
  77. ### If the entry doesn't already exist in the prebuilt array, insert into the database and put into the array
  78. if (!isset($ospf_areas_db[$ospf_area_id]))
  79. {
  80. dbInsert(array('device_id' => $device['device_id'], 'ospfAreaId' => $ospf_area_id), 'ospf_areas');
  81. echo("+");
  82. $entry = dbFetchRows("SELECT * FROM `ospf_areas` WHERE `device_id` = ? AND `ospfAreaId` = ?",array($device['device_id'], $ospf_area_id));
  83. $ospf_areas_db[$entry['ospf_area_id']] = $entry;
  84. }
  85. }
  86. if ($debug)
  87. {
  88. echo("\nPolled: ");
  89. print_r($ospf_areas_poll);
  90. echo("Database: ");
  91. print_r($ospf_areas_db);
  92. echo("\n");
  93. }
  94. ### Loop array of entries and update
  95. if (is_array($ospf_areas_db))
  96. {
  97. foreach ($ospf_areas_db as $ospf_area_db)
  98. {
  99. if (is_array($ospf_ports_poll[$ospf_port_db['ospf_port_id']]))
  100. {
  101. $ospf_area_poll = $ospf_areas_poll[$ospf_area_db['ospfAreaId']];
  102. foreach ($ospf_area_oids as $oid)
  103. { ## Loop the OIDs
  104. if ($ospf_area_db[$oid] != $ospf_area_poll[$oid])
  105. { ## If data has changed, build a query
  106. $ospf_area_update[$oid] = $ospf_area_poll[$oid];
  107. #log_event("$oid -> ".$this_port[$oid], $device, 'interface', $port['interface_id']); ## FIXME
  108. }
  109. }
  110. if ($ospf_area_update)
  111. {
  112. dbUpdate($ospf_area_update, 'ospf_areas', '`device_id` = ? AND `ospfAreaId` = ?', array($device['device_id'], $ospf_area_id));
  113. echo("U");
  114. unset($ospf_area_update);
  115. } else {
  116. echo(".");
  117. }
  118. unset($ospf_area_poll);
  119. unset($ospf_area_db);
  120. $ospf_area_count++;
  121. } else {
  122. dbDelete('ospf_ports', '`device_id` = ? AND `ospfAreaId` = ?', array($device['device_id'], $ospf_area_db['ospfAreaId']));
  123. }
  124. }
  125. }
  126. unset($ospf_areas_db);
  127. unset($ospf_areas_poll);
  128. #$ospf_ports = snmpwalk_cache_oid($device, "OSPF-MIB::ospfIfEntry", array(), "OSPF-MIB");
  129. #print_r($ospf_ports);
  130. echo(" Ports: ");
  131. $ospf_port_oids = array('ospfIfIpAddress','interface_id','ospfAddressLessIf','ospfIfAreaId','ospfIfType','ospfIfAdminStat','ospfIfRtrPriority','ospfIfTransitDelay','ospfIfRetransInterval','ospfIfHelloInterval','ospfIfRtrDeadInterval','ospfIfPollInterval','ospfIfState','ospfIfDesignatedRouter','ospfIfBackupDesignatedRouter','ospfIfEvents','ospfIfAuthKey','ospfIfStatus','ospfIfMulticastForwarding','ospfIfDemand','ospfIfAuthType');
  132. ### Build array of existing entries
  133. foreach (dbFetchRows("SELECT * FROM `ospf_ports` WHERE `device_id` = ?", array($device['device_id'])) as $entry)
  134. {
  135. $ospf_ports_db[$entry['ospf_port_id']] = $entry;
  136. }
  137. ### Pull data from device
  138. $ospf_ports_poll = snmpwalk_cache_oid($device, "OSPF-MIB::ospfIfEntry", array(), "OSPF-MIB");
  139. foreach ($ospf_ports_poll as $ospf_port_id => $ospf_port)
  140. {
  141. ### If the entry doesn't already exist in the prebuilt array, insert into the database and put into the array
  142. if (!isset($ospf_ports_db[$ospf_port_id]))
  143. {
  144. dbInsert(array('device_id' => $device['device_id'], 'ospf_port_id' => $ospf_port_id), 'ospf_ports');
  145. echo("+");
  146. $ospf_ports_db[$entry['ospf_port_id']] = dbFetchRow("SELECT * FROM `ospf_ports` WHERE `device_id` = ? AND `ospf_port_id` = ?", array($device['device_id'], $ospf_port_id));
  147. }
  148. }
  149. if ($debug)
  150. {
  151. echo("\nPolled: ");
  152. print_r($ospf_ports_poll);
  153. echo("Database: ");
  154. print_r($ospf_ports_db);
  155. echo("\n");
  156. }
  157. ### Loop array of entries and update
  158. if (is_array($ospf_ports_db))
  159. {
  160. foreach ($ospf_ports_db as $ospf_port_id => $ospf_port_db)
  161. {
  162. if (is_array($ospf_ports_poll[$ospf_port_db['ospf_port_id']]))
  163. {
  164. $ospf_port_poll = $ospf_ports_poll[$ospf_port_db['ospf_port_id']];
  165. if ($ospf_port_poll['ospfAddressLessIf'])
  166. {
  167. $ospf_port_poll['interface_id'] = @dbFetchCell("SELECT `interface_id` FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?", array($device['device_id'], $ospf_port_poll['ospfAddressLessIf']));
  168. } else {
  169. $ospf_port_poll['interface_id'] = @dbFetchCell("SELECT A.`interface_id` FROM ipv4_addresses AS A, ports AS I WHERE A.ipv4_address = ? AND I.interface_id = A.interface_id AND I.device_id = ?", array($ospf_port_poll['ospfIfIpAddress'], $device['device_id']));
  170. }
  171. foreach ($ospf_port_oids as $oid)
  172. { // Loop the OIDs
  173. if ($ospf_port_db[$oid] != $ospf_port_poll[$oid])
  174. { // If data has changed, build a query
  175. $ospf_port_update[$oid] = $ospf_port_poll[$oid];
  176. #log_event("$oid -> ".$this_port[$oid], $device, 'ospf', $port['interface_id']); ## FIXME
  177. }
  178. }
  179. if ($ospf_port_update)
  180. {
  181. dbUpdate($ospf_port_update, 'ospf_ports', '`device_id` = ? AND `ospf_port_id` = ?', array($device['device_id'], $ospf_port_id));
  182. echo("U");
  183. unset($ospf_port_update);
  184. } else {
  185. echo(".");
  186. }
  187. unset($ospf_port_poll);
  188. unset($ospf_port_db);
  189. $ospf_port_count++;
  190. } else {
  191. dbDelete('ospf_ports', '`device_id` = ? AND `ospf_port_id` = ?', array($device['device_id'], $ospf_port_db['ospf_port_id']));
  192. # "DELETE FROM `ospf_ports` WHERE `device_id` = '".$device['device_id']."' AND `ospf_port_id` = '".$ospf_port_db['ospf_port_id']."'");
  193. echo("-");
  194. }
  195. }
  196. }
  197. #OSPF-MIB::ospfNbrIpAddr.172.22.203.98.0 172.22.203.98
  198. #OSPF-MIB::ospfNbrAddressLessIndex.172.22.203.98.0 0
  199. #OSPF-MIB::ospfNbrRtrId.172.22.203.98.0 172.22.203.128
  200. #OSPF-MIB::ospfNbrOptions.172.22.203.98.0 2
  201. #OSPF-MIB::ospfNbrPriority.172.22.203.98.0 0
  202. #OSPF-MIB::ospfNbrState.172.22.203.98.0 full
  203. #OSPF-MIB::ospfNbrEvents.172.22.203.98.0 6
  204. #OSPF-MIB::ospfNbrLsRetransQLen.172.22.203.98.0 1
  205. #OSPF-MIB::ospfNbmaNbrStatus.172.22.203.98.0 active
  206. #OSPF-MIB::ospfNbmaNbrPermanence.172.22.203.98.0 dynamic
  207. #OSPF-MIB::ospfNbrHelloSuppressed.172.22.203.98.0 false
  208. echo(' Neighbours: ');
  209. $ospf_nbr_oids_db = array('ospfNbrIpAddr', 'ospfNbrAddressLessIndex', 'ospfNbrRtrId', 'ospfNbrOptions', 'ospfNbrPriority', 'ospfNbrState', 'ospfNbrEvents', 'ospfNbrLsRetransQLen', 'ospfNbmaNbrStatus', 'ospfNbmaNbrPermanence', 'ospfNbrHelloSuppressed');
  210. $ospf_nbr_oids_rrd = array();
  211. $ospf_nbr_oids = array_merge($ospf_nbr_oids_db, $ospf_nbr_oids_rrd);
  212. ### Build array of existing entries
  213. foreach (dbFetchRows("SELECT * FROM `ospf_nbrs` WHERE `device_id` = ?", array($device['device_id'])) as $nbr_entry)
  214. {
  215. $ospf_nbrs_db[$nbr_entry['ospf_nbr_id']] = $nbr_entry;
  216. }
  217. ### Pull data from device
  218. $ospf_nbrs_poll = snmpwalk_cache_oid($device, "OSPF-MIB::ospfNbrEntry", array(), "OSPF-MIB");
  219. foreach ($ospf_nbrs_poll as $ospf_nbr_id => $ospf_nbr)
  220. {
  221. ### If the entry doesn't already exist in the prebuilt array, insert into the database and put into the array
  222. if (!isset($ospf_nbrs_db[$ospf_nbr_id]))
  223. {
  224. dbInsert(array('device_id' => $device['device_id'], 'ospf_nbr_id' => $ospf_nbr_id), 'ospf_nbrs');
  225. echo("+");
  226. $entry = dbFetchRow("SELECT * FROM `ospf_nbrs` WHERE `device_id` = ? AND `ospf_nbr_id` = ?", array($device['device_id'], $ospf_nbr_id));
  227. $ospf_nbrs_db[$entry['ospf_nbr_id']] = $entry;
  228. }
  229. }
  230. if ($debug)
  231. {
  232. echo("\nPolled: ");
  233. print_r($ospf_nbrs_poll);
  234. echo("Database: ");
  235. print_r($ospf_nbrs_db);
  236. echo("\n");
  237. }
  238. ### Loop array of entries and update
  239. if (is_array($ospf_nbrs_db))
  240. {
  241. foreach ($ospf_nbrs_db as $ospf_nbr_id => $ospf_nbr_db)
  242. {
  243. if (is_array($ospf_nbrs_poll[$ospf_nbr_db['ospf_nbr_id']]))
  244. {
  245. $ospf_nbr_poll = $ospf_nbrs_poll[$ospf_nbr_db['ospf_nbr_id']];
  246. $ospf_nbr_poll['interface_id'] = @dbFetchCell("SELECT A.`interface_id` FROM ipv4_addresses AS A, nbrs AS I WHERE A.ipv4_address = ? AND I.interface_id = A.interface_id AND I.device_id = ?", array($ospf_nbr_poll['ospfNbrIpAddr'], $device['device_id']));
  247. if ($ospf_nbr_db['interface_id'] != $ospf_nbr_poll['interface_id'])
  248. {
  249. if ($ospf_nbr_poll['interface_id']) {
  250. $ospf_nbr_update = array('interface_id' => $ospf_nbr_poll['interface_id']);
  251. } else {
  252. $ospf_nbr_update = array('interface_id' => array('NULL'));
  253. }
  254. }
  255. foreach ($ospf_nbr_oids as $oid)
  256. { // Loop the OIDs
  257. if ($debug) { echo($ospf_nbr_db[$oid]."|".$ospf_nbr_poll[$oid]."\n"); }
  258. if ($ospf_nbr_db[$oid] != $ospf_nbr_poll[$oid])
  259. { // If data has changed, build a query
  260. $ospf_nbr_update[$oid] = $ospf_nbr_poll[$oid];
  261. #log_event("$oid -> ".$this_nbr[$oid], $device, 'ospf', $nbr['interface_id']); ## FIXME
  262. }
  263. }
  264. if ($ospf_nbr_update)
  265. {
  266. dbUpdate($ospf_nbr_update, 'ospf_nbrs', '`device_id` = ? AND `ospf_nbr_id` = ?', array($device['device_id'], $ospf_nbr_id));
  267. echo("U");
  268. unset($ospf_nbr_update);
  269. } else {
  270. echo(".");
  271. }
  272. unset($ospf_nbr_poll);
  273. unset($ospf_nbr_db);
  274. $ospf_nbr_count++;
  275. } else {
  276. dbDelete('ospf_nbrs', '`device_id` = ? AND `ospf_nbr_id` = ?', array($device['device_id'], $ospf_nbr_db['ospf_nbr_id']));
  277. echo("-");
  278. }
  279. }
  280. }
  281. ## Create device-wide statistics RRD
  282. $filename = $config['rrd_dir'] . "/" . $device['hostname'] . "/" . safename("ospf-statistics.rrd");
  283. if (!is_file($filename))
  284. {
  285. rrdtool_create($filename, "--step 300 \
  286. DS:instances:GAUGE:600:0:1000000 \
  287. DS:areas:GAUGE:600:0:1000000 \
  288. DS:ports:GAUGE:600:0:1000000 \
  289. DS:neighbours:GAUGE:600:0:1000000 \
  290. RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797 \
  291. RRA:MIN:0.5:1:600 RRA:MIN:0.5:6:700 RRA:MIN:0.5:24:775 RRA:MIN:0.5:288:797 \
  292. RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:288:797 \
  293. RRA:LAST:0.5:1:600 RRA:LAST:0.5:6:700 RRA:LAST:0.5:24:775 RRA:LAST:0.5:288:797");
  294. }
  295. $rrd_update = "N:".$ospf_instance_count.":".$ospf_area_count.":".$ospf_port_count.":".$ospf_neighbour_count;
  296. $ret = rrdtool_update("$filename", $rrd_update);
  297. unset($ospf_ports_db);
  298. unset($ospf_ports_poll);
  299. echo("\n");
  300. ?>