api.fastping.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Fast ping implementation
  4. */
  5. class FastPing {
  6. /**
  7. * Contains system alter.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains system billing.ini config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $billCfg = array();
  18. /**
  19. * StarDust process manager instance placeholder
  20. *
  21. * @var object
  22. */
  23. protected $pid = '';
  24. /**
  25. * System caching engine instance placeholder
  26. *
  27. * @var object
  28. */
  29. protected $cache = '';
  30. /**
  31. * Contains cached data from previous runs as ip=>state[1/0]
  32. *
  33. * @var array
  34. */
  35. protected $cachedData = array();
  36. /**
  37. * Contains cached dead devices data as ip=>location
  38. *
  39. * @var array
  40. */
  41. protected $deadCache = array();
  42. /**
  43. * Contains system sudo full path
  44. *
  45. * @var string
  46. */
  47. protected $sudoPath = '/usr/local/bin/sudo';
  48. /**
  49. * Contains default fping path
  50. *
  51. * @var string
  52. */
  53. protected $fpingPath = '/usr/local/sbin/fping -r 1 -t 10';
  54. /**
  55. * Contains some predefined stuff
  56. */
  57. const PID_NAME = 'FASTPING';
  58. const CACHE_KEY = 'FASTPING';
  59. const CACHE_DEAD = 'FASTDEAD';
  60. const LIST_PATH = 'exports/fastping_iplist';
  61. const MASK_ALIVE = 'is alive';
  62. const CACHE_TIMEOUT = 2592000;
  63. public function __construct() {
  64. $this->loadConfigs();
  65. $this->setOptions();
  66. $this->initStarDust();
  67. $this->initCache();
  68. $this->loadCache();
  69. }
  70. /**
  71. * Loads all required configs in protected propeties for futher usage
  72. *
  73. * @global object $ubillingConfig
  74. *
  75. * @return void
  76. */
  77. protected function loadConfigs() {
  78. global $ubillingConfig;
  79. $this->billCfg = $ubillingConfig->getBilling();
  80. $this->altCfg = $ubillingConfig->getAlter();
  81. }
  82. /**
  83. * Sets required system options
  84. *
  85. * @return void
  86. */
  87. protected function setOptions() {
  88. $this->fpingPath = $this->altCfg['FPING_PATH'];
  89. $this->sudoPath = $this->billCfg['SUDO'];
  90. }
  91. /**
  92. * Inits system process manager
  93. *
  94. * @return void
  95. */
  96. protected function initStarDust() {
  97. $this->pid = new StarDust(self::PID_NAME);
  98. }
  99. /**
  100. * Inits system cache
  101. *
  102. * @return void
  103. */
  104. protected function initCache() {
  105. $this->cache = new UbillingCache();
  106. }
  107. /**
  108. * Loads previous runs results into protected property cachedData
  109. *
  110. * @return void
  111. */
  112. protected function loadCache() {
  113. $this->cachedData = $this->cache->get(self::CACHE_KEY, self::CACHE_TIMEOUT);
  114. $this->deadCache = $this->cache->get(self::CACHE_DEAD, self::CACHE_TIMEOUT);
  115. if (empty($this->cachedData)) {
  116. $this->cachedData = array();
  117. }
  118. if (empty($this->deadCache)) {
  119. $this->deadCache = array();
  120. }
  121. }
  122. /**
  123. * Saves fastping results to cache
  124. *
  125. * @param array $data
  126. * @param array $deadSwitches
  127. *
  128. * @return void
  129. */
  130. protected function saveCache($data, $deadSwitches) {
  131. if (empty($data)) {
  132. $data = array();
  133. }
  134. if (empty($deadSwitches)) {
  135. $deadSwitches = array();
  136. }
  137. $this->cache->set(self::CACHE_KEY, $data, self::CACHE_TIMEOUT);
  138. $this->cache->set(self::CACHE_DEAD, $deadSwitches, self::CACHE_TIMEOUT);
  139. }
  140. /**
  141. * Returns all devices states from previous run
  142. *
  143. * @return array
  144. */
  145. public function getAllStates() {
  146. return($this->cachedData);
  147. }
  148. /**
  149. * Returns selected IP last state
  150. *
  151. * @param string $ip
  152. *
  153. * @return int
  154. */
  155. public function getState($ip) {
  156. $result = false;
  157. if (isset($this->cachedData[$ip])) {
  158. $result = $this->cachedData[$ip];
  159. }
  160. return($result);
  161. }
  162. /**
  163. * Performs fast check is some IP alive?
  164. *
  165. * @param string $ip
  166. *
  167. * @return bool
  168. */
  169. public function isAlive($ip) {
  170. $result = ( $this->getState($ip)) ? true : false;
  171. return($result);
  172. }
  173. /**
  174. * Performs fast check is some IP dead?
  175. *
  176. * @param string $ip
  177. *
  178. * @return bool
  179. */
  180. public function isDead($ip) {
  181. $result = ( $this->getState($ip)) ? false : true;
  182. return($result);
  183. }
  184. /**
  185. * Runs fping system binary and returns it result
  186. *
  187. * @return array
  188. */
  189. protected function runPing() {
  190. $result = '';
  191. if (file_exists(self::LIST_PATH)) {
  192. $command = $this->sudoPath . ' ' . $this->fpingPath . ' -f ' . self::LIST_PATH;
  193. $result = shell_exec($command);
  194. }
  195. return($result);
  196. }
  197. /**
  198. * Performs fast ping of all available active devices from switches directory
  199. *
  200. * @return array dead siwtches as ip=>location
  201. */
  202. public function repingDevices() {
  203. $result = array();
  204. $deadSwitches = array();
  205. if ($this->pid->notRunning()) {
  206. //starting process
  207. $this->pid->start();
  208. $allSwitches = zb_SwitchesGetAll();
  209. $ipsList = '';
  210. if (!empty($allSwitches)) {
  211. $uniqueIps = array();
  212. //preprocessing switches
  213. foreach ($allSwitches as $io => $each) {
  214. if (!empty($each['ip']) AND ! ispos($each['desc'], 'NP')) {
  215. if (!isset($uniqueIps[$each['ip']])) {
  216. $ipsList .= $each['ip'] . PHP_EOL;
  217. $uniqueIps[$each['ip']] = $each['location'];
  218. }
  219. }
  220. }
  221. //saving IPs list and running fping
  222. if (!empty($ipsList)) {
  223. file_put_contents(self::LIST_PATH, $ipsList);
  224. $fpingRaw = $this->runPing();
  225. if (!empty($fpingRaw)) {
  226. foreach ($uniqueIps as $devIp => $devLoc) {
  227. $aliveFilter = $devIp . ' ' . self::MASK_ALIVE;
  228. if (ispos($fpingRaw, $aliveFilter)) {
  229. $result[$devIp] = 1;
  230. } else {
  231. $result[$devIp] = 0;
  232. $deadSwitches[$devIp] = $devLoc;
  233. }
  234. }
  235. }
  236. //update cache
  237. $this->cachedData = $result;
  238. $this->saveCache($result, $deadSwitches);
  239. //stopping process
  240. $this->pid->stop();
  241. }
  242. }
  243. } else {
  244. //data from cache?
  245. if (!empty($this->cachedData)) {
  246. }
  247. }
  248. return($deadSwitches);
  249. }
  250. }