-
Notifications
You must be signed in to change notification settings - Fork 5
/
dash.php
173 lines (153 loc) · 5.42 KB
/
dash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
if ((include('config.php')) == FALSE) {
header("location: no_config.php");
exit;
}
// load the Zabbix Php API which is included in this build (tested on Zabbix v2.2.2)
require 'lib/php/ZabbixApiAbstract.class.php';
require 'lib/php/ZabbixApi.class.php';
// connect to Zabbix Json API
$api = new ZabbixApi($api_url, $api_user, base64_decode($api_pass));
// Set Defaults
$api->setDefaultParams(array(
'output' => 'extend',
));
# parse passed items
if (isset($_GET['gid'])) {
$groupids = explode(',', $_GET['gid']);
} else {
$groupids = array('1');
}
if (isset($_GET['name'])) {
$dashboard = $_GET['name'];
} else {
$dashboard = "Dashboard";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ZbxDash - <?php print $dashboard; ?></title>
<!-- Let's reset the default style properties -->
<link rel="stylesheet" type="text/css" href="<?php print $context; ?>/style/reset.css" />
<link rel="stylesheet" type="text/css" href="<?php print $context; ?>/style/theme-alt.css" />
<!-- added the jQuery library for reloading the page and future features -->
<script src="<?php print $context; ?>/lib/js/jquery-2.1.1.min.js"></script>
<!-- added the masonry js so all blocks are better alligned -->
<script src="<?php print $context; ?>/lib/js/masonry.pkgd.min.js"></script>
<!-- Removed this temporary because I disliked the look -->
<!-- <body class="js-masonry" data-masonry-options='{ "columnWidth": 250, "itemSelector": ".groupbox" }'> -->
</head>
<!-- Second piece of js to gracefully reload the page (value in ms) -->
<script>
function ReloadPage() {
location.reload();
};
$(document).ready(function() {
setTimeout("ReloadPage()", <?php print $reload_time; ?>);
});
</script>
<body id="bg-one">
<!-- START GET RENDER DATE - Which will show date and time of generating this file -->
<div id="timestamp">
<div id="date"><?php echo date("d F Y", time()); ?></div>
<div id="time"><?php echo date("H:i", time()); ?></div>
</div>
<!-- END GET RENDER DATE -->
<!-- We could use the Zabbix HostGroup name here, but would not work in a nice way when using a dozen of hostgroups, yet! So we hardcoded it here. -->
<div id="sheetname"><?php print $dashboard; ?></div>
<?php
$groups = $api->hostgroupGet(array(
'output' => array('name'),
'selectHosts' => array(
'flags',
'hostid',
'name',
'maintenance_status'),
'real_hosts ' => 1,
'groupids' => $groupids,
# 'with_monitored_triggers' => 1,
'sortfield' => 'name'
));
foreach($groups as $group) {
$groupIds[] = $group->groupid;
}
$triggers = $api->triggerGet(array(
'output' => array(
'priority',
'description'),
'selectHosts' => array('hostid'),
'groupids' => $groupIds,
'expandDescription' => 1,
'only_true' => 1,
'monitored' => 1,
'withLastEventUnacknowledged' => 1,
'sortfield' => 'priority',
'sortorder' => 'DESC'
));
foreach($triggers as $trigger) {
foreach($trigger->hosts as $host) {
$hostTriggers[$host->hostid][] = $trigger;
}
}
// get all hosts from each groupid
foreach($groups as $group) {
$groupname = $group->name;
$hosts = $group->hosts;
usort($hosts, function ($a, $b) {
if ($a->name == $b) return 0;
return ($a->name < $b->name ? -1 : 1);
});
echo "<div class=\"groupbox\">"; // Again, we dont want to use the groupfunction yet
echo "<div class=\"group-title\">" . strtoupper(preg_replace('/\//',' / ',$groupname)) . "</div>";
echo "<div class=\"groupbox js-masonry\" data-masonry-options='{ \"itemSelector\": \".hostbox\" }'\">";
if ($hosts) {
// print all host IDs
foreach($hosts as $host) {
// Check if host is not disabled, we don't want them!
if ($host->flags == "0") {
$hostid = $host->hostid;
$hostname = $host->name;
$maintenance = $host->maintenance_status;
if (array_key_exists($hostid, $hostTriggers)) {
// Highest Priority error
$hostboxprio = $hostTriggers[$hostid][0]->priority;
//First filter the hosts that are in maintenance and assign the maintenance class if is true
if ($maintenance != "0") {
echo "<div class=\"hostbox maintenance\">";
} else {
// If hosts are not in maintenance, check for trigger(s) and assign the appropriate class to the box
echo "<div class=\"hostbox nok" . $hostboxprio . "\">";
}
echo "<div class=\"title\">" . $hostname . "</div>";
$count = "0";
foreach ($hostTriggers[$hostid] as $event) {
if ($count++ <= 2 ) {
$priority = $event->priority;
$description = $event->description;
// Remove hostname or host.name in description
$search = array('{HOSTNAME}', '{HOST.NAME}');
$description = str_replace($search, "", $description);
// View
echo "<div class=\"description nok" . $priority ."\" title=\"" . $description . "\">" . $description . "</div>";
} else {
break;
}
}
echo "</div>";
} else {
// If there are no trigger(s) for the host found, assign the "ok" class to the box
echo "<div class=\"hostbox ok\">";
echo "<div class=\"title\">" . $hostname . "</div>";
echo "</div>";
}
}
}
echo "</div></div>";
}
}
#$api->userLogout(); # commented out due to a bug in php and Zabbix
?>
</body>
</html>