Projet

Général

Profil

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

root / plugins / drupal / drupal_node_distribution_count.php @ 29bdf34e

Historique | Voir | Annoter | Télécharger (3,06 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
 * env.table_prefix drupal_
18
 *
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
$host = $host.':'.$port;
45

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

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

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

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

    
69
  exit(0);
70
}
71

    
72

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

    
80

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

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

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

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

    
98
}
99

    
100

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

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

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

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

    
122
}
123