Projet

Général

Profil

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

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

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

1
#!/usr/bin/php
2
<?php
3
/**
4
 * Drupal Users Online
5
 * Munin plugin to count online 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
 *
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 Online Users\n";
45
  echo "graph_args --base 1000 --lower-limit 0\n";
46
  echo "graph_vlabel Online Users Count / ${graph_period}\n";
47
  echo "graph_category Drupal\n";
48
  echo "graph_scale no\n";
49
  echo "graph_info Displays the online users, members and anonymous, in your Drupal site\n";
50

    
51
  echo "online_users.label online users\n";
52
  echo "online_members.label online members\n";
53
  echo "online_anonymous.label online anonymous\n";
54

    
55
  echo "online_users.min 0\n";
56
  echo "online_members.min 0\n";
57
  echo "online_anonymous.min 0\n";
58

    
59
  exit(0);
60
}
61

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

    
69
// Print out the actual values
70
$users_online_all = (int) get_all_online_users($dbh);
71
$users_members = (int) get_online_registered_users($dbh);
72
$users_anonymous = (int) get_online_anonymous_users($dbh);
73
echo "online_users.value $users_online_all\n";
74
echo "online_members.value $users_members\n";
75
echo "online_anonymous.value $users_anonymous\n";
76

    
77
$dbh->close();
78

    
79

    
80
/**
81
 * Get count for all online users
82
 * @return integer $count total online users
83
 */
84
function get_all_online_users(&$dbh = NULL, $active_interval = 900) {
85

    
86
  $table_prefix = getenv('table_prefix');
87

    
88
  $sql = "SELECT COUNT(DISTINCT(uid)) AS count FROM {$table_prefix}sessions
89
    WHERE timestamp >= (UNIX_TIMESTAMP(now()) - ?)";
90
  $stmt = $dbh->prepare($sql);
91
  $stmt->bind_param("i", $active_interval);
92
  $stmt->execute();
93

    
94
  $stmt->bind_result($active_interval);
95
  $row = $stmt->fetch();
96

    
97
  return (int) $row['count'];
98

    
99
}
100

    
101

    
102
/**
103
 * Get count for online users which are registered members
104
 * @return integer $count total registered users online
105
 */
106
function get_online_registered_users(&$dbh = NULL, $active_interval = 900) {
107

    
108
  $table_prefix = getenv('table_prefix');
109

    
110
  $sql = "SELECT COUNT(DISTINCT(uid)) AS count FROM {$table_prefix}sessions WHERE uid != 0
111
    AND timestamp >= (UNIX_TIMESTAMP(now()) - ?)";
112
  $stmt = $dbh->prepare($sql);
113
  $stmt->bind_param("i", $active_interval);
114
  $stmt->execute();
115

    
116
  $stmt->bind_result($active_interval);
117
  $row = $stmt->fetch();
118

    
119
  return (int) $row['count'];
120

    
121
}
122

    
123

    
124
/**
125
 * Get count for anonymous users
126
 * @param object $dbh database object
127
 * @param integer $active_interval seconds of anonymous user activity to consider in results (defaults to 900)
128
 * @return integer $count total anonymous users online
129
 */
130
function get_online_anonymous_users(&$dbh = NULL, $active_interval = 900) {
131

    
132
  $table_prefix = getenv('table_prefix');
133

    
134
  /**
135
   * Returns anonymous users count who have been active for at least one hour
136
   */
137
  $sql = "SELECT COUNT(DISTINCT(hostname)) AS count FROM
138
    {$table_prefix}sessions WHERE uid = 0 AND timestamp >= (UNIX_TIMESTAMP(now()) - ?)";
139

    
140
  /**
141
   * If interested in all anonymous users count
142
   *
143
   * SELECT COUNT(DISTINCT(hostname)) AS count FROM sessions WHERE uid = 0
144
   */
145

    
146
  $stmt = $dbh->prepare($sql);
147
  $stmt->bind_param("i", $active_interval);
148
  $stmt->execute();
149

    
150
  $stmt->bind_result($active_interval);
151
  $row = $stmt->fetch();
152

    
153
  return (int) $row['count'];
154

    
155
}