queuemonitor.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. public $widgetOpts;
  32. public $scoped;
  33. protected $monSocket = null;
  34. /**
  35. * Increment monitoring statistics for a given counter, if configured.
  36. * Only explicitly listed thread/site/queue owners will be incremented.
  37. *
  38. * @param string $key counter name
  39. * @param array $owners list of owner keys like 'queue:xmpp' or 'site:stat01'
  40. */
  41. public function stats($key, $owners=array())
  42. {
  43. $this->ping(array('counter' => $key,
  44. 'owners' => $owners));
  45. }
  46. /**
  47. * Send thread state update to the monitoring server, if configured.
  48. *
  49. * @param string $thread ID (eg 'generic.1')
  50. * @param string $state ('init', 'queue', 'shutdown' etc)
  51. * @param string $substate (optional, eg queue name 'omb' 'sms' etc)
  52. */
  53. public function logState($threadId, $state, $substate='')
  54. {
  55. $this->ping(array('thread_id' => $threadId,
  56. 'state' => $state,
  57. 'substate' => $substate,
  58. 'ts' => microtime(true)));
  59. }
  60. /**
  61. * General call to the monitoring server
  62. */
  63. protected function ping($data)
  64. {
  65. $target = common_config('queue', 'monitor');
  66. if (empty($target)) {
  67. return;
  68. }
  69. $data = $this->prepMonitorData($data);
  70. if (substr($target, 0, 4) == 'udp:') {
  71. $this->pingUdp($target, $data);
  72. } else if (substr($target, 0, 5) == 'http:') {
  73. $this->pingHttp($target, $data);
  74. } else {
  75. common_log(LOG_ERR, __METHOD__ . ' unknown monitor target type ' . $target);
  76. }
  77. }
  78. protected function pingUdp($target, $data)
  79. {
  80. if (!$this->monSocket) {
  81. $this->monSocket = stream_socket_client($target, $errno, $errstr);
  82. }
  83. if ($this->monSocket) {
  84. $post = http_build_query($data, '', '&');
  85. stream_socket_sendto($this->monSocket, $post);
  86. } else {
  87. common_log(LOG_ERR, __METHOD__ . " UDP logging fail: $errstr");
  88. }
  89. }
  90. protected function pingHttp($target, $data)
  91. {
  92. $client = new HTTPClient();
  93. try {
  94. $result = $client->post($target, array(), $data);
  95. if (!$result->isOk()) {
  96. common_log(LOG_ERR, __METHOD__ . ' HTTP ' . $result->getStatus() . ': ' . $result->getBody());
  97. }
  98. } catch (NoHttpResponseException $e) {
  99. common_log(LOG_ERR, __METHOD__ . ':'.$e->getMessage());
  100. } catch (HTTP_Request2_Exception $e) {
  101. common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
  102. }
  103. }
  104. protected function prepMonitorData($data)
  105. {
  106. #asort($data);
  107. #$macdata = http_build_query($data, '', '&');
  108. #$key = 'This is a nice old key';
  109. #$data['hmac'] = hash_hmac('sha256', $macdata, $key);
  110. return $data;
  111. }
  112. }