Implement a set of module hooks. This would allow any third-party module to add custom queries and results to the Cacti module, and therefore expose the data to Cacti via the already defined data input method.

For example, I have a few unreleased custom modules. I'd like them to have a function to provide a query and statistics to this module and to Cacti so they can be graphed. This might be done via calling a hook_cacti function to allow for that custom query to be used.

CommentFileSizeAuthor
#1 cacti.module.patch.869276.txt2.58 KBraintonr

Comments

raintonr’s picture

Version: » 6.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new2.58 KB

Please find enclosed a patch to create hook_cacti_stats.

I've modified the userstats statistic to be created using this hook, and also added another example stat (nodestats) which tracks thread and comment posts.

This all works perfectly for me, but needs review/committing.

raintonr’s picture

Somehow that last attachment failed so trying again...

raintonr’s picture

Something is wrong with attachments right now, as the patch is small, here it is:

--- cacti.module	2010-08-07 07:03:40.000000000 +1000
+++ cacti.module.new	2010-09-23 08:41:45.158950315 +1000
@@ -88,13 +88,14 @@ function cacti_dispatch($type) {
 
     // We matched an IP, so dispatch to the worker functions.
     if ($allow) {
-      // Dispatch the queries for supported statistic types
-      switch ($type) {
-        case "userstats":
-          print trim(cacti_userstats());
-          // Die so the theme layer is not output.
-          die;
-          break;
+      $value_array = module_invoke_all('cacti_stats', $type);
+      if (is_array($value_array)) {
+        $out = array();
+        foreach ($value_array as $key => $value) {
+          $out[] = trim($key) . ':' . trim($value);
+        }
+        echo join(' ', $out);
+        die;
       }
     }
   }
@@ -108,21 +109,29 @@ function cacti_dispatch($type) {
 
 
 /**
- * Dispatch function.
+ * hook_cacti_stat - Dispatch function.
  * Obtain statistics on current logged in users vs. guest users.
  */
-function cacti_userstats() {
-  // Add count of active anonymous/authenticated users. Mostly copied from the admin_menu module.
-  // @see user_block(), user.module
-  $interval = time() - variable_get('user_block_seconds_online', 900);
-  $count_anon = sess_count($interval);
-  $count_auth = db_result(db_query("SELECT COUNT(DISTINCT uid) FROM {sessions} WHERE uid > 0 AND timestamp >= %d", $interval));
-
-  // Return a count formatted for Cacti
-  $userstats = "users_anon:" . $count_anon . " users_auth:" . $count_auth;
-  return $userstats;
-}
+function cacti_cacti_stats($type) {
+  switch($type) {
+    case 'userstats':
+      // Add count of active anonymous/authenticated users. Mostly copied from the admin_menu module.
+      // @see user_block(), user.module
+      $interval = time() - variable_get('user_block_seconds_online', 900);
+      $count_anon = sess_count($interval);
+      $count_auth = db_result(db_query("SELECT COUNT(DISTINCT uid) FROM {sessions} WHERE uid > 0 AND timestamp >= %d", $interval));
+
+      // Return a count formatted for Cacti
+      return array("users_anon" => $count_anon, "users_auth" => $count_auth);
+    case 'nodestats':
+      /* Return count of nodes & comments */
+      $count_node = db_result(db_query("SELECT COUNT(DISTINCT nid) FROM {node} WHERE status = 1"));
+      $count_comment = db_result(db_query("SELECT COUNT(DISTINCT cid) FROM {comments} WHERE status = 0"));
 
+      // Return a count formatted for Cacti
+      return array("node_count" => $count_node, "comment_count" => $count_comment);
+  }
+}
 
 /**
  * Basic validation of an IP. Mainly copied from the restrict_ip module.
rjbrown99’s picture

Thanks! I'll have a look this weekend and will roll it on up assuming it looks good. I set this issue as a placeholder and didn't expect a patch so this is a welcome addition :)

On another note, there is an important limitation I need to put in the README with the existing anonymous vs. authenticated user graph. If using Pressflow and not setting a cookie for anonymous, it can't graph them because they don't show up in the database as an active session. It still works to graph authenticated users, and should still work properly to graph both for the Drupal codebase.