Merge pull request #3102 from connortechnology/fix_3101

fix api monitor save daemon control
This commit is contained in:
Isaac Connor 2020-12-25 13:29:01 -05:00 committed by GitHub
commit a362f2abc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 49 deletions

View File

@ -156,32 +156,21 @@ class MonitorsController extends AppController {
return; return;
} }
$monitor = $this->Monitor->find('first', array(
'conditions' => array('Id' => $id)
))['Monitor'];
$message = ''; $message = '';
if ( $this->Monitor->save($this->request->data) ) { if ( $this->Monitor->save($this->request->data) ) {
$message = 'Saved'; $message = 'Saved';
$Monitor = $this->Monitor->find('first', array(
'fields' => array('Function','ServerId'), // Stop the monitor. Should happen before saving
$this->Monitor->daemonControl($monitor, 'stop');
$monitor = $this->Monitor->find('first', array(
'conditions' => array('Id' => $id) 'conditions' => array('Id' => $id)
))['Monitor']; ))['Monitor'];
// - restart or stop this monitor after change $this->Monitor->daemonControl($monitor, 'start');
$func = $Monitor['Function'];
// We don't pass the request data as the monitor object because it may be a subset of the full monitor array
$this->daemonControl($this->Monitor->id, 'stop');
if (
( $func != 'None' )
and
(
(!defined('ZM_SERVER_ID'))
or
($Monitor['ServerId']==ZM_SERVER_ID)
)
) {
if ( !defined('ZM_SERVER_ID')) {
ZM\Debug("Not defined ZM_SERVER_ID");
}
$this->daemonControl($this->Monitor->id, 'start');
}
} else { } else {
$message = 'Error ' . print_r($this->Monitor->invalidFields(), true); $message = 'Error ' . print_r($this->Monitor->invalidFields(), true);
} }
@ -353,7 +342,6 @@ class MonitorsController extends AppController {
} }
public function daemonControl($id, $command, $daemon=null) { public function daemonControl($id, $command, $daemon=null) {
// Need to see if it is local or remote // Need to see if it is local or remote
$monitor = $this->Monitor->find('first', array( $monitor = $this->Monitor->find('first', array(
'fields' => array('Type', 'Function', 'Device'), 'fields' => array('Type', 'Function', 'Device'),
@ -361,35 +349,8 @@ class MonitorsController extends AppController {
)); ));
$monitor = $monitor['Monitor']; $monitor = $monitor['Monitor'];
$daemons = array(); $status_text = $this->Monitor->daemonControl($monitor, $command, $daemon);
if ( ! $daemon ) {
if ( $monitor['Function'] == 'Monitor' ) {
array_push($daemons, 'zmc');
} else {
array_push($daemons, 'zmc', 'zma');
}
} else {
array_push($daemons, $daemon);
}
$zm_path_bin = Configure::read('ZM_PATH_BIN');
$status_text = '';
foreach ( $daemons as $daemon ) {
$args = '';
if ( $daemon == 'zmc' and $monitor['Type'] == 'Local' ) {
$args = '-d ' . $monitor['Device'];
} else if ( $daemon == 'zmcontrol.pl' ) {
$args = '--id '.$id;
} else {
$args = '-m ' . $id;
}
$shellcmd = escapeshellcmd("$zm_path_bin/zmdc.pl $command $daemon $args");
ZM\Debug("Command $shellcmd");
$status = exec($shellcmd);
$status_text .= $status."\n";
}
$this->set(array( $this->set(array(
'status' => 'ok', 'status' => 'ok',
'statustext' => $status_text, 'statustext' => $status_text,

View File

@ -136,4 +136,44 @@ class Monitor extends AppModel {
'joinTable' => 'Monitor_Status', 'joinTable' => 'Monitor_Status',
) )
); );
public function daemonControl($monitor, $command, $daemon=null) {
if ( $monitor['Function'] == 'None' ) {
ZM\Debug('Calling daemonControl when Function == None');
return;
}
if ( defined('ZM_SERVER_ID') and ($monitor['ServerId']!=ZM_SERVER_ID) ) {
ZM\Debug('Calling daemonControl for Monitor assigned to different server');
return;
}
$daemons = array();
if ( ! $daemon ) {
if ( $monitor['Function'] == 'Monitor' ) {
array_push($daemons, 'zmc');
} else {
array_push($daemons, 'zmc', 'zma');
}
} else {
array_push($daemons, $daemon);
}
$status_text = '';
foreach ( $daemons as $daemon ) {
$args = '';
if ( $daemon == 'zmc' and $monitor['Type'] == 'Local' ) {
$args = '-d ' . $monitor['Device'];
} else if ( $daemon == 'zmcontrol.pl' ) {
$args = '--id '.$monitor['Id'];
} else {
$args = '-m ' . $monitor['Id'];
}
$shellcmd = escapeshellcmd(ZM_PATH_BIN.'/zmdc.pl '.$command.' '.$daemon.' '.$args);
ZM\Debug("Command $shellcmd");
$status = exec($shellcmd);
$status_text .= $status.PHP_EOL;
} # end foreach daemon
return $status_text;
} # end function daemonControl
} }