|
47 | 47 | =cut |
48 | 48 | CUT; |
49 | 49 |
|
50 | | -ini_set('error_reporting','E_ALL & ~E_WARNING & ~E_NOTICE'); |
| 50 | +ini_set('error_reporting', 'E_ALL & ~E_WARNING & ~E_NOTICE'); |
51 | 51 | $bd = '/var/www'; |
52 | 52 | $sub = '/www'; |
53 | 53 | // Get the number of minutes from the name of this script |
54 | 54 | // (this requires maintaining the same name structure) |
55 | 55 | $scriptNameParts = preg_split('/_/', __FILE__); |
56 | | -$last_connect_minutes = $scriptNameParts[count($scriptNameParts)-2]; |
| 56 | +$last_connect_minutes = $scriptNameParts[count($scriptNameParts) - 2]; |
57 | 57 | if (intval($last_connect_minutes) != $last_connect_minutes) { |
58 | 58 | $last_connect_minutes = 5; |
59 | 59 | } |
60 | | -$connections = get_connections($bd, $sub, $last_connect_minutes); |
| 60 | +$connections = get_connections($bd, $sub, $last_connect_minutes); |
61 | 61 | $output = ''; |
62 | | -if ( !empty($argv[1]) && $argv[1] == 'config') { |
63 | | - // Global Munin attr., see http://munin-monitoring.org/wiki/protocol-config |
64 | | - $output .= "graph_title Chamilo active users v2\n"; |
65 | | - $output .= "graph_args --lower-limit 0\n"; |
66 | | - $output .= "graph_category chamilo\n"; |
67 | | - $output .= "graph_info This graph shows the number of connected users on Chamilo portals over time.\n"; |
68 | | - $output .= "graph_vlabel Users in last $last_connect_minutes min\n"; |
69 | | - $output .= "graph_scale off\n"; |
70 | | - foreach ($connections as $portal => $num) { |
71 | | - $output .= "portal$portal.label Host $portal\n"; |
| 62 | +if (!empty($argv[1]) && $argv[1] == 'config') { |
| 63 | + // Global Munin attr., see http://munin-monitoring.org/wiki/protocol-config |
| 64 | + $output .= "graph_title Chamilo active users v2\n"; |
| 65 | + $output .= "graph_args --lower-limit 0\n"; |
| 66 | + $output .= "graph_category chamilo\n"; |
| 67 | + $output .= "graph_info This graph shows the number of connected users on Chamilo portals over time.\n"; |
| 68 | + $output .= "graph_vlabel Users in last $last_connect_minutes min\n"; |
| 69 | + $output .= "graph_scale off\n"; |
| 70 | + foreach ($connections as $portal => $num) { |
| 71 | + $output .= "portal$portal.label Host $portal\n"; |
72 | 72 | // echo "portal$portal.type DERIVE\n"; |
73 | | - //echo "portal$portal.max 500\n"; |
| 73 | + //echo "portal$portal.max 500\n"; |
74 | 74 | // echo "portal$portal.min 0\n"; |
75 | 75 | // echo "portal$portal.max 500\n"; |
76 | | - $output .= "portal$portal.warning 8000\n"; |
77 | | - $output .= "portal$portal.critical 12500\n"; |
78 | | - $output .= "portal$portal.draw LINE2\n"; |
79 | | - } |
80 | | - file_put_contents('/tmp/get_connected_users_config_'.$last_connect_minutes, $output); |
81 | | - exit; |
| 76 | + $output .= "portal$portal.warning 8000\n"; |
| 77 | + $output .= "portal$portal.critical 12500\n"; |
| 78 | + $output .= "portal$portal.draw LINE2\n"; |
| 79 | + } |
| 80 | + file_put_contents('/tmp/get_connected_users_config_'.$last_connect_minutes, $output); |
| 81 | + exit; |
82 | 82 | } |
83 | | -if (is_array($connections) && count($connections)>0) { |
84 | | - foreach ($connections as $portal => $num) { |
85 | | - $output .= "portal$portal.value $num\n"; |
86 | | - } |
| 83 | +if (is_array($connections) && count($connections) > 0) { |
| 84 | + foreach ($connections as $portal => $num) { |
| 85 | + $output .= "portal$portal.value $num\n"; |
| 86 | + } |
87 | 87 | } |
88 | 88 | file_put_contents('/tmp/get_connected_users_'.$last_connect_minutes, $output); |
89 | 89 |
|
90 | | -function get_connections($bd, $sub, $last_connect_minutes) { |
91 | | - $match_count=0; |
92 | | - $connections = array(); |
93 | | - $list = scandir($bd); |
94 | | - foreach ($list as $dir) { |
95 | | - //skip system directories |
96 | | - if (substr($dir,0,1)=='.' or $dir == 'lost+found') continue; |
97 | | - //check the existence of configuration.php |
98 | | - $config_file = ''; |
99 | | - if (is_file($bd.'/'.$dir.$sub.'/app/config/configuration.php')) { |
100 | | - // Chamilo 1.10 |
101 | | - $config_file = $bd.'/'.$dir.$sub.'/app/config/configuration.php'; |
102 | | - } elseif (is_file($bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php')) { |
103 | | - // Chamilo 1.9 |
104 | | - $config_file = $bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php'; |
105 | | - } |
106 | | - if (!empty($config_file)) { |
107 | | - $inc = include_once($config_file); |
108 | | - $dbh = mysqli_connect($_configuration['db_host'],$_configuration['db_user'],$_configuration['db_password']); |
109 | | - if ($inc!==false && $dbh!==false) { |
| 90 | +function get_connections($bd, $sub, $last_connect_minutes) |
| 91 | +{ |
| 92 | + $match_count = 0; |
| 93 | + $connections = array(); |
| 94 | + $list = scandir($bd); |
| 95 | + foreach ($list as $dir) { |
| 96 | + //skip system directories |
| 97 | + if (substr($dir, 0, 1) == '.' or $dir == 'lost+found') { |
| 98 | + continue; |
| 99 | + } |
| 100 | + //check the existence of configuration.php |
| 101 | + $config_file = ''; |
| 102 | + if (is_file($bd.'/'.$dir.$sub.'/app/config/configuration.php')) { |
| 103 | + // Chamilo 1.10 |
| 104 | + $config_file = $bd.'/'.$dir.$sub.'/app/config/configuration.php'; |
| 105 | + } elseif (is_file($bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php')) { |
| 106 | + // Chamilo 1.9 |
| 107 | + $config_file = $bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php'; |
| 108 | + } |
| 109 | + if (!empty($config_file)) { |
| 110 | + $inc = include_once($config_file); |
| 111 | + $dbh = mysqli_connect($_configuration['db_host'], $_configuration['db_user'], |
| 112 | + $_configuration['db_password']); |
| 113 | + if ($inc !== false && $dbh !== false) { |
110 | 114 | $db = $_configuration['main_database']; |
111 | 115 | $sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())"; |
112 | | - $res = mysqli_query($dbh,$sql); |
| 116 | + $res = mysqli_query($dbh, $sql); |
113 | 117 | $row = mysqli_fetch_row($res); |
114 | 118 | $current_date = $row[0]; |
115 | | - $track_online_table = $db.'.track_e_online'; |
116 | | - $query = "SELECT count(login_user_id) ". |
117 | | - " FROM ".$track_online_table . |
| 119 | + $track_online_table = $db.'.track_e_online'; |
| 120 | + $query = "SELECT count(login_user_id) ". |
| 121 | + " FROM ".$track_online_table. |
118 | 122 | " WHERE DATE_ADD(login_date, ". |
119 | | - "INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' "; |
120 | | - //echo $query."\n"; |
121 | | - $res = mysqli_query($dbh, $query); |
122 | | - if ( $res === false ) { |
123 | | - $num = 0; |
124 | | - //echo " There was a query error for the following portal\n"; |
125 | | - } else { |
126 | | - $row = mysqli_fetch_row($res); |
127 | | - $num = $row[0]; |
128 | | - } |
129 | | - //echo sprintf("[%7d]",$num)." users connected to ".$_configuration['root_web']." last $last_connect_minutes'\n"; |
| 123 | + "INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' "; |
| 124 | + //echo $query."\n"; |
| 125 | + $res = mysqli_query($dbh, $query); |
| 126 | + if ($res === false) { |
| 127 | + $num = 0; |
| 128 | + //echo " There was a query error for the following portal\n"; |
| 129 | + } else { |
| 130 | + $row = mysqli_fetch_row($res); |
| 131 | + $num = $row[0]; |
| 132 | + } |
| 133 | + //echo sprintf("[%7d]",$num)." users connected to ".$_configuration['root_web']." last $last_connect_minutes'\n"; |
130 | 134 | $cut_point = 7; |
131 | | - if (substr($_configuration['root_web'],0,5) == 'https') { |
| 135 | + if (substr($_configuration['root_web'], 0, 5) == 'https') { |
132 | 136 | $cut_point = 8; |
133 | 137 | } |
134 | | - $connections[str_replace('.','_',substr($_configuration['root_web'],$cut_point,-1))] = $num; |
135 | | - $match_count += $num; |
136 | | - mysqli_close($dbh); |
137 | | - } else { |
138 | | - //echo "$bd/$dir$sub:could not open configuration.php or database:\n"; |
139 | | - } |
| 138 | + $connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num; |
| 139 | + $match_count += $num; |
| 140 | + mysqli_close($dbh); |
| 141 | + } else { |
| 142 | + //echo "$bd/$dir$sub:could not open configuration.php or database:\n"; |
| 143 | + } |
| 144 | + } |
140 | 145 | } |
141 | | - } |
142 | | - return $connections; |
| 146 | + return $connections; |
143 | 147 | } |
144 | 148 |
|
0 commit comments