root / plugins / drupal / drupal_forums_total.php @ ef960abc
Historique | Voir | Annoter | Télécharger (2,68 ko)
| 1 | 98e396b3 | Liran Tal | #!/usr/bin/php |
|---|---|---|---|
| 2 | <?php
|
||
| 3 | /**
|
||
| 4 | * Drupal Forums and Comments
|
||
| 5 | * Munin plugin to count total forums and comments
|
||
| 6 | *
|
||
| 7 | * It's required to define a container entry for this plugin in your
|
||
| 8 | * /etc/munin/plugin-conf.d/munin-node (or a separate and dedicated file).
|
||
| 9 | *
|
||
| 10 | * @example Example entry for configuration:
|
||
| 11 | * [drupal*]
|
||
| 12 | * env.db="drupal"
|
||
| 13 | * env.user="mysql_user"
|
||
| 14 | * env.pass="mysql_pass"
|
||
| 15 | * env.host="localhost"
|
||
| 16 | * env.port="3306"
|
||
| 17 | *
|
||
| 18 | * @author Liran Tal <liran.tal@hp.com>
|
||
| 19 | * @version 1.0 2013
|
||
| 20 | *
|
||
| 21 | */
|
||
| 22 | |||
| 23 | |||
| 24 | /**
|
||
| 25 | * Environment variabels are set by munin's configuration
|
||
| 26 | * @see /etc/munin/plugin-conf.d/munin-node
|
||
| 27 | */
|
||
| 28 | $db = getenv('db'); |
||
| 29 | $host = getenv('host'); |
||
| 30 | $user = getenv('user'); |
||
| 31 | $pass = getenv('pass'); |
||
| 32 | $port = getenv('port'); |
||
| 33 | if (!$port) |
||
| 34 | $port = 3306; |
||
| 35 | |||
| 36 | $graph_period = getenv('graph_period'); |
||
| 37 | |||
| 38 | if(count($argv) == 2 && $argv[1] == 'autoconf') { |
||
| 39 | echo "yes\n"; |
||
| 40 | exit(0); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (count($argv) === 2 && $argv[1] === 'config') { |
||
| 44 | echo "graph_title Drupal Total Forums\n"; |
||
| 45 | echo "graph_args --base 1000 --lower-limit 0\n"; |
||
| 46 | echo "graph_vlabel Total Forums and comments Count / ${graph_period}\n"; |
||
| 47 | echo "graph_category Drupal\n"; |
||
| 48 | echo "graph_scale no\n"; |
||
| 49 | echo "graph_info Displays the sum of forums and comments made in your Drupal site\n"; |
||
| 50 | |||
| 51 | echo "forums_total.label total forums\n"; |
||
| 52 | echo "forums_total.type GAUGE\n"; |
||
| 53 | |||
| 54 | echo "comments_total.label total comments\n"; |
||
| 55 | echo "comments_total.type GAUGE\n"; |
||
| 56 | |||
| 57 | echo "forums_total.min 0\n"; |
||
| 58 | echo "comments_total.min 0\n"; |
||
| 59 | |||
| 60 | exit(0); |
||
| 61 | } |
||
| 62 | |||
| 63 | // Connect to database
|
||
| 64 | $dbh = new mysqli($host, $user, $pass, $db, $port); |
||
| 65 | if (mysqli_connect_errno()) {
|
||
| 66 | echo "Connecction failed: ".mysqli_connect_error(). PHP_EOL; |
||
| 67 | exit(1); |
||
| 68 | } |
||
| 69 | |||
| 70 | // Print out the actual values
|
||
| 71 | $forums_total = (int) get_forums_total($dbh); |
||
| 72 | $comments_total = (int) get_comments_total($dbh); |
||
| 73 | echo "forums_total.value $forums_total\n"; |
||
| 74 | echo "comments_total.value $comments_total\n"; |
||
| 75 | |||
| 76 | $dbh->close();
|
||
| 77 | |||
| 78 | |||
| 79 | /**
|
||
| 80 | * Get count for all forums
|
||
| 81 | * @return integer $count total forums content type nodes created
|
||
| 82 | */
|
||
| 83 | function get_forums_total(&$dbh = NULL) { |
||
| 84 | |||
| 85 | $table_prefix = getenv('table_prefix'); |
||
| 86 | |||
| 87 | $sql = "SELECT COUNT(nid) AS count FROM {$table_prefix}node WHERE type = 'forum'"; |
||
| 88 | $result = $dbh->query($sql); |
||
| 89 | $row = $result->fetch_assoc(); |
||
| 90 | |||
| 91 | return (int) $row['count']; |
||
| 92 | |||
| 93 | } |
||
| 94 | |||
| 95 | /**
|
||
| 96 | * Get count for all comments made through-out the site for a 'forum' node content type
|
||
| 97 | * @return integer $count all comments
|
||
| 98 | */
|
||
| 99 | function get_comments_total(&$dbh = NULL) { |
||
| 100 | |||
| 101 | $table_prefix = getenv('table_prefix'); |
||
| 102 | |||
| 103 | $sql = "SELECT COUNT(c.cid) AS count FROM {$table_prefix}comments c JOIN {$table_prefix}node n ON c.nid = n.nid WHERE n.type = 'forum'"; |
||
| 104 | $result = $dbh->query($sql); |
||
| 105 | $row = $result->fetch_assoc(); |
||
| 106 | |||
| 107 | return (int) $row['count']; |
||
| 108 | |||
| 109 | } |
