Browse Source

Last commit was an older version. This is the last one.

master
Rogier Neeleman 12 years ago
parent
commit
478a883c33
  1. 44
      application/controllers/api.php
  2. 2
      application/controllers/config.php
  3. 46
      application/controllers/servers.php
  4. 2
      application/models/logbook_model.php
  5. 23
      application/models/servers_model.php
  6. 10
      application/models/services_model.php
  7. 73
      application/models/stats_model.php
  8. 2
      application/views/footer_view.php
  9. 6
      application/views/header_view.php
  10. 14
      application/views/logbook_view.php
  11. 148
      application/views/servers_show_view.php
  12. 2
      application/views/servers_view.php
  13. 29
      docs/database/mysql-0.1.sql

44
application/controllers/api.php

@ -141,7 +141,9 @@ @@ -141,7 +141,9 @@
echo "</pre>";
}
// In some cases the swapspace is zero, but don't exists in the array.
if (!isset($serverdata['monit']['platform']['swap'])) {$serverdata['monit']['platform']['swap'] = '0';}
// Update the serverinfo.
$this->load->model('serverinfo_model');
$this->serverinfo_model->update($server['id'],
@ -170,35 +172,63 @@ @@ -170,35 +172,63 @@
// check if service is disabled.
if ($service['monitor'] == '0') {
if ($option == 'debug') {echo $server['hostname'].": Service ".$service['name']." disabled.\n";}
$servicestatusmsg = 'Service is set to not monitored';
$servicestatus = 'na';
} else {
// Check if service contains the statistical data.
if ($service['name'] == $serverdata['monit']['server']['localhostname']) {
if ($service['system']['cpu']['wait']) {
$this->save_stats($server['id'],
$service['system']['load']['avg01'],
$service['system']['load']['avg05'],
$service['system']['load']['avg15'],
$service['system']['cpu']['user'],
$service['system']['cpu']['system'],
$service['system']['cpu']['wait'],
$service['system']['memory']['kilobyte'],
$service['system']['swap']['kilobyte']);
} else {
$this->save_stats($server['id'],
$service['system']['load']['avg01'],
$service['system']['load']['avg05'],
$service['system']['load']['avg15'],
$service['system']['cpu']['user'],
$service['system']['cpu']['system'],
'0',
$service['system']['memory']['kilobyte'],
$service['system']['swap']['kilobyte']);
}
}
// If status = 0, then everything is ok.
if ($service['status'] == '0') {
if ($option == 'debug') {echo $server['hostname'].": Service ".$service['name']." OK\n";}
$servicestatus = 'ok';
$servicestatusmsg = 'Servicestatus changed to ok';
// Warning for special items.
} elseif ($service['status'] == '2' || $service['status'] == '32768') {
if ($option == 'debug') {echo $server['hostname'].": Service ".$service['name']." Warning\n";}
if ($serverstatus != 'error') {$serverstatus = 'warning';}
$servicestatus = 'warning';
$servicestatusmsg = $service['status_message'];
// The rest is an error.
} else {
if ($option == 'debug') {echo $server['hostname'].": Service ".$service['name']." Error\n";}
$serverstatus = 'error';
$servicestatus = 'error';
$servicestatusmsg = $service['status_message'];
}
}
// Update the servicestatus information
$this->save_logitem($server['id'], $serviceinfo['id'], $servicestatus, 'Servicestatus changed to '.$servicestatus);
$this->save_logitem($server['id'], $serviceinfo['id'], $servicestatus, $servicestatusmsg);
$this->services_model->update_service_status($serviceinfo['id'], $servicestatus);
}
// Update the server information
$this->load->model('servers_model');
//$prevstatus = $this->servers_model->get_serverstatus($server['id']);
//if () {
$this->save_logitem($server['id'], '0', $serverstatus, 'Serverstatus changed to '.$serverstatus);
$this->servers_model->update_server_status($server['id'], $serverstatus);
@ -213,4 +243,12 @@ @@ -213,4 +243,12 @@
$this->logbook_model->save_logitem($serverid, $serviceid, $status, $message);
}
}
private function save_stats($serverid, $avg01, $avg05, $avg15, $user, $system, $wait, $memory, $swap)
{
$this->load->model('stats_model');
$this->stats_model->update_memory($serverid, $memory, $swap);
$this->stats_model->update_load($serverid, $avg01, $avg05, $avg15);
$this->stats_model->update_cpu($serverid, $user, $system, $wait);
}
}

2
application/controllers/config.php

@ -110,7 +110,7 @@ @@ -110,7 +110,7 @@
private function check_isvalidated(){
if(! $this->session->userdata('validated')){
redirect('login_view');
redirect('login');
}
}

46
application/controllers/servers.php

@ -36,9 +36,53 @@ @@ -36,9 +36,53 @@
$this->load->view('footer_view');
}
public function show($serverid = NULL, $tab = 'overview')
{
// Eerst de header laden.
$this->load->view('header_view');
// Menu laden.
$data['page'] = "servers";
$this->load->view('menu_view', $data);
// Model ladel.
if ($tab == 'overview') {
$this->load->model('servers_model');
$this->load->model('services_model');
$this->load->model('logbook_model');
if ($this->servers_model->check_serverid($serverid)) {
$data['server'] = $this->servers_model->get_server($serverid);
$data['serverinfo'] = $this->servers_model->get_serverinfo($serverid);
$data['services'] = $this->services_model->get_services($serverid);
foreach ($data['services'] as $service) {
$data['logbook'][$service['id']] = $this->logbook_model->get_lastitem($serverid, $service['id']);
}
}
} elseif ($tab = 'statistics') {
$this->load->model('servers_model');
$this->load->model('stats_model');
if ($this->servers_model->check_serverid($serverid)) {
$data['server'] = $this->servers_model->get_server($serverid);
$data['stats_cpu'] = $this->stats_model->get_stats_cpu($serverid);
$data['stats_load'] = $this->stats_model->get_stats_load($serverid);
$data['stats_memory'] = $this->stats_model->get_stats_memory($serverid);
}
}
// View options
$data['tab'] = $tab;
// Pagina laden.
$this->load->view('servers_show_view', $data);
// Als laatste de footer laden.
$this->load->view('footer_view');
}
private function check_isvalidated(){
if(! $this->session->userdata('validated')){
redirect('login_view');
redirect('login');
}
}

2
application/models/logbook_model.php

@ -32,7 +32,7 @@ @@ -32,7 +32,7 @@
public function get_lastitem($serverid, $serviceid)
{
$this->db->select('logbook.status');
$this->db->select('logbook.status, logbook.message, logbook.timestamp');
$this->db->from('logbook');
$this->db->where('serverid', $serverid);
$this->db->where('serviceid', $serviceid);

23
application/models/servers_model.php

@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
public function get_servers($group, $status)
{
$this->db->select('name, status, timestamp, hostname');
$this->db->select('servers.id, name, status, timestamp, hostname');
$this->db->from('servers');
$this->db->where('active', '1');
$this->db->where('groups', $group);
@ -145,4 +145,25 @@ order by timestamp desc @@ -145,4 +145,25 @@ order by timestamp desc
return;
}
public function get_server($id)
{
$this->db->select('id, hostname, groups, name, active, type, desc');
$this->db->from('servers');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->row_array();
}
public function get_serverinfo($id)
{
$this->db->select('id, os, kernel, cputype, cpucount, round(memory/1024/1024, 1) as memory, round(swap/1024/1024, 1) as swap, timestamp', FALSE);
$this->db->from('serverinfo');
$this->db->where('id', $id);
$this->db->order_by('timestamp', 'DESC');
$query = $this->db->get();
return $query->row_array();
}
}

10
application/models/services_model.php

@ -67,4 +67,14 @@ @@ -67,4 +67,14 @@
return;
}
public function get_services($serverid)
{
$this->db->select('service.id, service.active, status_services.status, service.name, status_services.timestamp');
$this->db->from('service');
$this->db->where('serverid', $serverid);
$this->db->join('status_services', 'service.id=status_services.id', 'left');
$query = $this->db->get();
return $query->result_array();
}
}

73
application/models/stats_model.php

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
<?php
class Stats_model extends CI_Model{
public function update_memory($serverid, $memory, $swap)
{
$data = array('id' => $serverid,
'used' => $memory,
'swap' => $swap);
$this->db->insert('stat-memory', $data);
return;
}
public function update_load($serverid, $avg01, $avg05, $avg15)
{
$data = array('id' => $serverid,
'avg01' => $avg01,
'avg05' => $avg05,
'avg15' => $avg15);
$this->db->insert('stat-load', $data);
return;
}
public function update_cpu($serverid, $user, $system, $wait)
{
$data = array('id' => $serverid,
'user' => $user,
'system' => $system,
'wait' => $wait);
$this->db->insert('stat-cpu', $data);
return;
}
public function get_stats_cpu($serverid)
{
$this->db->select('unix_timestamp(timestamp)*1000 AS time, user, system, wait');
$this->db->from('stat-cpu');
$this->db->where('id', $serverid);
$this->db->where('timestamp >=', '(NOW() - interval 2 day)', FALSE);
$this->db->order_by('timestamp', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
public function get_stats_load($serverid)
{
$this->db->select('unix_timestamp(timestamp)*1000 AS time, avg01, avg05, avg15');
$this->db->from('stat-load');
$this->db->where('id', $serverid);
$this->db->where('timestamp >=', '(NOW() - interval 2 day)', FALSE);
$this->db->order_by('timestamp', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
public function get_stats_memory($serverid)
{
$this->db->select('unix_timestamp(timestamp)*1000 AS time, round(used/1024,1) as used, round(swap/1024,1) as swap', FALSE);
$this->db->from('stat-memory');
$this->db->where('id', $serverid);
$this->db->where('timestamp >=', '(NOW() - interval 2 day)', FALSE);
$this->db->order_by('timestamp', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
}

2
application/views/footer_view.php

@ -7,8 +7,6 @@ @@ -7,8 +7,6 @@
</div>
<script src="<?php echo base_url();?>assets/jquery/jquery-latest.js"></script>
<script src="<?php echo base_url();?>assets/bootstrap/js/bootstrap.js"></script>
<script type='text/javascript'>
$(document).ready(function () {
if ($("[rel=tooltip]").length) {

6
application/views/header_view.php

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Dashboard for monit servers">
<meta name="author" content="R.P. Neeleman">
<?php if (strpos(uri_string(), 'servers/overview') || strpos(uri_string(), 'logbook/view') !== false ) { ?>
<?php if (strpos(uri_string(), 'servers/overview') !== false || strpos(uri_string(), 'logbook/view') !== false ) { ?>
<meta http-equiv="refresh" content="30">
<?php } ?>
@ -31,6 +31,10 @@ @@ -31,6 +31,10 @@
<link rel="apple-touch-icon-precomposed" href="<?php echo base_url();?>assets/ico/apple-touch-icon-57-precomposed.png">
<link rel="shortcut icon" href="<?php echo base_url();?>assets/ico/favicon.png">
<script src="<?php echo base_url();?>assets/jquery/jquery-latest.js"></script>
<script src="<?php echo base_url();?>assets/bootstrap/js/bootstrap.js"></script>
<script src="<?php echo base_url();?>assets/flot/jquery.flot.js"></script>
</head>
<body>

14
application/views/logbook_view.php

@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
<div class="row">
<div class="span10 offset1">
<div class="span12">
<div class="pagination">
<ul>
<?php if ($pagination['current'] != '1') {?>
@ -38,12 +38,12 @@ @@ -38,12 +38,12 @@
<table class="table table-hover">
<thead>
<tr>
<th>Time</th>
<th>Group</th>
<th>Server</th>
<th>Service</th>
<th>Status</th>
<th>Message</th>
<th class="span2">Time</th>
<th class="span2">Group</th>
<th class="span2">Server</th>
<th class="span1">Service</th>
<th class="span1">Status</th>
<th class="span4">Message</th>
</tr>
</thead>

148
application/views/servers_show_view.php

@ -0,0 +1,148 @@ @@ -0,0 +1,148 @@
<div class="container">
<h1>Server information</h1>
<div class="row">
<div class="span12">
<ul class="nav nav-tabs">
<?php if ($tab == "overview"){ ?>
<li class="active">
<?php } else { ?>
<li>
<?php } ?>
<a href="<?php echo base_url();?>servers/show/<?php echo $server['id'];?>">Overview</a>
</li>
<?php if ($tab == "statistics"){ ?>
<li class="active">
<?php } else { ?>
<li>
<?php } ?>
<a href="<?php echo base_url();?>servers/show/<?php echo $server['id'];?>/statistics/">Statistics</a>
</li>
<?php if ($tab == "logbook"){ ?>
<li class="active">
<?php } else { ?>
<li>
<?php } ?>
<a href="<?php echo base_url();?>servers/show/<?php echo $server['id'];?>/logbook/">Logbook</a>
</li>
</ul>
</div>
</div>
<?php if ($tab == "overview"){ ?>
<div class="row">
<div class="span5 offset1">
<h3>Information</h3>
<table>
<tbody>
<tr><td>Group:</td><td><?php echo $server['groups'];?></td></tr>
<tr><td>Function:</td><td><?php echo $server['name'];?></td></tr>
<tr><td>Hostname:</td><td><?php echo $server['hostname'];?></td></tr>
<tr><td>OS:</td><td><?php echo $serverinfo['os'];?></td></tr>
<tr><td>Kernel:</td><td><?php echo $serverinfo['kernel'];?></td></tr>
<tr><td>CPU Type:</td><td><?php echo $serverinfo['cputype'];?></td></tr>
<tr><td>CPU's:</td><td><?php echo $serverinfo['cpucount'];?></td></tr>
<tr><td>Memory:</td><td><?php echo $serverinfo['memory'];?> GB</td></tr>
<tr><td>Swapspace:</td><td><?php echo $serverinfo['swap'];?> GB</td></tr>
<tr><td>Last update:</td><td><?php echo $serverinfo['timestamp'];?></td></tr>
<tr><td>Description:</td><td><?php echo $server['desc'];?></td></tr>
</tbody>
</table>
</div>
<div class="span6">
<h3>Services</h3>
<?php foreach ($services as $service) { ?>
<?php $tooltipmsg = $logbook[$service['id']]['timestamp']." ".$logbook[$service['id']]['message'];?>
<p>
<?php if ($service['status'] == "ok") {?>
<span class="badge badge-success" rel="tooltip" title="<?php echo $tooltipmsg;?>"><i class="icon-ok icon-white"></i></span>
<?php } elseif ($service['status'] == "warning") { ?>
<span class="badge badge-warning" rel="tooltip" title="<?php echo $tooltipmsg;?>"><i class="icon-random icon-white"></i></span>
<?php } elseif ($service['status'] == "error") { ?>
<span class="badge badge-important" rel="tooltip" title="<?php echo $tooltipmsg;?>"><i class="icon-remove icon-white"></i></span>
<?php } else { ?>
<span class="badge" rel="tooltip" title="<?php echo $tooltipmsg;?>"><i class="icon-question-sign icon-white"></i></span>
<?php } ?>
<?php echo $service['name'];?></p>
<?php } ?>
</div>
</div>
<?php } elseif ($tab == "statistics") { ?>
<div id="graph_cpu" style="width:600px;height:300px;"></div>
<script id="source">
$(function () {
var user = { label: "user", data: [
<?php foreach ($stats_cpu as $stat) {
echo "[".$stat['time'].", ".$stat['user']."], ";
} ?>
]};
var system = { label: "system", data: [
<?php foreach ($stats_cpu as $stat) {
echo "[".$stat['time'].", ".$stat['system']."], ";
} ?>
]};
var wait = { label: "wait", data: [
<?php foreach ($stats_cpu as $stat) {
echo "[".$stat['time'].", ".$stat['wait']."], ";
} ?>
]};
$.plot($("#graph_cpu"), [user, system, wait], { xaxis: { mode: "time" } });
});
</script>
<div id="graph_load" style="width:600px;height:300px;"></div>
<script id="source">
$(function () {
var avg01 = { label: "1 minute", data: [
<?php foreach ($stats_load as $stat) {
echo "[".$stat['time'].", ".$stat['avg01']."], ";
} ?>
]};
var avg05 = { label: "5 minutes", data: [
<?php foreach ($stats_load as $stat) {
echo "[".$stat['time'].", ".$stat['avg05']."], ";
} ?>
]};
var avg15 = { label: "15 minutes", data: [
<?php foreach ($stats_load as $stat) {
echo "[".$stat['time'].", ".$stat['avg15']."], ";
} ?>
]};
$.plot($("#graph_load"), [avg01, avg05, avg15], { xaxis: { mode: "time" } });
});
</script>
<div id="graph_memory" style="width:600px;height:300px;"></div>
<script id="source">
$(function () {
var memory = { label: "memory", data: [
<?php foreach ($stats_memory as $stat) {
echo "[".$stat['time'].", ".$stat['used']."], ";
} ?>
]};
var swap = { label: "swap", data: [
<?php foreach ($stats_memory as $stat) {
echo "[".$stat['time'].", ".$stat['swap']."], ";
} ?>
]};
$.plot($("#graph_memory"), [memory, swap], { xaxis: { mode: "time" } });
});
</script>
<?php } elseif ($tab == "logbook") { ?>
<?php } ?>
</div>

2
application/views/servers_view.php

@ -48,7 +48,7 @@ @@ -48,7 +48,7 @@
<tr>
<td><span class="badge"><i class="icon-question-sign icon-white"></i></span>
<?php } ?>
<a href="#" rel="tooltip" title="<?php echo $server['timestamp'].' '.$server['hostname']; ?>"><?php echo $server['name']; ?></a></td>
<a href="<?php echo base_url();?>servers/show/<?php echo $server['id'];?>" rel="tooltip" title="<?php echo $server['timestamp'].' '.$server['hostname']; ?>"><?php echo $server['name']; ?></a></td>
</tr>
<?php } ?>
</tbody>

29
docs/database/mysql-0.1.sql

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `api_key` (
`key` varchar(40) NOT NULL,
UNIQUE KEY `key` (`key`)
@ -14,7 +15,7 @@ CREATE TABLE IF NOT EXISTS `logbook` ( @@ -14,7 +15,7 @@ CREATE TABLE IF NOT EXISTS `logbook` (
`message` text NOT NULL,
PRIMARY KEY (`id`),
KEY `timestamp` (`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3924 ;
CREATE TABLE IF NOT EXISTS `serverinfo` (
`id` int(11) NOT NULL,
@ -54,7 +55,7 @@ CREATE TABLE IF NOT EXISTS `service` ( @@ -54,7 +55,7 @@ CREATE TABLE IF NOT EXISTS `service` (
`active` tinyint(1) NOT NULL,
`name` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=940 ;
CREATE TABLE IF NOT EXISTS `settings` (
`name` varchar(32) NOT NULL,
@ -69,6 +70,29 @@ INSERT INTO `settings` (`name`, `value`) VALUES @@ -69,6 +70,29 @@ INSERT INTO `settings` (`name`, `value`) VALUES
('proxyuser', ''),
('proxypassword', '');
CREATE TABLE IF NOT EXISTS `stat-cpu` (
`id` int(11) NOT NULL,
`user` decimal(5,1) NOT NULL,
`system` decimal(5,1) NOT NULL,
`wait` decimal(5,1) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `stat-load` (
`id` int(11) NOT NULL,
`avg01` decimal(5,2) NOT NULL,
`avg05` decimal(5,2) NOT NULL,
`avg15` decimal(5,2) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `stat-memory` (
`id` int(11) NOT NULL,
`used` int(11) NOT NULL,
`swap` int(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `status` (
`id` int(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -94,4 +118,3 @@ CREATE TABLE IF NOT EXISTS `users` ( @@ -94,4 +118,3 @@ CREATE TABLE IF NOT EXISTS `users` (
INSERT INTO `users` (`username`, `password`, `fname`, `lname`, `email`) VALUES
('admin', 'd033e22ae348aeb5660fc2140aec35850c4da997', '', '', '');

Loading…
Cancel
Save