Projet

Général

Profil

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

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

Historique | Voir | Annoter | Télécharger (4,16 ko)

1 98e396b3 Liran Tal
#!/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 f4070faf 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 Online Users\n";
46
  echo "graph_args --base 1000 --lower-limit 0\n";
47
  echo "graph_vlabel Online Users Count / ${graph_period}\n";
48 29bdf34e dipohl
  echo "graph_category cms\n";
49 98e396b3 Liran Tal
  echo "graph_scale no\n";
50
  echo "graph_info Displays the online users, members and anonymous, in your Drupal site\n";
51
52
  echo "online_users.label online users\n";
53
  echo "online_members.label online members\n";
54
  echo "online_anonymous.label online anonymous\n";
55
56
  echo "online_users.min 0\n";
57 e15d4f76 Liran Tal
  echo "online_users.type GAUGE\n";
58
59 98e396b3 Liran Tal
  echo "online_members.min 0\n";
60 e15d4f76 Liran Tal
  echo "online_members.type GAUGE\n";
61 98e396b3 Liran Tal
62 e15d4f76 Liran Tal
  echo "online_anonymous.min 0\n";
63
  echo "online_anonymous.type GAUGE\n";
64 98e396b3 Liran Tal
  exit(0);
65
}
66
67
// Connect to database
68
$dbh = new mysqli($host, $user, $pass, $db, $port);
69
if (mysqli_connect_errno()) {
70
  echo "Connecction failed: ".mysqli_connect_error(). PHP_EOL;
71
  exit(1);
72
}
73
74
// Print out the actual values
75
$users_online_all = (int) get_all_online_users($dbh);
76
$users_members = (int) get_online_registered_users($dbh);
77
$users_anonymous = (int) get_online_anonymous_users($dbh);
78
echo "online_users.value $users_online_all\n";
79
echo "online_members.value $users_members\n";
80
echo "online_anonymous.value $users_anonymous\n";
81
82
$dbh->close();
83
84
85
/**
86
 * Get count for all online users
87
 * @return integer $count total online users
88
 */
89
function get_all_online_users(&$dbh = NULL, $active_interval = 900) {
90
91
  $table_prefix = getenv('table_prefix');
92
93
  $sql = "SELECT COUNT(DISTINCT(uid)) AS count FROM {$table_prefix}sessions
94
    WHERE timestamp >= (UNIX_TIMESTAMP(now()) - ?)";
95
  $stmt = $dbh->prepare($sql);
96
  $stmt->bind_param("i", $active_interval);
97
  $stmt->execute();
98
99 e15d4f76 Liran Tal
  $count = 0;
100
  $stmt->bind_result($count);
101
102
  if ($stmt->fetch() === TRUE)
103
    return (int) $count;
104 98e396b3 Liran Tal
105 e15d4f76 Liran Tal
  return 0;
106 98e396b3 Liran Tal
107
}
108
109
110
/**
111
 * Get count for online users which are registered members
112
 * @return integer $count total registered users online
113
 */
114
function get_online_registered_users(&$dbh = NULL, $active_interval = 900) {
115
116
  $table_prefix = getenv('table_prefix');
117
118 9daf3766 Mohammad Shokri
  $sql = "SELECT COUNT(DISTINCT(uid)) AS count FROM {$table_prefix}sessions WHERE uid != 0
119 98e396b3 Liran Tal
    AND timestamp >= (UNIX_TIMESTAMP(now()) - ?)";
120
  $stmt = $dbh->prepare($sql);
121 e15d4f76 Liran Tal
122 98e396b3 Liran Tal
  $stmt->bind_param("i", $active_interval);
123
  $stmt->execute();
124
125 e15d4f76 Liran Tal
  $count = 0;
126
  $stmt->bind_result($count);
127
128
  if ($stmt->fetch() === TRUE)
129
    return (int) $count;
130 98e396b3 Liran Tal
131 e15d4f76 Liran Tal
  return 0;
132 98e396b3 Liran Tal
133
}
134
135
136
/**
137
 * Get count for anonymous users
138
 * @param object $dbh database object
139
 * @param integer $active_interval seconds of anonymous user activity to consider in results (defaults to 900)
140
 * @return integer $count total anonymous users online
141
 */
142
function get_online_anonymous_users(&$dbh = NULL, $active_interval = 900) {
143
144
  $table_prefix = getenv('table_prefix');
145
146
  /**
147
   * Returns anonymous users count who have been active for at least one hour
148
   */
149
  $sql = "SELECT COUNT(DISTINCT(hostname)) AS count FROM
150
    {$table_prefix}sessions WHERE uid = 0 AND timestamp >= (UNIX_TIMESTAMP(now()) - ?)";
151
152
  /**
153
   * If interested in all anonymous users count
154
   *
155
   * SELECT COUNT(DISTINCT(hostname)) AS count FROM sessions WHERE uid = 0
156
   */
157
158
  $stmt = $dbh->prepare($sql);
159 e15d4f76 Liran Tal
160 98e396b3 Liran Tal
  $stmt->bind_param("i", $active_interval);
161
  $stmt->execute();
162
163 e15d4f76 Liran Tal
  $count = 0;
164
  $stmt->bind_result($count);
165
166
  if ($stmt->fetch() === TRUE)
167
    return (int) $count;
168 98e396b3 Liran Tal
169 e15d4f76 Liran Tal
  return 0;
170 98e396b3 Liran Tal
171
}