api.switches.php 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  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 .= sm_MapInitQuiet($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. $editinputs .= wf_tag('br');
  557. if (cfr('SWITCHGROUPS') and $swGroupsEnabled) {
  558. $switchGroups = new SwitchGroups();
  559. $editinputs .= $switchGroups->renderSwitchGroupsSelector('editswgroup', $switchid) . wf_delimiter();
  560. }
  561. if (cfr('SWITCHESEDIT')) {
  562. $editinputs .= wf_delimiter(0);
  563. $editinputs .= wf_Submit('Save');
  564. }
  565. $mainForm .= wf_Form('', 'POST', $editinputs, 'glamour');
  566. //main interface grid
  567. if (!empty($switchdata['ip'])) {
  568. $rightContainer .= wf_AjaxLoader();
  569. $rightContainer .= wf_AjaxContainer('icmppingcontainer');
  570. }
  571. $cells = wf_TableCell($mainForm, '50%', '', 'valign="top"');
  572. $cells .= wf_TableCell($rightContainer, '', '', 'valign="top"');
  573. $rows = wf_TableRow($cells);
  574. $result .= wf_TableBody($rows, '100%', 0, '');
  575. $result .= wf_CleanDiv();
  576. $result .= wf_delimiter();
  577. $result .= wf_BackLink('?module=switches');
  578. if (cfr('SWITCHPOLL')) {
  579. $fdbCacheName = 'exports/' . $switchdata['ip'] . '_fdb';
  580. if (file_exists($fdbCacheName)) {
  581. $fdbControls = wf_Link('?module=switchpoller&fdbfor=' . $switchdata['ip'], wf_img('skins/menuicons/switchpoller.png') . ' ' . __('FDB cache'), false, 'ubButton');
  582. $fdbControls .= wf_Link('?module=fdbarchive&switchidfilter=' . $switchid, wf_img('skins/fdbarchive.png') . ' ' . __('FDB') . ' ' . __('Archive'), false, 'ubButton');
  583. $result .= wf_modalAuto(wf_img('skins/menuicons/switchpoller.png') . ' ' . __('FDB'), __('FDB'), $fdbControls, 'ubButton');
  584. }
  585. if ((!empty($switchdata['snmp'])) and (ispos($switchdata['desc'], 'SWPOLL'))) {
  586. $result .= wf_Link('?module=switchpoller&switchid=' . $switchid, wf_img('skins/snmp.png') . ' ' . __('SNMP data'), false, 'ubButton');
  587. }
  588. }
  589. if (!empty($switchdata['ip'])) {
  590. $result .= wf_AjaxLink('?module=switches&backgroundicmpping=' . $switchdata['ip'], wf_img('skins/ping_icon.png') . ' ' . __('ICMP ping'), 'icmppingcontainer', false, 'ubButton');
  591. }
  592. if (isset($altCfg['SW_WEBNAV'])) {
  593. if ($altCfg['SW_WEBNAV']) {
  594. $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) . ' ';
  595. }
  596. }
  597. if (cfr('SWITCHESEDIT')) {
  598. if (!ispos($switchdata['desc'], 'NP')) {
  599. $result .= wf_JSAlertStyled('?module=switchreplace&switchid=' . $switchid, wf_img('skins/duplicate_icon.gif') . ' ' . __('Replacement'), __('Are you serious'), 'ubButton') . ' ';
  600. }
  601. }
  602. if (cfr('SWITCHESEDIT')) {
  603. if (empty($switchdata['geo'])) {
  604. $result .= wf_Link('?module=switchmap&locfinder=true&placesw=' . $switchid, wf_img('skins/ymaps/target.png') . ' ' . __('Place on map'), false, 'ubButton');
  605. }
  606. }
  607. if (cfr('SWITCHESEDIT')) {
  608. $result .= wf_AjaxLink('?module=switchhistory&ajax=true&switchid=' . $switchid, wf_img('skins/log_icon_small.png') . ' ' . __('History'), 'icmppingcontainer', false, 'ubButton') . ' ';
  609. }
  610. if (cfr('SWCASH')) {
  611. if (ispos($switchdata['desc'], 'SWCASH')) {
  612. if (@$altCfg['SW_CASH_ENABLED']) {
  613. $result .= wf_Link('?module=swcash&switchid=' . $switchid, wf_img('skins/ukv/dollar.png') . ' ' . __('Financial data'), false, 'ubButton');
  614. }
  615. }
  616. }
  617. if (cfr('TASKMAN')) {
  618. if (!empty($switchdata['location'])) {
  619. if (!ts_isMeBranchCursed()) {
  620. $taskCreateForm = ts_TaskCreateFormUnified($switchdata['location'], '', '', '', '', '');
  621. $taskCreateModal = wf_modalAuto(wf_img('skins/createtask_16.png', __('Create task')) . ' ' . __('Task'), __('Create task'), $taskCreateForm, 'ubButton');
  622. $result .= $taskCreateModal;
  623. }
  624. }
  625. }
  626. if (cfr('REPORTSWPORT')) {
  627. if (@$altCfg['SWITCHPORT_IN_PROFILE']) {
  628. $result .= wf_Link('?module=report_switchportassign&switchid=' . $switchid, wf_img('skins/icon_user_16.gif') . ' ' . __('Switch port assign'), false, 'ubButton');
  629. }
  630. }
  631. if (cfr('SWITCHESEDIT')) {
  632. $deletionUrl = '?module=switches&switchdelete=' . $switchid;
  633. $cancelUrl = '?module=switches&edit=' . $switchid;
  634. $deletionAlert = __('Removing this may lead to irreparable results');
  635. $delDialogTitle = __('Delete') . ' ' . __('Switch') . ': ' . $switchdata['location'] . '?';
  636. $result .= wf_ConfirmDialog($deletionUrl, web_delete_icon() . ' ' . __('Delete'), $deletionAlert, 'ubButton', $cancelUrl, $delDialogTitle);
  637. }
  638. //SWPOLL proposal
  639. if (!empty($switchdata['ip'])) {
  640. if (!ispos($switchdata['desc'], 'SWPOLL') and (!ispos($switchdata['desc'], 'NP')) and (!ispos($switchdata['desc'], 'OLT'))) {
  641. //this is not OLT
  642. if (!ispos($switchdata['desc'], 'AP') and (!ispos($switchdata['desc'], 'MTSIGMON'))) {
  643. //Or some wireless access point
  644. if (!empty($switchdata['snmp'])) {
  645. //with some non empty snmp read comunity
  646. if (!empty($switchdata['modelid'])) {
  647. $allModelsSnmpTemplates = sp_SnmpGetModelTemplatesAssoc();
  648. if (isset($allModelsSnmpTemplates[$switchdata['modelid']])) {
  649. //device model have some SNMP template assigned
  650. $messages = new UbillingMessageHelper();
  651. $result .= $messages->getStyledMessage(__('It looks like this device can be polled using SNMP if you specify SWPOLL in the notes'), 'info');
  652. }
  653. }
  654. }
  655. }
  656. }
  657. }
  658. return ($result);
  659. }
  660. /**
  661. * Returns array of all available switches with its full data
  662. *
  663. * @param string $order
  664. *
  665. * @return array
  666. */
  667. function zb_SwitchesGetAll($order = '') {
  668. if (empty($order)) {
  669. $order = 'ORDER BY `id` DESC';
  670. }
  671. $query = 'SELECT * FROM `switches` ' . $order . ';';
  672. $allswitches = simple_queryall($query);
  673. return ($allswitches);
  674. }
  675. /**
  676. * Returns array of all available switches with its full data with some %mask% in description as switchId=>switchData
  677. *
  678. * @param string $mask
  679. *
  680. * @return array
  681. */
  682. function zb_SwitchesGetAllMask($mask = '') {
  683. $result = array();
  684. if (!empty($mask)) {
  685. $where = "WHERE `desc` LIKE '%" . $mask . "%'";
  686. } else {
  687. $where = '';
  688. }
  689. $query = 'SELECT * FROM `switches` ' . $where . ';';
  690. $allSwitches = simple_queryall($query);
  691. if (!empty($allSwitches)) {
  692. foreach ($allSwitches as $io => $each) {
  693. $result[$each['id']] = $each;
  694. }
  695. }
  696. return ($result);
  697. }
  698. /**
  699. * Returns array of all available switches with its full data ordered by location
  700. *
  701. * @return array
  702. */
  703. function zb_SwitchesGetAllLocationOrder() {
  704. $query = 'SELECT * FROM `switches` ORDER BY `location` ASC';
  705. $allswitches = simple_queryall($query);
  706. return ($allswitches);
  707. }
  708. /**
  709. * Return geo data in ip->geo format
  710. *
  711. * @return array
  712. */
  713. function zb_SwitchesGetAllGeo() {
  714. $query = "SELECT `ip`,`geo` from `switches`";
  715. $alldata = simple_queryall($query);
  716. $result = array();
  717. if (!empty($alldata)) {
  718. foreach ($alldata as $io => $each) {
  719. $result[$each['ip']] = $each['geo'];
  720. }
  721. }
  722. return ($result);
  723. }
  724. /**
  725. * Return geo data in id->geo format
  726. *
  727. * @return array
  728. */
  729. function zb_SwitchesGetAllGeoId() {
  730. $query = "SELECT `id`,`geo` from `switches`";
  731. $alldata = simple_queryall($query);
  732. $result = array();
  733. if (!empty($alldata)) {
  734. foreach ($alldata as $io => $each) {
  735. $result[$each['id']] = $each['geo'];
  736. }
  737. }
  738. return ($result);
  739. }
  740. /**
  741. * Returns switch data by its ID
  742. *
  743. * @param int $switchid
  744. * @return array
  745. */
  746. function zb_SwitchGetData($switchid) {
  747. $switchid = vf($switchid, 3);
  748. $query = "SELECT * FROM `switches` WHERE `id`='" . $switchid . "' ";
  749. $result = simple_query($query);
  750. return ($result);
  751. }
  752. /**
  753. * Returns switch models array in format modelid=>name
  754. *
  755. * @return array
  756. */
  757. function zb_SwitchModelsGetAllTag() {
  758. $allmodels = zb_SwitchModelsGetAll();
  759. $result = array();
  760. if (!empty($allmodels)) {
  761. foreach ($allmodels as $io => $eachmodel) {
  762. $result[$eachmodel['id']] = $eachmodel['modelname'];
  763. }
  764. }
  765. return ($result);
  766. }
  767. /**
  768. * Returns result of fast icmp ping
  769. *
  770. * @param string $ip devide IP to ping
  771. *
  772. * @return bool
  773. */
  774. function zb_PingICMP($ip) {
  775. $globconf = parse_ini_file(CONFIG_PATH . "billing.ini");
  776. $ping = $globconf['PING'];
  777. $sudo = $globconf['SUDO'];
  778. $ping_command = $sudo . ' ' . $ping . ' -i 0.01 -c 1 ' . $ip;
  779. $ping_result = shell_exec($ping_command);
  780. if (strpos($ping_result, 'ttl')) {
  781. return (true);
  782. } else {
  783. return (false);
  784. }
  785. }
  786. /**
  787. * Returns result of fast ICMP ping with ability to set timeout in seconds(floating values allowed)
  788. *
  789. * @param string $ip
  790. * @param int $timeout
  791. *
  792. * @return bool
  793. */
  794. function zb_PingICMPTimeout($ip, $timeout = 0) {
  795. $globconf = parse_ini_file(CONFIG_PATH . "billing.ini");
  796. $ping = $globconf['PING'];
  797. $sudo = $globconf['SUDO'];
  798. $pingt_imeout = '';
  799. if (!empty($timeout)) {
  800. $curOS = php_uname('s');
  801. $pingt_imeout = (($curOS == 'FreeBSD') ? '-t ' : '-w ') . $timeout . ' ';
  802. }
  803. $ping_command = $sudo . ' ' . $ping . ' -i 0.01 -c 1 ' . $pingt_imeout . $ip;
  804. $ping_result = shell_exec($ping_command);
  805. if (strpos($ping_result, 'ttl')) {
  806. return (true);
  807. } else {
  808. return (false);
  809. }
  810. }
  811. /**
  812. * Returns result of slow icmp ping with some retries count
  813. *
  814. * @param string $ip devide IP to ping
  815. * @param int $retries number of retries to check host
  816. *
  817. * @return bool
  818. */
  819. function zb_PingICMPHope($ip, $retries = 3) {
  820. $result = false;
  821. $count = 0;
  822. for ($count = 0; $count < $retries; $count++) {
  823. deb($count);
  824. if (zb_PingICMP($ip)) {
  825. deb('true');
  826. $result = true;
  827. break;
  828. }
  829. }
  830. return ($result);
  831. }
  832. /**
  833. * Logs array of switches to deadlog (timemachine)
  834. *
  835. * @param int $currenttime current timestamp
  836. * @param array $deadSwitches dead switches array
  837. */
  838. function zb_SwitchesDeadLog($currenttime, $deadSwitches) {
  839. $date = curdatetime();
  840. $timestamp = $currenttime;
  841. $logData = serialize($deadSwitches);
  842. $query = "INSERT INTO `switchdeadlog` (`id` ,`date` ,`timestamp` ,`swdead`)
  843. VALUES (
  844. NULL , '" . $date . "', '" . $timestamp . "', '" . $logData . "');";
  845. nr_query($query);
  846. }
  847. /**
  848. * Performs all switches ping test and returns dead devices array
  849. *
  850. * @global object $ubillingConfig
  851. * @return array
  852. */
  853. function zb_SwitchesRepingAll() {
  854. global $ubillingConfig;
  855. $switchRepingProcess = new StarDust('SWPING');
  856. $altCfg = $ubillingConfig->getAlter();
  857. $deadswitches = array();
  858. $fastPingFlag = $ubillingConfig->getAlterParam('FASTPING_ENABLED');
  859. if ($fastPingFlag) {
  860. $fastPing = new FastPing();
  861. }
  862. if ($switchRepingProcess->notRunning()) {
  863. $switchRepingProcess->start();
  864. $deathTime = zb_SwitchesGetAllDeathTime();
  865. $allswitches = zb_SwitchesGetAllLocationOrder();
  866. if (!empty($allswitches)) {
  867. foreach ($allswitches as $io => $eachswitch) {
  868. if (!empty($eachswitch['ip']) and !ispos($eachswitch['desc'], 'NP')) {
  869. if (!$fastPingFlag) {
  870. //regular per-device ICMP polling
  871. if (!zb_PingICMP($eachswitch['ip'])) {
  872. $secondChance = zb_PingICMP($eachswitch['ip']);
  873. if (!$secondChance) {
  874. $lastChance = zb_PingICMP($eachswitch['ip']);
  875. if (!$lastChance) {
  876. if (empty($altCfg['SWITCH_PING_CUSTOM_SCRIPT'])) {
  877. //yep, switch looks like it really down
  878. $deadswitches[$eachswitch['ip']] = $eachswitch['location'];
  879. if (!isset($deathTime[$eachswitch['ip']])) {
  880. zb_SwitchDeathTimeSet($eachswitch['ip']);
  881. }
  882. } else {
  883. //really last-last chance
  884. $customTestCommand = $altCfg['SWITCH_PING_CUSTOM_SCRIPT'] . ' ' . $eachswitch['ip'];
  885. $customScriptRun = shell_exec($customTestCommand);
  886. $customScriptRun = trim($customScriptRun);
  887. if ($customScriptRun != '1') {
  888. $deadswitches[$eachswitch['ip']] = $eachswitch['location'];
  889. if (!isset($deathTime[$eachswitch['ip']])) {
  890. zb_SwitchDeathTimeSet($eachswitch['ip']);
  891. }
  892. } else {
  893. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  894. }
  895. }
  896. } else {
  897. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  898. }
  899. } else {
  900. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  901. }
  902. } else {
  903. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  904. }
  905. } else {
  906. //fast ping query
  907. if ($fastPing->isDead($eachswitch['ip'])) {
  908. zb_SwitchDeathTimeSet($eachswitch['ip']);
  909. $deadswitches[$eachswitch['ip']] = $eachswitch['location'];
  910. if (!isset($deathTime[$eachswitch['ip']])) {
  911. zb_SwitchDeathTimeSet($eachswitch['ip']);
  912. }
  913. } else {
  914. zb_SwitchDeathTimeResurrection($eachswitch['ip']);
  915. }
  916. }
  917. }
  918. }
  919. }
  920. $newdata = serialize($deadswitches);
  921. zb_StorageSet('SWDEAD', $newdata);
  922. $switchRepingProcess->stop();
  923. }
  924. return ($deadswitches);
  925. }
  926. /**
  927. * Performs switches alive state check
  928. *
  929. * @return void
  930. */
  931. function zb_SwitchesForcePing() {
  932. global $ubillingConfig;
  933. $alterconf = $ubillingConfig->getAlter();
  934. $allswitches = zb_SwitchesGetAll();
  935. $modelnames = zb_SwitchModelsGetAllTag();
  936. $currenttime = time();
  937. $reping_timeout = $alterconf['SW_PINGTIMEOUT'];
  938. $deathTime = zb_SwitchesGetAllDeathTime();
  939. $fastPingFlag = $ubillingConfig->getAlterParam('FASTPING_ENABLED');
  940. //counters
  941. $countTotal = 0;
  942. $countAlive = 0;
  943. $countDead = 0;
  944. $countNp = 0;
  945. $countOnMap = 0;
  946. $countSwpoll = 0;
  947. $countMtsigmon = 0;
  948. $countOlt = 0;
  949. $countLinked = 0;
  950. //non realtime switches pinging
  951. $last_pingtime = zb_StorageGet('SWPINGTIME');
  952. if (!$last_pingtime) {
  953. zb_SwitchesRepingAll();
  954. zb_StorageSet('SWPINGTIME', $currenttime);
  955. $last_pingtime = $currenttime;
  956. } else {
  957. if ($currenttime > ($last_pingtime + ($reping_timeout * 60))) {
  958. // normal timeout reping sub here
  959. zb_SwitchesRepingAll();
  960. zb_StorageSet('SWPINGTIME', $currenttime);
  961. }
  962. }
  963. //force total reping and update cache
  964. if (wf_CheckGet(array('forcereping'))) {
  965. if ($fastPingFlag) {
  966. $fastPing = new FastPing();
  967. $fastPing->repingDevices();
  968. }
  969. zb_SwitchesRepingAll();
  970. zb_StorageSet('SWPINGTIME', $currenttime);
  971. if (wf_CheckGet(array('ajaxping'))) {
  972. $dead_raw = zb_StorageGet('SWDEAD');
  973. $deathTime = zb_SwitchesGetAllDeathTime();
  974. $deadarr = array();
  975. $ajaxResult = '';
  976. if ($dead_raw) {
  977. $deadarr = unserialize($dead_raw);
  978. if (!empty($deadarr)) {
  979. //there is some dead switches
  980. $deadcount = sizeof($deadarr);
  981. if ($alterconf['SWYMAP_ENABLED']) {
  982. //getting geodata
  983. $switchesGeo = zb_SwitchesGetAllGeo();
  984. }
  985. //ajax container
  986. $ajaxResult .= wf_tag('div', false, '', 'id="switchping"');
  987. foreach ($deadarr as $ip => $switch) {
  988. if ($alterconf['SWYMAP_ENABLED']) {
  989. if (isset($switchesGeo[$ip])) {
  990. if (!empty($switchesGeo[$ip])) {
  991. $devicefind = wf_Link('?module=switchmap&finddevice=' . $switchesGeo[$ip], wf_img('skins/icon_search_small.gif', __('Find on map'))) . ' ';
  992. } else {
  993. $devicefind = '';
  994. }
  995. } else {
  996. $devicefind = '';
  997. }
  998. } else {
  999. $devicefind = '';
  1000. }
  1001. //check morgue records for death time
  1002. if (isset($deathTime[$ip])) {
  1003. $deathClock = wf_img('skins/clock.png', __('Switch dead since') . ' ' . $deathTime[$ip]) . ' ';
  1004. } else {
  1005. $deathClock = '';
  1006. }
  1007. //switch location link
  1008. $switchLocator = wf_Link('?module=switches&gotoswitchbyip=' . $ip, web_edit_icon(__('Go to switch')));
  1009. //add switch as dead
  1010. $ajaxResult .= $devicefind . ' ' . $switchLocator . ' ' . $deathClock . $ip . ' - ' . $switch . '<br>';
  1011. }
  1012. } else {
  1013. $ajaxResult = __('Switches are okay, everything is fine - I guarantee');
  1014. }
  1015. }
  1016. $ajaxResult .= wf_delimiter() . __('Cache state at time') . ': ' . date("H:i:s");
  1017. print($ajaxResult);
  1018. //darkvoid update
  1019. $notifyArea = new DarkVoid();
  1020. $notifyArea->flushCache();
  1021. die();
  1022. } else {
  1023. rcms_redirect('?module=switches');
  1024. }
  1025. }
  1026. }
  1027. /**
  1028. * Returns list of all available switches devices with its controls. Also catches ajaxping and forcereping events.
  1029. *
  1030. * @return string
  1031. */
  1032. function web_SwitchesShow() {
  1033. global $ubillingConfig;
  1034. $alterconf = $ubillingConfig->getAlter();
  1035. $allswitches = zb_SwitchesGetAll();
  1036. $modelnames = zb_SwitchModelsGetAllTag();
  1037. $currenttime = time();
  1038. $reping_timeout = $alterconf['SW_PINGTIMEOUT'];
  1039. $deathTime = zb_SwitchesGetAllDeathTime();
  1040. //counters
  1041. $countTotal = 0;
  1042. $countAlive = 0;
  1043. $countDead = 0;
  1044. $countNp = 0;
  1045. $countOnMap = 0;
  1046. $countSwpoll = 0;
  1047. $countMtsigmon = 0;
  1048. $countOlt = 0;
  1049. $countLinked = 0;
  1050. //non realtime switches pinging
  1051. $last_pingtime = zb_StorageGet('SWPINGTIME');
  1052. if (!$last_pingtime) {
  1053. zb_SwitchesRepingAll();
  1054. zb_StorageSet('SWPINGTIME', $currenttime);
  1055. $last_pingtime = $currenttime;
  1056. } else {
  1057. if ($currenttime > ($last_pingtime + ($reping_timeout * 60))) {
  1058. // normal timeout reping sub here
  1059. zb_SwitchesRepingAll();
  1060. zb_StorageSet('SWPINGTIME', $currenttime);
  1061. }
  1062. }
  1063. //force total reping and update cache
  1064. if (wf_CheckGet(array('forcereping'))) {
  1065. zb_SwitchesRepingAll();
  1066. zb_StorageSet('SWPINGTIME', $currenttime);
  1067. if (wf_CheckGet(array('ajaxping'))) {
  1068. $dead_raw = zb_StorageGet('SWDEAD');
  1069. $deathTime = zb_SwitchesGetAllDeathTime();
  1070. $deadarr = array();
  1071. $ajaxResult = '';
  1072. if ($dead_raw) {
  1073. $deadarr = unserialize($dead_raw);
  1074. if (!empty($deadarr)) {
  1075. //there is some dead switches
  1076. $deadcount = sizeof($deadarr);
  1077. if ($alterconf['SWYMAP_ENABLED']) {
  1078. //getting geodata
  1079. $switchesGeo = zb_SwitchesGetAllGeo();
  1080. }
  1081. //ajax container
  1082. $ajaxResult .= wf_tag('div', false, '', 'id="switchping"');
  1083. foreach ($deadarr as $ip => $switch) {
  1084. if ($alterconf['SWYMAP_ENABLED']) {
  1085. if (isset($switchesGeo[$ip])) {
  1086. if (!empty($switchesGeo[$ip])) {
  1087. $devicefind = wf_Link('?module=switchmap&finddevice=' . $switchesGeo[$ip], wf_img('skins/icon_search_small.gif', __('Find on map'))) . ' ';
  1088. } else {
  1089. $devicefind = '';
  1090. }
  1091. } else {
  1092. $devicefind = '';
  1093. }
  1094. } else {
  1095. $devicefind = '';
  1096. }
  1097. //check morgue records for death time
  1098. if (isset($deathTime[$ip])) {
  1099. $deathClock = wf_img('skins/clock.png', __('Switch dead since') . ' ' . $deathTime[$ip]) . ' ';
  1100. } else {
  1101. $deathClock = '';
  1102. }
  1103. //switch location link
  1104. $switchLocator = wf_Link('?module=switches&gotoswitchbyip=' . $ip, web_edit_icon(__('Go to switch')));
  1105. //add switch as dead
  1106. $ajaxResult .= $devicefind . ' ' . $switchLocator . ' ' . $deathClock . $ip . ' - ' . $switch . '<br>';
  1107. }
  1108. } else {
  1109. $ajaxResult = __('Switches are okay, everything is fine - I guarantee');
  1110. }
  1111. }
  1112. $ajaxResult .= wf_delimiter() . __('Cache state at time') . ': ' . date("H:i:s");
  1113. print($ajaxResult);
  1114. //darkvoid update
  1115. $notifyArea = new DarkVoid();
  1116. $notifyArea->flushCache();
  1117. die();
  1118. }
  1119. }
  1120. //load dead switches cache
  1121. $dead_switches_raw = zb_StorageGet('SWDEAD');
  1122. if (!$dead_switches_raw) {
  1123. $dead_switches = array();
  1124. } else {
  1125. $dead_switches = unserialize($dead_switches_raw);
  1126. }
  1127. //create new ADcomments object if enabled
  1128. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1129. $adcomments = new ADcomments('SWITCHES');
  1130. }
  1131. $tablecells = wf_TableCell(__('ID'));
  1132. $tablecells .= wf_TableCell(__('IP'));
  1133. $tablecells .= wf_TableCell(__('Location'));
  1134. $tablecells .= wf_TableCell(__('Active'));
  1135. $tablecells .= wf_TableCell(__('Model'));
  1136. $tablecells .= wf_TableCell(__('SNMP community'));
  1137. $tablecells .= wf_TableCell(__('Geo location'));
  1138. $tablecells .= wf_TableCell(__('Description'));
  1139. $tablecells .= wf_TableCell(__('Actions'));
  1140. $tablerows = wf_TableRow($tablecells, 'row1');
  1141. $lighter = 'onmouseover="this.className = \'row2\';" onmouseout="this.className = \'row3\';" ';
  1142. if (!empty($allswitches)) {
  1143. foreach ($allswitches as $io => $eachswitch) {
  1144. if (isset($dead_switches[$eachswitch['ip']])) {
  1145. if (isset($deathTime[$eachswitch['ip']])) {
  1146. $obituary = __('Switch dead since') . ' ' . $deathTime[$eachswitch['ip']];
  1147. } else {
  1148. $obituary = '';
  1149. }
  1150. $aliveled = web_red_led($obituary);
  1151. $aliveflag = '0';
  1152. $countDead++;
  1153. } else {
  1154. if (strpos($eachswitch['desc'], 'NP') === false) {
  1155. $aliveled = web_green_led();
  1156. $aliveflag = '1';
  1157. $countAlive++;
  1158. } else {
  1159. $aliveled = web_yellow_led();
  1160. $aliveflag = '2';
  1161. $countNp++;
  1162. }
  1163. }
  1164. $tablecells = wf_TableCell($eachswitch['id']);
  1165. $tablecells .= wf_TableCell($eachswitch['ip'], '', '', 'sorttable_customkey="' . ip2int($eachswitch['ip']) . '"');
  1166. $tablecells .= wf_TableCell($eachswitch['location']);
  1167. $tablecells .= wf_TableCell($aliveled, '', '', 'sorttable_customkey="' . $aliveflag . '"');
  1168. $tablecells .= wf_TableCell(@$modelnames[$eachswitch['modelid']]);
  1169. $tablecells .= wf_TableCell($eachswitch['snmp']);
  1170. $tablecells .= wf_TableCell($eachswitch['geo']);
  1171. $tablecells .= wf_TableCell($eachswitch['desc']);
  1172. $switchcontrols = '';
  1173. if (cfr('SWITCHESEDIT')) {
  1174. $switchcontrols .= wf_Link('?module=switches&edit=' . $eachswitch['id'], web_edit_icon());
  1175. }
  1176. if (cfr('SWITCHPOLL')) {
  1177. if ((!empty($eachswitch['snmp'])) and (ispos($eachswitch['desc'], 'SWPOLL'))) {
  1178. $switchcontrols .= '&nbsp;' . wf_Link('?module=switchpoller&switchid=' . $eachswitch['id'], wf_img('skins/snmp.png', __('SNMP query')));
  1179. $countSwpoll++;
  1180. }
  1181. }
  1182. if ($alterconf['SWYMAP_ENABLED']) {
  1183. if (!empty($eachswitch['geo'])) {
  1184. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'], wf_img('skins/icon_search_small.gif', __('Find on map')));
  1185. $countOnMap++;
  1186. }
  1187. if (!empty($eachswitch['parentid'])) {
  1188. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'] . '&showuplinks=true&traceid=' . $eachswitch['id'], wf_img('skins/ymaps/uplinks.png', __('Uplink switch')));
  1189. $countLinked++;
  1190. }
  1191. }
  1192. if (ispos($eachswitch['desc'], 'MTSIGMON')) {
  1193. $countMtsigmon++;
  1194. }
  1195. if (ispos($eachswitch['desc'], 'OLT')) {
  1196. $countOlt++;
  1197. }
  1198. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1199. $switchcontrols .= $adcomments->getCommentsIndicator($eachswitch['id']);
  1200. }
  1201. if (isset($alterconf['SW_WEBNAV'])) {
  1202. if ($alterconf['SW_WEBNAV']) {
  1203. $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);
  1204. }
  1205. }
  1206. $tablecells .= wf_TableCell($switchcontrols);
  1207. $tablerows .= wf_tag('tr', false, 'row3', $lighter);
  1208. $tablerows .= $tablecells;
  1209. $tablerows .= wf_tag('tr', true);
  1210. $countTotal++;
  1211. }
  1212. }
  1213. $result = wf_TableBody($tablerows, '100%', '0', 'sortable');
  1214. $result .= wf_img('skins/icon_active.gif') . ' ' . __('Alive switches') . ' - ' . ($countAlive + $countNp) . ' (' . $countAlive . '+' . $countNp . ')' . wf_tag('br');
  1215. $result .= wf_img('skins/icon_inactive.gif') . ' ' . __('Dead switches') . ' - ' . $countDead . wf_tag('br');
  1216. $result .= wf_img('skins/yellow_led.png') . ' ' . __('NP switches') . ' - ' . $countNp . wf_tag('br');
  1217. $result .= wf_img('skins/snmp.png') . ' ' . __('SWPOLL query') . ' - ' . $countSwpoll . wf_tag('br');
  1218. $result .= wf_img('skins/wifi.png') . ' ' . __('MTSIGMON devices') . ' - ' . $countMtsigmon . wf_tag('br');
  1219. $result .= wf_img('skins/pon_icon.gif') . ' ' . __('OLT devices') . ' - ' . $countOlt . wf_tag('br');
  1220. $result .= wf_img('skins/icon_search_small.gif') . ' ' . __('Placed on map') . ' - ' . $countOnMap . wf_tag('br');
  1221. $result .= wf_img('skins/ymaps/uplinks.png') . ' ' . __('Have uplinks') . ' - ' . $countLinked . wf_tag('br');
  1222. $result .= wf_tag('br') . wf_tag('b') . __('Total') . ': ' . $countTotal . wf_tag('b', true) . wf_tag('br');
  1223. return ($result);
  1224. }
  1225. /**
  1226. * Returns JQDT switches list container
  1227. *
  1228. * @return string
  1229. */
  1230. function web_SwitchesRenderList() {
  1231. $result = '';
  1232. global $ubillingConfig;
  1233. $alterconf = $ubillingConfig->getAlter();
  1234. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1235. $switchesExtended = $ubillingConfig->getAlterParam('SWITCHES_EXTENDED');
  1236. $switchesCompactFlag = $ubillingConfig->getAlterParam('SWITCHES_LIST_COMPACT');
  1237. $summaryCache = 'exports/switchcounterssummary.dat';
  1238. $columns = array('ID', 'IP');
  1239. if ($alterconf['SWITCHES_SNMP_MAC_EXORCISM']) {
  1240. $columns[] = ('MAC');
  1241. }
  1242. if ($switchesExtended) {
  1243. $columns[] = __('Uplink');
  1244. //separate port column
  1245. if ($switchesExtended == 3) {
  1246. $columns[] = __('Port');
  1247. }
  1248. }
  1249. array_push($columns, 'Location', 'Active', 'Model');
  1250. if (!$switchesCompactFlag) {
  1251. array_push($columns, 'SNMP community', 'Geo location');
  1252. }
  1253. $columns[] = 'Description';
  1254. if ($swGroupsEnabled) {
  1255. $columns[] = 'Group';
  1256. }
  1257. $columns[] = 'Actions';
  1258. $opts = '"order": [[ 0, "desc" ]]';
  1259. $result = wf_JqDtLoader($columns, '?module=switches&ajaxlist=true', false, __('Switch'), 100, $opts);
  1260. if (file_exists($summaryCache)) {
  1261. $result .= file_get_contents($summaryCache);
  1262. }
  1263. return ($result);
  1264. }
  1265. /**
  1266. * Renders ajax switches list data
  1267. *
  1268. * @return string
  1269. */
  1270. function zb_SwitchesRenderAjaxList() {
  1271. $result = '';
  1272. global $ubillingConfig;
  1273. $alterconf = $ubillingConfig->getAlter();
  1274. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1275. $switchesExtended = $ubillingConfig->getAlterParam('SWITCHES_EXTENDED');
  1276. $switchesCompactFlag = $ubillingConfig->getAlterParam('SWITCHES_LIST_COMPACT');
  1277. $allswitchgroups = '';
  1278. if ($swGroupsEnabled) {
  1279. $switchGroups = new SwitchGroups();
  1280. $allswitchgroups = $switchGroups->getSwitchesIdsWithGroupsData();
  1281. }
  1282. if ($switchesExtended) {
  1283. $switchesUplinks = new SwitchUplinks();
  1284. $switchesUplinks->loadAllUplinksData();
  1285. }
  1286. $allswitches = zb_SwitchesGetAll();
  1287. $modelnames = zb_SwitchModelsGetAllTag();
  1288. $deathTime = zb_SwitchesGetAllDeathTime();
  1289. $summaryCache = 'exports/switchcounterssummary.dat';
  1290. $jsonAAData = array();
  1291. //counters
  1292. $countTotal = 0;
  1293. $countAlive = 0;
  1294. $countDead = 0;
  1295. $countNp = 0;
  1296. $countOnMap = 0;
  1297. $countSwpoll = 0;
  1298. $countMtsigmon = 0;
  1299. $countAP = 0;
  1300. $countOlt = 0;
  1301. $countLinked = 0;
  1302. //load dead switches cache
  1303. $dead_switches_raw = zb_StorageGet('SWDEAD');
  1304. if (!$dead_switches_raw) {
  1305. $dead_switches = array();
  1306. } else {
  1307. $dead_switches = unserialize($dead_switches_raw);
  1308. }
  1309. //create new ADcomments object if enabled
  1310. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1311. $adcomments = new ADcomments('SWITCHES');
  1312. }
  1313. if (!empty($allswitches)) {
  1314. foreach ($allswitches as $io => $eachswitch) {
  1315. $jsonItem = array();
  1316. if (isset($dead_switches[$eachswitch['ip']])) {
  1317. if (isset($deathTime[$eachswitch['ip']])) {
  1318. $obituary = __('Switch dead since') . ' ' . $deathTime[$eachswitch['ip']];
  1319. } else {
  1320. $obituary = '';
  1321. }
  1322. $aliveled = web_red_led($obituary) . ' ' . __('No');
  1323. $aliveflag = '0';
  1324. $countDead++;
  1325. } else {
  1326. if (strpos($eachswitch['desc'], 'NP') === false) {
  1327. $aliveled = web_green_led() . ' ' . __('Yes');
  1328. $aliveflag = '1';
  1329. $countAlive++;
  1330. } else {
  1331. $aliveled = web_yellow_led() . ' ' . __('NP');
  1332. $aliveflag = '2';
  1333. $countNp++;
  1334. }
  1335. }
  1336. $jsonItem[] = $eachswitch['id'];
  1337. $jsonItem[] = $eachswitch['ip'];
  1338. if ($alterconf['SWITCHES_SNMP_MAC_EXORCISM']) {
  1339. $deviceMac = '';
  1340. $deviceMacCache = 'exports/' . $eachswitch['ip'] . '_MAC';
  1341. if (file_exists($deviceMacCache)) {
  1342. $deviceMacData = file_get_contents($deviceMacCache);
  1343. if (check_mac_format($deviceMacData)) {
  1344. if ($alterconf['SWITCHES_EXTENDED'] and $deviceMacData != $eachswitch['swid']) {
  1345. $deviceMac = $deviceMacData . ' ' . wf_img('skins/createtask.gif', __('MAC mismatch')) . ' ' . __('Oh no');
  1346. } else {
  1347. $deviceMac = $deviceMacData;
  1348. }
  1349. }
  1350. }
  1351. $jsonItem[] = $deviceMac;
  1352. }
  1353. if ($switchesExtended) {
  1354. $includePortFlag = ($switchesExtended == 2) ? true : false;
  1355. $jsonItem[] = $switchesUplinks->getUplinkTinyDesc($eachswitch['id'], $includePortFlag);
  1356. //port as separate column?
  1357. if ($switchesExtended == 3) {
  1358. $jsonItem[] = $switchesUplinks->getUplinkPort($eachswitch['id']);
  1359. }
  1360. }
  1361. $jsonItem[] = $eachswitch['location'];
  1362. $jsonItem[] = $aliveled;
  1363. $jsonItem[] = @$modelnames[$eachswitch['modelid']];
  1364. if (!$switchesCompactFlag) {
  1365. $jsonItem[] = $eachswitch['snmp'];
  1366. $jsonItem[] = $eachswitch['geo'];
  1367. }
  1368. $jsonItem[] = $eachswitch['desc'];
  1369. if ($swGroupsEnabled) {
  1370. $jsonItem[] = (isset($allswitchgroups[$eachswitch['id']])) ? $allswitchgroups[$eachswitch['id']]['groupname'] : '';
  1371. }
  1372. $switchcontrols = '';
  1373. if (cfr('SWITCHES')) {
  1374. $switchcontrols .= wf_Link('?module=switches&edit=' . $eachswitch['id'], web_edit_icon());
  1375. }
  1376. if (cfr('SWITCHPOLL')) {
  1377. if ((!empty($eachswitch['snmp'])) and (ispos($eachswitch['desc'], 'SWPOLL'))) {
  1378. $switchcontrols .= '&nbsp;' . wf_Link('?module=switchpoller&switchid=' . $eachswitch['id'], wf_img('skins/snmp.png', __('SNMP query')));
  1379. $countSwpoll++;
  1380. }
  1381. }
  1382. if ($alterconf['SWYMAP_ENABLED']) {
  1383. if (!empty($eachswitch['geo'])) {
  1384. if (cfr('SWITCHMAP')) {
  1385. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'], wf_img('skins/icon_search_small.gif', __('Find on map')));
  1386. }
  1387. $countOnMap++;
  1388. }
  1389. if (!empty($eachswitch['parentid'])) {
  1390. if (cfr('SWITCHMAP')) {
  1391. $switchcontrols .= wf_Link('?module=switchmap&finddevice=' . $eachswitch['geo'] . '&showuplinks=true&traceid=' . $eachswitch['id'], wf_img('skins/ymaps/uplinks.png', __('Uplink switch')));
  1392. }
  1393. $countLinked++;
  1394. }
  1395. if ((empty($eachswitch['geo'])) and (!ispos($eachswitch['desc'], 'NP'))) {
  1396. if ((cfr('SWITCHESEDIT')) and (cfr('SWITCHMAP'))) {
  1397. $switchcontrols .= wf_Link('?module=switchmap&locfinder=true&placesw=' . $eachswitch['id'], wf_img('skins/ymaps/target.png', __('Place on map')), false, '');
  1398. }
  1399. }
  1400. }
  1401. if (ispos($eachswitch['desc'], 'MTSIGMON')) {
  1402. $countMtsigmon++;
  1403. }
  1404. if (ispos($eachswitch['desc'], 'OLT')) {
  1405. $countOlt++;
  1406. }
  1407. if (ispos($eachswitch['desc'], 'AP')) {
  1408. $countAP++;
  1409. }
  1410. if ($alterconf['ADCOMMENTS_ENABLED']) {
  1411. $switchcontrols .= $adcomments->getCommentsIndicator($eachswitch['id']);
  1412. }
  1413. if (isset($alterconf['SW_WEBNAV'])) {
  1414. if ($alterconf['SW_WEBNAV']) {
  1415. $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);
  1416. }
  1417. }
  1418. if (@$alterconf['SW_CASH_ENABLED']) {
  1419. if (ispos($eachswitch['desc'], 'SWCASH')) {
  1420. $swCashUrl = SwitchCash::URL_ME . '&' . SwitchCash::ROUTE_EDIT . '=' . $eachswitch['id'];
  1421. $switchcontrols .= wf_Link($swCashUrl, wf_img('skins/ukv/dollar.png', __('Financial data')));
  1422. }
  1423. }
  1424. $jsonItem[] = $switchcontrols;
  1425. $countTotal++;
  1426. $jsonAAData[] = $jsonItem;
  1427. }
  1428. }
  1429. $countersSummary = wf_tag('br');
  1430. $countersSummary .= wf_img('skins/icon_active.gif') . ' ' . __('Alive switches') . ' - ' . ($countAlive + $countNp) . ' (' . $countAlive . '+' . $countNp . ')' . wf_tag('br');
  1431. $countersSummary .= wf_img('skins/icon_inactive.gif') . ' ' . __('Dead switches') . ' - ' . $countDead . wf_tag('br');
  1432. $countersSummary .= wf_img('skins/yellow_led.png') . ' ' . __('NP switches') . ' - ' . $countNp . wf_tag('br');
  1433. $countersSummary .= wf_img('skins/snmp.png') . ' ' . __('SWPOLL query') . ' - ' . $countSwpoll . wf_tag('br');
  1434. $countersSummary .= wf_img('skins/wifi.png') . ' ' . __('MTSIGMON devices') . ' - ' . $countMtsigmon . wf_tag('br');
  1435. $countersSummary .= wf_img('skins/pon_icon.gif') . ' ' . __('OLT devices') . ' - ' . $countOlt . wf_tag('br');
  1436. $countersSummary .= wf_img('skins/wifi.png') . ' ' . __('AP devices') . ' - ' . $countAP . wf_tag('br');
  1437. $countersSummary .= wf_img('skins/icon_search_small.gif') . ' ' . __('Placed on map') . ' - ' . $countOnMap . wf_tag('br');
  1438. $countersSummary .= wf_img('skins/ymaps/uplinks.png') . ' ' . __('Have uplinks') . ' - ' . $countLinked . wf_tag('br');
  1439. $countersSummary .= wf_tag('br') . wf_tag('b') . __('Total') . ': ' . $countTotal . wf_tag('b', true) . wf_tag('br');
  1440. file_put_contents($summaryCache, $countersSummary);
  1441. $jsonList = array("aaData" => $jsonAAData);
  1442. return (json_encode($jsonList));
  1443. }
  1444. /**
  1445. * Updates existing switch data
  1446. *
  1447. * @global object $ubillingConfig
  1448. *
  1449. * @return void
  1450. */
  1451. function ub_SwitchSave($switchid) {
  1452. global $ubillingConfig;
  1453. $altCfg = $ubillingConfig->getAlter();
  1454. $switchid = ubRouting::filters($switchid, 'int');
  1455. // some non-parameterized shit here, PFFFFF
  1456. simple_update_field('switches', 'modelid', ubRouting::post('editmodel'), "WHERE `id`='" . $switchid . "'");
  1457. simple_update_field('switches', 'ip', ubRouting::post('editip'), "WHERE `id`='" . $switchid . "'");
  1458. simple_update_field('switches', 'location', ub_SanitizeData(ubRouting::post('editlocation'), false), "WHERE `id`='" . $switchid . "'");
  1459. simple_update_field('switches', 'desc', ub_SanitizeData(ubRouting::post('editdesc'), false), "WHERE `id`='" . $switchid . "'");
  1460. simple_update_field('switches', 'snmp', ubRouting::post('editsnmp'), "WHERE `id`='" . $switchid . "'");
  1461. simple_update_field('switches', 'snmpwrite', ubRouting::post('editsnmpwrite'), "WHERE `id`='" . $switchid . "'");
  1462. if ($altCfg['SWITCHES_EXTENDED']) {
  1463. simple_update_field('switches', 'swid', ubRouting::post('editswid'), "WHERE `id`='" . $switchid . "'");
  1464. }
  1465. simple_update_field('switches', 'geo', preg_replace('/[^-?0-9\.,]/i', '', ubRouting::post('editgeo')), "WHERE `id`='" . $switchid . "'");
  1466. if (ubRouting::post('editparentid') != $switchid) {
  1467. //checks for preventing loops
  1468. $alllinks = array();
  1469. $tmpSwitches = zb_SwitchesGetAll();
  1470. if (!empty($tmpSwitches)) {
  1471. //transform array to id=>switchdata
  1472. foreach ($tmpSwitches as $io => $each) {
  1473. $allswitches[$each['id']] = $each;
  1474. }
  1475. //making id=>parentid array
  1476. foreach ($tmpSwitches as $io => $each) {
  1477. $alllinks[$each['id']] = $each['parentid'];
  1478. }
  1479. }
  1480. if (sm_CheckLoop($alllinks, $switchid, ubRouting::post('editparentid'))) {
  1481. simple_update_field('switches', 'parentid', ubRouting::post('editparentid'), "WHERE `id`='" . $switchid . "'");
  1482. }
  1483. }
  1484. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1485. if ($swGroupsEnabled) {
  1486. $switchGroups = new SwitchGroups();
  1487. $switchAlreadyInGroup = $switchGroups->getSwitchGroupBySwitchId($switchid);
  1488. if (empty($switchAlreadyInGroup) and ubRouting::post('editswgroup')) {
  1489. $query = "INSERT INTO `switch_groups_relations` (`switch_id`, `sw_group_id`) VALUES (" . $switchid . ", " . ubRouting::post('editswgroup') . ")";
  1490. nr_query($query);
  1491. } elseif (ubRouting::post('editswgroup')) {
  1492. if (ubRouting::post('editswgroup') == '0') {
  1493. $switchGroups->removeSwitchFromGroup($switchid);
  1494. } else {
  1495. simple_update_field('switch_groups_relations', 'sw_group_id', ubRouting::post('editswgroup'), "WHERE `switch_id`='" . $switchid . "'");
  1496. }
  1497. }
  1498. }
  1499. log_register('SWITCH CHANGE [' . $switchid . ']' . ' IP ' . ubRouting::post('editip') . " LOC `" . ubRouting::post('editlocation') . "`");
  1500. }
  1501. /**
  1502. * Creates new switch device in database
  1503. *
  1504. * @param int $modelid
  1505. * @param string $ip
  1506. * @param string $desc
  1507. * @param string $location
  1508. * @param string $snmp
  1509. * @param string $swid
  1510. * @param string $geo
  1511. * @param int $parentid
  1512. */
  1513. function ub_SwitchAdd($modelid, $ip, $desc, $location, $snmp, $swid, $geo, $parentid = '', $snmpwrite = '', $switchgroupid = '') {
  1514. $modelid = ubRouting::filters($modelid, 'int');
  1515. $ip = ubRouting::filters($ip, 'mres');
  1516. $desc = ub_SanitizeData($desc);
  1517. $location = ub_SanitizeData($location);
  1518. $snmp = ubRouting::filters($snmp, 'mres');
  1519. $snmpwrite = ubRouting::filters($snmpwrite, 'mres');
  1520. $swid = ubRouting::filters($swid, 'mres');
  1521. $parentid = ubRouting::filters($parentid, 'int');
  1522. if (!empty($parentid)) {
  1523. $parentid = "'" . $parentid . "'";
  1524. } else {
  1525. $parentid = 'NULL';
  1526. }
  1527. $query = "INSERT INTO `switches` (`id` ,`modelid` ,`ip` ,`desc` ,`location` ,`snmp`,`swid`,`geo`,`parentid`,`snmpwrite`) "
  1528. . "VALUES ('', '" . $modelid . "', '" . $ip . "', '" . $desc . "', '" . $location . "', '" . $snmp . "', '" . $swid . "','" . $geo . "', " . $parentid . ",'" . $snmpwrite . "' );";
  1529. nr_query($query);
  1530. $lastid = simple_get_lastid('switches');
  1531. if (!empty($switchgroupid)) {
  1532. $query = "INSERT INTO `switch_groups_relations` (`switch_id`, `sw_group_id`) VALUES (" . $lastid . ", " . $switchgroupid . ")";
  1533. nr_query($query);
  1534. }
  1535. log_register('SWITCH ADD [' . $lastid . '] IP `' . $ip . '` ON LOC `' . $location . '`');
  1536. show_window(__('Add switch'), __('Was added new switch') . ' ' . $ip . ' ' . $location);
  1537. }
  1538. /**
  1539. * Checks is switch parent for someone?
  1540. *
  1541. * @param int $switchid
  1542. * @return bool
  1543. */
  1544. function ub_SwitchIsParent($switchid) {
  1545. $switchid = vf($switchid, 3);
  1546. $result = false;
  1547. $query = "SELECT `id` from `switches` WHERE `parentid`='" . $switchid . "';";
  1548. $raw = simple_query($query);
  1549. if (!empty($raw)) {
  1550. $result = true;
  1551. }
  1552. return ($result);
  1553. }
  1554. /**
  1555. * Flushes child switches for some switch
  1556. *
  1557. * @param int $switchid
  1558. */
  1559. function ub_SwitchFlushChilds($switchid) {
  1560. $switchid = vf($switchid, 3);
  1561. $query = "UPDATE `switches` SET `parentid`=NULL WHERE `parentid`='" . $switchid . "';";
  1562. nr_query($query);
  1563. log_register('SWITCH FLUSH CHILDS [' . $switchid . ']');
  1564. }
  1565. /**
  1566. * Deletes switch from database by its ID
  1567. *
  1568. * @param int $switchid existing switch database ID
  1569. */
  1570. function ub_SwitchDelete($switchid) {
  1571. global $ubillingConfig;
  1572. $swGroupsEnabled = $ubillingConfig->getAlterParam('SWITCH_GROUPS_ENABLED');
  1573. $switchid = vf($switchid);
  1574. $switchdata = zb_SwitchGetData($switchid);
  1575. $query = "DELETE from `switches` WHERE `id`='" . $switchid . "'";
  1576. nr_query($query);
  1577. if ($swGroupsEnabled) {
  1578. $switchGroups = new SwitchGroups();
  1579. $switchGroups->removeSwitchFromGroup($switchid);
  1580. }
  1581. $switchesExtended = $ubillingConfig->getAlterParam('SWITCHES_EXTENDED');
  1582. if ($switchesExtended) {
  1583. $switchesUplinks = new SwitchUplinks();
  1584. $switchesUplinks->flush($switchid);
  1585. }
  1586. $query = 'DELETE FROM `switches_qinq` WHERE `switchid` = "' . $switchid . '"';
  1587. nr_query($query);
  1588. log_register('SWITCH DELETE [' . $switchid . '] IP ' . $switchdata['ip'] . ' LOC ' . $switchdata['location']);
  1589. }
  1590. /**
  1591. * Returns dead switches json data for timemachine calendar view
  1592. *
  1593. * @return string
  1594. */
  1595. function ub_JGetSwitchDeadLog() {
  1596. $cyear = curyear();
  1597. $query = "SELECT `id`,`date`,`timestamp`,`swdead` from `switchdeadlog` WHERE `date` LIKE '" . $cyear . "-%' ORDER BY `id` ASC";
  1598. $alldead = simple_queryall($query);
  1599. $i = 1;
  1600. $logcount = sizeof($alldead);
  1601. $result = '';
  1602. if (!empty($alldead)) {
  1603. foreach ($alldead as $io => $eachdead) {
  1604. if ($i != $logcount) {
  1605. $thelast = ',';
  1606. } else {
  1607. $thelast = '';
  1608. }
  1609. $startdate = strtotime($eachdead['date']);
  1610. $startdate = date("Y, n-1, j", $startdate);
  1611. $deadData_raw = $eachdead['swdead'];
  1612. $deadData = unserialize($deadData_raw);
  1613. $deadcount = sizeof($deadData);
  1614. $result .= "
  1615. {
  1616. title: '" . date("H:i:s", $eachdead['timestamp']) . " - (" . $deadcount . ")',
  1617. start: new Date(" . $startdate . "),
  1618. end: new Date(" . $startdate . "),
  1619. className : 'undone',
  1620. url: '?module=switches&timemachine=true&snapshot=" . $eachdead['id'] . "'
  1621. }
  1622. " . $thelast;
  1623. $i++;
  1624. }
  1625. }
  1626. return ($result);
  1627. }
  1628. /**
  1629. * Renders dead switches top
  1630. *
  1631. * @return string
  1632. */
  1633. function web_DeadSwitchesTop() {
  1634. global $ubillingConfig;
  1635. $altCfg = $ubillingConfig->getAlter();
  1636. if (isset($altCfg['SWITCH_PING_INTERVAL'])) {
  1637. $repingInterval = $altCfg['SWITCH_PING_INTERVAL'] * 60;
  1638. } else {
  1639. $repingInterval = 0;
  1640. }
  1641. $topThreshold = 0;
  1642. $result = '';
  1643. $cmonth = curmonth();
  1644. $query = "SELECT `id`,`date`,`timestamp`,`swdead` from `switchdeadlog` WHERE `date` LIKE '" . $cmonth . "-%' ORDER BY `id` ASC";
  1645. $rawData = simple_queryall($query);
  1646. $topTmp = array();
  1647. $totalCount = 0;
  1648. $totaldeadTime = 0;
  1649. if (!empty($rawData)) {
  1650. foreach ($rawData as $io => $each) {
  1651. if (!empty($each['swdead'])) {
  1652. $deadData = unserialize($each['swdead']);
  1653. if (!empty($deadData)) {
  1654. foreach ($deadData as $eachDeadIp => $eachDeadName) {
  1655. if (isset($topTmp[$eachDeadIp])) {
  1656. $topTmp[$eachDeadIp]['count']++;
  1657. } else {
  1658. $topTmp[$eachDeadIp]['count'] = 1;
  1659. $topTmp[$eachDeadIp]['name'] = $eachDeadName;
  1660. }
  1661. $totalCount++;
  1662. }
  1663. }
  1664. }
  1665. }
  1666. }
  1667. if (!empty($topTmp)) {
  1668. $cells = wf_TableCell(__('IP'));
  1669. $cells .= wf_TableCell(__('Location'));
  1670. $cells .= wf_TableCell(__('Count'));
  1671. if ($repingInterval) {
  1672. $cells .= wf_TableCell(__('Time'));
  1673. }
  1674. $cells .= wf_TableCell(__('Visual'));
  1675. $rows = wf_TableRow($cells, 'row1');
  1676. foreach ($topTmp as $io => $each) {
  1677. if ($each['count'] >= $topThreshold) {
  1678. $cells = wf_TableCell($io);
  1679. $cells .= wf_TableCell($each['name']);
  1680. $cells .= wf_TableCell($each['count']);
  1681. if ($repingInterval) {
  1682. $deadTime = $each['count'] * $repingInterval;
  1683. $cells .= wf_TableCell(zb_formatTime($deadTime));
  1684. $totaldeadTime += $deadTime;
  1685. }
  1686. $cells .= wf_TableCell(web_bar($each['count'], $totalCount), '', '', 'sorttable_customkey="' . $each['count'] . '"');
  1687. $rows .= wf_TableRow($cells, 'row3');
  1688. }
  1689. }
  1690. if ($repingInterval) {
  1691. $cells = wf_TableCell(__('Total'));
  1692. $cells .= wf_TableCell('');
  1693. $cells .= wf_TableCell('');
  1694. $cells .= wf_TableCell(zb_formatTime($totaldeadTime));
  1695. $cells .= wf_TableCell('');
  1696. $rows .= wf_TableRow($cells, 'row2');
  1697. }
  1698. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  1699. }
  1700. return ($result);
  1701. }
  1702. /**
  1703. * Shows time machine snapshot by its ID
  1704. *
  1705. * @param int $snapshotid
  1706. */
  1707. function ub_SwitchesTimeMachineShowSnapshot($snapshotid) {
  1708. $snapshotid = vf($snapshotid, 3);
  1709. $query = "SELECT * from `switchdeadlog` WHERE `id`='" . $snapshotid . "'";
  1710. $deaddata = simple_query($query);
  1711. $deathTime = zb_SwitchesGetAllDeathTime();
  1712. if (!empty($deaddata)) {
  1713. $deadarr = unserialize($deaddata['swdead']);
  1714. $cells = wf_TableCell(__('Switch dead since'));
  1715. $cells .= wf_TableCell(__('IP'));
  1716. $cells .= wf_TableCell(__('Location'));
  1717. $rows = wf_TableRow($cells, 'row1');
  1718. if (!empty($deadarr)) {
  1719. foreach ($deadarr as $ip => $location) {
  1720. $cells = wf_TableCell(@$deathTime[$ip]);
  1721. $cells .= wf_TableCell($ip);
  1722. $cells .= wf_TableCell($location);
  1723. $rows .= wf_TableRow($cells, 'row3');
  1724. }
  1725. }
  1726. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  1727. show_window(__('Dead switches') . ' ' . $deaddata['date'], $result);
  1728. show_window('', wf_BackLink("?module=switches&timemachine=true"));
  1729. }
  1730. }
  1731. /**
  1732. * Flushes time machine switchdead log table
  1733. *
  1734. * @return void
  1735. */
  1736. function ub_SwitchesTimeMachineCleanup() {
  1737. $query = "TRUNCATE TABLE `switchdeadlog`;";
  1738. nr_query($query);
  1739. log_register("SWITCH TIMEMACHINE FLUSH");
  1740. }
  1741. /**
  1742. * Returns time machine search form
  1743. *
  1744. * @return string
  1745. */
  1746. function web_SwitchTimeMachineSearchForm() {
  1747. $inputs = wf_TextInput('switchdeadlogsearch', __('Location') . ', ' . __('IP'), '', false, 30);
  1748. $inputs .= wf_Submit(__('Search'));
  1749. $result = wf_Form('', 'POST', $inputs, 'glamour');
  1750. return ($result);
  1751. }
  1752. /**
  1753. * Returns
  1754. *
  1755. * @param string $switchIp
  1756. *
  1757. * @return array
  1758. */
  1759. function ub_SwitchesTimeMachineGetByIp($switchIp) {
  1760. $result = array();
  1761. $query = "SELECT * from `switchdeadlog` ORDER BY `id` DESC";
  1762. $raw = simple_queryall($query);
  1763. if (!empty($raw)) {
  1764. foreach ($raw as $io => $each) {
  1765. if (!empty($each)) {
  1766. $logData = unserialize($each['swdead']);
  1767. if (isset($logData[$switchIp])) {
  1768. $result[$each['date']] = $logData[$switchIp];
  1769. }
  1770. }
  1771. }
  1772. }
  1773. return ($result);
  1774. }
  1775. /**
  1776. * Do the search in dead switches time machine
  1777. *
  1778. * @param string $query
  1779. * @return string
  1780. */
  1781. function ub_SwitchesTimeMachineSearch($request) {
  1782. $request = strtolower_utf8($request);
  1783. $result = '';
  1784. $query = "SELECT * from `switchdeadlog` ORDER BY `id` DESC";
  1785. $raw = simple_queryall($query);
  1786. $deadcount = 0;
  1787. $tmpArr = array();
  1788. if (!empty($raw)) {
  1789. foreach ($raw as $io => $each) {
  1790. if (!empty($each)) {
  1791. $switchData = unserialize($each['swdead']);
  1792. foreach ($switchData as $switchIp => $switchLocation) {
  1793. if ((ispos(strtolower_utf8($switchIp), $request)) or (ispos(strtolower_utf8($switchLocation), $request))) {
  1794. $searchId = zb_rand_string(8);
  1795. $tmpArr[$searchId]['date'] = $each['date'];
  1796. $tmpArr[$searchId]['ip'] = $switchIp;
  1797. $tmpArr[$searchId]['location'] = $switchLocation;
  1798. }
  1799. }
  1800. }
  1801. }
  1802. }
  1803. if (!empty($tmpArr)) {
  1804. $cells = wf_TableCell(__('Date'));
  1805. $cells .= wf_TableCell(__('IP'));
  1806. $cells .= wf_TableCell(__('Location'));
  1807. $rows = wf_TableRow($cells, 'row1');
  1808. foreach ($tmpArr as $ia => $eachResult) {
  1809. $cells = wf_TableCell($eachResult['date']);
  1810. $cells .= wf_TableCell($eachResult['ip']);
  1811. $cells .= wf_TableCell($eachResult['location']);
  1812. $rows .= wf_TableRow($cells, 'row3');
  1813. $deadcount++;
  1814. }
  1815. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  1816. $result .= __('Total') . ': ' . $deadcount;
  1817. } else {
  1818. $result = __('Nothing found');
  1819. }
  1820. return ($result);
  1821. }
  1822. /**
  1823. * Returns NP switches replacement form
  1824. *
  1825. * @param int $fromSwitchId
  1826. *
  1827. * @return string
  1828. */
  1829. function zb_SwitchReplaceForm($fromSwitchId) {
  1830. $fromSwitchId = vf($fromSwitchId, 3);
  1831. $result = '';
  1832. $query = "SELECT * from `switches` WHERE `desc` LIKE '%NP%' ORDER BY `ip` DESC";
  1833. $raw = simple_queryall($query);
  1834. $paramsNp = array();
  1835. $employee = array();
  1836. $employee = ts_GetActiveEmployee();
  1837. if (!empty($raw)) {
  1838. foreach ($raw as $io => $eachNp) {
  1839. $paramsNp[$eachNp['id']] = $eachNp['ip'] . ' - ' . $eachNp['location'];
  1840. }
  1841. }
  1842. $inputs = wf_HiddenInput('switchreplace', $fromSwitchId);
  1843. $inputs .= wf_SelectorSearchable('toswtichreplace', $paramsNp, 'NP ' . __('Switch'), '', false);
  1844. $inputs .= wf_SelectorSearchable('replaceemployeeid', $employee, __('Worker'), '', false);
  1845. $inputs .= wf_Submit('Save');
  1846. $result = wf_Form('', 'POST', $inputs, 'glamour');
  1847. $result .= wf_CleanDiv();
  1848. $result .= wf_delimiter();
  1849. $result .= wf_BackLink('?module=switches&edit=' . $fromSwitchId);
  1850. return ($result);
  1851. }
  1852. /**
  1853. * Performs switch replacement in database
  1854. *
  1855. * @param int $fromId
  1856. * @param int $toId
  1857. * @param int $employeeid
  1858. *
  1859. * @return void
  1860. */
  1861. function zb_SwitchReplace($fromId, $toId, $employeeId) {
  1862. global $ubillingConfig;
  1863. $fromId = ubRouting::filters($fromId, 'int');
  1864. $toId = ubRouting::filters($toId, 'int');
  1865. $employeeId = ubRouting::filters($employeeId, 'int');
  1866. $switchesDb = new NyanORM('switches');
  1867. $allEmployees = ts_GetAllEmployee();
  1868. $fromData = zb_SwitchGetData($fromId);
  1869. $toData = zb_SwitchGetData($toId);
  1870. if (!empty($fromData)) {
  1871. //updating new switch device
  1872. $switchesDb->where('id', '=', $toId);
  1873. //copy geo coordinates to new switch
  1874. $switchesDb->data('geo', $fromData['geo']);
  1875. //setting new description and remove NP flag
  1876. $newDescriptionTo = str_replace('NP', 'm:' . @$allEmployees[$employeeId], $toData['desc']);
  1877. $switchesDb->data('desc', $newDescriptionTo);
  1878. //copy location
  1879. $switchesDb->data('location', $fromData['location']);
  1880. //copy switch parent ID
  1881. if (!empty($fromData['parentid'])) {
  1882. $switchesDb->data('parentid', $fromData['parentid']);
  1883. } else {
  1884. //or dropping if not set before
  1885. $switchesDb->data('parentid', 'NULL');
  1886. }
  1887. //saving new switch
  1888. $switchesDb->save();
  1889. //updating old switch device
  1890. $switchesDb->where('id', '=', $fromId);
  1891. // doing old switch cleanup and disabling it
  1892. $switchesDb->data('geo', ''); //not located anywhere now
  1893. $newFromLocation = __('removed from') . ': ' . $fromData['location'];
  1894. $switchesDb->data('location', $newFromLocation); //unmouned from somwhere
  1895. $newFromDesc = 'NP u:' . @$allEmployees[$employeeId];
  1896. $switchesDb->data('desc', $newFromDesc); // NP + employee name
  1897. $switchesDb->data('parentid', 'NULL'); // unmounted switch have no parents
  1898. //saving old switch device
  1899. $switchesDb->save();
  1900. //moving childs if it present
  1901. $switchesDb->where('parentid', '=', $fromId);
  1902. $switchesDb->data('parentid', $toId);
  1903. $switchesDb->save();
  1904. //update user switchportassigns
  1905. if ($ubillingConfig->getAlterParam('SWITCHPORT_IN_PROFILE')) {
  1906. if ($ubillingConfig->getAlterParam('USER_SWITCHPORT_AUTOREPLACE')) {
  1907. $switchPortAssignDb = new NyanORM('switchportassign');
  1908. $switchPortAssignDb->where('switchid', '=', $fromId);
  1909. $switchPortAssignDb->data('switchid', $toId);
  1910. $switchPortAssignDb->save();
  1911. // update qinq swithc delegation
  1912. if ($ubillingConfig->getAlterParam('QINQ_ENABLED') and $ubillingConfig->getAlterParam('QINQ_SWITCH_AUTOREPLACE')) {
  1913. $switchesQinqDb = new NyanORM('switches_qinq');
  1914. $switchesQinqDb->where('switchid', '=', $fromId);
  1915. $switchesQinqDb->data('switchid', $toId);
  1916. $switchesQinqDb->save();
  1917. }
  1918. }
  1919. }
  1920. //log this replace
  1921. log_register('SWITCH REPLACE FROM [' . $fromId . '] TO [' . $toId . '] EMPLOYEE [' . $employeeId . ']');
  1922. } else {
  1923. show_error(__('Strange exception') . ': FROM_SWITCH_EMPTY_DATA');
  1924. }
  1925. }
  1926. /**
  1927. * Trys to detect switch ID by its IP
  1928. *
  1929. * @param string $ip
  1930. * @return int
  1931. */
  1932. function zb_SwitchGetIdbyIP($ip) {
  1933. $result = '';
  1934. $ip = mysql_real_escape_string($ip);
  1935. $query = "SELECT `id`,`ip` from `switches` WHERE `ip`='" . $ip . "' LIMIT 1;";
  1936. $raw = simple_query($query);
  1937. if (!empty($raw)) {
  1938. $result = $raw['id'];
  1939. }
  1940. return ($result);
  1941. }
  1942. /**
  1943. * Returns switch profile link with some square brackets
  1944. *
  1945. * @param int $switchId
  1946. *
  1947. * @return string
  1948. */
  1949. function web_SwitchProfileLink($switchId) {
  1950. $result = ' [' . trim(wf_Link('?module=switches&edit=' . $switchId, $switchId)) . '] ';
  1951. return ($result);
  1952. }
  1953. /**
  1954. * Returns all available users switches assigns as login=>switchid,switchip,port,location,label
  1955. *
  1956. * @return array
  1957. */
  1958. function zb_SwitchesGetAssignsAll() {
  1959. $result = array();
  1960. $allSwitches = array();
  1961. $allSwitchesTmp = zb_SwitchesGetAll();
  1962. if (!empty($allSwitchesTmp)) {
  1963. foreach ($allSwitchesTmp as $io => $each) {
  1964. $allSwitches[$each['id']] = $each;
  1965. }
  1966. $switchAssigns_q = "SELECT * from `switchportassign`";
  1967. $switchAssignsTmp = simple_queryall($switchAssigns_q);
  1968. if (!empty($switchAssignsTmp)) {
  1969. foreach ($switchAssignsTmp as $io => $each) {
  1970. if (isset($allSwitches[$each['switchid']])) {
  1971. $switchData = $allSwitches[$each['switchid']];
  1972. $result[$each['login']]['switchid'] = $switchData['id'];
  1973. $result[$each['login']]['switchip'] = $switchData['ip'];
  1974. $result[$each['login']]['port'] = $each['port'];
  1975. $result[$each['login']]['location'] = $switchData['location'];
  1976. $result[$each['login']]['label'] = $switchData['ip'] . ' - ' . $switchData['location'] . ' ' . __('Port') . ' ' . $each['port'];
  1977. }
  1978. }
  1979. }
  1980. }
  1981. return ($result);
  1982. }
  1983. /**
  1984. * Returns background switch ICMP ping
  1985. *
  1986. * @global object $ubillingConfig
  1987. *
  1988. * @return void
  1989. */
  1990. function zb_SwitchBackgroundIcmpPing($ip) {
  1991. global $ubillingConfig;
  1992. $billingConf = $ubillingConfig->getBilling();
  1993. $command = $billingConf['SUDO'] . ' ' . $billingConf['PING'] . ' -i 0.01 -c 10 ' . $ip;
  1994. $icmpPingResult = shell_exec($command);
  1995. die(wf_tag('pre') . $icmpPingResult . wf_tag('pre', true));
  1996. }