api.ponsfe90bt.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * OLT SmartFiber E9004-BT hardware abstraction layer
  4. */
  5. class PONSFE90BT extends PONProto {
  6. /**
  7. * Receives, preprocess and stores all required data from OLT
  8. *
  9. * @return void
  10. */
  11. public function collect() {
  12. $oltModelId = $this->oltParameters['MODELID'];
  13. $oltid = $this->oltParameters['ID'];
  14. $oltIp = $this->oltParameters['IP'];
  15. $oltCommunity = $this->oltParameters['COMMUNITY'];
  16. $oltNoFDBQ = $this->oltParameters['NOFDB'];
  17. $oltIPPORT = $oltIp . ':' . self::SNMPPORT;
  18. $sigIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  19. $this->snmpTemplates[$oltModelId]['signal']['SIGINDEX'],
  20. '',
  21. $this->snmpTemplates[$oltModelId]['signal']['SIGVALUE'],
  22. self::SNMPCACHE);
  23. $macIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  24. $this->snmpTemplates[$oltModelId]['signal']['MACINDEX'],
  25. '',
  26. $this->snmpTemplates[$oltModelId]['signal']['MACVALUE'],
  27. self::SNMPCACHE);
  28. $SFMACsProcessed = $this->macParseSF($macIndex);
  29. if (!empty($SFMACsProcessed)) {
  30. $this->signalParseSF($sigIndex, $SFMACsProcessed);
  31. $distIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  32. $this->snmpTemplates[$oltModelId]['misc']['DISTINDEX'],
  33. '',
  34. $this->snmpTemplates[$oltModelId]['misc']['DISTVALUE'],
  35. self::SNMPCACHE);
  36. $this->distanceParseSF($distIndex, $SFMACsProcessed);
  37. $ifaceIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  38. $this->snmpTemplates[$oltModelId]['misc']['IFACEDESCR'],
  39. '',
  40. array($this->snmpTemplates[$oltModelId]['misc']['IFACEVALUE'], '"'),
  41. self::SNMPCACHE);
  42. $ifaceCustDescrIndex = array();
  43. if (isset($this->snmpTemplates[$oltModelId]['misc']['IFACECUSTOMDESCR'])) {
  44. $ifaceCustDescrIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  45. $this->snmpTemplates[$oltModelId]['misc']['IFACECUSTOMDESCR'],
  46. '',
  47. array($this->snmpTemplates[$oltModelId]['misc']['IFACEVALUE'], '"'),
  48. self::SNMPCACHE);
  49. }
  50. $this->interfaceParseSF($ifaceIndex, $SFMACsProcessed, $ifaceCustDescrIndex);
  51. $lastDeregIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  52. $this->snmpTemplates[$oltModelId]['misc']['DEREGREASON'],
  53. '',
  54. $this->snmpTemplates[$oltModelId]['misc']['DEREGVALUE'],
  55. self::SNMPCACHE);
  56. $this->lastDeregParseSF($lastDeregIndex, $SFMACsProcessed);
  57. if (!$oltNoFDBQ) {
  58. // for some reason fdbVLANIndex for this OLT should be queried first
  59. // to prevent losing of the very first record from fdbVLANIndex
  60. $fdbVLANIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  61. $this->snmpTemplates[$oltModelId]['misc']['FDBVLANINDEX'],
  62. '',
  63. $this->snmpTemplates[$oltModelId]['misc']['FDBVLANVALUE'],
  64. self::SNMPCACHE);
  65. $fdbIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  66. $this->snmpTemplates[$oltModelId]['misc']['FDBMACINDEX'],
  67. '',
  68. $this->snmpTemplates[$oltModelId]['misc']['FDBMACVALUE'],
  69. self::SNMPCACHE);
  70. $this->fdbParseSF($SFMACsProcessed, $fdbIndex, $fdbVLANIndex);
  71. }
  72. }
  73. //getting others system data from OLT
  74. if (isset($this->snmpTemplates[$oltModelId]['system'])) {
  75. //OLT uptime
  76. if (isset($this->snmpTemplates[$oltModelId]['system']['UPTIME'])) {
  77. $uptimeIndexOid = $this->snmpTemplates[$oltModelId]['system']['UPTIME'];
  78. $oltSystemUptimeRaw = $this->snmp->walk($oltIPPORT, $oltCommunity, $uptimeIndexOid, self::SNMPCACHE);
  79. $this->uptimeParse($oltid, $oltSystemUptimeRaw);
  80. }
  81. //OLT temperature
  82. if (isset($this->snmpTemplates[$oltModelId]['system']['TEMPERATURE'])) {
  83. $temperatureIndexOid = $this->snmpTemplates[$oltModelId]['system']['TEMPERATURE'];
  84. $oltTemperatureRaw = $this->snmp->walk($oltIPPORT, $oltCommunity, $temperatureIndexOid, self::SNMPCACHE);
  85. $this->temperatureParse($oltid, $oltTemperatureRaw);
  86. }
  87. }
  88. }
  89. /**
  90. * Processes OLT MAC adresses and returns them in array: LLID=>MAC
  91. *
  92. * @param $macIndex
  93. *
  94. * @return array
  95. */
  96. protected function macParseSF($macIndex) {
  97. $ONUsMACs = array();
  98. if (!empty($macIndex)) {
  99. //mac index preprocessing
  100. foreach ($macIndex as $io => $eachmac) {
  101. $line = explode('=', $eachmac);
  102. if (empty($line[0]) || empty($line[1])) {
  103. continue;
  104. }
  105. $tmpONUPortLLID = trim($line[0]);
  106. $tmpONUMAC = strtolower(AddMacSeparator(RemoveMacAddressSeparator(trim($line[1]), array(':', '-', '.', ' ')))); //mac address
  107. //mac is present
  108. if (!empty($tmpONUPortLLID) and ! empty($tmpONUMAC)) {
  109. $ONUsMACs[$tmpONUPortLLID] = $tmpONUMAC;
  110. }
  111. }
  112. }
  113. return ($ONUsMACs);
  114. }
  115. /**
  116. * Performs signal preprocessing for sig/mac index arrays and stores it into cache
  117. *
  118. * @param array $sigIndex
  119. * @param array $macIndexProcessed
  120. *
  121. * @return void
  122. */
  123. protected function signalParseSF($sigIndex, $macIndexProcessed) {
  124. $ONUsModulesTemps = array();
  125. $ONUsModulesVoltages = array();
  126. $ONUsModulesCurrents = array();
  127. $ONUsSignals = array();
  128. $result = array();
  129. $macDevID = array();
  130. $curDate = curdatetime();
  131. //signal index preprocessing
  132. if ((!empty($sigIndex)) and ( !empty($macIndexProcessed))) {
  133. foreach ($sigIndex as $io => $eachsig) {
  134. $line = explode('=', $eachsig);
  135. //signal is present
  136. if (isset($line[0])) {
  137. $tmpONUPortLLID = trim($line[0]);
  138. if (empty($line[1])) {
  139. $ONUsSignals[$tmpONUPortLLID]['SignalRXRaw'] = '';
  140. $ONUsSignals[$tmpONUPortLLID]['SignalRXdBm'] = '';
  141. } else {
  142. $SignalRaw = trim($line[1]);
  143. $ONUsSignals[$tmpONUPortLLID]['SignalRXRaw'] = $SignalRaw;
  144. $ONUsSignals[$tmpONUPortLLID]['SignalRXdBm'] = trim(substr(stristr(stristr(stristr($SignalRaw, '('), ')', true), 'dBm', true), 1));
  145. }
  146. }
  147. }
  148. //storing results
  149. foreach ($macIndexProcessed as $devId => $eachMac) {
  150. if (isset($ONUsSignals[$devId])) {
  151. //signal history filling
  152. $signal = $ONUsSignals[$devId]['SignalRXdBm'];
  153. if (!empty($signal)) {
  154. $signal = round($signal, 2);
  155. $result[$eachMac] = $signal;
  156. }
  157. if (empty($signal) or $signal == 'Offline') {
  158. $signal = $this->onuOfflineSignalLevel; //over 9000 offline signal level :P
  159. }
  160. //saving each ONU signal history
  161. $this->olt->writeSignalHistory($eachMac, $signal);
  162. }
  163. }
  164. $macDevID = array_flip($macIndexProcessed);
  165. //writing signals cache
  166. $this->olt->writeSignals($result);
  167. //saving ONU cache
  168. $this->olt->writeOnuCache($macIndexProcessed);
  169. // saving macindex as MAC => devID
  170. $this->olt->writeMacIndex($macDevID);
  171. }
  172. }
  173. /**
  174. * Performs distance preprocessing for distance/mac index arrays and stores it into cache
  175. *
  176. * @param $DistIndex
  177. * @param $macIndexProcessed
  178. */
  179. protected function distanceParseSF($DistIndex, $macIndexProcessed) {
  180. $ONUDistances = array();
  181. $result = array();
  182. if (!empty($macIndexProcessed) and ! empty($DistIndex)) {
  183. //last dereg index preprocessing
  184. foreach ($DistIndex as $io => $eachRow) {
  185. $line = explode('=', $eachRow);
  186. if (empty($line[0]) || empty($line[1])) {
  187. continue;
  188. }
  189. $tmpONUPortLLID = trim($line[0]);
  190. $tmpONUDistance = trim($line[1]);
  191. $ONUDistances[$tmpONUPortLLID] = $tmpONUDistance;
  192. }
  193. //storing results
  194. foreach ($macIndexProcessed as $devId => $eachMac) {
  195. if (isset($ONUDistances[$devId])) {
  196. $result[$eachMac] = $ONUDistances[$devId];
  197. }
  198. }
  199. //saving distances
  200. $this->olt->writeDistances($result);
  201. }
  202. }
  203. /**
  204. * Performs interface preprocessing for interface/mac index arrays and stores it into cache
  205. *
  206. * @param $IfaceIndex
  207. * @param $macIndexProcessed
  208. * @param $ifaceCustDescrRaw
  209. *
  210. * @return void
  211. */
  212. protected function interfaceParseSF($IfaceIndex, $macIndexProcessed, $ifaceCustDescrRaw = array()) {
  213. $ONUIfaces = array();
  214. $result = array();
  215. $processIfaceCustDescr = !empty($ifaceCustDescrRaw);
  216. $ifaceCustDescrIdx = array();
  217. $ifaceCustDescrArr = array();
  218. // olt iface descr extraction
  219. if ($processIfaceCustDescr) {
  220. foreach ($ifaceCustDescrRaw as $io => $each) {
  221. if (empty($each)) {
  222. continue;
  223. }
  224. $ifDescr = explode('=', str_replace(array(" ", "\t", "\n", "\r", "\0", "\x0B"), '', $each));
  225. if (empty($ifDescr[0]) && empty($ifDescr[1])) {
  226. continue;
  227. }
  228. $ifaceCustDescrIdx[$ifDescr[0]] = $ifDescr[1];
  229. }
  230. }
  231. if (!empty($macIndexProcessed) and ! empty($IfaceIndex)) {
  232. //OLT iface index preprocessing
  233. foreach ($IfaceIndex as $io => $eachRow) {
  234. if (empty($eachRow)) {
  235. continue;
  236. }
  237. $line = explode('=', str_replace(array(" ", "\t", "\n", "\r", "\0", "\x0B"), '', $eachRow));
  238. if (empty($line[0]) || empty($line[1]) || trim($line[0]) < 9) {
  239. continue;
  240. }
  241. $tmpONUPortLLID = trim($line[0]) - 8; // some shitty math
  242. $tmpONUIface = trim($line[1]);
  243. $ONUIfaces[$tmpONUPortLLID] = $tmpONUIface;
  244. }
  245. //storing results
  246. foreach ($macIndexProcessed as $devId => $eachMac) {
  247. $tPONIfaceNum = substr($devId, 0, 1);
  248. if (array_key_exists($tPONIfaceNum, $ONUIfaces)) {
  249. $tPONIfaceName = $ONUIfaces[$tPONIfaceNum];
  250. $tPONIfaceStr = $tPONIfaceName . ' / ' . str_replace('.', ':', $devId);
  251. $cleanIface = strstr($tPONIfaceStr, ':', true);
  252. if ($processIfaceCustDescr && !isset($ifaceCustDescrArr[$cleanIface]) && array_key_exists($tPONIfaceNum, $ifaceCustDescrIdx)) {
  253. $ifaceCustDescrArr[$cleanIface] = $ifaceCustDescrIdx[$tPONIfaceNum];
  254. }
  255. } else {
  256. $tPONIfaceStr = str_replace('.', ':', $devId);
  257. }
  258. $result[$eachMac] = $tPONIfaceStr;
  259. }
  260. //saving ONU interfaces and interfaces descriptions
  261. $this->olt->writeInterfaces($result);
  262. $this->olt->writeInterfacesDescriptions($ifaceCustDescrArr);
  263. }
  264. }
  265. /**
  266. * Parses & stores to cache ONUs FDB cache (MACs behind ONU)
  267. *
  268. * @param $onuMACIndex
  269. * @param $fdbIndex
  270. * @param $fdbVLANIndex
  271. *
  272. * @return void
  273. */
  274. protected function fdbParseSF($onuMACIndex, $fdbIndex, $fdbVLANIndex) {
  275. if (!empty($onuMACIndex)) {
  276. $fdbDevIdxMAC = array();
  277. $fdbIdxVLAN = array();
  278. $fdbCahce = array();
  279. if (!empty($fdbIndex)) {
  280. // processing FDBIndex array to get pon [port number + ONU LLID + dev idx => FDB MAC] mapping
  281. foreach ($fdbIndex as $each => $eachIdx) {
  282. $line = explode('=', $eachIdx);
  283. // FDB MAC is present
  284. if (isset($line[1])) {
  285. $fdbMAC = trim(str_replace(array('"', 'STRING:', 'dev/ro'), '', $line[1]));
  286. if (empty($fdbMAC)) continue;
  287. $fdbMAC = strtolower(AddMacSeparator(RemoveMacAddressSeparator($fdbMAC, array(':', '-', '.', ' ')))); // FDB MAC in space-separated format
  288. $portLLIDDevIdx = trim($line[0]); // pon port number + ONU LLID + dev idx
  289. //$portLLID = substr($portLLIDDevIdx, 0, 3); // pon port number + ONU LLID
  290. $fdbDevIdxMAC[$portLLIDDevIdx] = $fdbMAC; // pon port number + ONU LLID + dev idx => FDB MAC
  291. //$fdbIdxMAC[$fdbMAC] = $portLLID; // FDB MAC => pon port number + ONU LLID
  292. }
  293. }
  294. }
  295. if (!empty($fdbVLANIndex)) {
  296. // processing $fdbVLANIndex array to get [pon port number + ONU LLID + dev idx mapping => FDB VLAN] mapping
  297. foreach ($fdbVLANIndex as $each => $eachIdx) {
  298. $line = explode('=', $eachIdx);
  299. // FDB VLAN is present
  300. if (isset($line[1])) {
  301. $fdbVLAN = trim($line[1]); // FDB VLAN
  302. $portLLIDDevIdx = trim($line[0]); // pon port number + ONU LLID + dev idx
  303. $fdbIdxVLAN[$portLLIDDevIdx] = $fdbVLAN; // pon port number + ONU LLID + dev idx => FDB MAC
  304. }
  305. }
  306. }
  307. if (!empty($fdbDevIdxMAC)) {
  308. // processing $fdbIdxMAC and $fdbIdxVLAN to prepare [pon port number + ONU LLID => ['mac' => $eachFDBMAC, 'vlan' => $tmpFDBVLAN]] array
  309. foreach ($fdbDevIdxMAC as $eachPortLLIDDevIdx => $eachFDBMAC) {
  310. $portLLID = substr($eachPortLLIDDevIdx, 0, 3);
  311. $devIdx = substr($eachPortLLIDDevIdx, -1, 1);
  312. $tmpFDBVLAN = empty($fdbIdxVLAN[$eachPortLLIDDevIdx]) ? '' : $fdbIdxVLAN[$eachPortLLIDDevIdx];
  313. $fdbMACVLAN[$portLLID][$devIdx] = array('mac' => $eachFDBMAC, 'vlan' => $tmpFDBVLAN);
  314. }
  315. if (!empty($fdbMACVLAN)) {
  316. foreach ($onuMACIndex as $eachLLID => $eachONUMAC) {
  317. if (!empty($fdbMACVLAN[$eachLLID])) {
  318. $fdbCahce[$eachONUMAC] = $fdbMACVLAN[$eachLLID];
  319. }
  320. }
  321. }
  322. }
  323. //saving OLT FDB
  324. $this->olt->writeFdb($fdbCahce);
  325. }
  326. }
  327. /**
  328. * Performs last dereg reason preprocessing for dereg reason/mac index arrays and stores it into cache
  329. *
  330. * @param $LastDeregIndex
  331. * @param $macIndexProcessed
  332. *
  333. * @return void
  334. */
  335. protected function lastDeregParseSF($LastDeregIndex, $macIndexProcessed) {
  336. $ONUDeRegs = array();
  337. $result = array();
  338. if (!empty($macIndexProcessed) and ! empty($LastDeregIndex)) {
  339. //last dereg index preprocessing
  340. foreach ($LastDeregIndex as $io => $eachRow) {
  341. $line = explode('=', $eachRow);
  342. if (empty($line[0]) || empty($line[1])) {
  343. continue;
  344. }
  345. $tmpONUPortLLID = trim($line[0]);
  346. $tmpONULastDeregReason = intval(trim($line[1]));
  347. switch ($tmpONULastDeregReason) {
  348. case 1:
  349. $TxtColor = '"#F80000"';
  350. $tmpONULastDeregReasonStr = 'Wire down';
  351. break;
  352. case 3:
  353. $TxtColor = '"#FF4400"';
  354. $tmpONULastDeregReasonStr = 'Power off';
  355. break;
  356. default:
  357. $TxtColor = '"#000000"';
  358. $tmpONULastDeregReasonStr = 'Unknown';
  359. break;
  360. }
  361. if (!empty($tmpONUPortLLID)) {
  362. $tmpONULastDeregReasonStr = wf_tag('font', false, '', 'color=' . $TxtColor . '') .
  363. $tmpONULastDeregReasonStr .
  364. wf_tag('font', true);
  365. $ONUDeRegs[$tmpONUPortLLID] = $tmpONULastDeregReasonStr;
  366. }
  367. }
  368. //storing results
  369. foreach ($macIndexProcessed as $devId => $eachMac) {
  370. if (isset($ONUDeRegs[$devId])) {
  371. $result[$eachMac] = $ONUDeRegs[$devId];
  372. }
  373. }
  374. //saving ONUs deregs reasons
  375. $this->olt->writeDeregs($result);
  376. }
  377. }
  378. }