Browse Source

Bumped version to 0.0.65

Jannick Knudsen 4 years ago
parent
commit
90bd14fb9e
7 changed files with 152 additions and 24 deletions
  1. 1 1
      VERSION
  2. 1 0
      VERSIONLOG
  3. 1 1
      package.json
  4. 123 0
      plug_prometheus.php
  5. 2 2
      plugin.php
  6. 1 20
      tpl_default.php
  7. 23 0
      tpl_default2.php

+ 1 - 1
VERSION

@@ -1 +1 @@
-v0.0.64
+v0.0.65

+ 1 - 0
VERSIONLOG

@@ -63,3 +63,4 @@ v0.0.61
 v0.0.62
 v0.0.63
 v0.0.64
+v0.0.65

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "dagsplug",
-  "version": "0.0.64",
+  "version": "0.0.65",
   "description": "",
   "main": "script_backend.js",
   "scripts": {

+ 123 - 0
plug_prometheus.php

@@ -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;
+
+}

+ 2 - 2
plugin.php

@@ -4,7 +4,7 @@ Plugin Name: dagsopt
 Plugin URI: https://git.tum.dk/tum.dk/dagsplug/
 Description: Tools
 Author: iskedk
-Version: 0.0.64
+Version: 0.0.65
 Date: 2021-01-14
 Author URI: https://iske.dk/
 Text Domain: dagsopt
@@ -17,7 +17,7 @@ function wppluginspage() {
 $wp_dagsopt = new dagsopt();
 
 class dagsopt {
-	var $version = "0.0.64";
+	var $version = "0.0.65";
 	var $publish_date = "2021-01-14";
 	var $pluginname;
 	var $plugintitle;

+ 1 - 20
tpl_default.php

@@ -1,23 +1,4 @@
 
-<?php /*$image = post_image_($post->ID,'medium'); */
-$post_images = post_images($post->ID);
-$imageurl = get_the_post_thumbnail_url($post->ID);
-?>
-
-<article  id="post-<?php the_ID();?>">
-	<div class="image">
-		<?php post_badge();?>
-			<a href="<?php the_permalink()?>" title="<?php the_title_attribute();?>"><img src="<?php echo $imageurl ? $imageurl : $post_images[0]['large'][0]; ?>" alt="<?php the_title_attribute();?>"></a>
-		<h2><a href="<?php the_permalink()?>" rel="bookmark" title="Permanent link til <?php the_title_attribute();?>"><?php the_title();?></a></h2>
-	</div>
-	<div class="content">
-		<?php echo (is_single() . "LL"); ?>
-		xxx<?php the_content();?>xxx
-
-	</div>
-
-
-</article>
-
 
+<?php get_template_part('content', 'single');?>
 

+ 23 - 0
tpl_default2.php

@@ -0,0 +1,23 @@
+
+<?php /*$image = post_image_($post->ID,'medium'); */
+$post_images = post_images($post->ID);
+$imageurl = get_the_post_thumbnail_url($post->ID);
+?>
+
+<article  id="post-<?php the_ID();?>">
+	<div class="image">
+		<?php post_badge();?>
+			<a href="<?php the_permalink()?>" title="<?php the_title_attribute();?>"><img src="<?php echo $imageurl ? $imageurl : $post_images[0]['large'][0]; ?>" alt="<?php the_title_attribute();?>"></a>
+		<h2><a href="<?php the_permalink()?>" rel="bookmark" title="Permanent link til <?php the_title_attribute();?>"><?php the_title();?></a></h2>
+	</div>
+	<div class="content">
+		<?php echo (is_single() . "LL"); ?>
+		xxx<?php the_content();?>xxx
+
+	</div>
+
+
+</article>
+
+
+