From 478a883c339bf6ebfbe0a9b4d97184c1645233af Mon Sep 17 00:00:00 2001 From: Rogier Neeleman Date: Sun, 5 May 2013 22:04:02 +0200 Subject: [PATCH] Last commit was an older version. This is the last one. --- application/controllers/api.php | 46 +++++++- application/controllers/config.php | 2 +- application/controllers/servers.php | 46 +++++++- application/models/logbook_model.php | 2 +- application/models/servers_model.php | 23 +++- application/models/services_model.php | 10 ++ application/models/stats_model.php | 73 ++++++++++++ application/views/footer_view.php | 2 - application/views/header_view.php | 6 +- application/views/logbook_view.php | 14 +-- application/views/servers_show_view.php | 148 ++++++++++++++++++++++++ application/views/servers_view.php | 2 +- docs/database/mysql-0.1.sql | 29 ++++- 13 files changed, 381 insertions(+), 22 deletions(-) create mode 100755 application/models/stats_model.php create mode 100755 application/views/servers_show_view.php diff --git a/application/controllers/api.php b/application/controllers/api.php index 57b0e97..de7fed7 100755 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -141,7 +141,9 @@ echo ""; } + // 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'], @@ -166,39 +168,67 @@ // Get service id number $this->load->model('services_model'); $serviceinfo = $this->services_model->get_serviceid($server['id'], $service['name']); - + // 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 @@ $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); + } } \ No newline at end of file diff --git a/application/controllers/config.php b/application/controllers/config.php index 215f789..dacf52d 100755 --- a/application/controllers/config.php +++ b/application/controllers/config.php @@ -110,7 +110,7 @@ private function check_isvalidated(){ if(! $this->session->userdata('validated')){ - redirect('login_view'); + redirect('login'); } } diff --git a/application/controllers/servers.php b/application/controllers/servers.php index ee255a1..71aab76 100755 --- a/application/controllers/servers.php +++ b/application/controllers/servers.php @@ -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'); } } diff --git a/application/models/logbook_model.php b/application/models/logbook_model.php index 4d2ffab..ce3555e 100755 --- a/application/models/logbook_model.php +++ b/application/models/logbook_model.php @@ -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); diff --git a/application/models/servers_model.php b/application/models/servers_model.php index 2dc4699..397708b 100755 --- a/application/models/servers_model.php +++ b/application/models/servers_model.php @@ -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 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(); + } + } \ No newline at end of file diff --git a/application/models/services_model.php b/application/models/services_model.php index e22788f..592b414 100755 --- a/application/models/services_model.php +++ b/application/models/services_model.php @@ -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(); + } } \ No newline at end of file diff --git a/application/models/stats_model.php b/application/models/stats_model.php new file mode 100755 index 0000000..ab8a367 --- /dev/null +++ b/application/models/stats_model.php @@ -0,0 +1,73 @@ + $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(); + } + + } \ No newline at end of file diff --git a/application/views/footer_view.php b/application/views/footer_view.php index 51e8de0..75549db 100755 --- a/application/views/footer_view.php +++ b/application/views/footer_view.php @@ -7,8 +7,6 @@ - - + + + diff --git a/application/views/logbook_view.php b/application/views/logbook_view.php index 6aa26b4..b95848e 100755 --- a/application/views/logbook_view.php +++ b/application/views/logbook_view.php @@ -9,7 +9,7 @@
-
+
+
+

Services

+ + +

+ + + + + + + + + +

+ +
+
+ + + +
+ + +
+ + +
+ + + + + + +
diff --git a/application/views/servers_view.php b/application/views/servers_view.php index 5047672..8abc374 100755 --- a/application/views/servers_view.php +++ b/application/views/servers_view.php @@ -48,7 +48,7 @@ - + diff --git a/docs/database/mysql-0.1.sql b/docs/database/mysql-0.1.sql index 0d9916a..131ab0a 100644 --- a/docs/database/mysql-0.1.sql +++ b/docs/database/mysql-0.1.sql @@ -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` ( `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` ( `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 ('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` ( INSERT INTO `users` (`username`, `password`, `fname`, `lname`, `email`) VALUES ('admin', 'd033e22ae348aeb5660fc2140aec35850c4da997', '', '', ''); -