api.switches.php 82 KB

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