Commit b38c0e60 authored by Seth D. Galitzer's avatar Seth D. Galitzer

import and sanitize files

parents
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.
require('../../js/transition.js')
require('../../js/alert.js')
require('../../js/button.js')
require('../../js/carousel.js')
require('../../js/collapse.js')
require('../../js/dropdown.js')
require('../../js/modal.js')
require('../../js/tooltip.js')
require('../../js/popover.js')
require('../../js/scrollspy.js')
require('../../js/tab.js')
require('../../js/affix.js')
\ No newline at end of file
<?php
$icinga_host = "your.icinga2.host.org"
$icinga_port = 5665 // change if using non-standard port
$icinga_api_path = "/v1/objects/hosts"
$icinga_url = "https://" . $icinga_host . ":" . $icinga_port . $icinga_api_path;
$icinga_user = "username";
$icinga_pass = "password";
// modify hostgroups in this list to include those you want to display
$icinga_groups = [
"group1",
"group2",
"group3"
];
// mysql database connection info
$mysql_host = "your.mysql.host.org";
$mysql_user = "mysqlusername";
$mysql_pass = "mysqlpassword";
$mysql_db = "mysqldb";
// used by WOL function
$socket = 7;
?>
/* need this to pad for fixed navbar */
body { padding-top: 80px; }
--
-- Table structure for table `cnames`
--
CREATE TABLE `cnames` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`host_id` int(11) DEFAULT NULL,
`cname` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `host_id` (`host_id`),
CONSTRAINT `cnames_ibfk_1` FOREIGN KEY (`host_id`) REFERENCES `hosts` (`id`)
);
--
-- Table structure for table `groups`
--
CREATE TABLE `groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`group_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
);
--
-- Table structure for table `hosts`
--
CREATE TABLE `hosts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`host_name` varchar(100) DEFAULT NULL,
`mac` varchar(20) DEFAULT NULL,
`ip` varchar(20) DEFAULT NULL,
`group_id` int(11) DEFAULT '1',
PRIMARY KEY (`id`),
KEY `group_id` (`group_id`),
CONSTRAINT `hosts_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`)
);
<?php
function getJSON() {
require "config.php";
$headers = array(
'Accept: application/json',
'X-HTTP-Method-Override: GET'
);
$filter = "";
foreach ($icinga_groups as $group) {
$filter = $filter . '("' . $group .'" in host.groups)||';
}
$filter = substr($filter, 0 , -2);
$data = array(
'attrs' => array('name', 'state'),
'filter' => $filter,
);
$hosts = array();
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $icinga_url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERPWD => $icinga_user . ":" . $icinga_pass,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPAUTH => true,
CURLAUTH_BASIC => true,
CURLOPT_POST => count($data),
CURLOPT_POSTFIELDS => json_encode($data)
));
$response = curl_exec($ch);
if ($response === false) {
print "Error: " . curl_error($ch) . "(" . $response . ")\n";
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code == 200) {
return json_decode($response, true);
} else {
return array("Error retrieving data");
}
}
function getData($table) {
require "config.php";
$ret = array();
$conn = mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db);
if ($conn === false) {
array_push($ret, mysqli_connect_error());
} else {
$sql = "select * from $table";
$result = mysqli_query($conn,$sql);
if ($result === false) {
array_push($ret, "No data for $table");
} else {
while ($row = mysqli_fetch_assoc($result)) {
array_push($ret, $row);
}
}
}
return $ret;
}
function getHostStates($hoststates) {
$ret = array();
foreach ($hoststates["results"] as $state) {
$ret[$state["attrs"]["name"]] = $state["attrs"]["state"];
}
return $ret;
}
function getHosts($rawhosts, $hoststates, $cnames) {
$ret = array();
foreach ($rawhosts as $rawhost) {
if (array_key_exists($rawhost["id"], $cnames)) {
$cn = $cnames[$rawhost["id"]];
} else {
$cn = array();
}
$ret[$rawhost["id"]] = array(
"id" => $rawhost["id"],
"name" => $rawhost["host_name"],
"mac" => $rawhost["mac"],
"ip" => $rawhost["ip"],
"group_id" => $rawhost["group_id"],
"state" => $hoststates[$rawhost["host_name"]],
"cnames" => $cn
);
}
return $ret;
}
function getHostGroups($rawgroups) {
$ret = array();
foreach ($rawgroups as $rawgroup) {
$ret[$rawgroup["id"]] = $rawgroup["group_name"];
}
return $ret;
}
function getCNames($rawcnames) {
$ret = array();
foreach ($rawcnames as $rawcname) {
if (!array_key_exists($rawcname["host_id"], $ret)) {
$ret[$rawcname["host_id"]] = array();
}
array_push($ret[$rawcname["host_id"]], $rawcname["cname"]);
}
return $ret;
}
function getState($state) {
if ($state == 0) {
return "success";
} elseif ($state ==1) {
return "danger";
} else {
return "warning";
}
}
function getHostsFromGroup($hosts, $hostgroup) {
$ret = array();
$subset = array_filter($hosts,
function ($h) use ($hostgroup) {
return ($h["group_id"] == $hostgroup);
}
);
foreach ($subset as $host_id => $host) {
array_push($ret, $host_id);
}
return json_encode($ret);
}
function getHostIDs($hosts) {
$ret = array();
foreach ($hosts as $host_id => $host) {
array_push($ret, $host_id);
}
return $ret;
}
function sortHostnames($a, $b){
return strnatcmp($a["name"], $b["name"]);
}
function printCnames($host) {
if (count($host["cnames"]) > 0) {
return "(" . implode(",", $host["cnames"]) . ")";
} else {
return "";
}
}
function printGroup($hosts) {
$i=1;
usort($hosts, "sortHostnames");
print "<div class=\"row\">\n";
foreach($hosts as $host){
print " <div class=\"col-lg-2\">\n";
print " <div class=\"alert alert-" . getState($host["state"]) . "\">\n";
print " <a href=\"wake.php?host=" . json_encode(array($host["id"]), JSON_NUMERIC_CHECK) . "\" class=\"alert-link\"><strong>" . $host["name"] . "</strong> " . printCnames($host) . "</a><br>\n";
print " IP: $host[ip]<br>\n";
print " MAC: $host[mac]<br>\n";
print " </div><!-- alert -->\n";
print " </div><!-- col -->\n";
if(($i%6) == 0) {
print "</div><!-- row -->\n";
print "<div class=\"clearfix visible-lg-block\"></div>\n";
print "<div class=\"row\">\n";
$i=1;
} else {
$i+=1;
}
};
print "</div><!-- row -->\n";
}
function printHosts($hosts, $hostgroups, $hostgroup) {
$displaygroups = array();
if (isset($hostgroup)) {
$displaygroups[$hostgroup] = $hostgroups[$hostgroup];
} else {
$displaygroups = $hostgroups;
}
foreach ($displaygroups as $group_id => $groupname) {
print "<h2>$groupname</h2>\n";
print "<a href=\"wake.php?host=" . getHostsFromGroup($hosts, $group_id) . "\" role=\"button\" class=\"btn btn-primary\">Wake all in this group</a>\n";
print "<p>\n";
printGroup(array_filter($hosts,
function ($h) use ($group_id) {
return ($h["group_id"] == $group_id);
}
));
}
}
function WakeOnLan($addr, $mac, $socket_number) {
/* borrowed from http://www.hackernotcracker.com/2006-04/wol-wake-on-lan-tutorial-with-bonus-php-script.html
Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
Modified by Allan Barizo http://www.hackernotcracker.com
*/
$addr_byte = explode(':', $mac);
$hw_addr = '';
for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
error_log("Error creating socket!");
error_log("Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s)));
return FALSE;
} else {
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret <0) {
error_log("setsockopt() failed, error: " . strerror($opt_ret));
return FALSE;
}
if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
error_log("Magic Packet sent successfully!");
socket_close($s);
return TRUE;
}
else {
error_log("Magic packet failed!");
return FALSE;
}
}
}
function wakeHosts($hosts, $wakelist, $socket) {
$ret = array();
foreach ($wakelist as $host) {
$result = WakeOnLan($hosts[$host]["ip"], $hosts[$host]["mac"], $socket);
$ret[$host] = $result;
}
return $ret;
}
?>
<?php
require_once "config.php";
require_once "handler.php";
$hoststates = getHostStates(getJSON());
$cnames = getCNames(getData("cnames"));
$hosts = getHosts(getData("hosts"), $hoststates, $cnames);
$hostgroups = getHostGroups(getData("groups"));
$hostgroup=NULL;
if (!empty($_GET)) {
if (isset($_GET["hostgroup"])) {
$hostgroup=htmlspecialchars($_GET["hostgroup"]);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Desktop Status</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/deskmon.css" />
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php include "nav.php"; ?>
<div class="container">
<div class="row collapse <?php if ($hostgroup==NULL) { print "in"; } ?>">
<div class="span12">
<a href="wake.php?host=<?php print json_encode(getHostIDs($hosts)); ?>" role="button" class="btn btn-primary">Wake all hosts</a>
</div><!-- /.span12 -->
</div><!-- /.row -->
<div class="row">
<div class="span12">
<?php printHosts($hosts, $hostgroups, $hostgroup); ?>
</div><!-- /.span12 -->
</div><!-- /.row -->
</div><!-- /.container -->
</body>
</html>
<?php
/*
* Kansas State University
* Computer Science
*
* CS System Monitoring
*
* nav.php
*
* prepare and display navagation header used for all pages
*
* Created by Seth Galitzer <sgsax@ksu.edu>
* Date Created: 10/22/2014
* Updated: 10/17/2016
*/
require_once "config.php";
require_once "handler.php";
function echoActiveClassIfRequestMatches($requestUri) {
/* highlight menu item for current page */
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
if ($current_file_name == $requestUri) {
echo 'class="active"';
}
}
function buildHostgroupMenu() {
/* get list of all hostgroups and generate list of links to show data for */
/* each of them; used in drop-down menu item */
$hostgrps = getData("groups");
foreach($hostgrps as $hostgrp) {
print "<li><a href=\"/index.php?hostgroup=$hostgrp[id]\">$hostgrp[group_name]</a></li>\n";
}
}
?>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Desktop Status</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li <?=echoActiveClassIfRequestMatches("")?>><a href="/">All Hosts</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Hostgroups <span class="caret"></span></a>
<ul class="dropdown-menu scrollable-menu" role="menu">
<li><a href="/">All</a></li>
<li class="divider"></li>
<?php buildHostgroupMenu() ?>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<?php
require_once "config.php";
require_once "handler.php";
$hoststates = getHostStates(getJSON());
$cnames = getCNames(getData("cnames"));
$hosts = getHosts(getData("hosts"), $hoststates, $cnames);
$hostgroups = getHostGroups(getData("groups"));
$wakelist=NULL;
if (!empty($_GET)) {
if (isset($_GET["host"])) {
$wakelist=json_decode(htmlspecialchars($_GET["host"]));
}
}
$result = wakeHosts($hosts, $wakelist, $socket);
?>
<!DOCTYPE html>
<html>
<head>
<title>Desktop Status - WOL</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/deskmon.css" />
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php include "nav.php"; ?>
<div class="container">
<div class="row">
<div class="span12">
<p class="lead">WakeOnLan Result</p>
<?php
foreach($wakelist as $host) {
print "<p>Magic packet sent to " . $hosts[$host]["name"] . " - " . $hosts[$host]["ip"] . ", " . $hosts[$host]["mac"] . " : ";
if ($result[$host]) {
print "Succeeded";
} else {
print "Failed";
}
print "</p>\n";
};
?>
</div><!-- /.span12 -->
</div><!-- /.row -->
</div><!-- /.container -->
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment