]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuemonitor.php
Always naming it 'plugin' is not good, it can easily confuse. So better name it
[quix0rs-gnu-social.git] / lib / queuemonitor.php
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
30 class QueueMonitor
31 {
32     protected $monSocket = null;
33
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     /**
48      * Send thread state update to the monitoring server, if configured.
49      *
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)
53      */
54     public function logState($threadId, $state, $substate='')
55     {
56         $this->ping(array('thread_id' => $threadId,
57                           'state' => $state,
58                           'substate' => $substate,
59                           'ts' => microtime(true)));
60     }
61
62     /**
63      * General call to the monitoring server
64      */
65     protected function ping($data)
66     {
67         $target = common_config('queue', 'monitor');
68         if (empty($target)) {
69             return;
70         }
71
72         $data = $this->prepMonitorData($data);
73
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);
78         } else {
79             common_log(LOG_ERR, __METHOD__ . ' unknown monitor target type ' . $target);
80         }
81     }
82
83     protected function pingUdp($target, $data)
84     {
85         if (!$this->monSocket) {
86             $this->monSocket = stream_socket_client($target, $errno, $errstr);
87         }
88         if ($this->monSocket) {
89             $post = http_build_query($data, '', '&');
90             stream_socket_sendto($this->monSocket, $post);
91         } else {
92             common_log(LOG_ERR, __METHOD__ . " UDP logging fail: $errstr");
93         }
94     }
95
96     protected function pingHttp($target, $data)
97     {
98         $client = new HTTPClient();
99         $result = $client->post($target, array(), $data);
100         
101         if (!$result->isOk()) {
102             common_log(LOG_ERR, __METHOD__ . ' HTTP ' . $result->getStatus() .
103                                 ': ' . $result->getBody());
104         }
105     }
106
107     protected function prepMonitorData($data)
108     {
109         #asort($data);
110         #$macdata = http_build_query($data, '', '&');
111         #$key = 'This is a nice old key';
112         #$data['hmac'] = hash_hmac('sha256', $macdata, $key);
113         return $data;
114     }
115
116 }