<?php require_once 'Connection.class.php'; if (isset($_GET['id'])) { // Retrieves an image by ID from Zabbix database. $dbh = Connection::GetDatabase(); $stmt = $dbh->prepare(' SELECT image FROM images WHERE imageid = :imageId '); $stmt->bindParam(':imageId', $_GET['id']); if (!$stmt->execute()) { $err = $stmt->errorInfo(); Connection::HttpError(500, "Failed to query image for '{$_GET['id']}'.<br/>{$err['0']} {$err['2']}"); } $row = $stmt->fetch(PDO::FETCH_ASSOC); header('Content-Type: image/png'); echo $row['image']; }
/** * Creates entries on the tables service_weight and service_threshold if they don't exist. * @param PDO $dbh Database connection handle. * @param sring $serviceId ID of service to be checked. */ public static function CreateThresholdWeightIfNotExist(PDO $dbh, $serviceId) { try { $stmt_w = Connection::QueryDatabase($dbh, 'SELECT idservice FROM service_weight WHERE idservice = ?', $serviceId); if (!$stmt_w->fetch(PDO::FETCH_NUM)) { Connection::QueryDatabase($dbh, 'INSERT INTO service_weight (idservice) VALUES (?)', $serviceId); } $stmt_t = Connection::QueryDatabase($dbh, 'SELECT idservice FROM service_threshold WHERE idservice = ?', $serviceId); if (!$stmt_t->fetch(PDO::FETCH_NUM)) { Connection::QueryDatabase($dbh, 'INSERT INTO service_threshold (idservice) VALUES (?)', $serviceId); } } catch (Exception $e) { Connection::HttpError(500, sprintf(I('Failed to verify weight/threshold for service "%s".'), $serviceId) . '<br/>' . $e->getMessage()); } }
} $out = $stmt->fetchAll(PDO::FETCH_ASSOC); header('Content-Type: application/json'); echo json_encode($out); // list of host groups } else { if ($_POST['r'] == 'hosts') { try { $stmt = Connection::QueryDatabase(Connection::GetDatabase(), "\n\t\t\tSELECT h.hostid, h.name\n\t\t\tFROM hosts h\n\t\t\tINNER JOIN hosts_groups hg ON hg.hostid = h.hostid\n\t\t\tWHERE hg.groupid = {$_POST['group']}\n\t\t\tORDER BY h.name\n\t\t"); } catch (Exception $e) { Connection::HttpError(500, sprintf(I('Failed to query hosts for group %s.'), $_POST['group']) . '<br/>' . $e->getMessage()); } $out = $stmt->fetchAll(PDO::FETCH_ASSOC); header('Content-Type: application/json'); echo json_encode($out); // list of hosts } else { if ($_POST['r'] == 'triggers') { try { $stmt = Connection::QueryDatabase(Connection::GetDatabase(), "\n\t\t\tSELECT t.triggerid, REPLACE(t.description, '{HOSTNAME}', h.host) AS description\n\t\t\tFROM triggers t\n\t\t\tINNER JOIN functions f ON f.triggerid = t.triggerid\n\t\t\tINNER JOIN items i ON i.itemid = f.itemid AND i.hostid = {$_POST['host']}\n\t\t\tINNER JOIN hosts h ON h.hostid = i.hostid\n\t\t\tORDER BY description\n\t\t"); } catch (Exception $e) { Connection::HttpError(500, sprintf(I('Failed to query triggers host %s.'), $_POST['host']) . '<br/>' . $e->getMessage()); } $out = $stmt->fetchAll(PDO::FETCH_ASSOC); header('Content-Type: application/json'); echo json_encode($out); // list of triggers } } } }
goodsla = ? WHERE serviceid = ? ', $data->name, $data->algorithm, $data->triggerid, $data->showsla, $data->goodsla, $data->id); Connection::QueryDatabase($dbh, 'DELETE FROM service_icon WHERE idservice = ?', $data->id); if ($data->imageid !== null) { Connection::QueryDatabase($dbh, 'INSERT INTO service_icon (idservice,idicon) VALUES (?,?)', $data->id, $data->imageid); } $dbh->commit(); } catch (Exception $e) { $dbh->rollback(); Connection::HttpError(500, sprintf(I('Failed to update service with id=%s.<br/>%s'), $data->id, $e->getMessage())); } header('Content-Type: application/json'); echo json_encode(array('status' => 'ok')); // output status } else { if (isset($_REQUEST['serviceId'])) { // --- Query service information. ------------------------------------------ header('Content-Type: application/json'); echo json_encode(ServiceTree::GetInfo($dbh, $_REQUEST['serviceId'])); } else { if (isset($_POST['images'])) { // --- Query all available images from Zabbix. ----------------------------- header('Content-Type: application/json'); echo json_encode(ServiceTree::GetAllImages($dbh)); } else { // --- No request? --------------------------------------------------------- Connection::HttpError(400, I('No retrieve/save service request... what are you trying to do?')); } } }
/** * Outputs latest database error. * @param PDO $dbh Database connection handle. * @param string $msg Additional text to be outputted. */ private static function _TellDbError(PDO $dbh, $msg) { $err = $dbh->errorInfo(); Connection::HttpError(500, "{$msg}<br/>{$err['0']} {$err['2']}"); }
<?php session_start(); require 'inc/Connection.class.php'; require 'inc/ServiceTree.class.php'; require_once '__conf.php'; require_once 'i18n/i18n.php'; i18n_set_map('en', $LANG, false); if (isset($_REQUEST['root'])) { // List of root service names. $dbh = Connection::GetDatabase(); header('Content-Type: application/json'); echo json_encode(ServiceTree::GetRootList($dbh)); } else { if (isset($_REQUEST['serviceId']) && $_REQUEST['serviceId'] != '' || isset($_REQUEST['serviceName']) && $_REQUEST['serviceName'] != '') { // Build and return the service tree. // Service may be queried by its ID or name. $dbh = Connection::GetDatabase(); $serviceId = isset($_REQUEST['serviceId']) && $_REQUEST['serviceId'] != '' ? $_REQUEST['serviceId'] : ServiceTree::GetIdByName($dbh, $_REQUEST['serviceName']); // Output JSON data. $statusCount = array(0, 0, 0, 0, 0, 0); $root = ServiceTree::GetAllToHtml5($dbh, $serviceId, $statusCount); header('Content-Type: application/json'); echo json_encode(array('tree' => $root, 'statusCount' => $statusCount)); } else { Connection::HttpError(400, I('Service tree: no parameters, nothing to query.')); } }
// Requests processing. if (isset($_POST['logoff'])) { // --- Logoff process. ----------------------------------------------------- if (isset($_SESSION['user'])) { unset($_SESSION['user']); // remove session data unset($_SESSION['hash']); } header('Content-Type: application/json'); echo json_encode(array('status' => 'ok')); // output status } else { if (isset($_POST['user']) && isset($_POST['pass'])) { // --- Login process. ------------------------------------------------------ $zabbix = Connection::GetZabbixApi(null); // no hash try { $hash = $zabbix->autenticar($_POST['user'], $_POST['pass']); } catch (Exception $e) { Connection::HttpError(401, sprintf(I('Zabbix login failed for user %s.'), $_POST['user'])); } $_SESSION['user'] = $_POST['user']; // save session data $_SESSION['hash'] = $hash; header('Content-Type: application/json'); echo json_encode(array('status' => 'ok', 'user' => $_POST['user'], 'hash' => $hash)); } else { // --- No request? --------------------------------------------------------- Connection::HttpError(400, I('No parameters... what do you want to do?')); } }