munin_core.module 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Core Drupal instrumentation for Munin
  6. *
  7. * @author Frederic G. MARAND
  8. *
  9. * @copyright (c) 2011 Ouest Systèmes Informatiques
  10. *
  11. * Licensed under the General Public License version 2 or later.
  12. */
  13. /**
  14. * Implements hook_munin_api_info().
  15. *
  16. * @return
  17. * An array of Munin probes informations, index by probe name.
  18. */
  19. function munin_core_munin_api_info() {
  20. $int = array(
  21. '#graph_printf' => "'%d'",
  22. );
  23. $ret = array(
  24. '#title' => t('Drupal'),
  25. '#description' => t('Graphs about Drupal core'),
  26. 'munin_core' => array(
  27. '#title' => t('Core user-related statistics'),
  28. '#info' => t('Core statistics regarding users and sessions.'),
  29. '#graph_vlabel' => t('Anon/blocked (-) vs Logged/Active (+)'),
  30. 'anon_current' => $int + array(
  31. '#label' => t('Sessions, anon'),
  32. '#type' => MUNIN_API_GAUGE,
  33. '#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.'),
  34. '#graph' => 'no',
  35. ),
  36. 'user_current' => $int + array(
  37. '#label' => t('Sessions'),
  38. '#type' => MUNIN_API_GAUGE,
  39. '#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 dispaly as negative.'),
  40. '#negative' => 'anon_current',
  41. ),
  42. 'user_blocked_count' => $int + array(
  43. '#label' => t('Users, blocked'),
  44. '#type' => MUNIN_API_GAUGE,
  45. '#info' => t('The number of users with status = 0'),
  46. '#graph' => 'no',
  47. ),
  48. 'user_active_count' => $int + array(
  49. '#label' => t('Users'),
  50. '#type' => MUNIN_API_GAUGE,
  51. '#info' => t('The number of users. Blocked users display as negative.'),
  52. '#negative' => 'user_blocked_count',
  53. ),
  54. ),
  55. 'munin_core_content' => array(
  56. '#title' => t('Core content statistics'),
  57. '#info' => t('Information about nodes, comments and terms.'),
  58. '#graph_vlabel' => t('Entries unpub(-)/pub(+)'),
  59. 'node_count_unpub' => $int + array(
  60. '#label' => t('Nodes, unpublished'),
  61. '#type' => MUNIN_API_GAUGE,
  62. '#graph' => 'no',
  63. ),
  64. 'node_count' => $int + array(
  65. '#label' => t('Nodes'),
  66. '#info' => t('Unpublished nodes display as negative.'),
  67. '#type' => MUNIN_API_GAUGE,
  68. '#negative' => 'node_count_unpub',
  69. ),
  70. 'comment_count_unpub' => $int + array(
  71. '#label' => t('Comments, unpublished'),
  72. '#type' => MUNIN_API_GAUGE,
  73. '#graph' => 'no',
  74. ),
  75. 'comment_count' => $int + array(
  76. '#label' => t('Comments'),
  77. '#info' => t('Unpublished comments display as negative.'),
  78. '#type' => MUNIN_API_GAUGE,
  79. '#negative' => 'comment_count_unpub',
  80. ),
  81. ),
  82. 'munin_core_taxonomy' => array(
  83. '#title' => t('Core taxonomy statistics'),
  84. '#info' => t('Core statistics regarding taxonomy.'),
  85. '#graph_vlabel' => t('Vocabularies (-)/Terms (+)'),
  86. 'vocab_count' => $int + array(
  87. '#label' => t('Vocabularies'),
  88. '#info' => t('Vocabularies'),
  89. '#type' => MUNIN_API_GAUGE,
  90. ),
  91. 'term_count' => $int + array(
  92. '#label' => t('Terms'),
  93. '#info' => t('Total terms across all vocabularies'),
  94. '#type' => MUNIN_API_GAUGE,
  95. ),
  96. ),
  97. );
  98. return $ret;
  99. }
  100. /**
  101. * Implements hook_munin_api_fetch().
  102. *
  103. * TODO Reduce the number of queries by integrating the filters.
  104. */
  105. function munin_core_munin_api_fetch($graph_name) {
  106. switch ($graph_name) {
  107. case 'munin_core':
  108. $sq = 'SELECT COUNT(u.uid) cnt, u.status FROM {users} u WHERE u.uid != 0 GROUP BY 2';
  109. $result = db_query($sq);
  110. $users = array(0 => 0, 1 => 0);
  111. while ($row = db_fetch_object($result)) {
  112. $users[$row->status] = $row->cnt;
  113. }
  114. $ret['user_active_count'] = $users[1];
  115. $ret['user_blocked_count'] = $users[0];
  116. $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid != 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
  117. $ret['user_current'] = db_result(db_query($sq));
  118. $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid = 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
  119. $ret['anon_current'] = db_result(db_query($sq));
  120. break;
  121. case 'munin_core_content':
  122. $ret = array(
  123. 'node_count_unpub' => 0,
  124. 'node_count' => 0,
  125. 'comment_count_unpub' => 0,
  126. 'comment_count' => 0,
  127. );
  128. $sq = 'SELECT COUNT(*) cnt, n.status FROM {node} n GROUP BY n.status';
  129. // No db_rewrite_sql(): this is an administrative mechanism
  130. $q = db_query($sq);
  131. while ($o = db_fetch_object($q)) {
  132. switch ($o->status) {
  133. case 0:
  134. $ret['node_count_unpub'] = $o->cnt;
  135. break;
  136. case 1:
  137. $ret['node_count'] = $o->cnt;
  138. break;
  139. default:
  140. watchdog('munin_core', 'Nodes with status @status reported: @count', array(
  141. '@status' => $o->status,
  142. '@count' => $o->cnt,
  143. ), WATCHDOG_NOTICE);
  144. }
  145. }
  146. $sq = 'SELECT COUNT(*) cnt, c.status FROM {comments} c GROUP BY c.status';
  147. $q = db_query($sq);
  148. // No db_rewrite_sql(): this is an administrative mechanism
  149. while ($o = db_fetch_object($q)) {
  150. switch ($o->status) {
  151. case 1:
  152. $ret['comment_count_unpub'] = $o->cnt;
  153. break;
  154. case 0:
  155. $ret['comment_count'] = $o->cnt;
  156. break;
  157. default:
  158. watchdog('munin_core', 'Comments with status @status reported: @count', array(
  159. '@status' => $o->status,
  160. '@count' => $o->cnt,
  161. ), WATCHDOG_NOTICE);
  162. }
  163. }
  164. break;
  165. case 'munin_core_taxonomy':
  166. $sq = 'SELECT COUNT(*) FROM {term_data}';
  167. $ret['term_count'] = db_result(db_query($sq));
  168. $sq = 'SELECT COUNT(*) FROM {vocabulary}';
  169. $ret['vocab_count'] = db_result(db_query($sq));
  170. break;
  171. }
  172. return $ret;
  173. }