api.ophanimharvester.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Performs traffic summary accounting for each of collected hosts
  4. */
  5. class OphanimHarvester {
  6. /**
  7. * Contains current year number
  8. *
  9. * @var int
  10. */
  11. protected $currentYear = 0;
  12. /**
  13. * Contains current month number without leading zero
  14. *
  15. * @var int
  16. */
  17. protected $currentMonth = 0;
  18. /**
  19. * Contains current day of month number
  20. *
  21. * @var int
  22. */
  23. protected $currentDay = 0;
  24. /**
  25. * Contains aggregated hosts in database abstraction layer
  26. *
  27. * @var object
  28. */
  29. protected $inDb = '';
  30. /**
  31. * Contains aggregated hosts out database abstraction layer
  32. *
  33. * @var object
  34. */
  35. protected $outDb = '';
  36. /**
  37. * Contains traffic summary table database abstration layer
  38. *
  39. * @var object
  40. */
  41. protected $traffDb = '';
  42. /**
  43. * Contains preloaded previous traffic summary data for current month as ip=>[dl/ul]
  44. *
  45. * @var array
  46. */
  47. protected $traffStats = array();
  48. /**
  49. * Contains loaded current run traffic as ip>in/out
  50. *
  51. * @var array
  52. */
  53. protected $currentTraff = array();
  54. /**
  55. * some predefined stuff here
  56. */
  57. const TABLE_HOST_IN = 'host_in';
  58. const TABLE_HOST_OUT = 'host_out';
  59. const TABLE_TRAFFSTAT = 'traffstat';
  60. public function __construct() {
  61. $this->setDates();
  62. $this->initDb();
  63. $this->loadTraffStats();
  64. }
  65. /**
  66. * Sets current datetime properties
  67. *
  68. * @return void
  69. */
  70. protected function setDates() {
  71. $this->currentYear = date("Y");
  72. $this->currentMonth = date("n");
  73. $this->currentDay = date("d");
  74. }
  75. /**
  76. * Inits database abstraction layers
  77. *
  78. * @return void
  79. */
  80. protected function initDb() {
  81. $this->inDb = new NyanORM(self::TABLE_HOST_IN);
  82. $this->outDb = new NyanORM(self::TABLE_HOST_OUT);
  83. $this->traffDb = new NyanORM(self::TABLE_TRAFFSTAT);
  84. }
  85. /**
  86. * Loads current month saved traffic stats
  87. *
  88. * @return void
  89. */
  90. protected function loadTraffStats() {
  91. $this->traffDb->where('year', '=', $this->currentYear);
  92. $this->traffDb->where('month', '=', $this->currentMonth);
  93. $this->traffStats = $this->traffDb->getAll('ip');
  94. }
  95. /**
  96. * Returns last run aggregates traffic stats as ip=>in/out
  97. *
  98. * @return array
  99. */
  100. protected function getLastTraff() {
  101. $tmp = array();
  102. $allOut = $this->outDb->getAll();
  103. $allIn = $this->inDb->getAll();
  104. if (!empty($allOut)) {
  105. foreach ($allOut as $io => $each) {
  106. $ip = $each['ip_dst'];
  107. if (isset($tmp[$ip]['in'])) {
  108. $tmp[$ip]['in'] += $each['bytes'];
  109. } else {
  110. $tmp[$ip]['in'] = $each['bytes'];
  111. $tmp[$ip]['out'] = 0;
  112. }
  113. }
  114. }
  115. if (!empty($allIn)) {
  116. foreach ($allIn as $io => $each) {
  117. $ip = $each['ip_src'];
  118. if (isset($tmp[$ip]['out'])) {
  119. $tmp[$ip]['out'] += $each['bytes'];
  120. } else {
  121. $tmp[$ip]['out'] = $each['bytes'];
  122. $tmp[$ip]['in'] = 0;
  123. }
  124. }
  125. }
  126. return ($tmp);
  127. }
  128. /**
  129. * Drops data from host in/out tables
  130. *
  131. * @return void
  132. */
  133. protected function flushLastTraff() {
  134. nr_query('TRUNCATE TABLE `' . self::TABLE_HOST_IN . '`');
  135. nr_query('TRUNCATE TABLE `' . self::TABLE_HOST_OUT . '`');
  136. }
  137. /**
  138. * Performs aggregated traffic processing, flushes raw data and updates summary database if required
  139. *
  140. * @return void
  141. */
  142. public function runTrafficProcessing() {
  143. $currentRunTraff = $this->getLastTraff();
  144. if (!empty($currentRunTraff)) {
  145. $this->flushLastTraff();
  146. foreach ($currentRunTraff as $eachIp => $eachTraff) {
  147. if (isset($this->traffStats[$eachIp])) {
  148. $savedData = $this->traffStats[$eachIp];
  149. $recordId = $savedData['id']; //current month
  150. $newDl = $savedData['dl'] + $eachTraff['in'];
  151. $newUl = $savedData['ul'] + $eachTraff['out'];
  152. //updating existing record
  153. if ($newDl != $savedData['dl'] or $newUl != $savedData['ul']) {
  154. $this->traffDb->data('dl', $newDl);
  155. $this->traffDb->data('ul', $newUl);
  156. $this->traffDb->where('id', '=', $recordId);
  157. $this->traffDb->save(true, true);
  158. }
  159. } else {
  160. //creating new record
  161. $this->traffDb->data('ip', $eachIp);
  162. $this->traffDb->data('month', $this->currentMonth);
  163. $this->traffDb->data('year', $this->currentYear);
  164. $this->traffDb->data('dl', $eachTraff['in']);
  165. $this->traffDb->data('ul', $eachTraff['out']);
  166. $this->traffDb->create();
  167. }
  168. }
  169. } else {
  170. show_warning('Nothing changed');
  171. }
  172. }
  173. /**
  174. * Returns traffic summary data for some period or current month
  175. *
  176. * @param string $year
  177. * @param string $month
  178. * @param string $ip
  179. *
  180. * @return array
  181. */
  182. public function getTraffCounters($year = '', $month = '', $ip = '') {
  183. $year = ubRouting::filters($year, 'int');
  184. $month = ubRouting::filters($month, 'int');
  185. $ip = ubRouting::filters($ip, 'mres');
  186. $result = array();
  187. if ($year and $month) {
  188. $this->traffDb->where('year', '=', $year);
  189. $this->traffDb->where('month', '=', $month);
  190. } else {
  191. $this->traffDb->where('year', '=', $this->currentYear);
  192. $this->traffDb->where('month', '=', $this->currentMonth);
  193. }
  194. if ($ip) {
  195. $this->traffDb->where('ip', '=', $ip);
  196. }
  197. $rawResult = $this->traffDb->getAll();
  198. if (!empty($rawResult)) {
  199. foreach ($rawResult as $io => $each) {
  200. $result[$each['ip']]['dl'] = $each['dl'];
  201. $result[$each['ip']]['ul'] = $each['ul'];
  202. }
  203. }
  204. return ($result);
  205. }
  206. }