munin_core.module 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. 'anon_current' => $int + array(
  30. '#label' => t('Sessions, anon'),
  31. '#type' => MUNIN_API_GAUGE,
  32. '#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.'),
  33. '#graph' => 'no',
  34. ),
  35. 'user_current' => $int + array(
  36. '#label' => t('Sessions'),
  37. '#type' => MUNIN_API_GAUGE,
  38. '#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.'),
  39. '#negative' => 'anon_current',
  40. ),
  41. 'user_blocked_count' => $int + array(
  42. '#label' => t('Users, blocked'),
  43. '#type' => MUNIN_API_GAUGE,
  44. '#info' => t('The number of users with status = 0'),
  45. '#graph' => 'no',
  46. ),
  47. 'user_active_count' => $int + array(
  48. '#label' => t('Users'),
  49. '#type' => MUNIN_API_GAUGE,
  50. '#info' => t('The number of users. Blocked users display as negative.'),
  51. '#negative' => 'user_blocked_count',
  52. ),
  53. ),
  54. 'munin_core_content' => array(
  55. '#title' => t('Core content statistics'),
  56. '#info' => t('Information about nodes, comments and terms.'),
  57. '#graph_vlabel' => t('Number of entries'),
  58. 'node_count_unpub' => $int + array(
  59. '#label' => t('Nodes, unpublished'),
  60. '#type' => MUNIN_API_GAUGE,
  61. '#graph' => 'no',
  62. ),
  63. 'node_count' => $int + array(
  64. '#label' => t('Nodes'),
  65. '#info' => t('Unpublished nodes display as negative.'),
  66. '#type' => MUNIN_API_GAUGE,
  67. '#negative' => 'node_count_unpub',
  68. ),
  69. 'comment_count_unpub' => $int + array(
  70. '#label' => t('Comments, unpublished'),
  71. '#type' => MUNIN_API_GAUGE,
  72. '#graph' => 'no',
  73. ),
  74. 'comment_count' => $int + array(
  75. '#label' => t('Comments'),
  76. '#info' => t('Unpublished comments display as negative.'),
  77. '#type' => MUNIN_API_GAUGE,
  78. '#negative' => 'comment_count_unpub',
  79. ),
  80. 'term_count' => $int + array(
  81. '#label' => t('Terms'),
  82. '#type' => MUNIN_API_GAUGE,
  83. ),
  84. ),
  85. );
  86. return $ret;
  87. }
  88. /**
  89. * Implements hook_munin_api_fetch().
  90. *
  91. * TODO Reduce the number of queries by integrating the filters.
  92. */
  93. function munin_core_munin_api_fetch($graph_name) {
  94. switch ($graph_name) {
  95. case 'munin_core':
  96. $sq = 'SELECT COUNT(u.uid) cnt, u.status FROM {users} u GROUP BY 2';
  97. $result = db_query($sq);
  98. while ($row = db_fetch_object($result)) {
  99. $users[$row->status] = $row->cnt;
  100. }
  101. $ret['user_active_count'] = $users[1];
  102. $ret['user_blocked_count'] = $users[0];
  103. $ret['user_count'] = $users[0] + $users[1];
  104. $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid != 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
  105. $ret['user_current'] = db_result(db_query($sq));
  106. $sq = 'SELECT COUNT(*) cnt FROM {sessions} s WHERE s.uid = 0 AND s.timestamp >= UNIX_TIMESTAMP() - 5*60';
  107. $ret['anon_current'] = db_result(db_query($sq));
  108. break;
  109. case 'munin_core_content':
  110. $ret = array(
  111. 'node_count_unpub' => 0,
  112. 'node_count' => 0,
  113. 'comment_count_unpub' => 0,
  114. 'comment_count' => 0,
  115. );
  116. $sq = 'SELECT COUNT(*) cnt, n.status FROM {node} n GROUP BY n.status';
  117. // No db_rewrite_sql(): this is an administrative mechanism
  118. $q = db_query($sq);
  119. while ($o = db_fetch_object($q)) {
  120. switch ($o->status) {
  121. case 0:
  122. $ret['node_count_unpub'] = $o->cnt;
  123. break;
  124. case 1:
  125. $ret['node_count'] = $o->cnt;
  126. break;
  127. default:
  128. watchdog('munin_core', 'Nodes with status @status reported: @count', array(
  129. '@status' => $o->status,
  130. '@count' => $o->cnt,
  131. ), WATCHDOG_NOTICE);
  132. }
  133. }
  134. $sq = 'SELECT COUNT(*) cnt, c.status FROM {comments} c GROUP BY c.status';
  135. $q = db_query($sq);
  136. // No db_rewrite_sql(): this is an administrative mechanism
  137. while ($o = db_fetch_object($q)) {
  138. switch ($o->status) {
  139. case 1:
  140. $ret['comment_count_unpub'] = $o->cnt;
  141. break;
  142. case 0:
  143. $ret['comment_count'] = $o->cnt;
  144. break;
  145. default:
  146. watchdog('munin_core', 'Comments with status @status reported: @count', array(
  147. '@status' => $o->status,
  148. '@count' => $o->cnt,
  149. ), WATCHDOG_NOTICE);
  150. }
  151. }
  152. $sq = 'SELECT COUNT(*) FROM {term_data}';
  153. $ret['term_count'] = db_result(db_query($sq));
  154. }
  155. return $ret;
  156. }