<commit_before>#!/usr/bin/php5
<?php
$doc = <<<CUT
=head1 NAME

chamilo_registered_sessions - Munin plugin to monitor the number of sessions created on Chamilo portals.

=head1 APPLICABLE SYSTEMS

Chamilo 1.* web applications.

=head1 CONFIGURATION

The plugin needs access to the root web directory /var/www (or any other given
path) in order to scan for Chamilo installations. It also offers a way to 
define subdirs if your Chamilo portals are generally stored under two folder
levels inside /var/www/. The plugin also needs the possibility to execute PHP
on the command line (php-cli package) and to connect to a MySQL database
(php5-mysql package).

=head1 INTERPRETATION

The plugin shows the number of sessions at any given time for any
Chamilo portal, indicating the URL of the corresponding portal.

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 BUGS

None known of

=head1 VERSION

  0.2

=head1 AUTHOR

<NAME> <<EMAIL>>

=head1 LICENSE

AGPLv3

=cut
CUT;

ini_set('error_reporting','E_ALL & ~E_WARNING & ~E_NOTICE');
$bd = '/var/www';
$sub = '/www';
$connections = get_sessions($bd, $sub); 
$output = '';
if ( !empty($argv[1]) && $argv[1] == 'config') {
  // Global Munin attr., see http://munin-monitoring.org/wiki/protocol-config
  $output .= "graph_title Chamilo registered sessions\n";
  //$output .= "graph_args --lower-limit 0\n";
  $output .= "graph_args --lower-limit 0 --base 1000 --logarithmic'\n";
  $output .= "graph_category chamilo\n";
  $output .= "graph_info This graph shows the number of sessions created on Chamilo portals over time.\n";
  $output .= "graph_vlabel Registered sessions\n";
  $output .= "graph_scale off\n";
  foreach ($connections as $portal => $num) {
    $warning = 100000;
    $critical = 200000;
    $output .= "portal$portal.label Host $portal\n";
//    $output .= "portal$portal.type DERIVE\n";
    //$output .= "portal$portal.max 500\n";
//    $output .= "portal$portal.min 0\n";
//    $output .= "portal$portal.max 11000\n";
    $output .= "portal$portal.warning $warning\n";
    $output .= "portal$portal.critical $critical\n";
    $output .= "portal$portal.draw LINE2\n";
  }
  file_put_contents('/tmp/get_registered_sessions_config',$output);
  exit;
}
if (is_array($connections) && count($connections)>0) {
  foreach ($connections as $portal => $num) {
    $output .= "portal$portal.value $num\n";
  }
}
file_put_contents('/tmp/get_registered_sessions',$output);

function get_sessions($bd, $sub) {
  $match_count=0;
  $connections = array();
  $list = scandir($bd);
  foreach ($list as $dir) {
    //skip system directories
    if (substr($dir,0,1)=='.' or $dir == 'lost+found') continue;
    //check the existence of configuration.php
    $config_file = '';
    if (is_file($bd.'/'.$dir.$sub.'/config/configuration.php')) {
        // Chamilo 1.10
        $config_file = $bd.'/'.$dir.$sub.'/config/configuration.php';
    } elseif (is_file($bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php')) {
        // Chamilo 1.9
        $config_file = $bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php';
    }
    if (!empty($config_file)) {
  	$inc = include_once($config_file);
	$dbh = mysql_connect($_configuration['db_host'],$_configuration['db_user'],$_configuration['db_password']);
	if ($inc!==false && $dbh!==false) {
		$db = $_configuration['main_database'];
		$current_date=date('Y-m-d H:i:s',time());
		$user_table = $db.'.session';
		$query = "SELECT count(code) ".
                    " FROM ".$user_table ;
		//echo $query."\n";
		$res = mysql_query($query);
		if ( $res === false ) {
			$num = 0;
			//echo "          There was a query error for the following portal\n";
		} else {
			$row = mysql_fetch_row($res);
			$num = $row[0];
		}
                $cut_point = 7;
                if (substr($_configuration['root_web'],0,5) == 'https') {
                    $cut_point = 8;
                }
                $connections[str_replace('.','_',substr($_configuration['root_web'],$cut_point,-1))] = $num;
		$match_count += $num;
		mysql_close($dbh);
	} else {
		//echo "$bd/$dir$sub:could not open configuration.php or database:\n";
	}
    }
  }
  return $connections;
}
<commit_msg>Fixed query in new sessions count script<commit_after>#!/usr/bin/php5
<?php
$doc = <<<CUT
=head1 NAME

chamilo_registered_sessions - Munin plugin to monitor the number of sessions created on Chamilo portals.

=head1 APPLICABLE SYSTEMS

Chamilo 1.* web applications.

=head1 CONFIGURATION

The plugin needs access to the root web directory /var/www (or any other given
path) in order to scan for Chamilo installations. It also offers a way to 
define subdirs if your Chamilo portals are generally stored under two folder
levels inside /var/www/. The plugin also needs the possibility to execute PHP
on the command line (php-cli package) and to connect to a MySQL database
(php5-mysql package).

=head1 INTERPRETATION

The plugin shows the number of sessions at any given time for any
Chamilo portal, indicating the URL of the corresponding portal.

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 BUGS

None known of

=head1 VERSION

  0.2

=head1 AUTHOR

<NAME> <<EMAIL>>

=head1 LICENSE

AGPLv3

=cut
CUT;

ini_set('error_reporting','E_ALL & ~E_WARNING & ~E_NOTICE');
$bd = '/var/www';
$sub = '/www';
$connections = get_sessions($bd, $sub); 
$output = '';
if ( !empty($argv[1]) && $argv[1] == 'config') {
  // Global Munin attr., see http://munin-monitoring.org/wiki/protocol-config
  $output .= "graph_title Chamilo registered sessions\n";
  //$output .= "graph_args --lower-limit 0\n";
  $output .= "graph_args --lower-limit 0 --base 1000 --logarithmic'\n";
  $output .= "graph_category chamilo\n";
  $output .= "graph_info This graph shows the number of sessions created on Chamilo portals over time.\n";
  $output .= "graph_vlabel Registered sessions\n";
  $output .= "graph_scale off\n";
  foreach ($connections as $portal => $num) {
    $warning = 100000;
    $critical = 200000;
    $output .= "portal$portal.label Host $portal\n";
//    $output .= "portal$portal.type DERIVE\n";
    //$output .= "portal$portal.max 500\n";
//    $output .= "portal$portal.min 0\n";
//    $output .= "portal$portal.max 11000\n";
    $output .= "portal$portal.warning $warning\n";
    $output .= "portal$portal.critical $critical\n";
    $output .= "portal$portal.draw LINE2\n";
  }
  file_put_contents('/tmp/get_registered_sessions_config',$output);
  exit;
}
if (is_array($connections) && count($connections)>0) {
  foreach ($connections as $portal => $num) {
    $output .= "portal$portal.value $num\n";
  }
}
file_put_contents('/tmp/get_registered_sessions',$output);

function get_sessions($bd, $sub) {
  $match_count=0;
  $connections = array();
  $list = scandir($bd);
  foreach ($list as $dir) {
    //skip system directories
    if (substr($dir,0,1)=='.' or $dir == 'lost+found') continue;
    //check the existence of configuration.php
    $config_file = '';
    if (is_file($bd.'/'.$dir.$sub.'/config/configuration.php')) {
        // Chamilo 1.10
        $config_file = $bd.'/'.$dir.$sub.'/config/configuration.php';
    } elseif (is_file($bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php')) {
        // Chamilo 1.9
        $config_file = $bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php';
    }
    if (!empty($config_file)) {
  	$inc = include_once($config_file);
	$dbh = mysql_connect($_configuration['db_host'],$_configuration['db_user'],$_configuration['db_password']);
	if ($inc!==false && $dbh!==false) {
		$db = $_configuration['main_database'];
		$current_date=date('Y-m-d H:i:s',time());
		$user_table = $db.'.session';
		$query = "SELECT count(id) ".
                    " FROM ".$user_table ;
		//echo $query."\n";
		$res = mysql_query($query);
		if ( $res === false ) {
			$num = 0;
			//echo "          There was a query error for the following portal\n";
		} else {
			$row = mysql_fetch_row($res);
			$num = $row[0];
		}
                $cut_point = 7;
                if (substr($_configuration['root_web'],0,5) == 'https') {
                    $cut_point = 8;
                }
                $connections[str_replace('.','_',substr($_configuration['root_web'],$cut_point,-1))] = $num;
		$match_count += $num;
		mysql_close($dbh);
	} else {
		//echo "$bd/$dir$sub:could not open configuration.php or database:\n";
	}
    }
  }
  return $connections;
}

{'fraction_non_alphanumeric': 0.16708378024073411, 'fraction_numerical': 0.014539387438922656, 'mean_word_length': 3.39141810570382, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '501832', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2862, 'n_words': 716}