PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

https://github.com/snc/MoZBX
PHP | 319 lines | 261 code | 38 blank | 20 comment | 24 complexity | 4c3a9e9ab75162e76dfa9b3caf1fedce MD5 | raw file
  1. <?php
  2. require_once("config.inc.php");
  3. require_once("functions.php");
  4. require_once("class_zabbix.php");
  5. // Debug
  6. error_reporting(E_ALL);
  7. ini_set("display errors", 1);
  8. // Check for php curl module
  9. if (!function_exists("curl_init")) {
  10. echo "<h2>Missing PHP Curl module</h2>";
  11. exit();
  12. }
  13. // Main Zabbix object
  14. $zabbix = new Zabbix($arrSettings);
  15. // Login
  16. if (!$arrSettings["promptCredentials"]) {
  17. // Login using supplied credentials from the config.php file
  18. $zabbix->login();
  19. }else {
  20. // It's a hosted version, perhaps we can recover username & password from cookies?
  21. $zabbixApi = "";
  22. $zabbixUser = "";
  23. $zabbixPass = "";
  24. // Get the values
  25. require_once("cookies.php");
  26. // Populate our class
  27. $zabbix->setUsername($zabbixUser);
  28. $zabbix->setPassword($zabbixPass);
  29. $zabbix->setZabbixApiUrl($zabbixApi);
  30. // Login
  31. if (strlen($zabbix->getUsername()) > 0 && strlen($zabbix->getPassword()) > 0 && strlen($zabbix->getZabbixApiUrl()) > 0) {
  32. $zabbix->login();
  33. }
  34. }
  35. //$zabbix->Login("mattias_api", "test");
  36. if ($zabbix->isLoggedIn()) {
  37. // Authenticated, save cookie
  38. setcookie("zabbixUsername", $zabbix->getUsername(), $arrSettings["cookieExpire"]);
  39. setcookie("zabbixPassword", $zabbix->getPassword(), $arrSettings["cookieExpire"]);
  40. setcookie("zabbixApi", $zabbix->getZabbixApiUrl(), $arrSettings["cookieExpire"]);
  41. setcookie("zabbixAuthHash", $zabbix->getAuthToken(), $arrSettings["cookieExpire"]);
  42. }
  43. // "templates"
  44. require_once("template/header.php");
  45. if ($zabbix->isLoggedIn()) {
  46. // Retrieve the data in one go
  47. $zabbix_auth = $zabbix->getAuthToken();
  48. $zabbix_version = $zabbix->getVersion();
  49. // Get all active triggers (for the counter on homepage);
  50. $triggersActive = $zabbix->getTriggersActive($arrSettings["minimalSeverity"]);
  51. if (!is_array($triggersActive))
  52. $triggersActive = array();
  53. ?>
  54. <div id="home" class="current">
  55. <div class="toolbar">
  56. <h1><?php echo $arrSettings["mZabbixName"]?></h1>
  57. <a class="button slideup" id="infoButton" href="#settings">Settings</a>
  58. </div>
  59. <ul class="rounded">
  60. <li><a href="#hostgroups"><img src="images/hostgroups.png" class="icon_list">Hosts Overview</a></li>
  61. <li><a href="#activetriggers"><img src="images/trigger.png" class="icon_list">Active Triggers</a> <small class="counter"><?php echo count($triggersActive)?></small></li>
  62. <li><a href="<?php echo $arrSettings["urlApplication"]?>" target="_webapp"><img src="images/refresh.png" class="icon_list">Refresh page</a></li>
  63. </ul>
  64. <ul class="rounded">
  65. <li><a href="<?php echo $arrSettings["urlApplication"]?>logout.php" target="_webapp"><img src="images/logout.png" class="icon_list">Logout</a></li>
  66. <li><a href="feedback.php" target="_webapp"><img src="images/feedback.png" class="icon_list">Send feedback</a></li>
  67. <li><a href="#about"><img src="images/about.png" class="icon_list">About version <?=$arrSettings["mZabbixVersion"]?></a></li>
  68. </ul>
  69. </div>
  70. <div id="settings" class="selectable">
  71. <div class="toolbar">
  72. <h1>Settings</h1>
  73. <a class="back" href="#">Home</a>
  74. </div>
  75. This should, one day, feature some customizable settings.
  76. </div>
  77. <div id="about" class="selectable">
  78. <div class="toolbar">
  79. <h1>About</h1>
  80. <a class="back" href="#">Home</a>
  81. </div>
  82. <ul class="rounded">
  83. <li class="title">Mobile Zabbix</li>
  84. <li class="small">
  85. Mobile Zabbix version <b><?php echo $arrSettings["mZabbixVersion"]?></b><br />
  86. In development by <b>Mattias Geniar</b>
  87. </li>
  88. <li class="title">Changelog: version 0.3</li>
  89. <li class="small">
  90. <u>New features</u> <br />
  91. Added support for Trigger Acknowledgements<br />
  92. Modified layout for detail items (smaller)<br />
  93. Ability to change the time-period on graphs<br />
  94. <br />
  95. <u>Bugfixes</u><br />
  96. Feedback form should work again<br />
  97. Logout button should work again<br />
  98. Refresh page should work again<br />
  99. </li>
  100. <li class="title">Changelog: version 0.2</li>
  101. <li class="small">
  102. <u>New features</u> <br />
  103. Listing active triggers<br />
  104. Retrieving host graphs<br />
  105. Browsing hostgroups &amp; hosts<br />
  106. </li>
  107. <li class="title">Zabbix Server</li>
  108. <li class="small">
  109. Zabbix API version <b><?php echo $zabbix_version?></b> on the server.<br />
  110. Retrieved data from <b><?php echo $arrSettings["zabbixHostname"]?></b>.<br />
  111. You are logged in as <b><?php echo $zabbix->getUsername()?></b>.<br />
  112. Your current session-id is <b><?php echo $zabbix->getAuthToken()?></b><br />
  113. </li>
  114. </ul>
  115. </div>
  116. <div id="hostgroups" class="selectable">
  117. <div class="toolbar">
  118. <h1>Hostgroups</h1>
  119. <a class="back" href="#">Home</a>
  120. </div>
  121. <ul class="rounded">
  122. <?php
  123. $zabbixHostgroups = $zabbix->getHostgroups();
  124. $zabbixHostgroups = $zabbix->sortHostgroupsByName($zabbixHostgroups);
  125. if (is_array($zabbixHostgroups) && count($zabbixHostgroups) > 0) {
  126. foreach ($zabbixHostgroups as $groupobject) {
  127. // Start list item
  128. $linkHostgroup = "hosts.php?hostgroupid=". $groupobject["groupid"];
  129. $hosts = $zabbix->getHostsByGroupId ($groupobject["groupid"]);
  130. $hosts = $zabbix->filterActiveHosts($hosts);
  131. $hostCount = is_array($hosts) ? count($hosts) : 0;
  132. if ($arrSettings["showEmptyHostgroups"] || $hostCount > 0) {
  133. ?>
  134. <li>
  135. <a href="<?=$linkHostgroup?>">
  136. <img src="images/hosts.png" class="icon_list"><?=$groupobject["name"]?>
  137. </a>
  138. <small class="counter"><?=$hostCount?></small>
  139. </li>
  140. <?php
  141. }
  142. }
  143. } else {
  144. ?>
  145. Sorry, you don't seem have access to any hostgroups.<br />
  146. <br />
  147. Does the user with which you login, have <b>API Access</b> enabled in the Zabbix <b>User Administration</b> screen?
  148. <?php
  149. }
  150. ?>
  151. </ul>
  152. </div>
  153. <div id="activetriggers" class="selectable">
  154. <div class="toolbar">
  155. <h1>Triggers</h1>
  156. <a class="back" href="#">Home</a>
  157. </div>
  158. <?php
  159. if (count($triggersActive) > 0) {
  160. // First, group our active triggers per host
  161. $arrSortedTriggers = array();
  162. foreach ($triggersActive as $triggerActive) {
  163. if (array_key_exists("hosts", $triggerActive) && array_key_exists(0, $triggerActive["hosts"])) {
  164. $arrSortedTriggers[$triggerActive["hosts"][0]->hostid]["host"] = $triggerActive["hosts"][0]->host;
  165. $arrSortedTriggers[$triggerActive["hosts"][0]->hostid]["triggers"][] =
  166. array(
  167. "description" => $triggerActive["description"],
  168. "comments" => $triggerActive["comments"],
  169. "lastchange" => $triggerActive["lastchange"],
  170. "priority" => $triggerActive["priority"],
  171. "triggerid" => $triggerActive["triggerid"],
  172. );
  173. }
  174. }
  175. asort($arrSortedTriggers);
  176. ?>
  177. <ul class="rounded">
  178. <?php
  179. foreach ($arrSortedTriggers as $hostid => $arrTriggers) {
  180. ?>
  181. <li>
  182. <a href="host.php?hostid=<?=$hostid?>">
  183. <img src="images/host.png" class="icon_list"><?=$arrTriggers["host"]?>
  184. </a>
  185. </li>
  186. <?php
  187. foreach ($arrTriggers["triggers"] as $arrTrigger) {
  188. $trigger_description = cleanTriggerDescription($arrTrigger["description"]);
  189. ?>
  190. <li class="severity_<?=$arrTrigger["priority"]?>">
  191. <a href="trigger_info.php?triggerid=<?=$arrTrigger["triggerid"]?>&hostid=<?=$hostid?>">
  192. <?=$trigger_description?>
  193. </a>
  194. </li>
  195. <?php
  196. }
  197. }
  198. ?>
  199. </ul>
  200. <?php
  201. } else {
  202. ?>
  203. <ul class="rounded">There don't seem to be any active triggers.</ul>
  204. <?php
  205. }
  206. ?>
  207. </div>
  208. <?php
  209. } else {
  210. // Show login screen
  211. $zabbixUser = isset($_GET['username']) && strlen($_GET['username']) > 0 ? $_GET['username'] : $zabbixUser;
  212. $zabbixApi = isset($_GET['api_url']) && strlen($_GET['api_url']) > 0 ? $_GET['api_url'] : $zabbixApi;
  213. if (strpos($zabbixApi, 'api_jsonrpc.php') === false)
  214. $zabbixApi .= "api_jsonrpc.php";
  215. $zabbixHideApi = isset($_GET['hideapi']) ? 'hideapi' : 'donthideapi';
  216. $zabbixPass = isset($_GET['password']) ? $_GET['password'] : $arrSettings["zabbixPassword"];
  217. ?>
  218. <div id="home_login" class="current">
  219. <div class="toolbar">
  220. <h1><?php echo $arrSettings["mZabbixName"]?></h1>
  221. </div>
  222. <ul class="rounded">
  223. <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>" class="form" >
  224. <?php
  225. if ($arrSettings["isHosted"] && !isset($_GET['hideapi'])) {
  226. // Hosted version, show input field for the JSON API
  227. ?>
  228. <li>
  229. API URL<br />
  230. <input type='text' name='zabbixApi' value='<?php echo isset($_POST['zabbixApi']) ? $_POST['zabbixApi'] : $zabbixApi ?>' style="<?php echo $arrSettings["cssStyleTextfield"]?>" />
  231. </li>
  232. <?php
  233. } else {
  234. // Fixed setup. Enter JSON API as hidden field
  235. ?>
  236. <input type='hidden' name='zabbixApi' value='<?php echo isset($_POST['zabbixApi']) ? $_POST['zabbixApi'] : $zabbixApi ?>' />
  237. <?php
  238. }
  239. ?>
  240. <li>
  241. Please enter your Zabbix ID.
  242. </li>
  243. <li>
  244. User<br />
  245. <input type="text" name="zabbixUsername" value="<?php echo isset($_POST['zabbixUsername']) ? $_POST['zabbixUsername'] : $zabbixUser?>" style="<?php echo $arrSettings["cssStyleTextfield"]?>" />
  246. </li>
  247. <li>
  248. Password <br />
  249. <input type="password" name="zabbixPassword" value="<?php echo isset($_POST['zabbixPassword']) ? $_POST['zabbixPassword'] : $zabbixPass?>" style="<?php echo $arrSettings["cssStyleTextfield"]?>" />
  250. </li>
  251. <?php
  252. if (isset($_POST['zabbixUsername'])) {
  253. // POST variables set, but still showint the form. Invalid credentials?
  254. if (count($zabbix->getLastError()) > 0) {
  255. $arrError = $zabbix->getLastError();
  256. $errormsg = $arrError["data"];
  257. //print_r($arrError);
  258. } else
  259. $errormsg = "invalid combination";
  260. ?>
  261. <li>
  262. <font color="red"><?php echo $errormsg?></font>
  263. </li>
  264. <?php
  265. }
  266. ?>
  267. <li>
  268. <input type="submit" name="mZabbixLogin" value="Login" style="<?php echo $arrSettings["cssStyleButton"]?>" onclick="submit()" />
  269. </li>
  270. </form>
  271. </ul>
  272. </div>
  273. <?php
  274. }
  275. require_once("template/footer.php");
  276. ?>