plug_prometheus.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. 'callback' => array(&$this, 'my_awesome_func'),
  18. ));
  19. });
  20. }
  21. function help() {
  22. ?>
  23. add /wp-json/metrics
  24. <?php
  25. }
  26. function admin_line() {
  27. ?>
  28. <?php
  29. }
  30. function my_awesome_func($data) {
  31. return "";
  32. }
  33. function multiformat_rest_pre_serve_request($served, $result, $request, $server) {
  34. // assumes 'format' was passed into the intial API route
  35. // example: https://baconipsum.com/wp-json/baconipsum/test-response?format=text
  36. // the default JSON response will be handled automatically by WP-API
  37. if ($request->get_route() == "/metrics") {
  38. header('Content-Type: text/plain; charset=' . get_option('blog_charset'));
  39. $metrics = $this->get_wordpress_metrics();
  40. echo $metrics;
  41. $served = true; // tells the WP-API that we sent the response already
  42. }
  43. return $served;
  44. }
  45. function get_wordpress_metrics() {
  46. global $wpdb, $table_prefix;
  47. $result = '';
  48. $users = count_users();
  49. $result .= "# HELP wp_users_total Total number of users.\n";
  50. $result .= "# TYPE wp_users_total counter\n";
  51. $result .= 'wp_users_total{host="' . get_site_url() . '"} ' . $users['total_users'] . "\n";
  52. $posts = wp_count_posts();
  53. $n_posts_pub = $posts->publish;
  54. $n_posts_dra = $posts->draft;
  55. $result .= "# HELP wp_posts_total Total number of posts published.\n";
  56. $result .= "# TYPE wp_posts_total counter\n";
  57. $result .= 'wp_posts_total{host="' . get_site_url() . '", status="published"} ' . $n_posts_pub . "\n";
  58. $result .= 'wp_posts_total{host="' . get_site_url() . '", status="draft"} ' . $n_posts_dra . "\n";
  59. $n_pages = wp_count_posts('page');
  60. $result .= "# HELP wp_pages_total Total number of pages published.\n";
  61. $result .= "# TYPE wp_pages_total counter\n";
  62. $result .= 'wp_pages_total{host="' . get_site_url() . '", status="published"} ' . $n_pages->publish . "\n";
  63. $result .= 'wp_pages_total{host="' . get_site_url() . '", status="draft"} ' . $n_pages->draft . "\n";
  64. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `autoload` = 'yes'", ARRAY_A); // phpcs:ignore WordPress.DB
  65. $result .= "# HELP wp_options_autoload Options in autoload.\n";
  66. $result .= "# TYPE wp_options_autoload counter\n";
  67. $result .= 'wp_options_autoload{host="' . get_site_url() . '"} ' . count($query) . "\n";
  68. $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
  69. $result .= "# HELP wp_options_autoload_size Options size in KB in autoload.\n";
  70. $result .= "# TYPE wp_options_autoload_size counter\n";
  71. $result .= 'wp_options_autoload_size{host="' . get_site_url() . '"} ' . $query[0]['value'] . "\n";
  72. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `autoload` = 'yes' AND `option_name` LIKE '%transient%'", ARRAY_A); // phpcs:ignore WordPress.DB
  73. $result .= "# HELP wp_transient_autoload DB Transient in autoload.\n";
  74. $result .= "# TYPE wp_transient_autoload counter\n";
  75. $result .= 'wp_transient_autoload{host="' . get_site_url() . '"} ' . count($query) . "\n";
  76. $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `option_name` LIKE '_wp_session_%'", ARRAY_A); // phpcs:ignore WordPress.DB
  77. $result .= "# HELP wp_user_sessions User sessions.\n";
  78. $result .= "# TYPE wp_user_sessions counter\n";
  79. $result .= 'wp_user_sessions{host="' . get_site_url() . '"} ' . count($query) . "\n";
  80. $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
  81. $result .= "# HELP wp_posts_without_title Post/Page without title.\n";
  82. $result .= "# TYPE wp_posts_without_title counter\n";
  83. $result .= 'wp_posts_without_title{host="' . get_site_url() . '"} ' . count($query) . "\n";
  84. $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
  85. $result .= "# HELP wp_posts_without_content Post/Page without content.\n";
  86. $result .= "# TYPE wp_posts_without_content counter\n";
  87. $result .= 'wp_posts_without_content{host="' . get_site_url() . '"} ' . count($query) . "\n";
  88. $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
  89. $result .= "# HELP wp_db_size Total DB size in MB.\n";
  90. $result .= "# TYPE wp_db_size counter\n";
  91. $result .= 'wp_db_size{host="' . get_site_url() . '"} ' . $query[0]['value'] . "\n";
  92. return $result;
  93. }
  94. }
  95. global $plug_prometheus;
  96. $plug_prometheus = new plug_prometheus($this);
  97. $this->dagsopt['plug_prometheus'] = $plug_prometheus;
  98. }