/** * Re-Validate Rule-Mappings * @param integer $device Device-ID * @param integer $rule Rule-ID * @return boolean */ function IsRuleValid($device, $rule) { global $rulescache; if (empty($rulescache[$device]) || !isset($rulescache[$device])) { foreach (GetRules($device) as $chk) { $rulescache[$device][$chk['id']] = true; } } if ($rulescache[$device][$rule] === true) { return true; } return false; }
/** * 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); } }
/** * 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 "; } } } } }
* @link http://librenms.org * @copyright 2016 Neil Lathwood * @author Neil Lathwood <*****@*****.**> */ if (!is_admin()) { echo "Insufficient Privileges"; exit; } $hostname = escapeshellcmd($_REQUEST['hostname']); $type = $_REQUEST['type']; switch ($type) { case 'alerts': $filename = "alerts-{$hostname}.txt"; $device_id = getidbyname($hostname); $device = device_by_id_cache($device_id); $rules = GetRules($device_id); $output = ''; foreach ($rules as $rule) { if (empty($rule['query'])) { $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;