plug_prometheus.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // reference https://github.com/WP-API/WP-API/blob/develop/lib/infrastructure/class-wp-rest-server.php
  3. // serve_request() function
  4. if (!class_exists("plug_prometheus")) {
  5. class plug_prometheus {
  6. function __construct($ns) {
  7. $this->title = __("prometheus", "dagsopt");
  8. $this->pluginname = $ns->pluginname;
  9. $this->file = $ns->file;
  10. $this->ns = $ns;
  11. }
  12. function start() {
  13. add_filter('rest_pre_serve_request', array(&$this, 'multiformat_rest_pre_serve_request'), 10, 4);
  14. add_action('rest_api_init', function () {
  15. register_rest_route('metrics', '/', array(
  16. 'methods' => 'GET',
  17. 'permission_callback' => '__return_true',
  18. 'callback' => array(&$this, 'my_awesome_func'),
  19. ));
  20. });
  21. add_filter('rest_authentication_errors', function ($result) {
  22. if (!empty($result)) {
  23. return $result;
  24. }
  25. if (!(is_user_logged_in() && (current_user_can('edit_posts'))) && !($_SERVER['REQUEST_URI'] == "/wp-json/metrics")) {
  26. return new WP_Error('rest_not_allowed', 'Morten - is that you? - You are not allowed here ;)', array('status' => 401));
  27. }
  28. return $result;
  29. });
  30. }
  31. function help() {
  32. ?>
  33. add /wp-json/metrics
  34. <?php
  35. }
  36. function admin_line() {
  37. ?>
  38. <?php
  39. }
  40. function my_awesome_func($data) {
  41. return "";
  42. }
  43. function multiformat_rest_pre_serve_request($served, $result, $request, $server) {
  44. // assumes 'format' was passed into the intial API route
  45. // example: https://baconipsum.com/wp-json/baconipsum/test-response?format=text
  46. // the default JSON response will be handled automatically by WP-API
  47. if ($request->get_route() == "/metrics") {
  48. header('Content-Type: text/plain; charset=' . get_option('blog_charset'));
  49. http_response_code(200);
  50. $metrics = $this->get_wordpress_metrics();
  51. echo $metrics;
  52. $served = true; // tells the WP-API that we sent the response already
  53. }
  54. return $served;
  55. }
  56. function get_wordpress_metrics() {
  57. global $wpdb, $table_prefix;
  58. $result = '';
  59. $users = count_users();
  60. $result .= "# HELP wp_users_total Total number of users.\n";
  61. $result .= "# TYPE wp_users_total counter\n";
  62. $result .= 'wp_users_total{host="' . get_site_url() . '"} ' . $users['total_users'] . "\n";
  63. $posts = wp_count_posts();
  64. $n_posts_pub = $posts->publish;
  65. $n_posts_dra = $posts->draft;
  66. $result .= "# HELP wp_posts_total Total number of posts published.\n";
  67. $result .= "# TYPE wp_posts_total counter\n";
  68. $result .= 'wp_posts_total{host="' . get_site_url() . '", status="published"} ' . $n_posts_pub . "\n";
  69. $result .= 'wp_posts_total{host="' . get_site_url() . '", status="draft"} ' . $n_posts_dra . "\n";
  70. $n_pages = wp_count_posts('page');
  71. $result .= "# HELP wp_pages_total Total number of pages published.\n";
  72. $result .= "# TYPE wp_pages_total counter\n";
  73. $result .= 'wp_pages_total{host="' . get_site_url() . '", status="published"} ' . $n_pages->publish . "\n";
  74. $result .= 'wp_pages_total{host="' . get_site_url() . '", status="draft"} ' . $n_pages->draft . "\n";
  75. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `autoload` = 'yes'", ARRAY_A); // phpcs:ignore WordPress.DB
  76. $result .= "# HELP wp_options_autoload Options in autoload.\n";
  77. $result .= "# TYPE wp_options_autoload counter\n";
  78. $result .= 'wp_options_autoload{host="' . get_site_url() . '"} ' . count($query) . "\n";
  79. $query = $wpdb->get_results('SELECT ROUND(SUM(LENGTH(option_value))/ 1024) as value FROM `' . $table_prefix . "options` WHERE `autoload` = 'yes'", ARRAY_A); // phpcs:ignore WordPress.DB
  80. $result .= "# HELP wp_options_autoload_size Options size in KB in autoload.\n";
  81. $result .= "# TYPE wp_options_autoload_size counter\n";
  82. $result .= 'wp_options_autoload_size{host="' . get_site_url() . '"} ' . $query[0]['value'] . "\n";
  83. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `autoload` = 'yes' AND `option_name` LIKE '%transient%'", ARRAY_A); // phpcs:ignore WordPress.DB
  84. $result .= "# HELP wp_transient_autoload DB Transient in autoload.\n";
  85. $result .= "# TYPE wp_transient_autoload counter\n";
  86. $result .= 'wp_transient_autoload{host="' . get_site_url() . '"} ' . count($query) . "\n";
  87. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `option_name` LIKE '_wp_session_%'", ARRAY_A); // phpcs:ignore WordPress.DB
  88. $result .= "# HELP wp_user_sessions User sessions.\n";
  89. $result .= "# TYPE wp_user_sessions counter\n";
  90. $result .= 'wp_user_sessions{host="' . get_site_url() . '"} ' . count($query) . "\n";
  91. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "posts` WHERE post_title='' AND post_status!='auto-draft' AND post_status!='draft' AND post_status!='trash' AND (post_type='post' OR post_type='page')", ARRAY_A); // phpcs:ignore WordPress.DB
  92. $result .= "# HELP wp_posts_without_title Post/Page without title.\n";
  93. $result .= "# TYPE wp_posts_without_title counter\n";
  94. $result .= 'wp_posts_without_title{host="' . get_site_url() . '"} ' . count($query) . "\n";
  95. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "posts` WHERE post_content='' AND post_status!='draft' AND post_status!='trash' AND post_status!='auto-draft' AND (post_type='post' OR post_type='page')", ARRAY_A); // phpcs:ignore WordPress.DB
  96. $result .= "# HELP wp_posts_without_content Post/Page without content.\n";
  97. $result .= "# TYPE wp_posts_without_content counter\n";
  98. $result .= 'wp_posts_without_content{host="' . get_site_url() . '"} ' . count($query) . "\n";
  99. $query = $wpdb->get_results("SELECT SUM(ROUND(((data_length + index_length) / 1024 / 1024), 2)) as value FROM information_schema.TABLES WHERE table_schema = '" . DB_NAME . "'", ARRAY_A); // phpcs:ignore WordPress.DB
  100. $result .= "# HELP wp_db_size Total DB size in MB.\n";
  101. $result .= "# TYPE wp_db_size counter\n";
  102. $result .= 'wp_db_size{host="' . get_site_url() . '"} ' . $query[0]['value'] . "\n";
  103. return $result;
  104. }
  105. }
  106. global $plug_prometheus;
  107. $plug_prometheus = new plug_prometheus($this);
  108. $this->dagsopt['plug_prometheus'] = $plug_prometheus;
  109. }