Пример #1
0
function dbQuery($sql, $parameters = array())
{
    global $fullSql, $debug, $sql_debug, $database_link;
    $fullSql = dbMakeQuery($sql, $parameters);
    if ($debug) {
        if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
            if (preg_match('/(INSERT INTO `alert_log`).*(details)/i', $fullSql)) {
                echo "\nINSERT INTO `alert_log` entry masked due to binary data\n";
            } else {
                c_echo('SQL[%y' . $fullSql . "%n] \n");
            }
        } else {
            $sql_debug[] = $fullSql;
        }
    }
    /*
       if($this->logFile)
       $time_start = microtime(true);
    */
    $result = mysqli_query($database_link, $fullSql);
    // sets $this->result
    /*
       if($this->logFile) {
       $time_end = microtime(true);
       fwrite($this->logFile, date('Y-m-d H:i:s') . "\n" . $fullSql . "\n" . number_format($time_end - $time_start, 8) . " seconds\n\n");
       }
    */
    if ($result === false && error_reporting() & 1) {
        // aye. this gets triggers on duplicate Contact insert
        // trigger_error('QDB - Error in query: ' . $fullSql . ' : ' . mysql_error(), E_USER_WARNING);
    }
    return $result;
}
Пример #2
0
function influx_update($device, $measurement, $tags, $fields)
{
    global $influxdb, $config;
    if ($influxdb !== false) {
        $tmp_fields = array();
        $tmp_tags['hostname'] = $device['hostname'];
        foreach ($tags as $k => $v) {
            $v = preg_replace(array('/ /', '/,/', '/=/'), array('\\ ', '\\,', '\\='), $v);
            if (empty($v)) {
                $v = '_blank_';
            }
            $tmp_tags[$k] = $v;
        }
        foreach ($fields as $k => $v) {
            $tmp_fields[$k] = force_influx_data($v);
            if ($tmp_fields[$k] === null) {
                unset($tmp_fields[$k]);
            }
        }
        d_echo("\nInfluxDB data:\n");
        d_echo($measurement);
        d_echo($tmp_tags);
        d_echo($tmp_fields);
        d_echo("\nEND\n");
        if ($config['noinfluxdb'] !== true) {
            $points = array(new InfluxDB\Point($measurement, null, $tmp_tags, $tmp_fields));
            try {
                $result = $influxdb->writePoints($points);
            } catch (Exception $e) {
                d_echo("Caught exception: " . $e->getMessage() . PHP_EOL);
                d_echo($e->getTrace());
            }
        } else {
            c_echo("[%gInfluxDB Disabled%n]\n");
        }
        //end if
    }
    //end if
}
Пример #3
0
function print_warn($msg)
{
    c_echo("[%YWARN%n]  {$msg}\n");
}
Пример #4
0
function print_message($text)
{
    if (isCli()) {
        c_echo("%g" . $text . "%n\n");
    } else {
        echo '<div class="alert alert-success"><img src="images/16/tick.png" align="absmiddle"> ' . $text . '</div>';
    }
}
Пример #5
0
        exit(0);
    } catch (HostUnreachableException $e) {
        print_error($e->getMessage());
        foreach ($e->getReasons() as $reason) {
            echo "  {$reason}\n";
        }
        exit(2);
    } catch (Exception $e) {
        print_error($e->getMessage());
        exit(3);
    }
} else {
    c_echo("\n" . $config['project_name_version'] . ' Add Host Tool

    Usage (SNMPv1/2c): ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> [community] [v1|v2c] [port] [' . implode('|', $config['snmp']['transports']) . ']
    Usage (SNMPv3)   :  Config Defaults : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> any v3 [user] [port] [' . implode('|', $config['snmp']['transports']) . ']
    No Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> nanp v3 [user] [port] [' . implode('|', $config['snmp']['transports']) . ']
       Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> anp v3 <user> <password> [md5|sha] [port] [' . implode('|', $config['snmp']['transports']) . ']
       Auth,    Priv : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> ap v3 <user> <password> <enckey> [md5|sha] [aes|dsa] [port] [' . implode('|', $config['snmp']['transports']) . ']

        -g <poller group> allows you to add a device to be pinned to a specific poller when using distributed polling. X can be any number associated with a poller group
        -f forces the device to be added by skipping the icmp and snmp check against the host.
	-p <port assoc mode> allow you to set a port association mode for this device. By default ports are associated by \'ifIndex\'.
	                     For Linux/Unix based devices \'ifName\' or \'ifDescr\' might be useful for a stable iface mapping.
	                     The default for this installation is \'' . $config['default_port_association_mode'] . '\'
	                     Valid port assoc modes are: ' . join(', ', $valid_assoc_modes) . '

    %rRemember to run discovery for the host afterwards.%n
');
    exit(1);
}
Пример #6
0
/**
 * Run all rules for a device
 * @param int $device Device-ID
 * @return void
 */
function RunRules($device)
{
    if (IsMaintenance($device) > 0) {
        echo "Under Maintenance, Skipping alerts.\r\n";
        return false;
    }
    foreach (GetRules($device) as $rule) {
        c_echo('Rule %p#' . $rule['id'] . ' (' . $rule['name'] . '):%n ');
        $inv = json_decode($rule['extra'], true);
        if (isset($inv['invert'])) {
            $inv = (bool) $inv['invert'];
        } else {
            $inv = false;
        }
        d_echo(PHP_EOL);
        $chk = dbFetchRow("SELECT state FROM alerts WHERE rule_id = ? && device_id = ? ORDER BY id DESC LIMIT 1", array($rule['id'], $device));
        $sql = GenSQL($rule['rule']);
        $qry = dbFetchRows($sql, array($device));
        if (isset($qry[0]['ip'])) {
            $qry[0]['ip'] = inet6_ntop($qry[0]['ip']);
        }
        $s = sizeof($qry);
        if ($s == 0 && $inv === false) {
            $doalert = false;
        } elseif ($s > 0 && $inv === false) {
            $doalert = true;
        } elseif ($s == 0 && $inv === true) {
            $doalert = true;
        } else {
            //( $s > 0 && $inv == false ) {
            $doalert = false;
        }
        if ($doalert) {
            if ($chk['state'] === "2") {
                c_echo('Status: %ySKIP');
            } elseif ($chk['state'] >= "1") {
                c_echo('Status: %bNOCHG');
            } else {
                $extra = gzcompress(json_encode(array('contacts' => GetContacts($qry), 'rule' => $qry)), 9);
                if (dbInsert(array('state' => 1, 'device_id' => $device, 'rule_id' => $rule['id'], 'details' => $extra), 'alert_log')) {
                    if (!dbUpdate(array('state' => 1, 'open' => 1), 'alerts', 'device_id = ? && rule_id = ?', array($device, $rule['id']))) {
                        dbInsert(array('state' => 1, 'device_id' => $device, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0), 'alerts');
                    }
                    c_echo(PHP_EOL . 'Status: %rALERT');
                }
            }
        } else {
            if ($chk['state'] === "0") {
                c_echo('Status: %bNOCHG');
            } else {
                if (dbInsert(array('state' => 0, 'device_id' => $device, 'rule_id' => $rule['id']), 'alert_log')) {
                    if (!dbUpdate(array('state' => 0, 'open' => 1), 'alerts', 'device_id = ? && rule_id = ?', array($device, $rule['id']))) {
                        dbInsert(array('state' => 0, 'device_id' => $device, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0), 'alerts');
                    }
                    c_echo(PHP_EOL . 'Status: %gOK');
                }
            }
        }
        c_echo('%n' . PHP_EOL);
    }
}
Пример #7
0
function print_message($text)
{
    if (isCli()) {
        c_echo("%g" . $text . "%n\n");
    } else {
        echo '<div class="alert alert-success"><i class="fa fa-fw fa-check-circle" aria-hidden="true"></i> ' . $text . '</div>';
    }
}
Пример #8
0
/**
 * Generates and pipes a command to rrdtool
 *
 * @internal
 * @param string $command create, update, updatev, graph, graphv, dump, restore, fetch, tune, first, last, lastupdate, info, resize, xport, flushcached
 * @param string $filename The full patth to the rrd file
 * @param string $options rrdtool command options
 * @return array the output of stdout and stderr in an array
 * @throws FileExistsException thrown when a create command is set to rrdtool < 1.4 and the rrd already exists
 * @throws Exception thrown when the rrdtool process(s) cannot be started
 */
function rrdtool($command, $filename, $options)
{
    global $config, $debug, $vdebug, $rrd_async_process, $rrd_sync_process;
    try {
        $cmd = rrdtool_build_command($command, $filename, $options);
    } catch (FileExistsException $e) {
        c_echo('RRD[%g' . $filename . " already exists%n]\n", $debug);
        return array(null, null);
    }
    c_echo("RRD[%g{$cmd}%n]\n", $debug);
    // do not write rrd files, but allow read-only commands
    $ro_commands = array('graph', 'graphv', 'dump', 'fetch', 'first', 'last', 'lastupdate', 'info', 'xport');
    if (!empty($config['norrd']) && !in_array($command, $ro_commands)) {
        c_echo('[%rRRD Disabled%n]');
        return array(null, null);
    }
    // send the command!
    if ($command == 'last' && rrdtool_initialize(false)) {
        // send this to our synchronous process so output is guaranteed
        $output = $rrd_sync_process->sendCommand($cmd);
    } elseif (rrdtool_initialize()) {
        // don't care about the return of other commands, so send them to the faster async process
        $output = $rrd_async_process->sendCommand($cmd);
    } else {
        throw new Exception('rrdtool could not start');
    }
    if ($vdebug) {
        echo 'RRDtool Output: ';
        echo $output[0];
        echo $output[1];
    }
    return $output;
}
Пример #9
0
<?php

require_once $config['install_dir'] . '/includes/common.php';
require_once $config['install_dir'] . '/includes/dbFacile.php';
require_once $config['install_dir'] . '/includes/mergecnf.inc.php';
// Connect to database
if ($config['db']['extension'] == 'mysqli') {
    $database_link = mysqli_connect('p:' . $config['db_host'], $config['db_user'], $config['db_pass']);
} else {
    $database_link = mysql_pconnect($config['db_host'], $config['db_user'], $config['db_pass']);
}
if (!$database_link) {
    if (isCli()) {
        c_echo("[%RFAIL%n]  Could not connect to MySQL\n");
    } else {
        echo '<h2>MySQL Error: could not connect</h2>';
    }
    if ($config['db']['extension'] == 'mysqli') {
        echo mysqli_error($database_link);
    } else {
        echo mysql_error();
    }
    die;
}
if ($config['db']['extension'] == 'mysqli') {
    $database_db = mysqli_select_db($database_link, $config['db_name']);
} else {
    $database_db = mysql_select_db($config['db_name'], $database_link);
}
if ($config['memcached']['enable'] === true) {
    if (class_exists('Memcached')) {