- added rrd_pipes to check script

- renamed service_* to *_service
This commit is contained in:
Aaron Daniels
2016-03-22 07:35:39 +10:00
parent 870d3aee56
commit 854812a457
10 changed files with 51 additions and 40 deletions

View File

@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php <?php
/* /*
@@ -36,8 +37,11 @@ else {
// ini_set('error_reporting', 0); // ini_set('error_reporting', 0);
} }
rrdtool_pipe_open($rrd_process, $rrd_pipes);
foreach (dbFetchRows('SELECT * FROM `devices` AS D, `services` AS S WHERE S.device_id = D.device_id ORDER by D.device_id DESC') as $service) { foreach (dbFetchRows('SELECT * FROM `devices` AS D, `services` AS S WHERE S.device_id = D.device_id ORDER by D.device_id DESC') as $service) {
// Run the polling function // Run the polling function
service_poll($service); poll_service($service);
} //end foreach } //end foreach
rrdtool_pipe_close($rrd_process, $rrd_pipes);

View File

@@ -26,7 +26,7 @@ $device_id = mres($vars['device_id']);
if (is_numeric($service_id) && $service_id > 0) { if (is_numeric($service_id) && $service_id > 0) {
// Need to edit. // Need to edit.
$update = array('service_desc' => $desc, 'service_ip' => $ip, 'service_param' => $param); $update = array('service_desc' => $desc, 'service_ip' => $ip, 'service_param' => $param);
if (service_edit($update, $service_id)) { if (edit_service($update, $service_id)) {
$status = array('status' =>0, 'message' => 'Modified Service: <i>'.$service_id.': '.$type.'</i>'); $status = array('status' =>0, 'message' => 'Modified Service: <i>'.$service_id.': '.$type.'</i>');
} }
else { else {
@@ -35,7 +35,7 @@ if (is_numeric($service_id) && $service_id > 0) {
} }
else { else {
// Need to add. // Need to add.
$service_id = service_add($device_id, $type, $desc, $ip, $param); $service_id = add_service($device_id, $type, $desc, $ip, $param);
if ($service_id == false) { if ($service_id == false) {
$status = array('status' =>1, 'message' => 'ERROR: Failed to add Service: <i>'.$type.'</i>'); $status = array('status' =>1, 'message' => 'ERROR: Failed to add Service: <i>'.$type.'</i>');
} }

View File

@@ -19,7 +19,7 @@ else {
$status = array('status' =>1, 'message' => 'No Service has been selected'); $status = array('status' =>1, 'message' => 'No Service has been selected');
} }
else { else {
if (service_delete($vars['service_id'])) { if (delete_service($vars['service_id'])) {
$status = array('status' =>0, 'message' => 'Service: <i>'.$vars['service_id'].', has been deleted.</i>'); $status = array('status' =>0, 'message' => 'Service: <i>'.$vars['service_id'].', has been deleted.</i>');
} }
else { else {

View File

@@ -3,7 +3,7 @@ require $config['install_dir'].'/includes/object-cache.inc.php';
// FIXME - this could do with some performance improvements, i think. possible rearranging some tables and setting flags at poller time (nothing changes outside of then anyways) // FIXME - this could do with some performance improvements, i think. possible rearranging some tables and setting flags at poller time (nothing changes outside of then anyways)
$service_status = service_status(); $service_status = get_service_status();
$if_alerts = dbFetchCell("SELECT COUNT(port_id) FROM `ports` WHERE `ifOperStatus` = 'down' AND `ifAdminStatus` = 'up' AND `ignore` = '0'"); $if_alerts = dbFetchCell("SELECT COUNT(port_id) FROM `ports` WHERE `ifOperStatus` = 'down' AND `ifAdminStatus` = 'up' AND `ignore` = '0'");
if ($_SESSION['userlevel'] >= 5) { if ($_SESSION['userlevel'] >= 5) {

View File

@@ -8,7 +8,7 @@ else {
if ($_SESSION['userlevel'] >= '10') { if ($_SESSION['userlevel'] >= '10') {
$updated = '1'; $updated = '1';
$service_id = service_add($vars['device'], $vars['type'], $vars['descr'], $vars['ip'], $vars['params'], 0); $service_id = add_service($vars['device'], $vars['type'], $vars['descr'], $vars['ip'], $vars['params'], 0);
if ($service_id) { if ($service_id) {
$message .= $message_break.'Service added ('.$service_id.')!'; $message .= $message_break.'Service added ('.$service_id.')!';
$message_break .= '<br />'; $message_break .= '<br />';

View File

@@ -5,7 +5,7 @@ if (is_admin() === true || is_read() === true) {
if ($_SESSION['userlevel'] >= '10') { if ($_SESSION['userlevel'] >= '10') {
$updated = '1'; $updated = '1';
$service_id = service_add($vars['device'], $vars['type'], $vars['descr'], $vars['ip'], $vars['params'], 0); $service_id = add_service($vars['device'], $vars['type'], $vars['descr'], $vars['ip'], $vars['params'], 0);
if ($service_id) { if ($service_id) {
$message .= $message_break.'Service added ('.$service_id.')!'; $message .= $message_break.'Service added ('.$service_id.')!';
$message_break .= '<br />'; $message_break .= '<br />';

View File

@@ -7,7 +7,7 @@ $ports['up'] = dbFetchCell("SELECT COUNT(*) FROM `ports` WHERE device_id =
$ports['down'] = dbFetchCell("SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifOperStatus` = 'down' AND `ifAdminStatus` = 'up'", array($device['device_id'])); $ports['down'] = dbFetchCell("SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifOperStatus` = 'down' AND `ifAdminStatus` = 'up'", array($device['device_id']));
$ports['disabled'] = dbFetchCell("SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifAdminStatus` = 'down'", array($device['device_id'])); $ports['disabled'] = dbFetchCell("SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifAdminStatus` = 'down'", array($device['device_id']));
$services = service_status($device['device_id']); $services = get_service_status($device['device_id']);
$services['total'] = array_sum($services); $services['total'] = array_sum($services);
if ($services[0]) { if ($services[0]) {

View File

@@ -4,6 +4,9 @@ $pagetitle[] = 'Services';
print_optionbar_start(); print_optionbar_start();
require_once 'includes/modal/new_service.inc.php';
require_once 'includes/modal/delete_service.inc.php';
echo "<span style='font-weight: bold;'>Services</span> &#187; "; echo "<span style='font-weight: bold;'>Services</span> &#187; ";
$menu_options = array( $menu_options = array(
@@ -86,15 +89,16 @@ if (isset($state)) {
} }
?> ?>
<div class="row col-sm-12" id="nagios-services"> <div class="row col-sm-12"><span id="message"></span></div>
<table class="table table-hover table-condensed table-striped"> <table class="table table-hover table-condensed table-striped">
<tr> <tr>
<th>Device</th> <th>Device</th>
<th>Service</th> <th>Service</th>
<th>Changed</th> <th>Changed</th>
<th>Message</th> <th>Message</th>
<th>Description</th> <th>Description</th>
</tr> <th>&nbsp;</th>
</tr>
<?php <?php
if ($_SESSION['userlevel'] >= '5') { if ($_SESSION['userlevel'] >= '5') {
$host_sql = 'SELECT * FROM devices AS D, services AS S WHERE D.device_id = S.device_id GROUP BY D.hostname ORDER BY D.hostname'; $host_sql = 'SELECT * FROM devices AS D, services AS S WHERE D.device_id = S.device_id GROUP BY D.hostname ORDER BY D.hostname';
@@ -109,7 +113,7 @@ $shift = 1;
foreach (dbFetchRows($host_sql, $host_par) as $device) { foreach (dbFetchRows($host_sql, $host_par) as $device) {
$device_id = $device['device_id']; $device_id = $device['device_id'];
$device_hostname = $device['hostname']; $device_hostname = $device['hostname'];
$devlink = generate_device_link($device); $devlink = generate_device_link($device,null,array('tab' => 'services'));
if ($shift == 1) { if ($shift == 1) {
array_unshift($sql_param, $device_id); array_unshift($sql_param, $device_id);
$shift = 0; $shift = 0;
@@ -129,18 +133,21 @@ foreach (dbFetchRows($host_sql, $host_par) as $device) {
$status = "<span class='grey'><b>".$service['service_type']."</b></span>"; $status = "<span class='grey'><b>".$service['service_type']."</b></span>";
} }
?> ?>
<tr> <tr id="row_<?=$service['service_id']?>">
<td><?=$devlink?></td> <td><?=$devlink?></td>
<td><?=$status?></td> <td><?=$status?></td>
<td><?=formatUptime(time() - $service['service_changed'])?></td> <td><?=formatUptime(time() - $service['service_changed'])?></td>
<td><span class='box-desc'><?=nl2br(trim($service['service_message']))?></span></td> <td><span class='box-desc'><?=nl2br(trim($service['service_message']))?></span></td>
<td><span class='box-desc'><?=nl2br(trim($service['service_desc']))?></span></td> <td><span class='box-desc'><?=nl2br(trim($service['service_desc']))?></span></td>
</tr> <td>
<button type='button' class='btn btn-primary btn-sm' aria-label='Edit' data-toggle='modal' data-target='#create-service' data-service_id='<?=$service['service_id']?>' name='edit-service'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></button>
<button type='button' class='btn btn-danger btn-sm' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-service_id='<?=$service['service_id']?>' name='delete-service'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></button>
</td>
</tr>
<?php <?php
}//end foreach }//end foreach
unset($samehost); unset($samehost);
}//end foreach }//end foreach
?> ?>
</table> </table>
</div>

View File

@@ -24,7 +24,7 @@ if ($config['discover_services']) {
$split_oid = explode('.', $oid); $split_oid = explode('.', $oid);
$tcp_port = $split_oid[(count($split_oid) - 6)]; $tcp_port = $split_oid[(count($split_oid) - 6)];
if ($known_services[$tcp_port]) { if ($known_services[$tcp_port]) {
service_discover($device, $known_services[$tcp_port]); discover_service($device, $known_services[$tcp_port]);
} }
} }
} }

View File

@@ -1,6 +1,6 @@
<?php <?php
function service_status($device = null) { function get_service_status($device = null) {
$sql_query = "SELECT service_status, count(service_status) as count FROM services WHERE"; $sql_query = "SELECT service_status, count(service_status) as count FROM services WHERE";
$sql_param = array(); $sql_param = array();
$add = 0; $add = 0;
@@ -33,7 +33,7 @@ function service_status($device = null) {
return $service_count; return $service_count;
} }
function service_add($device, $type, $desc, $ip='localhost', $param = "", $ignore = 0) { function add_service($device, $type, $desc, $ip='localhost', $param = "", $ignore = 0) {
if (!is_array($device)) { if (!is_array($device)) {
$device = device_by_id_cache($device); $device = device_by_id_cache($device);
@@ -83,7 +83,7 @@ function service_get($device = null, $service = null) {
return $services; return $services;
} }
function service_edit($update=array(), $service=null) { function edit_service($update=array(), $service=null) {
if (!is_numeric($service)) { if (!is_numeric($service)) {
return false; return false;
} }
@@ -91,7 +91,7 @@ function service_edit($update=array(), $service=null) {
return dbUpdate($update, 'services', '`service_id`=?', array($service)); return dbUpdate($update, 'services', '`service_id`=?', array($service));
} }
function service_delete($service=null) { function delete_service($service=null) {
if (!is_numeric($service)) { if (!is_numeric($service)) {
return false; return false;
} }
@@ -99,17 +99,17 @@ function service_delete($service=null) {
return dbDelete('services', '`service_id` = ?', array($service)); return dbDelete('services', '`service_id` = ?', array($service));
} }
function service_discover($device, $service) { function discover_service($device, $service) {
if (! dbFetchCell('SELECT COUNT(service_id) FROM `services` WHERE `service_type`= ? AND `device_id` = ?', array($service, $device['device_id']))) { if (! dbFetchCell('SELECT COUNT(service_id) FROM `services` WHERE `service_type`= ? AND `device_id` = ?', array($service, $device['device_id']))) {
service_add($device, $service, "(Auto discovered) $service"); add_service($device, $service, "(Auto discovered) $service");
log_event('Autodiscovered service: type '.mres($service), $device, 'service'); log_event('Autodiscovered service: type '.mres($service), $device, 'service');
echo '+'; echo '+';
} }
echo "$service "; echo "$service ";
} }
function service_poll($service) { function poll_service($service) {
global $config, $device; global $config;
$update = array(); $update = array();
$old_status = $service['service_status']; $old_status = $service['service_status'];
@@ -128,7 +128,7 @@ function service_poll($service) {
// Some debugging // Some debugging
d_echo("\nNagios Service - ".$service['service_id']."\n"); d_echo("\nNagios Service - ".$service['service_id']."\n");
d_echo("Request: ".$check_cmd."\n"); d_echo("Request: ".$check_cmd."\n");
list($status, $msg, $perf) = service_check($check_cmd); list($status, $msg, $perf) = check_service($check_cmd);
d_echo("Response: ".$msg."\n"); d_echo("Response: ".$msg."\n");
// TODO: Use proper Nagios service status. 0=Ok,1=Warning,2=Critical,Else=Unknown // TODO: Use proper Nagios service status. 0=Ok,1=Warning,2=Critical,Else=Unknown
@@ -154,7 +154,7 @@ function service_poll($service) {
if (count($perf) > 0) { if (count($perf) > 0) {
// Yes, We have perf data. // Yes, We have perf data.
$filename = "services-".$service['service_id'].".rrd"; $filename = "services-".$service['service_id'].".rrd";
$rrd_filename = $config['rrd_dir'] . "/" . $device['hostname'] . "/" . safename ($filename); $rrd_filename = $config['rrd_dir'] . "/" . $service['hostname'] . "/" . safename ($filename);
// Set the DS in the DB if it is blank. // Set the DS in the DB if it is blank.
$DS = array(); $DS = array();
@@ -198,13 +198,13 @@ function service_poll($service) {
} }
if (count($update) > 0) { if (count($update) > 0) {
service_edit($update,$service['service_id']); edit_service($update,$service['service_id']);
} }
return true; return true;
} }
function service_check($command) { function check_service($command) {
// This array is used to test for valid UOM's to be used for graphing. // This array is used to test for valid UOM's to be used for graphing.
// Valid values from: https://nagios-plugins.org/doc/guidelines.html#AEN200 // Valid values from: https://nagios-plugins.org/doc/guidelines.html#AEN200
// Note: This array must be decend from 2 char to 1 char so that the search works correctly. // Note: This array must be decend from 2 char to 1 char so that the search works correctly.