123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- // $Id$
- /**
- * @file
- * Core Drupal instrumentation for Munin
- *
- * @author Frederic G. MARAND
- *
- * @copyright (c) 2011 Ouest Systèmes Informatiques
- *
- * Licensed under the General Public License version 2 or later.
- */
- /**
- * Implements hook_munin_api_info().
- *
- * @return
- * An array of Munin probes informations, index by probe name.
- */
- function munin_core_munin_api_info() {
- $int = array(
- '#graph_printf' => "'%d'",
- );
- /**
- * Ignore anon and auth roles: no user carries them in users_roles anyway.
- */
- $sq = 'SELECT r.rid, r.name FROM {role} r WHERE r.rid > 2 ORDER BY 2';
- $q = db_query($sq);
- $roles = array();
- while ($o = db_fetch_object($q)) {
- $roles[$o->rid] = $o->name;
- }
- $ret = array(
- '#title' => t('Drupal'),
- '#description' => t('Graphs about Drupal core'),
- 'munin_core' => array(
- '#title' => t('Core user-related statistics'),
- '#info' => t('Core statistics regarding users and sessions.'),
- '#graph_vlabel' => t('Anon/blocked (-) vs Logged/Active (+)'),
- 'anon_current' => $int + array(
- '#label' => t('Sessions, anon'),
- '#type' => MUNIN_API_GAUGE,
- '#info' => t('The number of anonymous sessions not older than 5 minutes. Only meaningful if you are not using an alternate sessions implementation not using the sessions table.'),
- '#graph' => 'no',
- ),
- 'user_current' => $int + array(
- '#label' => t('Sessions'),
- '#type' => MUNIN_API_GAUGE,
- '#info' => t('The number of sessions not older than 5 minutes. Only meaningful if you are not using an alternate sessions implementation not using the sessions table. Anonymous sessions display as negative.'),
- '#negative' => 'anon_current',
- ),
- 'user_blocked_count' => $int + array(
- '#label' => t('Users, blocked'),
- '#type' => MUNIN_API_GAUGE,
- '#info' => t('The number of users with status = 0'),
- '#graph' => 'no',
- ),
- 'user_active_count' => $int + array(
- '#label' => t('Users'),
- '#type' => MUNIN_API_GAUGE,
- '#info' => t('The number of users. Blocked users display as negative.'),
- '#negative' => 'user_blocked_count',
- ),
- ),
- 'munin_core_roles' => array(
- '#title' => t('Core role statistics'),
- '#info' => t('Information about users roles.'),
- '#graph_vlabel' => t('Users by roles'),
- ),
- 'munin_core_content' => array(
- '#title' => t('Core content statistics'),
- '#info' => t('Information about nodes, comments and terms.'),
- '#graph_vlabel' => t('Entries unpub(-)/pub(+)'),
- 'node_count_unpub' => $int + array(
- '#label' => t('Nodes, unpublished'),
- '#type' => MUNIN_API_GAUGE,
- '#graph' => 'no',
- ),
- 'node_count' => $int + array(
- '#label' => t('Nodes'),
- '#info' => t('Unpublished nodes display as negative.'),
- '#type' => MUNIN_API_GAUGE,
- '#negative' => 'node_count_unpub',
- ),
- 'comment_count_unpub' => $int + array(
- '#label' => t('Comments, unpublished'),
- '#type' => MUNIN_API_GAUGE,
- '#graph' => 'no',
- ),
- 'comment_count' => $int + array(
- '#label' => t('Comments'),
- '#info' => t('Unpublished comments display as negative.'),
- '#type' => MUNIN_API_GAUGE,
- '#negative' => 'comment_count_unpub',
- ),
- ),
- 'munin_core_taxonomy' => array(
- '#title' => t('Core taxonomy statistics'),
- '#info' => t('Core statistics regarding taxonomy.'),
- '#graph_vlabel' => t('Vocabularies (-)/Terms (+)'),
- 'vocab_count' => $int + array(
- '#label' => t('Vocabularies'),
- '#info' => t('Vocabularies'),
- '#type' => MUNIN_API_GAUGE,
- ),
- 'term_count' => $int + array(
- '#label' => t('Terms'),
- '#info' => t('Total terms across all vocabularies'),
- '#type' => MUNIN_API_GAUGE,
- ),
- ),
- );
- foreach ($roles as $rid => $name) {
- $machine_name = str_replace(' ', '_', $name);
- $ret['munin_core_roles']['role_'. $machine_name] = $int + array(
- '#label' => $name,
- '#info' => t('Number of users with the @role role', array('@role' => $name)),
- '#type' => MUNIN_API_GAUGE,
- );
- }
- variable_set('munin_core_roles', $roles);
- return $ret;
- }
- /**
- * Implements hook_munin_api_fetch().
- *
- * TODO Reduce the number of queries by integrating the filters.
- */
- function munin_core_munin_api_fetch($graph_name) {
- switch ($graph_name) {
- case 'munin_core':
- $sq = 'SELECT COUNT(u.uid) cnt, u.status FROM {users} u WHERE u.uid <> 0 GROUP BY 2';
- $result = db_query($sq);
- $users = array(0 => 0, 1 => 0);
- while ($row = db_fetch_object($result)) {
- $users[$row->status] = $row->cnt;
- }
- $ret['user_active_count'] = $users[1];
- $ret['user_blocked_count'] = $users[0];
- $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid <> 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
- $ret['user_current'] = db_result(db_query($sq));
- $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid = 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
- $ret['anon_current'] = db_result(db_query($sq));
- break;
- case 'munin_core_roles':
- $roles = variable_get('munin_core_roles', array());
- $ph = db_placeholders($roles);
- $sq = <<<EOT
- SELECT COUNT(ur.uid) cnt, r.rid, r.name
- FROM {role} r
- LEFT JOIN {users_roles} ur ON r.rid = ur.rid
- WHERE r.rid IN ($ph)
- GROUP BY 2, 3
- EOT;
- $ret = array();
- $q = db_query($sq, array_keys($roles));
- while ($o = db_fetch_object($q)) {
- $name = str_replace(' ', '_', $o->name);
- $ret['role_'. $name] = empty($o->cnt) ? 0 : $o->cnt; // Can be NULL
- }
- break;
- case 'munin_core_content':
- $ret = array(
- 'node_count_unpub' => 0,
- 'node_count' => 0,
- 'comment_count_unpub' => 0,
- 'comment_count' => 0,
- );
- $sq = 'SELECT COUNT(*) cnt, n.status FROM {node} n GROUP BY n.status';
- // No db_rewrite_sql(): this is an administrative mechanism
- $q = db_query($sq);
- while ($o = db_fetch_object($q)) {
- switch ($o->status) {
- case 0:
- $ret['node_count_unpub'] = $o->cnt;
- break;
- case 1:
- $ret['node_count'] = $o->cnt;
- break;
- default:
- watchdog('munin_core', 'Nodes with status @status reported: @count', array(
- '@status' => $o->status,
- '@count' => $o->cnt,
- ), WATCHDOG_NOTICE);
- }
- }
- $sq = 'SELECT COUNT(*) cnt, c.status FROM {comments} c GROUP BY c.status';
- $q = db_query($sq);
- // No db_rewrite_sql(): this is an administrative mechanism
- while ($o = db_fetch_object($q)) {
- switch ($o->status) {
- case 1:
- $ret['comment_count_unpub'] = $o->cnt;
- break;
- case 0:
- $ret['comment_count'] = $o->cnt;
- break;
- default:
- watchdog('munin_core', 'Comments with status @status reported: @count', array(
- '@status' => $o->status,
- '@count' => $o->cnt,
- ), WATCHDOG_NOTICE);
- }
- }
- break;
- case 'munin_core_taxonomy':
- $sq = 'SELECT COUNT(*) FROM {term_data}';
- $ret['term_count'] = db_result(db_query($sq));
- $sq = 'SELECT COUNT(*) FROM {vocabulary}';
- $ret['vocab_count'] = db_result(db_query($sq));
- break;
- }
- return $ret;
- }
|