api.nasmon.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Network access servers management class
  4. */
  5. class NasMon {
  6. /**
  7. * Contains array of all available grouped by their IP
  8. *
  9. * @var array
  10. */
  11. protected $allNas = array();
  12. /**
  13. * Path to saving NAS checking data
  14. */
  15. const CACHE_PATH = 'exports/';
  16. /**
  17. * Name of cache file that contains table of NAS states
  18. */
  19. const LIST_NAME = 'nasmonlist.dat';
  20. /**
  21. * Name of cache file that contains dead NAS-es count
  22. */
  23. const DEADCOUNT_NAME = 'nasmondead.dat';
  24. /**
  25. * URL to report module
  26. */
  27. const URL_ME = '?module=report_nasmon';
  28. public function __construct() {
  29. }
  30. /**
  31. * Loads available NAS array into protected property
  32. *
  33. * @return void
  34. */
  35. protected function loadAllNas() {
  36. $query = "SELECT * from `nas` GROUP BY `nasip`";
  37. $all = simple_queryall($query);
  38. if (!empty($all)) {
  39. $this->allNas = $all;
  40. }
  41. }
  42. /**
  43. * Performs fast check of all available NASes and returns result as array
  44. *
  45. * @return array
  46. */
  47. protected function checkAllNas() {
  48. $this->loadAllNas();
  49. $deadCount = 0;
  50. $nasCount = 0;
  51. $result = array();
  52. $list = '';
  53. if (!empty($this->allNas)) {
  54. $cells = wf_TableCell(__('NAS name'));
  55. $cells .= wf_TableCell(__('IP'));
  56. $cells .= wf_TableCell(__('Status'));
  57. $rows = wf_TableRow($cells, 'row1');
  58. foreach ($this->allNas as $io => $each) {
  59. $icmpState = zb_PingICMP($each['nasip']);
  60. //second try
  61. if (!$icmpState) {
  62. $icmpState = zb_PingICMP($each['nasip']);
  63. }
  64. if ($icmpState) {
  65. $aliveFlag = web_bool_led($icmpState) . ' ' . __('Alive');
  66. } else {
  67. $aliveFlag = web_bool_led($icmpState) . ' ' . __('Dead');
  68. $deadCount++;
  69. }
  70. $cells = wf_TableCell($each['nasname']);
  71. $cells .= wf_TableCell($each['nasip']);
  72. $cells .= wf_TableCell($aliveFlag);
  73. $rows .= wf_TableRow($cells, 'row5');
  74. $nasCount++;
  75. }
  76. $list = wf_TableBody($rows, '100%', 0, 'sortable');
  77. $list .= wf_tag('b') . __('Total') . ': ' . wf_tag('b', true) . $nasCount;
  78. } else {
  79. $messages = new UbillingMessageHelper();
  80. $list = $messages->getStyledMessage(__('No NAS available in database'), 'warning');
  81. }
  82. $result['list'] = $list;
  83. $result['deadcount'] = $deadCount;
  84. return ($result);
  85. }
  86. /**
  87. * Performs all checks and stores results in cache - required to run periodically
  88. *
  89. * @return void
  90. */
  91. public function saveCheckResults() {
  92. $checkResults = $this->checkAllNas();
  93. if (!empty($checkResults)) {
  94. if (isset($checkResults['list'])) {
  95. file_put_contents(self::CACHE_PATH . self::LIST_NAME, $checkResults['list']);
  96. }
  97. if (isset($checkResults['deadcount'])) {
  98. file_put_contents(self::CACHE_PATH . self::DEADCOUNT_NAME, $checkResults['deadcount']);
  99. }
  100. }
  101. }
  102. /**
  103. * Renders cached results of nas checking
  104. *
  105. * @return string
  106. */
  107. public function renderList() {
  108. $result = '';
  109. if (file_exists(self::CACHE_PATH . self::LIST_NAME)) {
  110. $result = file_get_contents(self::CACHE_PATH . self::LIST_NAME);
  111. } else {
  112. $messages = new UbillingMessageHelper();
  113. $result = $messages->getStyledMessage(__('Nothing to show'), 'warning');
  114. }
  115. return ($result);
  116. }
  117. /**
  118. * Returns count of dead NAS servers from cache
  119. *
  120. * @return int
  121. */
  122. protected function getDeadCount() {
  123. $result = 0;
  124. if (file_exists(self::CACHE_PATH . self::DEADCOUNT_NAME)) {
  125. $result = file_get_contents(self::CACHE_PATH . self::DEADCOUNT_NAME);
  126. }
  127. return ($result);
  128. }
  129. /**
  130. * Returns alert control if required. Used in DarkVoid.
  131. *
  132. * @return string
  133. */
  134. public function getNasAlerts() {
  135. $result = '';
  136. $deadCount = $this->getDeadCount();
  137. if ($deadCount > 0) {
  138. $result = wf_Link(self::URL_ME, wf_img('skins/nasmonalert.png', $deadCount . ' ' . __('NAS servers is dead')), false, '');
  139. }
  140. return ($result);
  141. }
  142. }