Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / drupal / drupal_forums_total.php @ f4070faf

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