3 * StatusNet, the distributed open-source microblogging tool
5 * A snapshot of site stats that can report itself to headquarters
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/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * A snapshot of site stats that can report itself to headquarters
37 * This class will collect statistics on the site and report them to
38 * a statistics server of the admin's choice. (Default is the big one
41 * It can either be called from a cron job, or run occasionally by the
46 * @author Evan Prodromou <evan@status.net>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://status.net/
57 * Constructor for a snapshot
60 function __construct()
65 * Static function for reporting statistics
67 * This function checks whether it should report statistics, based on
68 * the current configuation settings. If it should, it creates a new
69 * Snapshot object, takes a snapshot, and reports it to headquarters.
74 static function check()
76 switch (common_config('snapshot', 'run')) {
78 // skip if we're not running on the Web.
79 if (!isset($_SERVER) || !array_key_exists('REQUEST_METHOD', $_SERVER)) {
82 // Run once every frequency hits
83 // XXX: do frequency by time (once a week, etc.) rather than
85 if (rand() % common_config('snapshot', 'frequency') == 0) {
86 $snapshot = new Snapshot();
92 // skip if we're running on the Web
93 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
96 common_log(LOG_INFO, 'Running snapshot from cron job');
97 // We're running from the command line; assume
99 $snapshot = new Snapshot();
101 common_log(LOG_INFO, count($snapshot->stats) . " statistics being uploaded.");
108 common_log(LOG_WARNING, "Unrecognized value for snapshot run config.");
113 * Take a snapshot of the server
115 * Builds an array of statistical and configuration data based
116 * on the local database and config files. We avoid grabbing any
117 * information that could be personal or private.
124 $this->stats = array();
126 // Some basic identification stuff
128 $this->stats['version'] = STATUSNET_VERSION;
129 $this->stats['phpversion'] = phpversion();
130 $this->stats['name'] = common_config('site', 'name');
131 $this->stats['root'] = common_root_url();
133 // non-identifying stats on various tables. Primary
134 // interest is size and rate of activity of service.
136 $tables = array('user',
142 foreach ($tables as $table) {
143 $this->tableStats($table);
146 // stats on some important config options
148 $this->stats['theme'] = common_config('site', 'theme');
149 $this->stats['dbtype'] = common_config('db', 'type');
150 $this->stats['xmpp'] = common_config('xmpp', 'enabled');
151 $this->stats['inboxes'] = common_config('inboxes', 'enabled');
152 $this->stats['queue'] = common_config('queue', 'enabled');
153 $this->stats['license'] = common_config('license', 'url');
154 $this->stats['fancy'] = common_config('site', 'fancy');
155 $this->stats['private'] = common_config('site', 'private');
156 $this->stats['closed'] = common_config('site', 'closed');
157 $this->stats['memcached'] = common_config('memcached', 'enabled');
158 $this->stats['language'] = common_config('site', 'language');
159 $this->stats['timezone'] = common_config('site', 'timezone');
164 * Reports statistics to headquarters
166 * Posts statistics to a reporting server.
173 // XXX: Use OICU2 and OAuth to make authorized requests
175 $postdata = http_build_query($this->stats);
181 'header' => 'Content-type: '.
182 'application/x-www-form-urlencoded',
183 'content' => $postdata,
184 'user_agent' => 'StatusNet/'.STATUSNET_VERSION
188 $context = stream_context_create($opts);
190 $reporturl = common_config('snapshot', 'reporturl');
192 $result = @file_get_contents($reporturl, false, $context);
198 * Updates statistics for a single table
200 * Determines the size of a table and its oldest and newest rows.
201 * Goal here is to see how active a site is. Note that it
202 * fills up the instance stats variable.
204 * @param string $table name of table to check
209 function tableStats($table)
211 $inst = DB_DataObject::factory($table);
214 $inst->selectAdd('count(*) as cnt, '.
215 'min(created) as first, '.
216 'max(created) as last');
218 if ($inst->find(true)) {
219 $this->stats[$table.'count'] = $inst->cnt;
220 $this->stats[$table.'first'] = $inst->first;
221 $this->stats[$table.'last'] = $inst->last;