|
@@ -0,0 +1,123 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+// reference https://github.com/WP-API/WP-API/blob/develop/lib/infrastructure/class-wp-rest-server.php
|
|
|
+// serve_request() function
|
|
|
+
|
|
|
+if (!class_exists("plug_prometheus")) {
|
|
|
+ class plug_prometheus {
|
|
|
+ function __construct($ns) {
|
|
|
+ $this->title = __("prometheus", "dagsopt");
|
|
|
+ $this->pluginname = $ns->pluginname;
|
|
|
+ $this->file = $ns->file;
|
|
|
+ $this->ns = $ns;
|
|
|
+
|
|
|
+ }
|
|
|
+ function start() {
|
|
|
+ add_filter('rest_pre_serve_request', array(&$this, 'multiformat_rest_pre_serve_request'), 10, 4);
|
|
|
+
|
|
|
+ add_action('rest_api_init', function () {
|
|
|
+ register_rest_route('metrics', '/', array(
|
|
|
+ 'methods' => 'GET',
|
|
|
+ 'callback' => array(&$this, 'my_awesome_func'),
|
|
|
+ ));
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ function help() {
|
|
|
+ ?>
|
|
|
+ add /wp-json/metrics
|
|
|
+
|
|
|
+ <?php
|
|
|
+}
|
|
|
+
|
|
|
+ function admin_line() {
|
|
|
+ ?>
|
|
|
+ <?php
|
|
|
+}
|
|
|
+
|
|
|
+ function my_awesome_func($data) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ function multiformat_rest_pre_serve_request($served, $result, $request, $server) {
|
|
|
+ // assumes 'format' was passed into the intial API route
|
|
|
+ // example: https://baconipsum.com/wp-json/baconipsum/test-response?format=text
|
|
|
+ // the default JSON response will be handled automatically by WP-API
|
|
|
+ if ($request->get_route() == "/metrics") {
|
|
|
+ header('Content-Type: text/plain; charset=' . get_option('blog_charset'));
|
|
|
+ $metrics = $this->get_wordpress_metrics();
|
|
|
+ echo $metrics;
|
|
|
+ $served = true; // tells the WP-API that we sent the response already
|
|
|
+ }
|
|
|
+ return $served;
|
|
|
+ }
|
|
|
+
|
|
|
+ function get_wordpress_metrics() {
|
|
|
+
|
|
|
+ global $wpdb, $table_prefix;
|
|
|
+
|
|
|
+ $result = '';
|
|
|
+
|
|
|
+ $users = count_users();
|
|
|
+ $result .= "# HELP wp_users_total Total number of users.\n";
|
|
|
+ $result .= "# TYPE wp_users_total counter\n";
|
|
|
+ $result .= 'wp_users_total{host="' . get_site_url() . '"} ' . $users['total_users'] . "\n";
|
|
|
+
|
|
|
+ $posts = wp_count_posts();
|
|
|
+ $n_posts_pub = $posts->publish;
|
|
|
+ $n_posts_dra = $posts->draft;
|
|
|
+ $result .= "# HELP wp_posts_total Total number of posts published.\n";
|
|
|
+ $result .= "# TYPE wp_posts_total counter\n";
|
|
|
+ $result .= 'wp_posts_total{host="' . get_site_url() . '", status="published"} ' . $n_posts_pub . "\n";
|
|
|
+ $result .= 'wp_posts_total{host="' . get_site_url() . '", status="draft"} ' . $n_posts_dra . "\n";
|
|
|
+
|
|
|
+ $n_pages = wp_count_posts('page');
|
|
|
+ $result .= "# HELP wp_pages_total Total number of pages published.\n";
|
|
|
+ $result .= "# TYPE wp_pages_total counter\n";
|
|
|
+ $result .= 'wp_pages_total{host="' . get_site_url() . '", status="published"} ' . $n_pages->publish . "\n";
|
|
|
+ $result .= 'wp_pages_total{host="' . get_site_url() . '", status="draft"} ' . $n_pages->draft . "\n";
|
|
|
+
|
|
|
+ $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `autoload` = 'yes'", ARRAY_A); // phpcs:ignore WordPress.DB
|
|
|
+ $result .= "# HELP wp_options_autoload Options in autoload.\n";
|
|
|
+ $result .= "# TYPE wp_options_autoload counter\n";
|
|
|
+ $result .= 'wp_options_autoload{host="' . get_site_url() . '"} ' . count($query) . "\n";
|
|
|
+ $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
|
|
|
+ $result .= "# HELP wp_options_autoload_size Options size in KB in autoload.\n";
|
|
|
+ $result .= "# TYPE wp_options_autoload_size counter\n";
|
|
|
+ $result .= 'wp_options_autoload_size{host="' . get_site_url() . '"} ' . $query[0]['value'] . "\n";
|
|
|
+
|
|
|
+ $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `autoload` = 'yes' AND `option_name` LIKE '%transient%'", ARRAY_A); // phpcs:ignore WordPress.DB
|
|
|
+ $result .= "# HELP wp_transient_autoload DB Transient in autoload.\n";
|
|
|
+ $result .= "# TYPE wp_transient_autoload counter\n";
|
|
|
+ $result .= 'wp_transient_autoload{host="' . get_site_url() . '"} ' . count($query) . "\n";
|
|
|
+
|
|
|
+ $query = $wpdb->get_results('SELECT * FROM `' . $table_prefix . "options` WHERE `option_name` LIKE '_wp_session_%'", ARRAY_A); // phpcs:ignore WordPress.DB
|
|
|
+ $result .= "# HELP wp_user_sessions User sessions.\n";
|
|
|
+ $result .= "# TYPE wp_user_sessions counter\n";
|
|
|
+ $result .= 'wp_user_sessions{host="' . get_site_url() . '"} ' . count($query) . "\n";
|
|
|
+
|
|
|
+ $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
|
|
|
+ $result .= "# HELP wp_posts_without_title Post/Page without title.\n";
|
|
|
+ $result .= "# TYPE wp_posts_without_title counter\n";
|
|
|
+ $result .= 'wp_posts_without_title{host="' . get_site_url() . '"} ' . count($query) . "\n";
|
|
|
+
|
|
|
+ $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
|
|
|
+ $result .= "# HELP wp_posts_without_content Post/Page without content.\n";
|
|
|
+ $result .= "# TYPE wp_posts_without_content counter\n";
|
|
|
+ $result .= 'wp_posts_without_content{host="' . get_site_url() . '"} ' . count($query) . "\n";
|
|
|
+
|
|
|
+ $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
|
|
|
+ $result .= "# HELP wp_db_size Total DB size in MB.\n";
|
|
|
+ $result .= "# TYPE wp_db_size counter\n";
|
|
|
+ $result .= 'wp_db_size{host="' . get_site_url() . '"} ' . $query[0]['value'] . "\n";
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ global $plug_prometheus;
|
|
|
+ $plug_prometheus = new plug_prometheus($this);
|
|
|
+ $this->dagsopt['plug_prometheus'] = $plug_prometheus;
|
|
|
+
|
|
|
+}
|