munin_core.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 'term_count' => $int + array(
  82. '#label' => t('Terms'),
  83. '#type' => MUNIN_API_GAUGE,
  84. ),
  85. ),
  86. );
  87. return $ret;
  88. }
  89. /**
  90. * Implements hook_munin_api_fetch().
  91. *
  92. * TODO Reduce the number of queries by integrating the filters.
  93. */
  94. function munin_core_munin_api_fetch($graph_name) {
  95. switch ($graph_name) {
  96. case 'munin_core':
  97. $sq = 'SELECT COUNT(u.uid) cnt, u.status FROM {users} u WHERE u.uid != 0 GROUP BY 2';
  98. $result = db_query($sq);
  99. $users = array(0 => 0, 1 => 0);
  100. while ($row = db_fetch_object($result)) {
  101. $users[$row->status] = $row->cnt;
  102. }
  103. $ret['user_active_count'] = $users[1];
  104. $ret['user_blocked_count'] = $users[0];
  105. $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid != 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
  106. $ret['user_current'] = db_result(db_query($sq));
  107. $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid = 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
  108. $ret['anon_current'] = db_result(db_query($sq));
  109. break;
  110. case 'munin_core_content':
  111. $ret = array(
  112. 'node_count_unpub' => 0,
  113. 'node_count' => 0,
  114. 'comment_count_unpub' => 0,
  115. 'comment_count' => 0,
  116. );
  117. $sq = 'SELECT COUNT(*) cnt, n.status FROM {node} n GROUP BY n.status';
  118. // No db_rewrite_sql(): this is an administrative mechanism
  119. $q = db_query($sq);
  120. while ($o = db_fetch_object($q)) {
  121. switch ($o->status) {
  122. case 0:
  123. $ret['node_count_unpub'] = $o->cnt;
  124. break;
  125. case 1:
  126. $ret['node_count'] = $o->cnt;
  127. break;
  128. default:
  129. watchdog('munin_core', 'Nodes with status @status reported: @count', array(
  130. '@status' => $o->status,
  131. '@count' => $o->cnt,
  132. ), WATCHDOG_NOTICE);
  133. }
  134. }
  135. $sq = 'SELECT COUNT(*) cnt, c.status FROM {comments} c GROUP BY c.status';
  136. $q = db_query($sq);
  137. // No db_rewrite_sql(): this is an administrative mechanism
  138. while ($o = db_fetch_object($q)) {
  139. switch ($o->status) {
  140. case 1:
  141. $ret['comment_count_unpub'] = $o->cnt;
  142. break;
  143. case 0:
  144. $ret['comment_count'] = $o->cnt;
  145. break;
  146. default:
  147. watchdog('munin_core', 'Comments with status @status reported: @count', array(
  148. '@status' => $o->status,
  149. '@count' => $o->cnt,
  150. ), WATCHDOG_NOTICE);
  151. }
  152. }
  153. $sq = 'SELECT COUNT(*) FROM {term_data}';
  154. $ret['term_count'] = db_result(db_query($sq));
  155. }
  156. return $ret;
  157. }