queuemonitor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Monitoring output helper for IoMaster and IoManager/QueueManager
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category QueueManager
  23. * @package StatusNet
  24. * @author Brion Vibber <brion@status.net>
  25. * @copyright 2010 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. class QueueMonitor
  30. {
  31. protected $monSocket = null;
  32. /**
  33. * Increment monitoring statistics for a given counter, if configured.
  34. * Only explicitly listed thread/site/queue owners will be incremented.
  35. *
  36. * @param string $key counter name
  37. * @param array $owners list of owner keys like 'queue:xmpp' or 'site:stat01'
  38. */
  39. public function stats($key, $owners=array())
  40. {
  41. $this->ping(array('counter' => $key,
  42. 'owners' => $owners));
  43. }
  44. /**
  45. * Send thread state update to the monitoring server, if configured.
  46. *
  47. * @param string $thread ID (eg 'generic.1')
  48. * @param string $state ('init', 'queue', 'shutdown' etc)
  49. * @param string $substate (optional, eg queue name 'omb' 'sms' etc)
  50. */
  51. public function logState($threadId, $state, $substate='')
  52. {
  53. $this->ping(array('thread_id' => $threadId,
  54. 'state' => $state,
  55. 'substate' => $substate,
  56. 'ts' => microtime(true)));
  57. }
  58. /**
  59. * General call to the monitoring server
  60. */
  61. protected function ping($data)
  62. {
  63. $target = common_config('queue', 'monitor');
  64. if (empty($target)) {
  65. return;
  66. }
  67. $data = $this->prepMonitorData($data);
  68. if (substr($target, 0, 4) == 'udp:') {
  69. $this->pingUdp($target, $data);
  70. } else if (substr($target, 0, 5) == 'http:') {
  71. $this->pingHttp($target, $data);
  72. } else {
  73. common_log(LOG_ERR, __METHOD__ . ' unknown monitor target type ' . $target);
  74. }
  75. }
  76. protected function pingUdp($target, $data)
  77. {
  78. if (!$this->monSocket) {
  79. $this->monSocket = stream_socket_client($target, $errno, $errstr);
  80. }
  81. if ($this->monSocket) {
  82. $post = http_build_query($data, '', '&');
  83. stream_socket_sendto($this->monSocket, $post);
  84. } else {
  85. common_log(LOG_ERR, __METHOD__ . " UDP logging fail: $errstr");
  86. }
  87. }
  88. protected function pingHttp($target, $data)
  89. {
  90. $client = new HTTPClient();
  91. $result = $client->post($target, array(), $data);
  92. if (!$result->isOk()) {
  93. common_log(LOG_ERR, __METHOD__ . ' HTTP ' . $result->getStatus() .
  94. ': ' . $result->getBody());
  95. }
  96. }
  97. protected function prepMonitorData($data)
  98. {
  99. #asort($data);
  100. #$macdata = http_build_query($data, '', '&');
  101. #$key = 'This is a nice old key';
  102. #$data['hmac'] = hash_hmac('sha256', $macdata, $key);
  103. return $data;
  104. }
  105. }