]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/snapshot.php
Rebuilt HTTPClient class as an extension of PEAR HTTP_Request2 package, adding redire...
[quix0rs-gnu-social.git] / lib / snapshot.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * A snapshot of site stats that can report itself to headquarters
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  Stats
23  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * A snapshot of site stats that can report itself to headquarters
36  *
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
39  * at status.net.)
40  *
41  * It can either be called from a cron job, or run occasionally by the
42  * Web site.
43  *
44  * @category Stats
45  * @package  StatusNet
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/
49  *
50  */
51
52 class Snapshot
53 {
54     var $stats = null;
55
56     /**
57      * Constructor for a snapshot
58      */
59
60     function __construct()
61     {
62     }
63
64     /**
65      * Static function for reporting statistics
66      *
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.
70      *
71      * @return void
72      */
73
74     static function check()
75     {
76         switch (common_config('snapshot', 'run')) {
77         case 'web':
78             // skip if we're not running on the Web.
79             if (!isset($_SERVER) || !array_key_exists('REQUEST_METHOD', $_SERVER)) {
80                 break;
81             }
82             // Run once every frequency hits
83             // XXX: do frequency by time (once a week, etc.) rather than
84             // hits
85             if (rand() % common_config('snapshot', 'frequency') == 0) {
86                 $snapshot = new Snapshot();
87                 $snapshot->take();
88                 $snapshot->report();
89             }
90             break;
91         case 'cron':
92             // skip if we're running on the Web
93             if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
94                 break;
95             }
96             common_log(LOG_INFO, 'Running snapshot from cron job');
97             // We're running from the command line; assume
98
99             $snapshot = new Snapshot();
100             $snapshot->take();
101             common_log(LOG_INFO, count($snapshot->stats) . " statistics being uploaded.");
102             $snapshot->report();
103
104             break;
105         case 'never':
106             break;
107         default:
108             common_log(LOG_WARNING, "Unrecognized value for snapshot run config.");
109         }
110     }
111
112     /**
113      * Take a snapshot of the server
114      *
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.
118      *
119      * @return void
120      */
121
122     function take()
123     {
124         $this->stats = array();
125
126         // Some basic identification stuff
127
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();
132
133         // non-identifying stats on various tables. Primary
134         // interest is size and rate of activity of service.
135
136         $tables = array('user',
137                         'notice',
138                         'subscription',
139                         'remote_profile',
140                         'user_group');
141
142         foreach ($tables as $table) {
143             $this->tableStats($table);
144         }
145
146         // stats on some important config options
147
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');
160
161     }
162
163     /**
164      * Reports statistics to headquarters
165      *
166      * Posts statistics to a reporting server.
167      *
168      * @return void
169      */
170
171     function report()
172     {
173         // XXX: Use OICU2 and OAuth to make authorized requests
174
175         $reporturl = common_config('snapshot', 'reporturl');
176         $request = HTTPClient::start();
177         $request->post($reporturl, null, $this->stats);
178     }
179
180     /**
181      * Updates statistics for a single table
182      *
183      * Determines the size of a table and its oldest and newest rows.
184      * Goal here is to see how active a site is. Note that it
185      * fills up the instance stats variable.
186      *
187      * @param string $table name of table to check
188      *
189      * @return void
190      */
191
192     function tableStats($table)
193     {
194         $inst = DB_DataObject::factory($table);
195
196         $inst->selectAdd();
197         $inst->selectAdd('count(*) as cnt, '.
198                          'min(created) as first, '.
199                          'max(created) as last');
200
201         if ($inst->find(true)) {
202             $this->stats[$table.'count'] = $inst->cnt;
203             $this->stats[$table.'first'] = $inst->first;
204             $this->stats[$table.'last']  = $inst->last;
205         }
206
207         $inst->free();
208         unset($inst);
209     }
210 }