api.switches.php 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. <?php
  2. /**
  3. * Returns array of all currently dead devices
  4. *
  5. * @return array
  6. */
  7. function zb_SwitchesGetAllDead() {
  8. $dead_switches_raw = zb_StorageGet('SWDEAD');
  9. if (!$dead_switches_raw) {
  10. $result = array();
  11. } else {
  12. $result = unserialize($dead_switches_raw);
  13. }
  14. return ($result);
  15. }
  16. /**
  17. * Returns array of each curently dead switches death time as ip=>datetime
  18. *
  19. * @return array
  20. */
  21. function zb_SwitchesGetAllDeathTime() {
  22. $result = array();
  23. $deathTimeDb = new NyanORM('deathtime');
  24. $all = $deathTimeDb->getAll();
  25. if (!empty($all)) {
  26. foreach ($all as $io => $each) {
  27. $result[$each['ip']] = $each['date'];
  28. }
  29. }
  30. return ($result);
  31. }
  32. /**
  33. * Sets dead switch death time
  34. *
  35. * @param $ip Switch IP
  36. *
  37. * @return void
  38. */
  39. function zb_SwitchDeathTimeSet($ip) {
  40. $ip = ubRouting::filters($ip, 'mres');
  41. $curdatetime = curdatetime();
  42. $deathTimeDb = new NyanORM('deathtime');
  43. $deathTimeDb->data('ip', $ip);
  44. $deathTimeDb->data('date', $curdatetime);
  45. $deathTimeDb->create();
  46. }
  47. /**
  48. * Function than resurrects dead switch :)
  49. *
  50. * @param $ip Switch IP
  51. *
  52. * @return void
  53. */
  54. function zb_SwitchDeathTimeResurrection($ip) {
  55. $ip = ubRouting::filters($ip, 'mres');
  56. $deathTimeDb = new NyanORM('deathtime');
  57. $deathTimeDb->where('ip', '=', $ip);
  58. $deathTimeDb->delete();
  59. }
  60. /**
  61. * Returns array of all available snmp model templates as name=>device description
  62. *
  63. * @return array
  64. */
  65. function zb_SwitchModelsSnmpTemplatesGetAll() {
  66. $allSnmpTemplatesRaw = sp_SnmpGetAllModelTemplates();
  67. $allSnmpTemplates = array('' => __('None'));
  68. if (!empty($allSnmpTemplatesRaw)) {
  69. foreach ($allSnmpTemplatesRaw as $io => $each) {
  70. if (isset($each['define'])) {
  71. if (isset($each['define']['DEVICE'])) {
  72. $allSnmpTemplates[$io] = $each['define']['DEVICE'];
  73. } else {
  74. $allSnmpTemplates[$io] = '⚠️ ' . __('Template') . ' ' . $io . ' - ' . __('is corrupted');
  75. }
  76. } else {
  77. $allSnmpTemplates[$io] = '⚠️ ' . __('Template') . ' ' . $io . ' - ' . __('is corrupted');
  78. }
  79. }
  80. }
  81. return ($allSnmpTemplates);
  82. }
  83. /**
  84. * Returns switch model add form
  85. *
  86. * @return string
  87. */
  88. function web_SwitchModelAddForm() {
  89. $allSnmpTemplates = zb_SwitchModelsSnmpTemplatesGetAll();
  90. $addinputs = wf_TextInput('newsm', 'Model', '', true);
  91. $addinputs .= wf_TextInput('newsmp', 'Ports', '', true, '5');
  92. $addinputs .= wf_Selector('newsst', $allSnmpTemplates, 'SNMP template', '');
  93. $addinputs .= wf_delimiter() . web_add_icon() . ' ' . wf_Submit('Create');
  94. $addform = wf_Form('', 'POST', $addinputs, 'glamour');
  95. $result = $addform;
  96. return ($result);
  97. }
  98. /**
  99. * Returns list of all available switch models
  100. *
  101. * @return string
  102. */
  103. function web_SwitchModelsShow() {
  104. global $ubillingConfig;
  105. $result = '';
  106. $allmodels = zb_SwitchModelsGetAll();
  107. $allSwitches = zb_SwitchesGetAll();
  108. $allSnmpTemplates = zb_SwitchModelsSnmpTemplatesGetAll();
  109. $modelsCount = array();
  110. //switch devices count
  111. if (!empty($allSwitches)) {
  112. foreach ($allSwitches as $io => $eachSwitchData) {
  113. if (isset($modelsCount[$eachSwitchData['modelid']])) {
  114. $modelsCount[$eachSwitchData['modelid']]++;
  115. } else {
  116. $modelsCount[$eachSwitchData['modelid']] = 1;
  117. }
  118. }
  119. }
  120. //PON devices count
  121. if ($ubillingConfig->getAlterParam('PON_ENABLED')) {
  122. $onuDevicesDb = new NyanORM('pononu');
  123. $onuDevicesDb->selectable(array('id', 'onumodelid'));
  124. $allOnu = $onuDevicesDb->getAll();
  125. if (!empty($allOnu)) {
  126. foreach ($allOnu as $io => $eachOnuData) {
  127. if (isset($modelsCount[$eachOnuData['onumodelid']])) {
  128. $modelsCount[$eachOnuData['onumodelid']]++;
  129. } else {
  130. $modelsCount[$eachOnuData['onumodelid']] = 1;
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Now its time to break up with the system
  137. * Our reasons are clear and listed
  138. * Come on and change the cause of the history
  139. * Take off disguise of that rotten mystery
  140. */
  141. if (!empty($allmodels)) {
  142. $tablecells = wf_TableCell(__('ID'));
  143. $tablecells .= wf_TableCell(__('Model'));
  144. $tablecells .= wf_TableCell(__('Devices'));
  145. $tablecells .= wf_TableCell(__('Ports'));
  146. $tablecells .= wf_TableCell(__('SNMP template'));
  147. $tablecells .= wf_TableCell(__('Actions'));
  148. $tablerows = wf_TableRow($tablecells, 'row1');
  149. foreach ($allmodels as $io => $eachmodel) {
  150. $availDevicesCount = (isset($modelsCount[$eachmodel['id']])) ? $modelsCount[$eachmodel['id']] : 0;
  151. $snmpLabel = '';
  152. $snmpTemplate = $eachmodel['snmptemplate'];
  153. if (!empty($snmpTemplate)) {
  154. if (isset($allSnmpTemplates[$snmpTemplate])) {
  155. $snmpLabel .= $allSnmpTemplates[$snmpTemplate];
  156. } else {
  157. $snmpLabel .= __('Template') . ' ' . $snmpTemplate . ' - ' . __('Not exists');
  158. show_error(__('Template') . ' ' . $snmpTemplate . ' ' . __('for') . ' ' . __('Equipment models') . ' ' . __('ID') . ' [' . $eachmodel['id'] . ']' . ' - ' . __('Not exists'));
  159. }
  160. }
  161. $tablecells = wf_TableCell($eachmodel['id']);
  162. $tablecells .= wf_TableCell($eachmodel['modelname']);
  163. $tablecells .= wf_TableCell($availDevicesCount);
  164. $tablecells .= wf_TableCell($eachmodel['ports']);
  165. $tablecells .= wf_TableCell($snmpLabel);
  166. $switchmodelcontrols = wf_JSAlert('?module=switchmodels&deletesm=' . $eachmodel['id'], web_delete_icon(), 'Removing this may lead to irreparable results');
  167. $switchmodelcontrols .= wf_Link('?module=switchmodels&edit=' . $eachmodel['id'], web_edit_icon());
  168. $tablecells .= wf_TableCell($switchmodelcontrols);
  169. $tablerows .= wf_TableRow($tablecells, 'row5');
  170. }
  171. $result .= wf_TableBody($tablerows, '100%', '0', 'sortable');
  172. } else {
  173. $messages = new UbillingMessageHelper();
  174. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  175. }
  176. return ($result);
  177. }
  178. /**
  179. * Returns array of all available switch models
  180. *
  181. * @return array
  182. */
  183. function zb_SwitchModelsGetAll() {
  184. global $ubillingConfig;
  185. $sortByModelName = $ubillingConfig->getAlterParam('DEVICES_LISTS_SORT_BY_MODELNAME');
  186. $query = "SELECT * from `switchmodels`";
  187. $result = simple_queryall($query);
  188. if (!empty($result) and $sortByModelName) {
  189. $result = zb_sortArray($result, 'modelname');
  190. }
  191. return ($result);
  192. }
  193. /**
  194. * Return some switch model data by id
  195. *
  196. * @param int $modelid
  197. * @return array
  198. */
  199. function zb_SwitchModelGetData($modelid) {
  200. $modelid = vf($modelid, 3);
  201. $query = "SELECT * from `switchmodels` where `id`='" . $modelid . "'";
  202. $result = simple_query($query);
  203. return ($result);
  204. }
  205. /**
  206. * Returns switch model selector
  207. *
  208. * @param string $selectname Name of input element
  209. * @param array $allmodels available models array
  210. * @return string
  211. */
  212. function web_SwitchModelSelector($selectname = 'switchmodelid', $allmodels = array()) {
  213. $tmpArr = array();
  214. if (empty($allmodels)) {
  215. $allmodels = zb_SwitchModelsGetAll();
  216. }
  217. if (!empty($allmodels)) {
  218. foreach ($allmodels as $io => $each) {
  219. $tmpArr[$each['id']] = $each['modelname'];
  220. }
  221. }
  222. $selector = wf_Selector($selectname, $tmpArr, __('Model'), '', false);
  223. return ($selector);
  224. }
  225. /**
  226. * Creates new switch model in database
  227. *
  228. * @param string $name
  229. * @param string $ports
  230. * @param string $snmptemplate
  231. */
  232. function ub_SwitchModelAdd($name, $ports, $snmptemplate = '') {
  233. $ports = vf($ports, 3);
  234. $nameClean = mysql_real_escape_string($name);
  235. $snmptemplate = mysql_real_escape_string($snmptemplate);
  236. if (empty($ports)) {
  237. $ports = 'NULL';
  238. } else {
  239. $ports = "'" . $ports . "'";
  240. }
  241. if (empty($snmptemplate)) {
  242. $snmptemplate = 'NULL';
  243. } else {
  244. $snmptemplate = "'" . $snmptemplate . "'";
  245. }
  246. $query = "INSERT INTO `switchmodels` (`id` ,`modelname` ,`ports`,`snmptemplate`) VALUES (NULL , '" . $nameClean . "', " . $ports . "," . $snmptemplate . ");";
  247. nr_query($query);
  248. $newId = simple_get_lastid('switchmodels');
  249. log_register('SWITCHMODEL ADD `' . $name . '` [' . $newId . ']');
  250. }
  251. /**
  252. * Deletes switch model from database by its ID
  253. *
  254. * @param integer $modelId
  255. *
  256. * @return void/string on error
  257. */
  258. function ub_SwitchModelDelete($modelId) {
  259. $modelId = ubRouting::filters($modelId, 'int');
  260. $result = '';
  261. if (!empty($modelId)) {
  262. $switches = new NyanORM('switches');
  263. $switches->where('modelid', '=', $modelId);
  264. $switches->selectable('id');
  265. $switchesUsingThisModel = $switches->getAll();
  266. $ponOnus = new NyanORM('pononu');
  267. $ponOnus->where('onumodelid', '=', $modelId);
  268. $ponOnus->selectable('id');
  269. $onuUsingThisModel = $ponOnus->getAll();
  270. //is this model used by some devices?
  271. if (empty($switchesUsingThisModel) and empty($onuUsingThisModel)) {
  272. $switchModels = new NyanORM('switchmodels');
  273. $switchModels->where('id', '=', $modelId);
  274. $switchModels->delete();
  275. log_register('SWITCHMODEL DELETE [' . $modelId . ']');
  276. } else {
  277. $result .= __('You know, we really would like to let you perform this action, but our conscience does not allow us to do');
  278. log_register('SWITCHMODEL DELETE [' . $modelId . '] FAIL IN_USE');
  279. }
  280. } else {
  281. $result .= __('Model') . ' ' . __('is empty');
  282. }
  283. return ($result);
  284. }
  285. /**
  286. * Returns switch ID selector
  287. *
  288. * @param string $name Input element name
  289. * @param string $label Input element label
  290. * @param int $selected preselected in switch ID
  291. * @param int $currentSwitchId switch for whom this widget showed
  292. * @param bool $notSearchable Explictly disables searchable functionality
  293. *
  294. * @return string
  295. */
  296. function web_SwitchUplinkSelector($name, $label = '', $selected = '', $currentSwitchId = '', $notSearchable = false) {
  297. global $ubillingConfig;
  298. $result = '';
  299. $tmpArr = array('' => '-');
  300. $validSwitches = array();
  301. $allswitchesRaw = array();
  302. $searchableFlag = ($ubillingConfig->getAlterParam('SWITCHUPL_SEARCHBL')) ? true : false;
  303. if ($notSearchable) {
  304. $searchableFlag = false;
  305. }
  306. $query = "SELECT * from `switches` WHERE `desc` NOT LIKE '%NP%' AND `geo` != '' ORDER BY `location` ASC;";
  307. $allswitches = simple_queryall($query);
  308. if (!empty($allswitches)) {
  309. foreach ($allswitches as $io => $each) {
  310. //switches with geo and without NP
  311. $validSwitches[$each['id']] = $each;
  312. }
  313. }
  314. if (!empty($allswitches)) {
  315. //checks for preventing loops
  316. $alllinks = array();
  317. $tmpSwitches = zb_SwitchesGetAll("ORDER BY `location` ASC");
  318. if (!empty($tmpSwitches)) {
  319. foreach ($tmpSwitches as $io => $each) {
  320. //transform array to id=>switchdata
  321. $allswitchesRaw[$each['id']] = $each;
  322. }
  323. //making id=>parentid array
  324. foreach ($tmpSwitches as $io => $each) {
  325. $alllinks[$each['id']] = $each['parentid'];
  326. }
  327. }
  328. foreach ($allswitchesRaw as $io => $each) {
  329. if ((sm_CheckLoop($alllinks, $currentSwitchId, $each['id'])) and ($each['id'] != $currentSwitchId)) {
  330. if (isset($validSwitches[$each['id']])) {
  331. $tmpArr[$each['id']] = $each['location'] . ' - ' . $each['ip'];
  332. }
  333. }
  334. }
  335. }
  336. if ($searchableFlag) {
  337. $result .= wf_SelectorSearchable($name, $tmpArr, $label, $selected, false);
  338. } else {
  339. $result .= wf_Selector($name, $tmpArr, $label, $selected, false);
  340. }
  341. return ($result);
  342. }
  343. /**
  344. * Returns switch creation form
  345. *
  346. * @return string
  347. */
  348. function web_SwitchFormAdd() {
  349. global $ubillingConfig;
  350. $altCfg = $ubillingConfig->getAlter();
  351. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  352. $equipmentModels = zb_SwitchModelsGetAll();
  353. if (!empty($equipmentModels)) {
  354. $addinputs = wf_TextInput('newip', 'IP', '', true, 20, 'ip');
  355. $addinputs .= wf_TextInput('newlocation', 'Location', '', true, 30);
  356. $addinputs .= wf_TextInput('newdesc', 'Description', '', true, 30);
  357. $addinputs .= wf_TextInput('newsnmp', 'SNMP community', '', true, 20);
  358. $addinputs .= wf_TextInput('newsnmpwrite', 'SNMP write community', '', true, 20);
  359. if ($altCfg['SWITCHES_EXTENDED']) {
  360. $addinputs .= wf_TextInput('newswid', 'Switch ID', '', true, 20, 'mac');
  361. }
  362. $addinputs .= wf_TextInput('newgeo', 'Geo location', '', true, 20, 'geo');
  363. $addinputs .= web_SwitchModelSelector('newswitchmodel', $equipmentModels);
  364. $addinputs .= wf_tag('br');
  365. $addinputs .= web_SwitchUplinkSelector('newparentid', __('Uplink switch'), '', '', true);
  366. $addinputs .= wf_tag('br');
  367. if (cfr('SWITCHGROUPS') and $swGroupsEnabled) {
  368. $switchGroups = new SwitchGroups();
  369. $addinputs .= $switchGroups->renderSwitchGroupsSelector('newswgroup') . wf_delimiter();
  370. }
  371. $addinputs .= wf_tag('br');
  372. $addinputs .= wf_Submit('Save');
  373. $addform = wf_Form("", 'POST', $addinputs, 'glamour');
  374. } else {
  375. $messages = new UbillingMessageHelper();
  376. $errorNotice = __('Equipment models') . ': ' . __('Not exists');
  377. $addform = $messages->getStyledMessage($errorNotice, 'error');
  378. }
  379. return ($addform);
  380. }
  381. /**
  382. * Returns switch mini-map
  383. *
  384. * @param array $switchdata
  385. * @return string
  386. */
  387. function web_SwitchMiniMap($switchdata) {
  388. global $ubillingConfig;
  389. $ymconf = $ubillingConfig->getYmaps();
  390. $result = '';
  391. $result .= wf_tag('div', false, '', 'id="ubmap" class="glamour" style="width: 97%; height:300px;"') . wf_tag('div', true);
  392. $result .= wf_delimiter();
  393. $placemarks = sm_MapDrawSwitches();
  394. $placemarks .= sm_MapDrawSwitchUplinks($switchdata['id']);
  395. $radius = 30;
  396. $area = sm_MapAddCircle($switchdata['geo'], $radius, __('Search area radius') . ' ' . $radius . ' ' . __('meters'), __('Search area'));
  397. $result .= generic_MapInit($switchdata['geo'], $ymconf['FINDING_ZOOM'], $ymconf['TYPE'], $area . $placemarks, '', $ymconf['LANG']);
  398. $result .= wf_tag('div', false, '', 'style="clear:both;"') . wf_tag('div', true);
  399. return ($result);
  400. }
  401. /**
  402. * Shows list of all available downlink switches
  403. *
  404. * @param int $switchId
  405. *
  406. * @return void
  407. */
  408. function web_SwitchDownlinksList($switchId) {
  409. global $ubillingConfig;
  410. $alterconf = $ubillingConfig->getAlter();
  411. $switchesExtended = $ubillingConfig->getAlterParam('SWITCHES_EXTENDED');
  412. if ($switchesExtended) {
  413. $switchesUplinks = new SwitchUplinks();
  414. $switchesUplinks->loadAllUplinksData();
  415. }
  416. $switchId = vf($switchId, 3);
  417. $all = zb_SwitchesGetAll();
  418. $downlinks = array();
  419. $result = '';
  420. if (!empty($all)) {
  421. if (!empty($switchId)) {
  422. foreach ($all as $io => $each) {
  423. if ($each['parentid'] == $switchId) {
  424. $downlinks[$each['id']] = $each;
  425. }
  426. }
  427. }
  428. }
  429. //load dead switches cache
  430. $dead_switches_raw = zb_StorageGet('SWDEAD');
  431. if (!$dead_switches_raw) {
  432. $dead_switches = array();
  433. } else {
  434. $dead_switches = unserialize($dead_switches_raw);
  435. }
  436. $deathTime = zb_SwitchesGetAllDeathTime();
  437. if (!empty($downlinks)) {
  438. $allModels = zb_SwitchModelsGetAllTag();
  439. $cells = wf_TableCell(__('ID'));
  440. $cells .= wf_TableCell(__('IP'));
  441. if ($switchesExtended) {
  442. $cells .= wf_TableCell(__('Uplink'));
  443. //separate uplink port
  444. if ($switchesExtended == 3) {
  445. $cells .= wf_TableCell(__('Port'));
  446. }
  447. }
  448. $cells .= wf_TableCell(__('Location'));
  449. $cells .= wf_TableCell(__('Active'));
  450. $cells .= wf_TableCell(__('Model'));
  451. $cells .= wf_TableCell(__('SNMP community'));
  452. $cells .= wf_TableCell(__('Geo location'));
  453. $cells .= wf_TableCell(__('Description'));
  454. $cells .= wf_TableCell(__('Actions'));
  455. $rows = wf_TableRow($cells, 'row1');
  456. foreach ($downlinks as $io => $each) {
  457. if (isset($dead_switches[$each['ip']])) {
  458. if (isset($deathTime[$each['ip']])) {
  459. $obituary = __('Switch dead since') . ' ' . $deathTime[$each['ip']];
  460. } else {
  461. $obituary = '';
  462. }
  463. $aliveled = web_red_led($obituary) . ' ' . __('No');
  464. $aliveflag = '0';
  465. } else {
  466. if (strpos($each['desc'], 'NP') === false) {
  467. $aliveled = web_green_led() . ' ' . __('Yes');
  468. $aliveflag = '1';
  469. } else {
  470. $aliveled = web_yellow_led() . ' ' . __('NP');
  471. $aliveflag = '2';
  472. }
  473. }
  474. $cells = wf_TableCell($each['id']);
  475. $cells .= wf_TableCell($each['ip']);
  476. if ($switchesExtended) {
  477. $includePortFlag = ($switchesExtended == 2) ? true : false;
  478. $cells .= wf_TableCell($switchesUplinks->getUplinkTinyDesc($each['id'], $includePortFlag));
  479. //separate uplink port
  480. if ($switchesExtended == 3) {
  481. $cells .= wf_TableCell($switchesUplinks->getUplinkPort($each['id']));
  482. }
  483. }
  484. $cells .= wf_TableCell($each['location']);
  485. $cells .= wf_TableCell($aliveled);
  486. $cells .= wf_TableCell(@$allModels[$each['modelid']]);
  487. $cells .= wf_TableCell($each['snmp']);
  488. $cells .= wf_TableCell($each['geo']);
  489. $cells .= wf_TableCell($each['desc']);
  490. $actLinks = wf_Link('?module=switches&edit=' . $each['id'], web_edit_icon(), false);
  491. $cells .= wf_TableCell($actLinks);
  492. $rows .= wf_TableRow($cells, 'row3');
  493. }
  494. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  495. show_window(__('Downlinks'), $result);
  496. }
  497. }
  498. /**
  499. * Returns switch edit form for some existing device ID aka "switch profile"
  500. *
  501. * @param int $switchid
  502. * @return string
  503. */
  504. function web_SwitchEditForm($switchid) {
  505. global $ubillingConfig;
  506. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  507. $switchid = vf($switchid, 3);
  508. $altCfg = $ubillingConfig->getAlter();
  509. $result = '';
  510. $mainForm = '';
  511. $rightContainer = '';
  512. $allswitchmodels = zb_SwitchModelsGetAllTag();
  513. $switchdata = zb_SwitchGetData($switchid);
  514. $editinputs = wf_Selector('editmodel', $allswitchmodels, 'Model', $switchdata['modelid'], true);
  515. $editinputs .= wf_TextInput('editip', 'IP', $switchdata['ip'], true, 20, 'ip');
  516. $editinputs .= wf_TextInput('editlocation', 'Location', $switchdata['location'], true, 30);
  517. $editinputs .= wf_TextInput('editdesc', 'Description', $switchdata['desc'], true, 30);
  518. $editinputs .= wf_TextInput('editsnmp', 'SNMP community', $switchdata['snmp'], true, 20);
  519. $editinputs .= wf_TextInput('editsnmpwrite', 'SNMP write community', $switchdata['snmpwrite'], true, 20);
  520. if ($altCfg['SWITCHES_EXTENDED']) {
  521. $macVenControl = '';
  522. if ((!empty($switchdata['swid'])) and ($altCfg['MACVEN_ENABLED'])) {
  523. if (cfr('MACVEN')) {
  524. $macVenControl = wf_AjaxLink('?module=macvendor&mac=' . $switchdata['swid'] . '&raw=true', wf_img('skins/macven.gif', __('Device vendor')), 'swvendorcontainer', false, '');
  525. $swvendorStyle = 'style="text-align: left; font-size:150%; font-weight: bold;"';
  526. $rightContainer .= wf_tag('div', false, '', 'id="swvendorcontainer"' . $swvendorStyle) . '' . wf_tag('div', true);
  527. }
  528. }
  529. $editinputs .= wf_TextInput('editswid', __('Switch ID') . ' (MAC) ' . $macVenControl, $switchdata['swid'], true, 20, 'mac');
  530. }
  531. $editinputs .= wf_TextInput('editgeo', 'Geo location', $switchdata['geo'], true, 20, 'geo');
  532. if (!empty($switchdata['parentid'])) {
  533. $uplinkSwitchLabel = wf_Link('?module=switches&edit=' . $switchdata['parentid'], wf_img_sized('skins/icon_ok.gif', '', '10', '10') . ' ' . __('Uplink switch'), false, '');
  534. } else {
  535. $uplinkSwitchLabel = wf_img_sized('skins/icon_minus.png', '', '10', '10') . ' ' . __('Uplink switch');
  536. }
  537. $editinputs .= web_SwitchUplinkSelector('editparentid', $uplinkSwitchLabel, $switchdata['parentid'], $switchid);
  538. //switch uplink detailed data here
  539. if ($ubillingConfig->getAlterParam('SWITCHES_EXTENDED')) {
  540. $swUplink = new SwitchUplinks($switchid);
  541. //saving changes if required
  542. if (ubRouting::checkPost($swUplink::ROUTE_SWID)) {
  543. $swUplink->save();
  544. ubRouting::nav($swUplink::URL_SWPROFILE . ubRouting::post($swUplink::ROUTE_SWID));
  545. }
  546. $editinputs .= wf_delimiter(0) . $swUplink->renderSwitchUplinkData();
  547. if (cfr('SWITCHESEDIT')) {
  548. if (!ubRouting::checkGet($swUplink::ROUTE_EDITINTERFACE)) {
  549. $editinputs .= ' ' . wf_Link($swUplink::URL_SWPROFILE . $switchid . '&' . $swUplink::ROUTE_EDITINTERFACE . '=true', '⬇️');
  550. } else {
  551. $editinputs .= ' ' . wf_Link($swUplink::URL_SWPROFILE . $switchid, '⬆️');
  552. $editinputs .= wf_delimiter(0) . $swUplink->renderEditForm();
  553. }
  554. }
  555. }
  556. //switch auth data directory enabled?
  557. if ($ubillingConfig->getAlterParam('SWITCHES_AUTH_ENABLED')) {
  558. $swAuth = new SwitchAuth($switchid);
  559. $swAuthData = $swAuth->getAuthData($switchid);
  560. if (empty($swAuthData)) {
  561. $authLabel = '🔒 ' . __('Device authorization data not set');
  562. } else {
  563. $authLabel = '🔑 ' . __('Device authorization data available');
  564. }
  565. $editinputs .= wf_delimiter(0);
  566. if (cfr('SWITCHESEDIT')) {
  567. $editinputs .= wf_Link($swAuth::URL_ME . '&' . $swAuth::ROUTE_DEVID . '=' . $switchid, $authLabel);
  568. } else {
  569. $editinputs .= $authLabel;
  570. }
  571. }
  572. $editinputs .= wf_tag('br');
  573. if (cfr('SWITCHGROUPS') and $swGroupsEnabled) {
  574. $switchGroups = new SwitchGroups();
  575. $editinputs .= $switchGroups->renderSwitchGroupsSelector('editswgroup', $switchid) . wf_delimiter();
  576. }
  577. if (cfr('SWITCHESEDIT')) {
  578. $editinputs .= wf_delimiter(0);
  579. $editinputs .= wf_Submit('Save');
  580. }
  581. $mainForm .= wf_Form('', 'POST', $editinputs, 'glamour');
  582. //main interface grid
  583. if (!empty($switchdata['ip'])) {
  584. $rightContainer .= wf_AjaxLoader();
  585. $rightContainer .= wf_AjaxContainer('icmppingcontainer');
  586. }
  587. $cells = wf_TableCell($mainForm, '50%', '', 'valign="top"');
  588. $cells .= wf_TableCell($rightContainer, '', '', 'valign="top"');
  589. $rows = wf_TableRow($cells);
  590. $result .= wf_TableBody($rows, '100%', 0, '');
  591. $result .= wf_CleanDiv();
  592. $result .= wf_delimiter();
  593. $result .= wf_BackLink('?module=switches');
  594. if (cfr('SWITCHPOLL')) {
  595. $fdbCacheName = 'exports/' . $switchdata['ip'] . '_fdb';
  596. if (file_exists($fdbCacheName)) {
  597. $fdbControls = wf_Link('?module=switchpoller&fdbfor=' . $switchdata['ip'], wf_img('skins/menuicons/switchpoller.png') . ' ' . __('FDB cache'), false, 'ubButton');
  598. $fdbControls .= wf_Link('?module=fdbarchive&switchidfilter=' . $switchid, wf_img('skins/fdbarchive.png') . ' ' . __('FDB') . ' ' . __('Archive'), false, 'ubButton');
  599. $result .= wf_modalAuto(wf_img('skins/menuicons/switchpoller.png') . ' ' . __('FDB'), __('FDB'), $fdbControls, 'ubButton');
  600. }
  601. if ((!empty($switchdata['snmp'])) and (ispos($switchdata['desc'], 'SWPOLL'))) {
  602. $result .= wf_Link('?module=switchpoller&switchid=' . $switchid, wf_img('skins/snmp.png') . ' ' . __('SNMP data'), false, 'ubButton');
  603. }
  604. }
  605. if (!empty($switchdata['ip'])) {
  606. $result .= wf_AjaxLink('?module=switches&backgroundicmpping=' . $switchdata['ip'], wf_img('skins/ping_icon.png') . ' ' . __('ICMP ping'), 'icmppingcontainer', false, 'ubButton');
  607. }
  608. if (isset($altCfg['SW_WEBNAV'])) {
  609. if ($altCfg['SW_WEBNAV']) {
  610. $result .= ' ' . wf_tag('a', false, 'ubButton', 'href="http://' . $switchdata['ip'] . '" target="_BLANK"') . wf_img('skins/ymaps/globe.png') . ' ' . __('Go to the web interface') . wf_tag('a', true) . ' ';
  611. }
  612. }
  613. if (cfr('SWITCHESEDIT')) {
  614. if (!ispos($switchdata['desc'], 'NP')) {
  615. $result .= wf_JSAlertStyled('?module=switchreplace&switchid=' . $switchid, wf_img('skins/duplicate_icon.gif') . ' ' . __('Replacement'), __('Are you serious'), 'ubButton') . ' ';
  616. }
  617. }
  618. if (cfr('SWITCHESEDIT')) {
  619. if (empty($switchdata['geo'])) {
  620. $result .= wf_Link('?module=switchmap&locfinder=true&placesw=' . $switchid, wf_img('skins/ymaps/target.png') . ' ' . __('Place on map'), false, 'ubButton');
  621. }
  622. }
  623. if (cfr('SWITCHESEDIT')) {
  624. $result .= wf_AjaxLink('?module=switchhistory&ajax=true&switchid=' . $switchid, wf_img('skins/log_icon_small.png') . ' ' . __('History'), 'icmppingcontainer', false, 'ubButton') . ' ';
  625. }
  626. if (cfr('SWCASH')) {
  627. if (ispos($switchdata['desc'], 'SWCASH')) {
  628. if (@$altCfg['SW_CASH_ENABLED']) {
  629. $result .= wf_Link('?module=swcash&switchid=' . $switchid, wf_img('skins/ukv/dollar.png') . ' ' . __('Financial data'), false, 'ubButton');
  630. }
  631. }
  632. }
  633. if (cfr('TASKMAN')) {
  634. if (!empty($switchdata['location'])) {
  635. if (!ts_isMeBranchCursed()) {
  636. $taskCreateForm = ts_TaskCreateFormUnified($switchdata['location'], '', '', '', '', '');
  637. $taskCreateModal = wf_modalAuto(wf_img('skins/createtask_16.png', __('Create task')) . ' ' . __('Task'), __('Create task'), $taskCreateForm, 'ubButton');
  638. $result .= $taskCreateModal;
  639. }
  640. }
  641. }
  642. if (cfr('REPORTSWPORT')) {
  643. if (@$altCfg['SWITCHPORT_IN_PROFILE']) {
  644. $result .= wf_Link('?module=report_switchportassign&switchid=' . $switchid, wf_img('skins/icon_user_16.gif') . ' ' . __('Switch port assign'), false, 'ubButton');
  645. }
  646. }
  647. if (cfr('SWITCHESEDIT')) {
  648. $deletionUrl = '?module=switches&switchdelete=' . $switchid;
  649. $cancelUrl = '?module=switches&edit=' . $switchid;
  650. $deletionAlert = __('Removing this may lead to irreparable results');
  651. $delDialogTitle = __('Delete') . ' ' . __('Switch') . ': ' . $switchdata['location'] . '?';
  652. $result .= wf_ConfirmDialog($deletionUrl, web_delete_icon() . ' ' . __('Delete'), $deletionAlert, 'ubButton', $cancelUrl, $delDialogTitle);
  653. }
  654. //SWPOLL proposal
  655. if (!empty($switchdata['ip'])) {
  656. if (!ispos($switchdata['desc'], 'SWPOLL') and (!ispos($switchdata['desc'], 'NP')) and (!ispos($switchdata['desc'], 'OLT'))) {
  657. //this is not OLT
  658. if (!ispos($switchdata['desc'], 'AP') and (!ispos($switchdata['desc'], 'MTSIGMON'))) {
  659. //Or some wireless access point
  660. if (!empty($switchdata['snmp'])) {
  661. //with some non empty snmp read comunity
  662. if (!empty($switchdata['modelid'])) {
  663. $allModelsSnmpTemplates = sp_SnmpGetModelTemplatesAssoc();
  664. if (isset($allModelsSnmpTemplates[$switchdata['modelid']])) {
  665. //device model have some SNMP template assigned
  666. $messages = new UbillingMessageHelper();
  667. $result .= $messages->getStyledMessage(__('It looks like this device can be polled using SNMP if you specify SWPOLL in the notes'), 'info');
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. return ($result);
  675. }
  676. /**
  677. * Returns array of all available switches with its full data
  678. *
  679. * @param string $order
  680. *
  681. * @return array
  682. */
  683. function zb_SwitchesGetAll($order = '') {
  684. if (empty($order)) {
  685. $order = 'ORDER BY `id` DESC';
  686. }
  687. $query = 'SELECT * FROM `switches` ' . $order . ';';
  688. $allswitches = simple_queryall($query);
  689. return ($allswitches);
  690. }
  691. /**
  692. * Returns array of all available switches with its full data with some %mask% in description as switchId=>switchData
  693. *
  694. * @param string $mask
  695. *
  696. * @return array
  697. */
  698. function zb_SwitchesGetAllMask($mask = '') {
  699. $result = array();
  700. if (!empty($mask)) {
  701. $where = "WHERE `desc` LIKE '%" . $mask . "%'";
  702. } else {
  703. $where = '';
  704. }
  705. $query = 'SELECT * FROM `switches` ' . $where . ';';
  706. $allSwitches = simple_queryall($query);
  707. if (!empty($allSwitches)) {
  708. foreach ($allSwitches as $io => $each) {
  709. $result[$each['id']] = $each;
  710. }
  711. }
  712. return ($result);
  713. }
  714. /**
  715. * Returns array of all available switches with its full data ordered by location
  716. *
  717. * @return array
  718. */
  719. function zb_SwitchesGetAllLocationOrder() {
  720. $query = 'SELECT * FROM `switches` ORDER BY `location` ASC';
  721. $allswitches = simple_queryall($query);
  722. return ($allswitches);
  723. }
  724. /**
  725. * Return geo data in ip->geo format
  726. *
  727. * @return array
  728. */
  729. function zb_SwitchesGetAllGeo() {
  730. $query = "SELECT `ip`,`geo` from `switches`";
  731. $alldata = simple_queryall($query);
  732. $result = array();
  733. if (!empty($alldata)) {
  734. foreach ($alldata as $io => $each) {
  735. $result[$each['ip']] = $each['geo'];
  736. }
  737. }
  738. return ($result);
  739. }
  740. /**
  741. * Return geo data in id->geo format
  742. *
  743. * @return array
  744. */
  745. function zb_SwitchesGetAllGeoId() {
  746. $query = "SELECT `id`,`geo` from `switches`";
  747. $alldata = simple_queryall($query);
  748. $result = array();
  749. if (!empty($alldata)) {
  750. foreach ($alldata as $io => $each) {
  751. $result[$each['id']] = $each['geo'];
  752. }
  753. }
  754. return ($result);
  755. }
  756. /**
  757. * Returns switch data by its ID
  758. *
  759. * @param int $switchid
  760. * @return array
  761. */
  762. function zb_SwitchGetData($switchid) {
  763. $switchid = vf($switchid, 3);
  764. $query = "SELECT * FROM `switches` WHERE `id`='" . $switchid . "' ";
  765. $result = simple_query($query);
  766. return ($result);
  767. }
  768. /**
  769. * Returns switch models array in format modelid=>name
  770. *
  771. * @return array
  772. */
  773. function zb_SwitchModelsGetAllTag() {
  774. $allmodels = zb_SwitchModelsGetAll();
  775. $result = array();
  776. if (!empty($allmodels)) {
  777. foreach ($allmodels as $io => $eachmodel) {
  778. $result[$eachmodel['id']] = $eachmodel['modelname'];
  779. }
  780. }
  781. return ($result);
  782. }
  783. /**
  784. * Returns result of fast icmp ping
  785. *
  786. * @param string $ip devide IP to ping
  787. *
  788. * @return bool
  789. */
  790. function zb_PingICMP($ip) {
  791. $globconf = parse_ini_file(CONFIG_PATH . "billing.ini");
  792. $ping = $globconf['PING'];
  793. $sudo = $globconf['SUDO'];
  794. $ping_command = $sudo . ' ' . $ping . ' -i 0.01 -c 1 ' . $ip;
  795. $ping_result = shell_exec($ping_command);
  796. if (strpos($ping_result, 'ttl')) {
  797. return (true);
  798. } else {
  799. return (false);
  800. }
  801. }
  802. /**
  803. * Returns result of fast ICMP ping with ability to set timeout in seconds(floating values allowed)
  804. *
  805. * @param string $ip
  806. * @param int $timeout
  807. *
  808. * @return bool
  809. */
  810. function zb_PingICMPTimeout($ip, $timeout = 0) {
  811. $globconf = parse_ini_file(CONFIG_PATH . "billing.ini");
  812. $ping = $globconf['PING'];
  813. $sudo = $globconf['SUDO'];
  814. $pingt_imeout = '';
  815. if (!empty($timeout)) {
  816. $curOS = php_uname('s');
  817. $pingt_imeout = (($curOS == 'FreeBSD') ? '-t ' : '-w ') . $timeout . ' ';
  818. }
  819. $ping_command = $sudo . ' ' . $ping . ' -i 0.01 -c 1 ' . $pingt_imeout . $ip;
  820. $ping_result = shell_exec($ping_command);
  821. if (strpos($ping_result, 'ttl')) {
  822. return (true);
  823. } else {
  824. return (false);
  825. }
  826. }
  827. /**
  828. * Returns result of slow icmp ping with some retries count
  829. *
  830. * @param string $ip devide IP to ping
  831. * @param int $retries number of retries to check host
  832. *
  833. * @return bool
  834. */
  835. function zb_PingICMPHope($ip, $retries = 3) {
  836. $result = false;
  837. $count = 0;
  838. for ($count = 0; $count < $retries; $count++) {
  839. deb($count);
  840. if (zb_PingICMP($ip)) {
  841. deb('true');
  842. $result = true;
  843. break;
  844. }
  845. }
  846. return ($result);
  847. }
  848. /**
  849. * Logs array of switches to deadlog (timemachine)
  850. *
  851. * @param int $currenttime current timestamp
  852. * @param array $deadSwitches dead switches array
  853. */
  854. function zb_SwitchesDeadLog($currenttime, $deadSwitches) {
  855. $date = curdatetime();
  856. $timestamp = $currenttime;
  857. $logData = serialize($deadSwitches);
  858. $query = "INSERT INTO `switchdeadlog` (`id` ,`date` ,`timestamp` ,`swdead`)
  859. VALUES (
  860. NULL , '" . $date . "', '" . $timestamp . "', '" . $logData . "');";
  861. nr_query($query);
  862. }
  863. /**
  864. * Performs all switches ping test and returns dead devices array
  865. *
  866. * @global object $ubillingConfig
  867. * @return array
  868. */
  869. function zb_SwitchesRepingAll() {
  870. global $ubillingConfig;
  871. $switchRepingProcess = new StarDust('SWPING');
  872. $altCfg = $ubillingConfig->getAlter();
  873. $deadswitches = array();
  874. $fastPingFlag = $ubillingConfig->getAlterParam('FASTPING_ENABLED');
  875. if ($fastPingFlag) {
  876. $fastPing = new FastPing();
  877. }
  878. if ($switchRepingProcess->notRunning()) {
  879. $switchRepingProcess->start();
  880. $deathTime = zb_SwitchesGetAllDeathTime();
  881. $allswitches = zb_SwitchesGetAllLocationOrder();
  882. if (!empty($allswitches)) {
  883. foreach ($allswitches as $io => $eachswitch) {
  884. if (!empty($eachswitch['ip']) and !ispos($eachswitch['desc'], 'NP')) {
  885. if (!$fastPingFlag) {
  886. //regular per-device ICMP polling
  887. if (!zb_PingICMP($eachswitch['ip'])) {
  888. $secondChance = zb_PingICMP($eachswitch['ip']);
  889. if (!$secondChance) {
  890. $lastChance = zb_PingICMP($eachswitch['ip']);
  891. if (!$lastChance) {
  892. if (empty($altCfg['SWITCH_PING_CUSTOM_SCRIPT'])) {
  893. //yep, switch looks like it really down
  894. $deadswitches[$eachswitch['ip']] = $eachswitch['location'];
  895. if (!isset($deathTime[$eachswitch['ip']])) {
  896. zb_SwitchDeathTimeSet($eachswitch['ip']);
  897. }
  898. } else {
  899. //really last-last chance
  900. $customTestCommand = $altCfg['SWITCH_PING_CUSTOM_SCRIPT'] . ' ' . $eachswitch['ip'];
  901. $customScriptRun = shell_exec($customTestCommand);
  902. $customScriptRun = trim($customScriptRun);
  903. if ($customScriptRun != '1') {
  904. $deadswitches[$eachswitch['ip']] = $eachswitch['location'];
  905. if (!isset($deathTime[$eachswitch['ip']])) {
  906. zb_SwitchDeathTimeSet($eachswitch['ip']);
  907. }
  908. } else {
  909. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  910. }
  911. }
  912. } else {
  913. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  914. }
  915. } else {
  916. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  917. }
  918. } else {
  919. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  920. }
  921. } else {
  922. //fast ping query
  923. if ($fastPing->isDead($eachswitch['ip'])) {
  924. zb_SwitchDeathTimeSet($eachswitch['ip']);
  925. $deadswitches[$eachswitch['ip']] = $eachswitch['location'];
  926. if (!isset($deathTime[$eachswitch['ip']])) {
  927. zb_SwitchDeathTimeSet($eachswitch['ip']);
  928. }
  929. } else {
  930. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  931. }
  932. }
  933. }
  934. }
  935. }
  936. $newdata = serialize($deadswitches);
  937. zb_StorageSet('SWDEAD', $newdata);
  938. $switchRepingProcess->stop();
  939. }
  940. return ($deadswitches);
  941. }
  942. /**
  943. * Performs switches alive state check
  944. *
  945. * @return void
  946. */
  947. function zb_SwitchesForcePing() {
  948. global $ubillingConfig;
  949. $alterconf = $ubillingConfig->getAlter();
  950. $allswitches = zb_SwitchesGetAll();
  951. $modelnames = zb_SwitchModelsGetAllTag();
  952. $currenttime = time();
  953. $reping_timeout = $alterconf['SW_PINGTIMEOUT'];
  954. $deathTime = zb_SwitchesGetAllDeathTime();
  955. $fastPingFlag = $ubillingConfig->getAlterParam('FASTPING_ENABLED');
  956. //counters
  957. $countTotal = 0;
  958. $countAlive = 0;
  959. $countDead = 0;
  960. $countNp = 0;
  961. $countOnMap = 0;
  962. $countSwpoll = 0;
  963. $countMtsigmon = 0;
  964. $countOlt = 0;
  965. $countLinked = 0;
  966. //non realtime switches pinging
  967. $last_pingtime = zb_StorageGet('SWPINGTIME');
  968. if (!$last_pingtime) {
  969. zb_SwitchesRepingAll();
  970. zb_StorageSet('SWPINGTIME', $currenttime);
  971. $last_pingtime = $currenttime;
  972. } else {
  973. if ($currenttime > ($last_pingtime + ($reping_timeout * 60))) {
  974. // normal timeout reping sub here
  975. zb_SwitchesRepingAll();
  976. zb_StorageSet('SWPINGTIME', $currenttime);
  977. }
  978. }
  979. //force total reping and update cache
  980. if (wf_CheckGet(array('forcereping'))) {
  981. if ($fastPingFlag) {
  982. $fastPing = new FastPing();
  983. $fastPing->repingDevices();
  984. }
  985. zb_SwitchesRepingAll();
  986. zb_StorageSet('SWPINGTIME', $currenttime);
  987. if (wf_CheckGet(array('ajaxping'))) {
  988. $dead_raw = zb_StorageGet('SWDEAD');
  989. $deathTime = zb_SwitchesGetAllDeathTime();
  990. $deadarr = array();
  991. $ajaxResult = '';
  992. if ($dead_raw) {
  993. $deadarr = unserialize($dead_raw);
  994. if (!empty($deadarr)) {
  995. //there is some dead switches
  996. $deadcount = sizeof($deadarr);
  997. if ($alterconf['SWYMAP_ENABLED']) {
  998. //getting geodata
  999. $switchesGeo = zb_SwitchesGetAllGeo();
  1000. }
  1001. //ajax container
  1002. $ajaxResult .= wf_tag('div', false, '', 'id="switchping"');
  1003. foreach ($deadarr as $ip => $switch) {
  1004. if ($alterconf['SWYMAP_ENABLED']) {
  1005. if (isset($switchesGeo[$ip])) {
  1006. if (!empty($switchesGeo[$ip])) {
  1007. $devicefind = wf_Link('?module=switchmap&finddevice=' . $switchesGeo[$ip], wf_img('skins/icon_search_small.gif', __('Find on map'))) . ' ';
  1008. } else {
  1009. $devicefind = '';
  1010. }
  1011. } else {
  1012. $devicefind = '';
  1013. }
  1014. } else {
  1015. $devicefind = '';
  1016. }
  1017. //check morgue records for death time
  1018. if (isset($deathTime[$ip])) {
  1019. $deathClock = wf_img('skins/clock.png', __('Switch dead since') . ' ' . $deathTime[$ip]) . ' ';
  1020. } else {
  1021. $deathClock = '';
  1022. }
  1023. //switch location link
  1024. $switchLocator = wf_Link('?module=switches&gotoswitchbyip=' . $ip, web_edit_icon(__('Go to switch')));
  1025. //add switch as dead
  1026. $ajaxResult .= $devicefind . ' ' . $switchLocator . ' ' . $deathClock . $ip . ' - ' . $switch . '<br>';
  1027. }
  1028. } else {
  1029. $ajaxResult = __('Switches are okay, everything is fine - I guarantee');
  1030. }
  1031. }
  1032. $ajaxResult .= wf_delimiter() . __('Cache state at time') . ': ' . date("H:i:s");
  1033. print($ajaxResult);
  1034. //darkvoid update
  1035. $notifyArea = new DarkVoid();
  1036. $notifyArea->flushCache();
  1037. die();
  1038. } else {
  1039. rcms_redirect('?module=switches');
  1040. }
  1041. }
  1042. }
  1043. /**
  1044. * Returns list of all available switches devices with its controls. Also catches ajaxping and forcereping events.
  1045. *
  1046. * @return string
  1047. */
  1048. function web_SwitchesShow() {
  1049. global $ubillingConfig;
  1050. $alterconf = $ubillingConfig->getAlter();
  1051. $allswitches = zb_SwitchesGetAll();
  1052. $modelnames = zb_SwitchModelsGetAllTag();
  1053. $currenttime = time();
  1054. $reping_timeout = $alterconf['SW_PINGTIMEOUT'];
  1055. $deathTime = zb_SwitchesGetAllDeathTime();
  1056. //counters
  1057. $countTotal = 0;
  1058. $countAlive = 0;
  1059. $countDead = 0;
  1060. $countNp = 0;
  1061. $countOnMap = 0;
  1062. $countSwpoll = 0;
  1063. $countMtsigmon = 0;
  1064. $countOlt = 0;
  1065. $countLinked = 0;
  1066. //non realtime switches pinging
  1067. $last_pingtime = zb_StorageGet('SWPINGTIME');
  1068. if (!$last_pingtime) {
  1069. zb_SwitchesRepingAll();
  1070. zb_StorageSet('SWPINGTIME', $currenttime);
  1071. $last_pingtime = $currenttime;
  1072. } else {
  1073. if ($currenttime > ($last_pingtime + ($reping_timeout * 60))) {
  1074. // normal timeout reping sub here
  1075. zb_SwitchesRepingAll();
  1076. zb_StorageSet('SWPINGTIME', $currenttime);
  1077. }
  1078. }
  1079. //force total reping and update cache
  1080. if (wf_CheckGet(array('forcereping'))) {
  1081. zb_SwitchesRepingAll();
  1082. zb_StorageSet('SWPINGTIME', $currenttime);
  1083. if (wf_CheckGet(array('ajaxping'))) {
  1084. $dead_raw = zb_StorageGet('SWDEAD');
  1085. $deathTime = zb_SwitchesGetAllDeathTime();
  1086. $deadarr = array();
  1087. $ajaxResult = '';
  1088. if ($dead_raw) {
  1089. $deadarr = unserialize($dead_raw);
  1090. if (!empty($deadarr)) {
  1091. //there is some dead switches
  1092. $deadcount = sizeof($deadarr);
  1093. if ($alterconf['SWYMAP_ENABLED']) {
  1094. //getting geodata
  1095. $switchesGeo = zb_SwitchesGetAllGeo();
  1096. }
  1097. //ajax container
  1098. $ajaxResult .= wf_tag('div', false, '', 'id="switchping"');
  1099. foreach ($deadarr as $ip => $switch) {
  1100. if ($alterconf['SWYMAP_ENABLED']) {
  1101. if (isset($switchesGeo[$ip])) {
  1102. if (!empty($switchesGeo[$ip])) {
  1103. $devicefind = wf_Link('?module=switchmap&finddevice=' . $switchesGeo[$ip], wf_img('skins/icon_search_small.gif', __('Find on map'))) . ' ';
  1104. } else {
  1105. $devicefind = '';
  1106. }
  1107. } else {
  1108. $devicefind = '';
  1109. }
  1110. } else {
  1111. $devicefind = '';
  1112. }
  1113. //check morgue records for death time
  1114. if (isset($deathTime[$ip])) {
  1115. $deathClock = wf_img('skins/clock.png', __('Switch dead since') . ' ' . $deathTime[$ip]) . ' ';
  1116. } else {
  1117. $deathClock = '';
  1118. }
  1119. //switch location link
  1120. $switchLocator = wf_Link('?module=switches&gotoswitchbyip=' . $ip, web_edit_icon(__('Go to switch')));
  1121. //add switch as dead
  1122. $ajaxResult .= $devicefind . ' ' . $switchLocator . ' ' . $deathClock . $ip . ' - ' . $switch . '<br>';
  1123. }
  1124. } else {
  1125. $ajaxResult = __('Switches are okay, everything is fine - I guarantee');
  1126. }
  1127. }
  1128. $ajaxResult .= wf_delimiter() . __('Cache state at time') . ': ' . date("H:i:s");
  1129. print($ajaxResult);
  1130. //darkvoid update
  1131. $notifyArea = new DarkVoid();
  1132. $notifyArea->flushCache();
  1133. die();
  1134. }
  1135. }
  1136. //load dead switches cache
  1137. $dead_switches_raw = zb_StorageGet('SWDEAD');
  1138. if (!$dead_switches_raw) {
  1139. $dead_switches = array();
  1140. } else {
  1141. $dead_switches = unserialize($dead_switches_raw);
  1142. }
  1143. //create new ADcomments object if enabled
  1144. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1145. $adcomments = new ADcomments('SWITCHES');
  1146. }
  1147. $tablecells = wf_TableCell(__('ID'));
  1148. $tablecells .= wf_TableCell(__('IP'));
  1149. $tablecells .= wf_TableCell(__('Location'));
  1150. $tablecells .= wf_TableCell(__('Active'));
  1151. $tablecells .= wf_TableCell(__('Model'));
  1152. $tablecells .= wf_TableCell(__('SNMP community'));
  1153. $tablecells .= wf_TableCell(__('Geo location'));
  1154. $tablecells .= wf_TableCell(__('Description'));
  1155. $tablecells .= wf_TableCell(__('Actions'));
  1156. $tablerows = wf_TableRow($tablecells, 'row1');
  1157. $lighter = 'onmouseover="this.className = \'row2\';" onmouseout="this.className = \'row3\';" ';
  1158. if (!empty($allswitches)) {
  1159. foreach ($allswitches as $io => $eachswitch) {
  1160. if (isset($dead_switches[$eachswitch['ip']])) {
  1161. if (isset($deathTime[$eachswitch['ip']])) {
  1162. $obituary = __('Switch dead since') . ' ' . $deathTime[$eachswitch['ip']];
  1163. } else {
  1164. $obituary = '';
  1165. }
  1166. $aliveled = web_red_led($obituary);
  1167. $aliveflag = '0';
  1168. $countDead++;
  1169. } else {
  1170. if (strpos($eachswitch['desc'], 'NP') === false) {
  1171. $aliveled = web_green_led();
  1172. $aliveflag = '1';
  1173. $countAlive++;
  1174. } else {
  1175. $aliveled = web_yellow_led();
  1176. $aliveflag = '2';
  1177. $countNp++;
  1178. }
  1179. }
  1180. $tablecells = wf_TableCell($eachswitch['id']);
  1181. $tablecells .= wf_TableCell($eachswitch['ip'], '', '', 'sorttable_customkey="' . ip2int($eachswitch['ip']) . '"');
  1182. $tablecells .= wf_TableCell($eachswitch['location']);
  1183. $tablecells .= wf_TableCell($aliveled, '', '', 'sorttable_customkey="' . $aliveflag . '"');
  1184. $tablecells .= wf_TableCell(@$modelnames[$eachswitch['modelid']]);
  1185. $tablecells .= wf_TableCell($eachswitch['snmp']);
  1186. $tablecells .= wf_TableCell($eachswitch['geo']);
  1187. $tablecells .= wf_TableCell($eachswitch['desc']);
  1188. $switchcontrols = '';
  1189. if (cfr('SWITCHESEDIT')) {
  1190. $switchcontrols .= wf_Link('?module=switches&edit=' . $eachswitch['id'], web_edit_icon());
  1191. }
  1192. if (cfr('SWITCHPOLL')) {
  1193. if ((!empty($eachswitch['snmp'])) and (ispos($eachswitch['desc'], 'SWPOLL'))) {
  1194. $switchcontrols .= '&nbsp;' . wf_Link('?module=switchpoller&switchid=' . $eachswitch['id'], wf_img('skins/snmp.png', __('SNMP query')));
  1195. $countSwpoll++;
  1196. }
  1197. }
  1198. if ($alterconf['SWYMAP_ENABLED']) {
  1199. if (!empty($eachswitch['geo'])) {
  1200. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'], wf_img('skins/icon_search_small.gif', __('Find on map')));
  1201. $countOnMap++;
  1202. }
  1203. if (!empty($eachswitch['parentid'])) {
  1204. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'] . '&showuplinks=true&traceid=' . $eachswitch['id'], wf_img('skins/ymaps/uplinks.png', __('Uplink switch')));
  1205. $countLinked++;
  1206. }
  1207. }
  1208. if (ispos($eachswitch['desc'], 'MTSIGMON')) {
  1209. $countMtsigmon++;
  1210. }
  1211. if (ispos($eachswitch['desc'], 'OLT')) {
  1212. $countOlt++;
  1213. }
  1214. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1215. $switchcontrols .= $adcomments->getCommentsIndicator($eachswitch['id']);
  1216. }
  1217. if (isset($alterconf['SW_WEBNAV'])) {
  1218. if ($alterconf['SW_WEBNAV']) {
  1219. $switchcontrols .= ' ' . wf_tag('a', false, '', 'href="http://' . $eachswitch['ip'] . '" target="_BLANK"') . wf_img('skins/ymaps/globe.png', __('Go to the web interface')) . wf_tag('a', true);
  1220. }
  1221. }
  1222. $tablecells .= wf_TableCell($switchcontrols);
  1223. $tablerows .= wf_tag('tr', false, 'row3', $lighter);
  1224. $tablerows .= $tablecells;
  1225. $tablerows .= wf_tag('tr', true);
  1226. $countTotal++;
  1227. }
  1228. }
  1229. $result = wf_TableBody($tablerows, '100%', '0', 'sortable');
  1230. $result .= wf_img('skins/icon_active.gif') . ' ' . __('Alive switches') . ' - ' . ($countAlive + $countNp) . ' (' . $countAlive . '+' . $countNp . ')' . wf_tag('br');
  1231. $result .= wf_img('skins/icon_inactive.gif') . ' ' . __('Dead switches') . ' - ' . $countDead . wf_tag('br');
  1232. $result .= wf_img('skins/yellow_led.png') . ' ' . __('NP switches') . ' - ' . $countNp . wf_tag('br');
  1233. $result .= wf_img('skins/snmp.png') . ' ' . __('SWPOLL query') . ' - ' . $countSwpoll . wf_tag('br');
  1234. $result .= wf_img('skins/wifi.png') . ' ' . __('MTSIGMON devices') . ' - ' . $countMtsigmon . wf_tag('br');
  1235. $result .= wf_img('skins/pon_icon.gif') . ' ' . __('OLT devices') . ' - ' . $countOlt . wf_tag('br');
  1236. $result .= wf_img('skins/icon_search_small.gif') . ' ' . __('Placed on map') . ' - ' . $countOnMap . wf_tag('br');
  1237. $result .= wf_img('skins/ymaps/uplinks.png') . ' ' . __('Have uplinks') . ' - ' . $countLinked . wf_tag('br');
  1238. $result .= wf_tag('br') . wf_tag('b') . __('Total') . ': ' . $countTotal . wf_tag('b', true) . wf_tag('br');
  1239. return ($result);
  1240. }
  1241. /**
  1242. * Returns JQDT switches list container
  1243. *
  1244. * @return string
  1245. */
  1246. function web_SwitchesRenderList() {
  1247. $result = '';
  1248. global $ubillingConfig;
  1249. $alterconf = $ubillingConfig->getAlter();
  1250. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1251. $switchesExtended = $ubillingConfig->getAlterParam('SWITCHES_EXTENDED');
  1252. $switchesCompactFlag = $ubillingConfig->getAlterParam('SWITCHES_LIST_COMPACT');
  1253. $summaryCache = 'exports/switchcounterssummary.dat';
  1254. $columns = array('ID', 'IP');
  1255. if ($alterconf['SWITCHES_SNMP_MAC_EXORCISM']) {
  1256. $columns[] = ('MAC');
  1257. }
  1258. if ($switchesExtended) {
  1259. $columns[] = __('Uplink');
  1260. //separate port column
  1261. if ($switchesExtended == 3) {
  1262. $columns[] = __('Port');
  1263. }
  1264. }
  1265. array_push($columns, 'Location', 'Active', 'Model');
  1266. if (!$switchesCompactFlag) {
  1267. array_push($columns, 'SNMP community', 'Geo location');
  1268. }
  1269. $columns[] = 'Description';
  1270. if ($swGroupsEnabled) {
  1271. $columns[] = 'Group';
  1272. }
  1273. $columns[] = 'Actions';
  1274. $opts = '"order": [[ 0, "desc" ]]';
  1275. $result = wf_JqDtLoader($columns, '?module=switches&ajaxlist=true', false, __('Switch'), 100, $opts);
  1276. if (file_exists($summaryCache)) {
  1277. $result .= file_get_contents($summaryCache);
  1278. }
  1279. return ($result);
  1280. }
  1281. /**
  1282. * Renders ajax switches list data
  1283. *
  1284. * @return string
  1285. */
  1286. function zb_SwitchesRenderAjaxList() {
  1287. $result = '';
  1288. global $ubillingConfig;
  1289. $alterconf = $ubillingConfig->getAlter();
  1290. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1291. $switchesExtended = $ubillingConfig->getAlterParam('SWITCHES_EXTENDED');
  1292. $switchesCompactFlag = $ubillingConfig->getAlterParam('SWITCHES_LIST_COMPACT');
  1293. $allswitchgroups = '';
  1294. if ($swGroupsEnabled) {
  1295. $switchGroups = new SwitchGroups();
  1296. $allswitchgroups = $switchGroups->getSwitchesIdsWithGroupsData();
  1297. }
  1298. if ($switchesExtended) {
  1299. $switchesUplinks = new SwitchUplinks();
  1300. $switchesUplinks->loadAllUplinksData();
  1301. }
  1302. $allswitches = zb_SwitchesGetAll();
  1303. $modelnames = zb_SwitchModelsGetAllTag();
  1304. $deathTime = zb_SwitchesGetAllDeathTime();
  1305. $summaryCache = 'exports/switchcounterssummary.dat';
  1306. $jsonAAData = array();
  1307. //counters
  1308. $countTotal = 0;
  1309. $countAlive = 0;
  1310. $countDead = 0;
  1311. $countNp = 0;
  1312. $countOnMap = 0;
  1313. $countSwpoll = 0;
  1314. $countMtsigmon = 0;
  1315. $countAP = 0;
  1316. $countOlt = 0;
  1317. $countLinked = 0;
  1318. //load dead switches cache
  1319. $dead_switches_raw = zb_StorageGet('SWDEAD');
  1320. if (!$dead_switches_raw) {
  1321. $dead_switches = array();
  1322. } else {
  1323. $dead_switches = unserialize($dead_switches_raw);
  1324. }
  1325. //create new ADcomments object if enabled
  1326. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1327. $adcomments = new ADcomments('SWITCHES');
  1328. }
  1329. if (!empty($allswitches)) {
  1330. foreach ($allswitches as $io => $eachswitch) {
  1331. $jsonItem = array();
  1332. if (isset($dead_switches[$eachswitch['ip']])) {
  1333. if (isset($deathTime[$eachswitch['ip']])) {
  1334. $obituary = __('Switch dead since') . ' ' . $deathTime[$eachswitch['ip']];
  1335. } else {
  1336. $obituary = '';
  1337. }
  1338. $aliveled = web_red_led($obituary) . ' ' . __('No');
  1339. $aliveflag = '0';
  1340. $countDead++;
  1341. } else {
  1342. if (strpos($eachswitch['desc'], 'NP') === false) {
  1343. $aliveled = web_green_led() . ' ' . __('Yes');
  1344. $aliveflag = '1';
  1345. $countAlive++;
  1346. } else {
  1347. $aliveled = web_yellow_led() . ' ' . __('NP');
  1348. $aliveflag = '2';
  1349. $countNp++;
  1350. }
  1351. }
  1352. $jsonItem[] = $eachswitch['id'];
  1353. $jsonItem[] = $eachswitch['ip'];
  1354. if ($alterconf['SWITCHES_SNMP_MAC_EXORCISM']) {
  1355. $deviceMac = '';
  1356. $deviceMacCache = 'exports/' . $eachswitch['ip'] . '_MAC';
  1357. if (file_exists($deviceMacCache)) {
  1358. $deviceMacData = file_get_contents($deviceMacCache);
  1359. if (check_mac_format($deviceMacData)) {
  1360. if ($alterconf['SWITCHES_EXTENDED'] and $deviceMacData != $eachswitch['swid']) {
  1361. $deviceMac = $deviceMacData . ' ' . wf_img('skins/createtask.gif', __('MAC mismatch')) . ' ' . __('Oh no');
  1362. } else {
  1363. $deviceMac = $deviceMacData;
  1364. }
  1365. }
  1366. }
  1367. $jsonItem[] = $deviceMac;
  1368. }
  1369. if ($switchesExtended) {
  1370. $includePortFlag = ($switchesExtended == 2) ? true : false;
  1371. $jsonItem[] = $switchesUplinks->getUplinkTinyDesc($eachswitch['id'], $includePortFlag);
  1372. //port as separate column?
  1373. if ($switchesExtended == 3) {
  1374. $jsonItem[] = $switchesUplinks->getUplinkPort($eachswitch['id']);
  1375. }
  1376. }
  1377. $jsonItem[] = $eachswitch['location'];
  1378. $jsonItem[] = $aliveled;
  1379. $jsonItem[] = @$modelnames[$eachswitch['modelid']];
  1380. if (!$switchesCompactFlag) {
  1381. $jsonItem[] = $eachswitch['snmp'];
  1382. $jsonItem[] = $eachswitch['geo'];
  1383. }
  1384. $jsonItem[] = $eachswitch['desc'];
  1385. if ($swGroupsEnabled) {
  1386. $jsonItem[] = (isset($allswitchgroups[$eachswitch['id']])) ? $allswitchgroups[$eachswitch['id']]['groupname'] : '';
  1387. }
  1388. $switchcontrols = '';
  1389. if (cfr('SWITCHES')) {
  1390. $switchcontrols .= wf_Link('?module=switches&edit=' . $eachswitch['id'], web_edit_icon());
  1391. }
  1392. if (cfr('SWITCHPOLL')) {
  1393. if ((!empty($eachswitch['snmp'])) and (ispos($eachswitch['desc'], 'SWPOLL'))) {
  1394. $switchcontrols .= '&nbsp;' . wf_Link('?module=switchpoller&switchid=' . $eachswitch['id'], wf_img('skins/snmp.png', __('SNMP query')));
  1395. $countSwpoll++;
  1396. }
  1397. }
  1398. if ($alterconf['SWYMAP_ENABLED']) {
  1399. if (!empty($eachswitch['geo'])) {
  1400. if (cfr('SWITCHMAP')) {
  1401. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'], wf_img('skins/icon_search_small.gif', __('Find on map')));
  1402. }
  1403. $countOnMap++;
  1404. }
  1405. if (!empty($eachswitch['parentid'])) {
  1406. if (cfr('SWITCHMAP')) {
  1407. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'] . '&showuplinks=true&traceid=' . $eachswitch['id'], wf_img('skins/ymaps/uplinks.png', __('Uplink switch')));
  1408. }
  1409. $countLinked++;
  1410. }
  1411. if ((empty($eachswitch['geo'])) and (!ispos($eachswitch['desc'], 'NP'))) {
  1412. if ((cfr('SWITCHESEDIT')) and (cfr('SWITCHMAP'))) {
  1413. $switchcontrols .= wf_Link('?module=switchmap&locfinder=true&placesw=' . $eachswitch['id'], wf_img('skins/ymaps/target.png', __('Place on map')), false, '');
  1414. }
  1415. }
  1416. }
  1417. if (ispos($eachswitch['desc'], 'MTSIGMON')) {
  1418. $countMtsigmon++;
  1419. }
  1420. if (ispos($eachswitch['desc'], 'OLT')) {
  1421. $countOlt++;
  1422. }
  1423. if (ispos($eachswitch['desc'], 'AP')) {
  1424. $countAP++;
  1425. }
  1426. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1427. $switchcontrols .= $adcomments->getCommentsIndicator($eachswitch['id']);
  1428. }
  1429. if (isset($alterconf['SW_WEBNAV'])) {
  1430. if ($alterconf['SW_WEBNAV']) {
  1431. $switchcontrols .= ' ' . wf_tag('a', false, '', 'href="http://' . $eachswitch['ip'] . '" target="_BLANK"') . wf_img('skins/ymaps/globe.png', __('Go to the web interface')) . wf_tag('a', true);
  1432. }
  1433. }
  1434. if (@$alterconf['SW_CASH_ENABLED']) {
  1435. if (ispos($eachswitch['desc'], 'SWCASH')) {
  1436. $swCashUrl = SwitchCash::URL_ME . '&' . SwitchCash::ROUTE_EDIT . '=' . $eachswitch['id'];
  1437. $switchcontrols .= wf_Link($swCashUrl, wf_img('skins/ukv/dollar.png', __('Financial data')));
  1438. }
  1439. }
  1440. $jsonItem[] = $switchcontrols;
  1441. $countTotal++;
  1442. $jsonAAData[] = $jsonItem;
  1443. }
  1444. }
  1445. $countersSummary = wf_tag('br');
  1446. $countersSummary .= wf_img('skins/icon_active.gif') . ' ' . __('Alive switches') . ' - ' . ($countAlive + $countNp) . ' (' . $countAlive . '+' . $countNp . ')' . wf_tag('br');
  1447. $countersSummary .= wf_img('skins/icon_inactive.gif') . ' ' . __('Dead switches') . ' - ' . $countDead . wf_tag('br');
  1448. $countersSummary .= wf_img('skins/yellow_led.png') . ' ' . __('NP switches') . ' - ' . $countNp . wf_tag('br');
  1449. $countersSummary .= wf_img('skins/snmp.png') . ' ' . __('SWPOLL query') . ' - ' . $countSwpoll . wf_tag('br');
  1450. $countersSummary .= wf_img('skins/wifi.png') . ' ' . __('MTSIGMON devices') . ' - ' . $countMtsigmon . wf_tag('br');
  1451. $countersSummary .= wf_img('skins/pon_icon.gif') . ' ' . __('OLT devices') . ' - ' . $countOlt . wf_tag('br');
  1452. $countersSummary .= wf_img('skins/wifi.png') . ' ' . __('AP devices') . ' - ' . $countAP . wf_tag('br');
  1453. $countersSummary .= wf_img('skins/icon_search_small.gif') . ' ' . __('Placed on map') . ' - ' . $countOnMap . wf_tag('br');
  1454. $countersSummary .= wf_img('skins/ymaps/uplinks.png') . ' ' . __('Have uplinks') . ' - ' . $countLinked . wf_tag('br');
  1455. $countersSummary .= wf_tag('br') . wf_tag('b') . __('Total') . ': ' . $countTotal . wf_tag('b', true) . wf_tag('br');
  1456. file_put_contents($summaryCache, $countersSummary);
  1457. $jsonList = array("aaData" => $jsonAAData);
  1458. return (json_encode($jsonList));
  1459. }
  1460. /**
  1461. * Updates existing switch data
  1462. *
  1463. * @global object $ubillingConfig
  1464. *
  1465. * @return void
  1466. */
  1467. function ub_SwitchSave($switchid) {
  1468. global $ubillingConfig;
  1469. $altCfg = $ubillingConfig->getAlter();
  1470. $switchid = ubRouting::filters($switchid, 'int');
  1471. // some non-parameterized shit here, PFFFFF
  1472. simple_update_field('switches', 'modelid', ubRouting::post('editmodel'), "WHERE `id`='" . $switchid . "'");
  1473. simple_update_field('switches', 'ip', ubRouting::post('editip'), "WHERE `id`='" . $switchid . "'");
  1474. simple_update_field('switches', 'location', ub_SanitizeData(ubRouting::post('editlocation'), false), "WHERE `id`='" . $switchid . "'");
  1475. simple_update_field('switches', 'desc', ub_SanitizeData(ubRouting::post('editdesc'), false), "WHERE `id`='" . $switchid . "'");
  1476. simple_update_field('switches', 'snmp', ubRouting::post('editsnmp'), "WHERE `id`='" . $switchid . "'");
  1477. simple_update_field('switches', 'snmpwrite', ubRouting::post('editsnmpwrite'), "WHERE `id`='" . $switchid . "'");
  1478. if ($altCfg['SWITCHES_EXTENDED']) {
  1479. simple_update_field('switches', 'swid', ubRouting::post('editswid'), "WHERE `id`='" . $switchid . "'");
  1480. }
  1481. simple_update_field('switches', 'geo', preg_replace('/[^-?0-9\.,]/i', '', ubRouting::post('editgeo')), "WHERE `id`='" . $switchid . "'");
  1482. if (ubRouting::post('editparentid') != $switchid) {
  1483. //checks for preventing loops
  1484. $alllinks = array();
  1485. $tmpSwitches = zb_SwitchesGetAll();
  1486. if (!empty($tmpSwitches)) {
  1487. //transform array to id=>switchdata
  1488. foreach ($tmpSwitches as $io => $each) {
  1489. $allswitches[$each['id']] = $each;
  1490. }
  1491. //making id=>parentid array
  1492. foreach ($tmpSwitches as $io => $each) {
  1493. $alllinks[$each['id']] = $each['parentid'];
  1494. }
  1495. }
  1496. if (sm_CheckLoop($alllinks, $switchid, ubRouting::post('editparentid'))) {
  1497. simple_update_field('switches', 'parentid', ubRouting::post('editparentid'), "WHERE `id`='" . $switchid . "'");
  1498. }
  1499. }
  1500. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1501. if ($swGroupsEnabled) {
  1502. $switchGroups = new SwitchGroups();
  1503. $switchAlreadyInGroup = $switchGroups->getSwitchGroupBySwitchId($switchid);
  1504. if (empty($switchAlreadyInGroup) and ubRouting::post('editswgroup')) {
  1505. $query = "INSERT INTO `switch_groups_relations` (`switch_id`, `sw_group_id`) VALUES (" . $switchid . ", " . ubRouting::post('editswgroup') . ")";
  1506. nr_query($query);
  1507. } elseif (ubRouting::post('editswgroup')) {
  1508. if (ubRouting::post('editswgroup') == '0') {
  1509. $switchGroups->removeSwitchFromGroup($switchid);
  1510. } else {
  1511. simple_update_field('switch_groups_relations', 'sw_group_id', ubRouting::post('editswgroup'), "WHERE `switch_id`='" . $switchid . "'");
  1512. }
  1513. }
  1514. }
  1515. log_register('SWITCH CHANGE [' . $switchid . ']' . ' IP ' . ubRouting::post('editip') . " LOC `" . ubRouting::post('editlocation') . "`");
  1516. }
  1517. /**
  1518. * Creates new switch device in database
  1519. *
  1520. * @param int $modelid
  1521. * @param string $ip
  1522. * @param string $desc
  1523. * @param string $location
  1524. * @param string $snmp
  1525. * @param string $swid
  1526. * @param string $geo
  1527. * @param int $parentid
  1528. */
  1529. function ub_SwitchAdd($modelid, $ip, $desc, $location, $snmp, $swid, $geo, $parentid = '', $snmpwrite = '', $switchgroupid = '') {
  1530. $modelid = ubRouting::filters($modelid, 'int');
  1531. $ip = ubRouting::filters($ip, 'mres');
  1532. $desc = ub_SanitizeData($desc);
  1533. $location = ub_SanitizeData($location);
  1534. $snmp = ubRouting::filters($snmp, 'mres');
  1535. $snmpwrite = ubRouting::filters($snmpwrite, 'mres');
  1536. $swid = ubRouting::filters($swid, 'mres');
  1537. $parentid = ubRouting::filters($parentid, 'int');
  1538. if (!empty($parentid)) {
  1539. $parentid = "'" . $parentid . "'";
  1540. } else {
  1541. $parentid = 'NULL';
  1542. }
  1543. $query = "INSERT INTO `switches` (`id` ,`modelid` ,`ip` ,`desc` ,`location` ,`snmp`,`swid`,`geo`,`parentid`,`snmpwrite`) "
  1544. . "VALUES ('', '" . $modelid . "', '" . $ip . "', '" . $desc . "', '" . $location . "', '" . $snmp . "', '" . $swid . "','" . $geo . "', " . $parentid . ",'" . $snmpwrite . "' );";
  1545. nr_query($query);
  1546. $lastid = simple_get_lastid('switches');
  1547. if (!empty($switchgroupid)) {
  1548. $query = "INSERT INTO `switch_groups_relations` (`switch_id`, `sw_group_id`) VALUES (" . $lastid . ", " . $switchgroupid . ")";
  1549. nr_query($query);
  1550. }
  1551. log_register('SWITCH ADD [' . $lastid . '] IP `' . $ip . '` ON LOC `' . $location . '`');
  1552. show_window(__('Add switch'), __('Was added new switch') . ' ' . $ip . ' ' . $location);
  1553. }
  1554. /**
  1555. * Checks is switch parent for someone?
  1556. *
  1557. * @param int $switchid
  1558. * @return bool
  1559. */
  1560. function ub_SwitchIsParent($switchid) {
  1561. $switchid = vf($switchid, 3);
  1562. $result = false;
  1563. $query = "SELECT `id` from `switches` WHERE `parentid`='" . $switchid . "';";
  1564. $raw = simple_query($query);
  1565. if (!empty($raw)) {
  1566. $result = true;
  1567. }
  1568. return ($result);
  1569. }
  1570. /**
  1571. * Flushes child switches for some switch
  1572. *
  1573. * @param int $switchid
  1574. */
  1575. function ub_SwitchFlushChilds($switchid) {
  1576. $switchid = vf($switchid, 3);
  1577. $query = "UPDATE `switches` SET `parentid`=NULL WHERE `parentid`='" . $switchid . "';";
  1578. nr_query($query);
  1579. log_register('SWITCH FLUSH CHILDS [' . $switchid . ']');
  1580. }
  1581. /**
  1582. * Deletes switch from database by its ID
  1583. *
  1584. * @param int $switchid existing switch database ID
  1585. */
  1586. function ub_SwitchDelete($switchid) {
  1587. global $ubillingConfig;
  1588. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1589. $switchid = vf($switchid);
  1590. $switchdata = zb_SwitchGetData($switchid);
  1591. $query = "DELETE from `switches` WHERE `id`='" . $switchid . "'";
  1592. nr_query($query);
  1593. if ($swGroupsEnabled) {
  1594. $switchGroups = new SwitchGroups();
  1595. $switchGroups->removeSwitchFromGroup($switchid);
  1596. }
  1597. $switchesExtended = $ubillingConfig->getAlterParam('SWITCHES_EXTENDED');
  1598. if ($switchesExtended) {
  1599. $switchesUplinks = new SwitchUplinks();
  1600. $switchesUplinks->flush($switchid);
  1601. }
  1602. $swAuth = $ubillingConfig->getAlterParam('SWITCHES_AUTH_ENABLED');
  1603. if ($swAuth) {
  1604. $switchAuth = new SwitchAuth($switchid);
  1605. $switchAuth->flushAuthData($switchid);
  1606. }
  1607. $query = 'DELETE FROM `switches_qinq` WHERE `switchid` = "' . $switchid . '"';
  1608. nr_query($query);
  1609. log_register('SWITCH DELETE [' . $switchid . '] IP ' . $switchdata['ip'] . ' LOC ' . $switchdata['location']);
  1610. }
  1611. /**
  1612. * Returns dead switches json data for timemachine calendar view
  1613. *
  1614. * @return string
  1615. */
  1616. function ub_JGetSwitchDeadLog() {
  1617. $cyear = curyear();
  1618. $query = "SELECT `id`,`date`,`timestamp`,`swdead` from `switchdeadlog` WHERE `date` LIKE '" . $cyear . "-%' ORDER BY `id` ASC";
  1619. $alldead = simple_queryall($query);
  1620. $i = 1;
  1621. $logcount = sizeof($alldead);
  1622. $result = '';
  1623. if (!empty($alldead)) {
  1624. foreach ($alldead as $io => $eachdead) {
  1625. if ($i != $logcount) {
  1626. $thelast = ',';
  1627. } else {
  1628. $thelast = '';
  1629. }
  1630. $startdate = strtotime($eachdead['date']);
  1631. $startdate = date("Y, n-1, j", $startdate);
  1632. $deadData_raw = $eachdead['swdead'];
  1633. $deadData = unserialize($deadData_raw);
  1634. $deadcount = sizeof($deadData);
  1635. $result .= "
  1636. {
  1637. title: '" . date("H:i:s", $eachdead['timestamp']) . " - (" . $deadcount . ")',
  1638. start: new Date(" . $startdate . "),
  1639. end: new Date(" . $startdate . "),
  1640. className : 'undone',
  1641. url: '?module=switches&timemachine=true&snapshot=" . $eachdead['id'] . "'
  1642. }
  1643. " . $thelast;
  1644. $i++;
  1645. }
  1646. }
  1647. return ($result);
  1648. }
  1649. /**
  1650. * Renders dead switches top
  1651. *
  1652. * @return string
  1653. */
  1654. function web_DeadSwitchesTop() {
  1655. global $ubillingConfig;
  1656. $altCfg = $ubillingConfig->getAlter();
  1657. if (isset($altCfg['SWITCH_PING_INTERVAL'])) {
  1658. $repingInterval = $altCfg['SWITCH_PING_INTERVAL'] * 60;
  1659. } else {
  1660. $repingInterval = 0;
  1661. }
  1662. $topThreshold = 0;
  1663. $result = '';
  1664. $cmonth = curmonth();
  1665. $query = "SELECT `id`,`date`,`timestamp`,`swdead` from `switchdeadlog` WHERE `date` LIKE '" . $cmonth . "-%' ORDER BY `id` ASC";
  1666. $rawData = simple_queryall($query);
  1667. $topTmp = array();
  1668. $totalCount = 0;
  1669. $totaldeadTime = 0;
  1670. if (!empty($rawData)) {
  1671. foreach ($rawData as $io => $each) {
  1672. if (!empty($each['swdead'])) {
  1673. $deadData = unserialize($each['swdead']);
  1674. if (!empty($deadData)) {
  1675. foreach ($deadData as $eachDeadIp => $eachDeadName) {
  1676. if (isset($topTmp[$eachDeadIp])) {
  1677. $topTmp[$eachDeadIp]['count']++;
  1678. } else {
  1679. $topTmp[$eachDeadIp]['count'] = 1;
  1680. $topTmp[$eachDeadIp]['name'] = $eachDeadName;
  1681. }
  1682. $totalCount++;
  1683. }
  1684. }
  1685. }
  1686. }
  1687. }
  1688. if (!empty($topTmp)) {
  1689. $cells = wf_TableCell(__('IP'));
  1690. $cells .= wf_TableCell(__('Location'));
  1691. $cells .= wf_TableCell(__('Count'));
  1692. if ($repingInterval) {
  1693. $cells .= wf_TableCell(__('Time'));
  1694. }
  1695. $cells .= wf_TableCell(__('Visual'));
  1696. $rows = wf_TableRow($cells, 'row1');
  1697. foreach ($topTmp as $io => $each) {
  1698. if ($each['count'] >= $topThreshold) {
  1699. $cells = wf_TableCell($io);
  1700. $cells .= wf_TableCell($each['name']);
  1701. $cells .= wf_TableCell($each['count']);
  1702. if ($repingInterval) {
  1703. $deadTime = $each['count'] * $repingInterval;
  1704. $cells .= wf_TableCell(zb_formatTime($deadTime));
  1705. $totaldeadTime += $deadTime;
  1706. }
  1707. $cells .= wf_TableCell(web_bar($each['count'], $totalCount), '', '', 'sorttable_customkey="' . $each['count'] . '"');
  1708. $rows .= wf_TableRow($cells, 'row3');
  1709. }
  1710. }
  1711. if ($repingInterval) {
  1712. $cells = wf_TableCell(__('Total'));
  1713. $cells .= wf_TableCell('');
  1714. $cells .= wf_TableCell('');
  1715. $cells .= wf_TableCell(zb_formatTime($totaldeadTime));
  1716. $cells .= wf_TableCell('');
  1717. $rows .= wf_TableRow($cells, 'row2');
  1718. }
  1719. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  1720. }
  1721. return ($result);
  1722. }
  1723. /**
  1724. * Shows time machine snapshot by its ID
  1725. *
  1726. * @param int $snapshotid
  1727. */
  1728. function ub_SwitchesTimeMachineShowSnapshot($snapshotid) {
  1729. $snapshotid = vf($snapshotid, 3);
  1730. $query = "SELECT * from `switchdeadlog` WHERE `id`='" . $snapshotid . "'";
  1731. $deaddata = simple_query($query);
  1732. $deathTime = zb_SwitchesGetAllDeathTime();
  1733. if (!empty($deaddata)) {
  1734. $deadarr = unserialize($deaddata['swdead']);
  1735. $cells = wf_TableCell(__('Switch dead since'));
  1736. $cells .= wf_TableCell(__('IP'));
  1737. $cells .= wf_TableCell(__('Location'));
  1738. $rows = wf_TableRow($cells, 'row1');
  1739. if (!empty($deadarr)) {
  1740. foreach ($deadarr as $ip => $location) {
  1741. $cells = wf_TableCell(@$deathTime[$ip]);
  1742. $cells .= wf_TableCell($ip);
  1743. $cells .= wf_TableCell($location);
  1744. $rows .= wf_TableRow($cells, 'row3');
  1745. }
  1746. }
  1747. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  1748. show_window(__('Dead switches') . ' ' . $deaddata['date'], $result);
  1749. show_window('', wf_BackLink("?module=switches&timemachine=true"));
  1750. }
  1751. }
  1752. /**
  1753. * Flushes time machine switchdead log table
  1754. *
  1755. * @return void
  1756. */
  1757. function ub_SwitchesTimeMachineCleanup() {
  1758. $query = "TRUNCATE TABLE `switchdeadlog`;";
  1759. nr_query($query);
  1760. log_register("SWITCH TIMEMACHINE FLUSH");
  1761. }
  1762. /**
  1763. * Returns time machine search form
  1764. *
  1765. * @return string
  1766. */
  1767. function web_SwitchTimeMachineSearchForm() {
  1768. $inputs = wf_TextInput('switchdeadlogsearch', __('Location') . ', ' . __('IP'), '', false, 30);
  1769. $inputs .= wf_Submit(__('Search'));
  1770. $result = wf_Form('', 'POST', $inputs, 'glamour');
  1771. return ($result);
  1772. }
  1773. /**
  1774. * Returns
  1775. *
  1776. * @param string $switchIp
  1777. *
  1778. * @return array
  1779. */
  1780. function ub_SwitchesTimeMachineGetByIp($switchIp) {
  1781. $result = array();
  1782. $query = "SELECT * from `switchdeadlog` ORDER BY `id` DESC";
  1783. $raw = simple_queryall($query);
  1784. if (!empty($raw)) {
  1785. foreach ($raw as $io => $each) {
  1786. if (!empty($each)) {
  1787. $logData = unserialize($each['swdead']);
  1788. if (isset($logData[$switchIp])) {
  1789. $result[$each['date']] = $logData[$switchIp];
  1790. }
  1791. }
  1792. }
  1793. }
  1794. return ($result);
  1795. }
  1796. /**
  1797. * Do the search in dead switches time machine
  1798. *
  1799. * @param string $query
  1800. * @return string
  1801. */
  1802. function ub_SwitchesTimeMachineSearch($request) {
  1803. $request = strtolower_utf8($request);
  1804. $result = '';
  1805. $query = "SELECT * from `switchdeadlog` ORDER BY `id` DESC";
  1806. $raw = simple_queryall($query);
  1807. $deadcount = 0;
  1808. $tmpArr = array();
  1809. if (!empty($raw)) {
  1810. foreach ($raw as $io => $each) {
  1811. if (!empty($each)) {
  1812. $switchData = unserialize($each['swdead']);
  1813. foreach ($switchData as $switchIp => $switchLocation) {
  1814. if ((ispos(strtolower_utf8($switchIp), $request)) or (ispos(strtolower_utf8($switchLocation), $request))) {
  1815. $searchId = zb_rand_string(8);
  1816. $tmpArr[$searchId]['date'] = $each['date'];
  1817. $tmpArr[$searchId]['ip'] = $switchIp;
  1818. $tmpArr[$searchId]['location'] = $switchLocation;
  1819. }
  1820. }
  1821. }
  1822. }
  1823. }
  1824. if (!empty($tmpArr)) {
  1825. $cells = wf_TableCell(__('Date'));
  1826. $cells .= wf_TableCell(__('IP'));
  1827. $cells .= wf_TableCell(__('Location'));
  1828. $rows = wf_TableRow($cells, 'row1');
  1829. foreach ($tmpArr as $ia => $eachResult) {
  1830. $cells = wf_TableCell($eachResult['date']);
  1831. $cells .= wf_TableCell($eachResult['ip']);
  1832. $cells .= wf_TableCell($eachResult['location']);
  1833. $rows .= wf_TableRow($cells, 'row3');
  1834. $deadcount++;
  1835. }
  1836. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  1837. $result .= __('Total') . ': ' . $deadcount;
  1838. } else {
  1839. $result = __('Nothing found');
  1840. }
  1841. return ($result);
  1842. }
  1843. /**
  1844. * Returns NP switches replacement form
  1845. *
  1846. * @param int $fromSwitchId
  1847. *
  1848. * @return string
  1849. */
  1850. function zb_SwitchReplaceForm($fromSwitchId) {
  1851. $fromSwitchId = vf($fromSwitchId, 3);
  1852. $result = '';
  1853. $query = "SELECT * from `switches` WHERE `desc` LIKE '%NP%' ORDER BY `ip` DESC";
  1854. $raw = simple_queryall($query);
  1855. $paramsNp = array();
  1856. $employee = array();
  1857. $employee = ts_GetActiveEmployee();
  1858. if (!empty($raw)) {
  1859. foreach ($raw as $io => $eachNp) {
  1860. $paramsNp[$eachNp['id']] = $eachNp['ip'] . ' - ' . $eachNp['location'];
  1861. }
  1862. }
  1863. $inputs = wf_HiddenInput('switchreplace', $fromSwitchId);
  1864. $inputs .= wf_SelectorSearchable('toswtichreplace', $paramsNp, 'NP ' . __('Switch'), '', false);
  1865. $inputs .= wf_SelectorSearchable('replaceemployeeid', $employee, __('Worker'), '', false);
  1866. $inputs .= wf_Submit('Save');
  1867. $result = wf_Form('', 'POST', $inputs, 'glamour');
  1868. $result .= wf_CleanDiv();
  1869. $result .= wf_delimiter();
  1870. $result .= wf_BackLink('?module=switches&edit=' . $fromSwitchId);
  1871. return ($result);
  1872. }
  1873. /**
  1874. * Performs switch replacement in database
  1875. *
  1876. * @param int $fromId
  1877. * @param int $toId
  1878. * @param int $employeeid
  1879. *
  1880. * @return void
  1881. */
  1882. function zb_SwitchReplace($fromId, $toId, $employeeId) {
  1883. global $ubillingConfig;
  1884. $fromId = ubRouting::filters($fromId, 'int');
  1885. $toId = ubRouting::filters($toId, 'int');
  1886. $employeeId = ubRouting::filters($employeeId, 'int');
  1887. $switchesDb = new NyanORM('switches');
  1888. $allEmployees = ts_GetAllEmployee();
  1889. $fromData = zb_SwitchGetData($fromId);
  1890. $toData = zb_SwitchGetData($toId);
  1891. if (!empty($fromData)) {
  1892. //updating new switch device
  1893. $switchesDb->where('id', '=', $toId);
  1894. //copy geo coordinates to new switch
  1895. $switchesDb->data('geo', $fromData['geo']);
  1896. //setting new description and remove NP flag
  1897. $newDescriptionTo = str_replace('NP', 'm:' . @$allEmployees[$employeeId], $toData['desc']);
  1898. $switchesDb->data('desc', $newDescriptionTo);
  1899. //copy location
  1900. $switchesDb->data('location', $fromData['location']);
  1901. //copy switch parent ID
  1902. if (!empty($fromData['parentid'])) {
  1903. $switchesDb->data('parentid', $fromData['parentid']);
  1904. } else {
  1905. //or dropping if not set before
  1906. $switchesDb->data('parentid', 'NULL');
  1907. }
  1908. //saving new switch
  1909. $switchesDb->save();
  1910. //updating old switch device
  1911. $switchesDb->where('id', '=', $fromId);
  1912. // doing old switch cleanup and disabling it
  1913. $switchesDb->data('geo', ''); //not located anywhere now
  1914. $newFromLocation = __('removed from') . ': ' . $fromData['location'];
  1915. $switchesDb->data('location', $newFromLocation); //unmouned from somwhere
  1916. $newFromDesc = 'NP u:' . @$allEmployees[$employeeId];
  1917. $switchesDb->data('desc', $newFromDesc); // NP + employee name
  1918. $switchesDb->data('parentid', 'NULL'); // unmounted switch have no parents
  1919. //saving old switch device
  1920. $switchesDb->save();
  1921. //moving childs if it present
  1922. $switchesDb->where('parentid', '=', $fromId);
  1923. $switchesDb->data('parentid', $toId);
  1924. $switchesDb->save();
  1925. //update user switchportassigns
  1926. if ($ubillingConfig->getAlterParam('SWITCHPORT_IN_PROFILE')) {
  1927. if ($ubillingConfig->getAlterParam('USER_SWITCHPORT_AUTOREPLACE')) {
  1928. $switchPortAssignDb = new NyanORM('switchportassign');
  1929. $switchPortAssignDb->where('switchid', '=', $fromId);
  1930. $switchPortAssignDb->data('switchid', $toId);
  1931. $switchPortAssignDb->save();
  1932. // update qinq swithc delegation
  1933. if ($ubillingConfig->getAlterParam('QINQ_ENABLED') and $ubillingConfig->getAlterParam('QINQ_SWITCH_AUTOREPLACE')) {
  1934. $switchesQinqDb = new NyanORM('switches_qinq');
  1935. $switchesQinqDb->where('switchid', '=', $fromId);
  1936. $switchesQinqDb->data('switchid', $toId);
  1937. $switchesQinqDb->save();
  1938. }
  1939. }
  1940. }
  1941. //log this replace
  1942. log_register('SWITCH REPLACE FROM [' . $fromId . '] TO [' . $toId . '] EMPLOYEE [' . $employeeId . ']');
  1943. } else {
  1944. show_error(__('Strange exception') . ': FROM_SWITCH_EMPTY_DATA');
  1945. }
  1946. }
  1947. /**
  1948. * Trys to detect switch ID by its IP
  1949. *
  1950. * @param string $ip
  1951. * @return int
  1952. */
  1953. function zb_SwitchGetIdbyIP($ip) {
  1954. $result = '';
  1955. $ip = mysql_real_escape_string($ip);
  1956. $query = "SELECT `id`,`ip` from `switches` WHERE `ip`='" . $ip . "' LIMIT 1;";
  1957. $raw = simple_query($query);
  1958. if (!empty($raw)) {
  1959. $result = $raw['id'];
  1960. }
  1961. return ($result);
  1962. }
  1963. /**
  1964. * Returns switch profile link with some square brackets
  1965. *
  1966. * @param int $switchId
  1967. *
  1968. * @return string
  1969. */
  1970. function web_SwitchProfileLink($switchId) {
  1971. $result = ' [' . trim(wf_Link('?module=switches&edit=' . $switchId, $switchId)) . '] ';
  1972. return ($result);
  1973. }
  1974. /**
  1975. * Returns all available users switches assigns as login=>switchid,switchip,port,location,label
  1976. *
  1977. * @return array
  1978. */
  1979. function zb_SwitchesGetAssignsAll() {
  1980. $result = array();
  1981. $allSwitches = array();
  1982. $allSwitchesTmp = zb_SwitchesGetAll();
  1983. if (!empty($allSwitchesTmp)) {
  1984. foreach ($allSwitchesTmp as $io => $each) {
  1985. $allSwitches[$each['id']] = $each;
  1986. }
  1987. $switchAssigns_q = "SELECT * from `switchportassign`";
  1988. $switchAssignsTmp = simple_queryall($switchAssigns_q);
  1989. if (!empty($switchAssignsTmp)) {
  1990. foreach ($switchAssignsTmp as $io => $each) {
  1991. if (isset($allSwitches[$each['switchid']])) {
  1992. $switchData = $allSwitches[$each['switchid']];
  1993. $result[$each['login']]['switchid'] = $switchData['id'];
  1994. $result[$each['login']]['switchip'] = $switchData['ip'];
  1995. $result[$each['login']]['port'] = $each['port'];
  1996. $result[$each['login']]['location'] = $switchData['location'];
  1997. $result[$each['login']]['label'] = $switchData['ip'] . ' - ' . $switchData['location'] . ' ' . __('Port') . ' ' . $each['port'];
  1998. }
  1999. }
  2000. }
  2001. }
  2002. return ($result);
  2003. }
  2004. /**
  2005. * Returns background switch ICMP ping
  2006. *
  2007. * @global object $ubillingConfig
  2008. *
  2009. * @return void
  2010. */
  2011. function zb_SwitchBackgroundIcmpPing($ip) {
  2012. global $ubillingConfig;
  2013. $billingConf = $ubillingConfig->getBilling();
  2014. $command = $billingConf['SUDO'] . ' ' . $billingConf['PING'] . ' -i 0.01 -c 10 ' . $ip;
  2015. $icmpPingResult = shell_exec($command);
  2016. die(wf_tag('pre') . $icmpPingResult . wf_tag('pre', true));
  2017. }