Projet

Général

Profil

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

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

Historique | Voir | Annoter | Télécharger (2,43 ko)

1
#!/usr/bin/php
2
<?php
3
/**
4
 * Drupal Users Total
5
 * Munin plugin to count total users
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
if (count($argv) === 2 && $argv[1] === 'config') {
45
  echo "graph_title Drupal Total Users\n";
46
  echo "graph_args --base 1000 --lower-limit 0\n";
47
  echo "graph_vlabel Total Users Count / ${graph_period}\n";
48
  echo "graph_category cms\n";
49
  echo "graph_scale no\n";
50
  echo "graph_info Displays the sum of users, as well as disabled count, in your Drupal site\n";
51

    
52
  echo "users_total.label total users\n";
53
  echo "users_blocked.label blocked users\n";
54

    
55
  echo "users_total.min 0\n";
56
  echo "users_blocked.min 0\n";
57

    
58
  exit(0);
59
}
60

    
61
// Connect to database
62
$dbh = new mysqli($host, $user, $pass, $db, $port);
63
if (mysqli_connect_errno()) {
64
  echo "Connecction failed: ".mysqli_connect_error(). PHP_EOL;
65
  exit(1);
66
}
67

    
68
// Print out the actual values
69
$users_total = (int) get_total_users($dbh);
70
$users_blocked = (int) get_blocked_users($dbh);
71
echo "users_total.value $users_total\n";
72
echo "users_blocked.value $users_blocked\n";
73

    
74
$dbh->close();
75

    
76

    
77
/**
78
 * Get count for all users
79
 * @return integer $count total users
80
 */
81
function get_total_users(&$dbh = NULL) {
82

    
83
  $table_prefix = getenv('table_prefix');
84

    
85
  $sql = "SELECT COUNT(uid) AS count FROM {$table_prefix}users";
86
  $result = $dbh->query($sql);
87
  $row = $result->fetch_assoc();
88

    
89
  return (int) $row['count'];
90

    
91
}
92

    
93
/**
94
 * Get count for all blocked users
95
 * @return integer $count all blocked users
96
 */
97
function get_blocked_users(&$dbh = NULL) {
98

    
99
  $table_prefix = getenv('table_prefix');
100

    
101
  $sql = "SELECT COUNT(uid) AS count FROM {$table_prefix}users WHERE status = 0";
102
  $result = $dbh->query($sql);
103
  $row = $result->fetch_assoc();
104

    
105
  return (int) $row['count'];
106

    
107
}