#!/usr/bin/perl
$conf = '';

$conf .= '<?php
/* LEGACY (but still working) */
$ipfailover = $mustlisten = array();

// IPFAILOVER
// $ipfailover[] = array( \'ismaster\' => false, \'ip\' => \'10.93.253.2/32\');' . "\n\n";

$conf .= '// MUSTLISTEN' . "\n";
foreach(`/sbin/ss -an -A tcp,udp`)
{
    if ( /^(udp|tcp)\s+\S+\s+\d+\s+\d+\s+(\S+):(\d+)\s+(\S+):\*\s*$/ )
    {
        $ip = $2;
        $port = $3;
        $type = $1;
        if ( $ip =~ /:/ ) { $type .= '6'; }
        if ( $ip eq '*' ) { $ip = '0.0.0.0'; }
        $conf .= '$mustlisten[] = \'' . $type . '/' . $ip . ':' . $port . '\';' . "\n";
    }
}
$conf .= "\n";


$conf .= '// CHECKMOUNTS' . "\n";
$conf .= '// ATTENTION : checkmounts n\'est executé que si "warnings" (de l\'url) n\'est pas à "yes"' . "\n";
foreach(`mount`)
{
    if ( /^(.+)\s+on\s+(.+)\s+type\s+(.+)\s+\(.+\)\s*$/ ) { $conf .= 'checkmounts(\'' . $1 . '\','. "\t" . ' \'' . $2 . '\',' . "\t" . ' \'' . $3 . '\');' . "\n"; }
}
$conf .= "\n";

$conf .= "\n\n" . '/* NEW SYNTAX */' . "\n";
$conf .= '$test_diskfree_args = $test_io_args = $test_mysql_args = $test_raid_args = $test_smart_args = $test_supervise_args = $extra_tests = $extra_tests_warnings = $test_postgres_args = array();' . "\n";
$conf .= '$basepath = \'/home/sdfrance/www\';' . "\n";
$conf .= '$disabling_by_file = FALSE; // check of $basepath/disable-monitoring, only set to true if this is a zenexity server' . "\n";
$conf .= "\n"; 

$conf .= '// DISKFREE (warning only)' . "\n";
$conf .= '$test_diskfree = TRUE;' . "\n";
foreach(`mount`)
{
    if ( /^\/dev\/sd.+\s+on\s+(.+)\s+type\s+.+\s+\(.+\)\s*$/ ) { $conf .= '$test_diskfree_args[] = array(\'' . $1 . '\', 95);' . "\n"; }
}
$conf .= "\n";

$conf .= '// DNS' . "\n";
$conf .= '$test_dns = TRUE;' . "\n";
$conf .= "\n";

$conf .= '// GLSA (cron, has it\'s own url)' . "\n";
$conf .= '$test_glsa = TRUE;' . "\n";
$conf .= '$test_glsa_file = $basepath . \'/glsa.html\';' . "\n";
$conf .= "\n";

$conf .= '// IO' . "\n";
$conf .= '$test_io = TRUE;' . "\n";
foreach(`mount`)
{
    if ( /^\/dev\/sd.+\s+on\s+(.+)\s+type\s+.+\s+\(.+\)\s*$/ ) 
    {
        $conf .= '$test_io_args[] = \'';
        if ( $1 ne '/' ) {  $conf .= $1; }
        $conf .= '/tmp\';' . "\n"; 
    }
}
$conf .= "\n";

$conf .= '// KERNEL' . "\n";
$conf .= '$test_kernel = TRUE;' . "\n";
$conf .= "\n";

$conf .= '// LOAD (warning only)' . "\n";
$conf .= '$test_load = TRUE;' . "\n";
$conf .= "\n";

$conf .= '// MAIL' . "\n";
$conf .= '$test_mail = TRUE;' . "\n";
$conf .= "\n";

$conf .= '// MEMORY (warning only)' . "\n";
$conf .= '$test_memory = TRUE;' . "\n";
$conf .= "\n";

$conf .= '// MYSQL' . "\n";
$conf .= '$test_mysql = FALSE;' . "\n";
$conf .= '//$test_mysql_args[] = array(\'host\', \'user\', \'pass\', \'database\', \'slave\', array(\'09:00\' => 20, \'12:00\' => 2000, \'15:00\' => 30, \'19:30\' => 25) );' . "\n";
$conf .= '//$test_mysql_args[] = array(\'host\', \'user\', \'pass\', \'database\', \'master\' );' . "\n";
$conf .= "\n";

$conf .= '// Postgres' . "\n";
$conf .= '$test_postgres = FALSE;' . "\n";
$conf .= '//$test_postgres_args[] = array(\'host\', \'port\', \'database\', \'user\', \'password\');' . "\n";
$conf .= "\n";

# TODO ?
$conf .= '// RAID (cron)' . "\n";
$conf .= '$test_raid = FALSE;' . "\n";
$conf .= '//$test_raid_args[] = array(\'tw_cli\', \'c0:p0 c0:p1\');' . "\n";
$conf .= '//$test_raid_args[] = array(\'megacli\', \'c0:e1:s1 c0:e1:s2\');' . "\n";
$conf .= '//$test_raid_args[] = array(\'sas2ircu\', \'c0:e1:s1 c0:e1:s2\');' . "\n";
$conf .= "\n";

$conf .= '// SMART (cron, warning only)' . "\n";
$conf .= '$test_smart = TRUE;' . "\n";
$conf .= '$test_smart_file = \'/var/run/sdf-smart\';' . "\n";
foreach (`ls /dev/sd?`)
{
    chomp();
    $conf .= '$test_smart_args[] = \'' . $_ . '\';' . "\n";
}
$conf .= '//$test_smart_args[] = \'-d 3ware,2 /dev/sdb\';' . "\n";
$conf .= "\n";

$conf .= '// SUPERVISE (cron)' . "\n";
$conf .= '$test_supervise = TRUE;' . "\n";
$conf .= '$test_supervise_file = \'/var/run/sdf-supervise\';' . "\n";
foreach(`ls /service`)
{
    chomp();
    if ( -d '/service/' . $_ ) { $conf .= '$test_supervise_args[] = \'' . $_ . '\';' . "\n"; }
}
$conf .= "\n";


$conf .= '// TIME' . "\n";
$conf .= '$test_time = TRUE;' . "\n";

$conf .= "\n"; 
$conf .= "\n"; 
$conf .= '// EXTRA TESTS' . "\n";
$conf .= '//$extra_tests[] = array(\'file_name\', \'function_name\', \'1st_argument_variable_name\', \'optional_2nd_argument_variable_name\');' . "\n"; 
$conf .= "\n";


# diff requested
if ( @ARGV == 1 )
{
    open($fh, '>/tmp/.sdf-monitoring.tmp') or die('could not write /tmp/.sdf-monitoring.tmp: ' . $!);
    print $fh $conf;
    close($fh);

    system('diff -ud \'' . $ARGV[0] . '\' /tmp/.sdf-monitoring.tmp');
}

# print guessed conf otherwise
else { print $conf; }
