api.ponscripts.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. * Just runs some PON OLT custom scripts
  4. */
  5. class PONScripts {
  6. /**
  7. * Contains available scripts as scriptId=>[path,vendor,type,class,icon,name]
  8. *
  9. * @var array
  10. */
  11. protected $allScripts = array();
  12. /**
  13. * Contains available script types as substring=>typeName
  14. *
  15. * @var array
  16. */
  17. protected $typesAvail = array();
  18. /**
  19. * Contains available script/interface classes as substring=>className
  20. *
  21. * @var array
  22. */
  23. protected $classesAvail = array();
  24. /**
  25. * Contains available script applicable vendors as substring=>vendorName
  26. *
  27. * @var array
  28. */
  29. protected $vendorsAvail = array();
  30. /**
  31. * Contains available scripts icons as substring=>iconName
  32. *
  33. * @var array
  34. */
  35. protected $scriptIcons = array();
  36. /**
  37. * Contains available scripts names as substring=>scriptName
  38. *
  39. * @var array
  40. */
  41. protected $scriptNames = array();
  42. /**
  43. * Contains all OLT devices as id=>IP
  44. *
  45. * @var array
  46. */
  47. protected $allOltIps = array();
  48. /**
  49. * Contains all OLT dev modelIds as oltId=>modelId
  50. *
  51. * @var array
  52. */
  53. protected $allOltModelIds = array();
  54. /**
  55. * Contains all devive models data as modelId=>modelData
  56. *
  57. * @var array
  58. */
  59. protected $allOltModelsData = array();
  60. /**
  61. * Contains all switchAuth devices auth data
  62. *
  63. * @var array
  64. */
  65. protected $allAuthData = array();
  66. /**
  67. * Devices auth data object instance
  68. *
  69. * @var object
  70. */
  71. protected $swAuth = '';
  72. /**
  73. * Contains full expect path
  74. *
  75. * @var string
  76. */
  77. protected $expectPath = '';
  78. /**
  79. * System message helper placeholder
  80. *
  81. * @var object
  82. */
  83. protected $messages = '';
  84. //some predefined stuff
  85. const PATH_SCRIPTS = 'config/scripts/ponscripts/';
  86. const PATH_CUSTOM = 'content/documents/myscripts/';
  87. const ICONS_PATH = 'skins/';
  88. const URL_ME = '?module=ponscripts';
  89. const ROUTE_RUN_IFSCRIPT = 'runifscript';
  90. const ROUTE_RUN_OLTID = 'oltid';
  91. const ROUTE_RUN_IFNAME = 'iface';
  92. const RUN_PID = 'PONSCRIPT_';
  93. /**
  94. * Creates new PON scripts instance
  95. *
  96. * @param array $allOltIps
  97. * @param array $allOltModelIds
  98. * @param array $allOltModelsData
  99. */
  100. public function __construct($allOltIps, $allOltModelIds, $allOltModelsData) {
  101. $this->initMessages();
  102. $this->setOptions();
  103. $this->loadScripts();
  104. $this->initSwitchAuth();
  105. $this->loadSwitchAuthData();
  106. $this->setOltIps($allOltIps);
  107. $this->setOltModelIds($allOltModelIds);
  108. $this->setOltModelsData($allOltModelsData);
  109. }
  110. /**
  111. * Sets some object defaults
  112. *
  113. * @return void
  114. */
  115. protected function setOptions() {
  116. global $ubillingConfig;
  117. $billCfg = $ubillingConfig->getBilling();
  118. $this->expectPath = $billCfg['EXPECT_PATH'];
  119. $this->typesAvail = array(
  120. '_if_' => 'iface',
  121. '_onu_' => 'onu',
  122. '_olt_' => 'olt',
  123. '_mac_' => 'mac',
  124. );
  125. $this->classesAvail = array(
  126. '_epon_' => 'epon',
  127. '_gpon_' => 'gpon',
  128. );
  129. $this->vendorsAvail = array(
  130. 'bdcom_' => 'BDCOM',
  131. 'zte_' => 'ZTE',
  132. 'stels_' => 'STELS',
  133. 'gcom_' => 'GCOM',
  134. 'huawei_' => 'Huawei',
  135. 'vsol_' => 'V_SOL',
  136. );
  137. $this->scriptIcons = array(
  138. '_activeonu' => 'icon_active.gif',
  139. '_inactiveonu' => 'icon_inactive.gif',
  140. '_tflush' => 'icon_cleanup.png',
  141. '_optdiag' => 'pon_icon.gif',
  142. );
  143. $this->scriptNames = array(
  144. '_activeonu' => __('Active') . ' ' . __('ONU'),
  145. '_inactiveonu' => __('Inactive') . ' ' . __('ONU'),
  146. '_tflush' => __('Clear') . ' ' . __('Interface'),
  147. '_optdiag' => __('Optical diagnostics'),
  148. );
  149. }
  150. /**
  151. * Initializes the messages property with an instance of UbillingMessageHelper.
  152. *
  153. * @return void
  154. */
  155. protected function initMessages() {
  156. $this->messages = new UbillingMessageHelper();
  157. }
  158. /**
  159. * Inits switchesAuth instance into protected prop
  160. *
  161. * @return void
  162. */
  163. protected function initSwitchAuth() {
  164. $this->swAuth = new SwitchAuth();
  165. }
  166. /**
  167. * Loads devices auth data for further usage
  168. *
  169. * @return void
  170. */
  171. protected function loadSwitchAuthData() {
  172. $this->allAuthData = $this->swAuth->getAllAuthData();
  173. }
  174. /**
  175. * Sets OLTs IPs property
  176. *
  177. * @param array $allOltIps
  178. *
  179. * @return void
  180. */
  181. protected function setOltIps($allOltIps) {
  182. $this->allOltIps = $allOltIps;
  183. }
  184. /**
  185. * Sets OLTs modelIds property
  186. *
  187. * @param array $allOltModelIds
  188. *
  189. * @return void
  190. */
  191. protected function setOltModelIds($allOltModelIds) {
  192. $this->allOltModelIds = $allOltModelIds;
  193. }
  194. /**
  195. * Sets all dev models data property
  196. *
  197. * @param array $allOltModelsData
  198. *
  199. * @return void
  200. */
  201. protected function setOltModelsData($allOltModelsData) {
  202. $this->allOltModelsData = $allOltModelsData;
  203. }
  204. protected function getScriptParam($paramArr, $scriptName, $default) {
  205. $result = $default;
  206. if (!empty($paramArr)) {
  207. foreach ($paramArr as $paramKey => $paramVal) {
  208. if (ispos($scriptName, $paramKey)) {
  209. $result = $paramVal;
  210. }
  211. }
  212. }
  213. return ($result);
  214. }
  215. /**
  216. * Loads and preprocess available PON-scripts from fs
  217. *
  218. * @return void
  219. */
  220. protected function loadScripts() {
  221. $allScripts = rcms_scandir(self::PATH_SCRIPTS);
  222. if (!empty($allScripts)) {
  223. foreach ($allScripts as $io => $each) {
  224. $scriptType = $this->getScriptParam($this->typesAvail, $each, 'unknown');
  225. $scriptClass = $this->getScriptParam($this->classesAvail, $each, 'unknown');
  226. $scriptVendor = $this->getScriptParam($this->vendorsAvail, $each, 'unknown');
  227. $scriptIcon = $this->getScriptParam($this->scriptIcons, $each, 'script16.png');
  228. $scriptLabel = $this->getScriptParam($this->scriptNames, $each, $each);
  229. $eachScriptPath = self::PATH_SCRIPTS . $each;
  230. if (file_exists(self::PATH_CUSTOM . $each)) {
  231. $eachScriptPath = self::PATH_CUSTOM . $each;
  232. }
  233. $this->allScripts[$each]['path'] = $eachScriptPath;
  234. $this->allScripts[$each]['vendor'] = $scriptVendor;
  235. $this->allScripts[$each]['type'] = $scriptType;
  236. $this->allScripts[$each]['class'] = $scriptClass;
  237. $this->allScripts[$each]['icon'] = $scriptIcon;
  238. $this->allScripts[$each]['name'] = $scriptLabel;
  239. }
  240. }
  241. }
  242. /**
  243. * Returns vendor string of some OLT
  244. *
  245. * @param int $oltId
  246. *
  247. * @return string|void
  248. */
  249. protected function getOltVendor($oltId) {
  250. $result = '';
  251. if (isset($this->allOltModelIds[$oltId])) {
  252. $oltModelId = $this->allOltModelIds[$oltId];
  253. if (isset($this->allOltModelsData[$oltModelId])) {
  254. $oltModelData = $this->allOltModelsData[$oltModelId];
  255. if (!empty($oltModelData['snmptemplate'])) {
  256. $oltSnmpTemplateName = $oltModelData['snmptemplate'];
  257. foreach ($this->vendorsAvail as $vendorId => $vendorName) {
  258. if (ispos($oltSnmpTemplateName, $vendorName)) {
  259. $result = $vendorName;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. return ($result);
  266. }
  267. /**
  268. * Returns interface class by interface name
  269. *
  270. * @param string $ifaceName
  271. *
  272. * @return string|void
  273. */
  274. protected function getIfaceClass($ifaceName) {
  275. $result = '';
  276. $lowerIf = strtolower($ifaceName);
  277. foreach ($this->classesAvail as $classId => $className) {
  278. if (ispos($lowerIf, $className)) {
  279. $result = $className;
  280. }
  281. }
  282. return ($result);
  283. }
  284. /**
  285. * Checks is OLT authorization data avail or not?
  286. *
  287. * @param int $oltId
  288. *
  289. * @return bool
  290. */
  291. protected function isAuthDataOk($oltId) {
  292. $result = false;
  293. if (isset($this->allAuthData[$oltId])) {
  294. if ($this->allAuthData[$oltId]['login'] and $this->allAuthData[$oltId]['password']) {
  295. $result = true;
  296. }
  297. }
  298. return ($result);
  299. }
  300. /**
  301. * Renders some interface script controls for specified OLT
  302. *
  303. * @param int $oltId
  304. * @param string $interface
  305. *
  306. * @return string
  307. */
  308. public function renderIfaceControls($oltId, $interface) {
  309. $result = '';
  310. if (!empty($this->allScripts)) {
  311. $oltVendor = $this->getOltVendor($oltId);
  312. $ifClass = $this->getIfaceClass($interface);
  313. $authDataOk = $this->isAuthDataOk($oltId);
  314. foreach ($this->allScripts as $scriptId => $scriptData) {
  315. if ($scriptData['type'] == 'iface') {
  316. //is this PON OLT?
  317. if (isset($this->allOltIps[$oltId])) {
  318. //any auth data available?
  319. if ($authDataOk) {
  320. if ($scriptData['vendor'] == $oltVendor and $scriptData['class'] == $ifClass) {
  321. $actionUrl = self::URL_ME . '&' . self::ROUTE_RUN_IFSCRIPT . '=' . $scriptId . '&' . self::ROUTE_RUN_OLTID . '=' . $oltId . '&' . self::ROUTE_RUN_IFNAME . '=' . $interface;
  322. $cancelUrl = PONizer::URL_ME . '&oltstats=true';
  323. $scriptLabel = wf_img_sized(self::ICONS_PATH . $scriptData['icon'], $scriptData['name'], '12', '12');
  324. $scriptText = __('Run script') . ' «' . $scriptData['name'] . '» ' . __('for') . ' ' . $oltVendor . ' ' . $this->allOltIps[$oltId] . ' ' . $interface . '?';
  325. $result .= wf_ConfirmDialog($actionUrl, $scriptLabel, $scriptText, '', $cancelUrl, $this->messages->getEditAlert());
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. return ($result);
  333. }
  334. /**
  335. * Executes some script using expect
  336. *
  337. * @param string $scriptPath
  338. * @param string $parameters
  339. *
  340. * @return string
  341. */
  342. protected function executeScript($scriptPath, $parameters) {
  343. $result = '';
  344. if (!empty($this->expectPath)) {
  345. if (file_exists($scriptPath)) {
  346. $command = $this->expectPath . ' ' . $scriptPath . ' ' . $parameters;
  347. $result .= shell_exec($command);
  348. } else {
  349. log_register('PONSCRIPT FAIL `' . $scriptPath . '` NOT EXISTS');
  350. }
  351. }
  352. return ($result);
  353. }
  354. /**
  355. * Runs a specified OLT interface script.
  356. *
  357. * @param int $scriptId The ID of the script to be executed.
  358. * @param int $oltId The ID of the OLT
  359. * @param string $ifaceName The name of the interface.
  360. *
  361. * @return string
  362. */
  363. public function runIfaceScript($scriptId, $oltId, $ifaceName) {
  364. $result = '';
  365. if (isset($this->allScripts[$scriptId])) {
  366. if (isset($this->allOltIps[$oltId])) {
  367. $oltIp = $this->allOltIps[$oltId];
  368. $scriptData = $this->allScripts[$scriptId];
  369. $oltVendor = $this->getOltVendor($oltId);
  370. $ifClass = $this->getIfaceClass($ifaceName);
  371. $authDataOk = $this->isAuthDataOk($oltId);
  372. if ($authDataOk) {
  373. if ($scriptData['class'] == $ifClass) {
  374. if ($scriptData['vendor'] == $oltVendor) {
  375. $process = new StarDust(self::RUN_PID . $oltId);
  376. if ($process->notRunning()) {
  377. $process->start();
  378. $oltLogin = $this->allAuthData[$oltId]['login'];
  379. $oltPassword = $this->allAuthData[$oltId]['password'];
  380. $oltEnable = $this->allAuthData[$oltId]['enable'];
  381. $scriptParams = $oltIp . ' ' . $oltLogin . ' ' . $oltPassword . ' ' . $oltEnable . ' ' . $ifaceName;
  382. $executionResultRaw = $this->executeScript($scriptData['path'], $scriptParams);
  383. if (!empty($executionResultRaw)) {
  384. $inputs = wf_tag('textarea', false, 'fileeditorarea', 'name="ponscriptsresult" cols="145" rows="30" spellcheck="false"');
  385. $inputs .= print_r($executionResultRaw, true);
  386. $inputs .= wf_tag('textarea', true);
  387. $result .= $inputs;
  388. } else {
  389. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Result') . ' ' . __('is empty'), 'error');
  390. }
  391. log_register('PONSCRIPT RUN `' . $scriptId . '` OLT [' . $oltId . '] IFACE `' . $ifaceName . '`');
  392. $process->stop();
  393. } else {
  394. $result .= $this->messages->getStyledMessage(__('Script') . ' ' . __('for') . ' ' . __('OLT') . ' [' . $oltId . '] ' . __('Already running'), 'error');
  395. log_register('PONSCRIPT FAIL `' . $scriptId . '` OLT [' . $oltId . '] IFACE `' . $ifaceName . '` ALREADY RUNNING');
  396. }
  397. } else {
  398. $result .= $this->messages->getStyledMessage(__('Wrong OLT vendor') . ': ' . $oltVendor . ', ' . $scriptData['vendor'] . ' ' . __('expected'), 'error');
  399. }
  400. } else {
  401. $result .= $this->messages->getStyledMessage(__('Wrong interface class') . ': ' . $ifaceName, 'error');
  402. }
  403. } else {
  404. $result .= $this->messages->getStyledMessage(__('Device authorization data') . ' ' . __('is empty'), 'error');
  405. }
  406. } else {
  407. $result .= $this->messages->getStyledMessage(__('OLT') . ' ' . $oltId . ' ' . __('not exists'), 'error');
  408. }
  409. } else {
  410. $result .= $this->messages->getStyledMessage(__('Script') . ' ' . $scriptId . ' ' . __('not exists'), 'error');
  411. }
  412. $result .= wf_delimiter();
  413. $result .= wf_BackLink(PONizer::URL_ME . '&oltstats=true');
  414. return ($result);
  415. }
  416. }