Projet

Général

Profil

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

root / plugins / drupal / drupal_node_distribution_count.php @ 98e396b3

Historique | Voir | Annoter | Télécharger (3,04 ko)

1
#!/usr/bin/php
2
<?php
3
/**
4
 * Drupal Node Distribution Count
5
 * Munin plugin to count node content type usage across a Drupal site
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
$host = $host.':'.$port;
44

    
45
// Connect to database
46
mysql_connect($host, $user, $pass) or die ("Could not connect to database: ".
47
  mysql_error());
48
mysql_select_db($db) or die ("Could not select database: ".mysql_error());
49

    
50
// Get all node types (required for configuration information too)
51
$node_types = get_all_node_types_count();
52

    
53
if (count($argv) === 2 && $argv[1] === 'config') {
54
  echo "graph_title Drupal Node Distribution Count\n";
55
  echo "graph_args --base 1000 --lower-limit 0\n";
56
  echo "graph_vlabel Node Distribution Count / ${graph_period}\n";
57
  echo "graph_category Drupal\n";
58
  echo "graph_scale nol\n";
59
  echo "graph_info Displays the nodes content type distribution count in your Drupal site\n";
60

    
61
  foreach ($node_types as $node_type => $node_count) {
62
    echo "$node_type.label $node_type\n";
63
    echo "$node_type.type GAUGE\n";
64
    echo "$node_type.draw LINE2\n";
65
    echo "$node_type.min 0\n";
66
  }
67

    
68
  exit(0);
69
}
70

    
71

    
72
// Print out the actual values for each node type and it's count
73
//$node_count_all = (int) get_all_node_count();
74
//echo "nodes_total.value $node_count_all\n";
75
foreach ($node_types as $node_type => $node_count) {
76
  echo "$node_type.value $node_count\n";
77
}
78

    
79

    
80
/**
81
 * Get count for all nodes
82
 * @return integer $count total nodes created
83
 */
84
function get_all_node_count() {
85

    
86
  $table_prefix = getenv('table_prefix');
87
  $node_count = 0;
88

    
89
  // Get all node types and their count
90
  $result = mysql_query("SELECT COUNT(nid) AS count FROM {$table_prefix}node");
91
  $row = mysql_fetch_array($result, MYSQL_ASSOC);
92
  $node_count = (int) $row['count'];
93

    
94
  mysql_free_result($result);
95
  return $node_count;
96

    
97
}
98

    
99

    
100
/**
101
 * Get all node types and their count
102
 * @return array $node_types associative array of node types and their count
103
 */
104
function get_all_node_types_count() {
105

    
106
  $node_types = array();
107
  $table_prefix = getenv('table_prefix');
108

    
109
  // Get all node types and their count
110
  $result = mysql_query("SELECT COUNT(nid) AS count, type FROM
111
    {$table_prefix}node GROUP BY type");
112
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
113
    if (empty($row['type']))
114
      continue;
115
    $node_types[$row['type']] = $row['count'];
116
  }
117

    
118
  mysql_free_result($result);
119
  return $node_types;
120

    
121
}
122