3 * StatusNet, the distributed open-source microblogging tool
5 * Monitoring output helper for IoMaster and IoManager/QueueManager
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.
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.
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/>.
22 * @category QueueManager
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/
32 protected $monSocket = null;
35 * Increment monitoring statistics for a given counter, if configured.
36 * Only explicitly listed thread/site/queue owners will be incremented.
38 * @param string $key counter name
39 * @param array $owners list of owner keys like 'queue:xmpp' or 'site:stat01'
41 public function stats($key, $owners=array())
43 $this->ping(array('counter' => $key,
44 'owners' => $owners));
48 * Send thread state update to the monitoring server, if configured.
50 * @param string $thread ID (eg 'generic.1')
51 * @param string $state ('init', 'queue', 'shutdown' etc)
52 * @param string $substate (optional, eg queue name 'omb' 'sms' etc)
54 public function logState($threadId, $state, $substate='')
56 $this->ping(array('thread_id' => $threadId,
58 'substate' => $substate,
59 'ts' => microtime(true)));
63 * General call to the monitoring server
65 protected function ping($data)
67 $target = common_config('queue', 'monitor');
72 $data = $this->prepMonitorData($data);
74 if (substr($target, 0, 4) == 'udp:') {
75 $this->pingUdp($target, $data);
76 } else if (substr($target, 0, 5) == 'http:') {
77 $this->pingHttp($target, $data);
79 common_log(LOG_ERR, __METHOD__ . ' unknown monitor target type ' . $target);
83 protected function pingUdp($target, $data)
85 if (!$this->monSocket) {
86 $this->monSocket = stream_socket_client($target, $errno, $errstr);
88 if ($this->monSocket) {
89 $post = http_build_query($data, '', '&');
90 stream_socket_sendto($this->monSocket, $post);
92 common_log(LOG_ERR, __METHOD__ . " UDP logging fail: $errstr");
96 protected function pingHttp($target, $data)
98 $client = new HTTPClient();
99 $result = $client->post($target, array(), $data);
101 if (!$result->isOk()) {
102 common_log(LOG_ERR, __METHOD__ . ' HTTP ' . $result->getStatus() .
103 ': ' . $result->getBody());
107 protected function prepMonitorData($data)
110 #$macdata = http_build_query($data, '', '&');
111 #$key = 'This is a nice old key';
112 #$data['hmac'] = hash_hmac('sha256', $macdata, $key);