Exemplo n.º 1
0
/**
 * Issue Alert-Object
 * @param array $alert
 * @return boolean
 */
function IssueAlert($alert)
{
    global $config;
    if (dbFetchCell('SELECT attrib_value FROM devices_attribs WHERE attrib_type = "disable_notify" && device_id = ?', array($alert['device_id'])) == "1") {
        return true;
    }
    $default_tpl = "%title\r\nSeverity: %severity\r\n{if %state == 0}Time elapsed: %elapsed\r\n{/if}Timestamp: %timestamp\r\nUnique-ID: %uid\r\nRule: {if %name}%name{else}%rule{/if}\r\n{if %faults}Faults:\r\n{foreach %faults}  #%key: %value.string\r\n{/foreach}{/if}Alert sent to: {foreach %contacts}%value <%key> {/foreach}";
    //FIXME: Put somewhere else?
    if ($config['alert']['fixed-contacts'] == false) {
        $alert['details']['contacts'] = GetContacts($alert['details']['rule']);
    }
    $obj = DescribeAlert($alert);
    if (is_array($obj)) {
        $tpl = dbFetchRow("SELECT `template` FROM `alert_templates` JOIN `alert_template_map` ON `alert_template_map`.`alert_templates_id`=`alert_templates`.`id` WHERE `alert_template_map`.`alert_rule_id`=?", array($alert['rule_id']));
        if (isset($tpl['template'])) {
            $tpl = $tpl['template'];
        } else {
            $tpl = $default_tpl;
        }
        echo "Issuing Alert-UID #" . $alert['id'] . "/" . $alert['state'] . ": ";
        $msg = FormatAlertTpl($tpl, $obj);
        $obj['msg'] = $msg;
        if (!empty($config['alert']['transports'])) {
            ExtTransports($obj);
        }
        echo "\r\n";
    }
    return true;
}
Exemplo n.º 2
0
/**
 * Run all rules for a device
 * @param int $device Device-ID
 * @return void
 */
function RunRules($device)
{
    global $debug;
    $chk = dbFetchRow("SELECT id FROM alert_schedule WHERE alert_schedule.device_id = ? AND NOW() BETWEEN alert_schedule.start AND alert_schedule.end", array($device));
    if ($chk['id'] > 0) {
        return false;
    }
    foreach (dbFetchRows("SELECT * FROM alert_rules WHERE alert_rules.disabled = 0 && ( alert_rules.device_id = -1 || alert_rules.device_id = ? ) ORDER BY device_id,id", array($device)) as $rule) {
        echo " #" . $rule['id'] . ":";
        $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 (sizeof($qry) > 0) {
            if ($chk['state'] === "2") {
                echo " SKIP  ";
            } elseif ($chk['state'] === "1") {
                echo " NOCHG ";
            } 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), 'alerts');
                    }
                    echo " ALERT ";
                }
            }
        } else {
            if ($chk['state'] === "0") {
                echo " NOCHG ";
            } 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), 'alerts');
                    }
                    echo " OK    ";
                }
            }
        }
    }
}
Exemplo n.º 3
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);
    }
}
Exemplo n.º 4
0
/**
 * Issue Alert-Object
 * @param array $alert
 * @return boolean
 */
function IssueAlert($alert)
{
    global $config;
    if (dbFetchCell('SELECT attrib_value FROM devices_attribs WHERE attrib_type = "disable_notify" && device_id = ?', array($alert['device_id'])) == '1') {
        return true;
    }
    if ($config['alert']['fixed-contacts'] == false) {
        $alert['details']['contacts'] = GetContacts($alert['details']['rule']);
    }
    $obj = DescribeAlert($alert);
    if (is_array($obj)) {
        echo 'Issuing Alert-UID #' . $alert['id'] . '/' . $alert['state'] . ': ';
        if (!empty($config['alert']['transports'])) {
            ExtTransports($obj);
        }
        echo "\r\n";
    }
    return true;
}
Exemplo n.º 5
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) {
        echo " #" . $rule['id'] . ":";
        $inv = json_decode($rule['extra'], true);
        if (isset($inv['invert'])) {
            $inv = (bool) $inv['invert'];
        } else {
            $inv = false;
        }
        $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));
        $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") {
                echo " SKIP  ";
            } elseif ($chk['state'] >= "1") {
                echo " NOCHG ";
            } 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');
                    }
                    echo " ALERT ";
                }
            }
        } else {
            if ($chk['state'] === "0") {
                echo " NOCHG ";
            } 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');
                    }
                    echo " OK    ";
                }
            }
        }
    }
}
Exemplo n.º 6
0
         $rule['query'] = GenSQL($rule['rule']);
     }
     $sql = $rule['query'];
     $qry = dbFetchRow($sql, array($device_id));
     if (is_array($qry)) {
         $response = 'matches';
     } else {
         $response = 'no match';
     }
     $output .= 'Rule name: ' . $rule['name'] . PHP_EOL;
     $output .= 'Alert rule: ' . $rule['rule'] . PHP_EOL;
     $output .= 'Alert query: ' . $rule['query'] . PHP_EOL;
     $output .= 'Rule match: ' . $response . PHP_EOL . PHP_EOL;
 }
 if ($config['alert']['transports']['mail'] === true) {
     $contacts = GetContacts($rules);
     if (count($contacts) > 0) {
         $output .= 'Found ' . count(contacts) . ' contacts to send alerts to.' . PHP_EOL;
     }
     foreach ($contacts as $email => $name) {
         $output .= $name . '<' . $email . '>' . PHP_EOL;
     }
     $output .= PHP_EOL;
 }
 $transports = '';
 $x = 0;
 foreach ($config['alert']['transports'] as $name => $v) {
     if ($config['alert']['transports'][$name] === true) {
         $transports .= 'Transport: ' . $name . PHP_EOL;
         $x++;
     }
Exemplo n.º 7
0
 *
 * Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.  Please see LICENSE.txt at the top level of
 * the source code distribution for details.
 */
if (is_admin() === false) {
    header('Content-type: text/plain');
    die('ERROR: You need to be admin');
}
$transport = mres($_POST['transport']);
require_once $config['install_dir'] . '/includes/alerts.inc.php';
$tmp = array(dbFetchRow('select device_id,hostname from devices order by device_id asc limit 1'));
$tmp['contacts'] = GetContacts($tmp);
$obj = array("hostname" => $tmp[0]['hostname'], "device_id" => $tmp[0]['device_id'], "title" => "Testing transport from " . $config['project_name'], "elapsed" => "11s", "id" => "000", "faults" => false, "uid" => "000", "severity" => "critical", "rule" => "%macros.device = 1", "name" => "Test-Rule", "timestamp" => date("Y-m-d H:i:s"), "contacts" => $tmp['contacts'], "state" => "1", "msg" => "This is a test alert");
$status = 'error';
if (file_exists($config['install_dir'] . "/includes/alerts/transport." . $transport . ".php")) {
    $opts = $config['alert']['transports'][$transport];
    if ($opts) {
        eval('$tmp = function($obj,$opts) { global $config; ' . file_get_contents($config['install_dir'] . '/includes/alerts/transport.' . $transport . '.php') . ' return false; };');
        $tmp = $tmp($obj, $opts);
        if ($tmp) {
            $status = 'ok';
        }
    }
}
header('Content-type: application/json');
echo _json_encode(array('status' => $status));