Merge branch 'master' into feature/sg

Conflicts:
	VERSION.json
pull/762/head
Iglocska 2015-07-22 17:19:13 +02:00
commit 1bf2995f4e
7 changed files with 221 additions and 78 deletions

View File

@ -13,4 +13,4 @@ CREATE TABLE cake_sessions (
data text,
expires int(11) default NULL,
PRIMARY KEY (id)
);
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

View File

@ -517,31 +517,10 @@ class ServersController extends AppController {
);
$viewVars = array_merge($viewVars, $additionalViewVars);
foreach ($viewVars as $viewVar) $this->set($viewVar, ${$viewVar});
$workerIssueCount = 0;
if (Configure::read('MISP.background_jobs')) {
$worker_array = array(
'cache' => array(),
'default' => array(),
'email' => array(),
'_schdlr_' => array()
);
// disable notice errors, getWorkers() is meant to be run from the command line and throws a notice
// because STDIN is not defined - since we don't actually log anything this is safe to ignore.
$error_reporting = error_reporting();
error_reporting(0);
$results = CakeResque::getWorkers();
error_reporting($error_reporting);
foreach ($results as $result) {
$result = (array)$result;
if (in_array($result["\0*\0queues"][0], array_keys($worker_array))) {
$worker_array[$result["\0*\0queues"][0]][] = $result["\0*\0id"];
}
}
$workerIssueCount = 0;
foreach ($worker_array as $k => $queue) {
if (empty($queue)) $workerIssueCount++;
}
$this->set('worker_array', $worker_array);
$this->set('worker_array', $this->Server->workerDiagnostics($workerIssueCount));
} else {
$workerIssueCount = 4;
$this->set('worker_array', array());
@ -564,6 +543,21 @@ class ServersController extends AppController {
$this->set('priorityErrorColours', $priorityErrorColours);
}
}
public function startWorker($type) {
if (!$this->_isSiteAdmin() || !$this->request->is('Post')) throw new MethodNotAllowedException();
$validTypes = array('default', 'email', 'scheduler', 'cache');
if (!in_array($type, $validTypes)) throw new MethodNotAllowedException('Invalid worker type.');
if ($type != 'scheduler') shell_exec(APP . 'Console' . DS . 'cake ' . DS . 'CakeResque.CakeResque start --interval 5 --queue ' . $type .' > /dev/null &');
else shell_exec(APP . 'Console' . DS . 'cake ' . DS . 'CakeResque.CakeResque startscheduler -i 5 > /dev/null &');
$this->redirect('/servers/serverSettings/workers');
}
public function stopWorker($pid) {
if (!$this->_isSiteAdmin() || !$this->request->is('Post')) throw new MethodNotAllowedException();
$this->Server->killWorker($pid, $this->Auth->user());
$this->redirect('/servers/serverSettings/workers');
}
private function __checkVersion() {
if (!$this->_isSiteAdmin()) throw new MethodNotAllowedException();
@ -691,7 +685,8 @@ class ServersController extends AppController {
}
public function restartWorkers() {
if (!$this->_isSiteAdmin()) throw new MethodNotAllowedException();
if (!$this->_isSiteAdmin() || !$this->request->is('post')) throw new MethodNotAllowedException();
$this->Server->workerRemoveDead($this->Auth->user());
shell_exec(APP . 'Console' . DS . 'worker' . DS . 'start.sh > /dev/null &');
$this->redirect(array('controller' => 'servers', 'action' => 'serverSettings', 'workers'));
}
@ -829,7 +824,8 @@ class ServersController extends AppController {
else return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Could not kill the previous instance of the ZeroMQ script.')),'status'=>200));
}
public function statusZeroMQServer() {
private function statusZeroMQServer() {
if (!$this->_isSiteAdmin()) throw new MethodNotAllowedException();
App::uses('PubSubTool', 'Tools');
$pubSubTool = new PubSubTool();
$result = $pubSubTool->statusCheck();

View File

@ -124,29 +124,24 @@ class ComplexTypeTool {
// check for domain name, hostname, filename
if (strpos($input, '.') !== false) {
$extra = '';
$temp = explode('.', $input);
if (strpos($temp[0], ':')) {
$extra = '([a-z0-9]+):\/\/';
}
// check if it is a URL
// Adding http:// infront of the input in case it was left off. github.com/MISP/MISP should still be counted as a valid link
if (filter_var($input2, FILTER_VALIDATE_URL) || filter_var('http://' . $input2, FILTER_VALIDATE_URL)) {
if (preg_match('/^https:\/\/www.virustotal.com\//i', $input2)) return array('types' => array('link'), 'to_ids' => true, 'default_type' => 'link', 'comment' => $comment, 'value' => $input2);
return array('types' => array('url'), 'to_ids' => true, 'default_type' => 'url', 'comment' => $comment, 'value' => $input2);
}
//if (filter_var($input, FILTER_VALIDATE_URL)) {
if (preg_match('/^' . $extra . '([-\pL\pN]+\.)+([a-z][a-z]|biz|cat|com|edu|gov|int|mil|net|org|pro|tel|aero|arpa|asia|coop|info|jobs|mobi|name|museum|travel)(:[0-9]{2,5})?$/u', $input)) {
if (preg_match('/^([-\pL\pN]+\.)+([a-z][a-z]|biz|cat|com|edu|gov|int|mil|net|org|pro|tel|aero|arpa|asia|coop|info|jobs|mobi|name|museum|travel)(:[0-9]{2,5})?$/u', $input)) {
if (count($temp) > 2) {
return array('types' => array('hostname', 'domain'), 'to_ids' => true, 'default_type' => 'hostname', 'comment' => $comment, 'value' => $input2);
return array('types' => array('hostname', 'domain', 'url'), 'to_ids' => true, 'default_type' => 'hostname', 'comment' => $comment, 'value' => $input2);
} else {
return array('types' => array('domain'), 'to_ids' => true, 'default_type' => 'domain', 'comment' => $comment, 'value' => $input2);
}
} else {
// check if it is a URL
// Adding http:// infront of the input in case it was left off. github.com/MISP/MISP should still be counted as a valid link
if (filter_var($input2, FILTER_VALIDATE_URL) || filter_var('http://' . $input2, FILTER_VALIDATE_URL)) {
if (preg_match('/^https:\/\/www.virustotal.com\//i', $input2)) return array('types' => array('link'), 'to_ids' => true, 'default_type' => 'link', 'comment' => $comment, 'value' => $input2);
return array('types' => array('url'), 'to_ids' => true, 'default_type' => 'url', 'comment' => $comment, 'value' => $input2);
}
if ($this->__resolveFilename($input)) return array('types' => array('filename'), 'to_ids' => true, 'default_type' => 'filename');
}
}
}
if (strpos($input, '\\') !== false) {

View File

@ -29,7 +29,8 @@ class Log extends AppModel {
'warning',
'error',
'email',
'serverSettingsEdit'
'serverSettingsEdit',
'remove_dead_workers'
)),
'message' => 'Options : ...'
)

View File

@ -572,7 +572,7 @@ class Server extends AppModel {
'branch' => 1,
'RPZ_policy' => array(
'level' => 1,
'description' => 'The duration (in seconds) of how long the user will be locked out when the allowed number of login attempts are exhausted.',
'description' => 'The default policy action for the values added to the RPZ.',
'value' => 0,
'errorMessage' => '',
'test' => 'testForRPZBehaviour',
@ -1663,6 +1663,39 @@ class Server extends AppModel {
return $proxyStatus;
}
public function workerDiagnostics(&$workerIssueCount) {
$this->ResqueStatus = new ResqueStatus\ResqueStatus(Resque::redis());
$workers = $this->ResqueStatus->getWorkers();
$currentUser = get_current_user();
$worker_array = array(
'cache' => array('ok' => true),
'default' => array('ok' => true),
'email' => array('ok' => true),
'scheduler' => array('ok' => true)
);
foreach ($workers as $pid => $worker) {
$alive = file_exists("/proc/$pid");
$entry = ($worker['type'] == 'regular') ? $worker['queue'] : $worker['type'];
$correct_user = ($currentUser === $worker['user']);
$ok = true;
if (!$alive || !$correct_user) {
$ok = false;
$workerIssueCount++;
$worker_array[$entry]['ok'] = false;
}
$worker_array[$entry]['workers'][] = array('pid' => $pid, 'user' => $worker['user'], 'alive' => $alive, 'correct_user' => $correct_user, 'ok' => $ok);
}
foreach ($worker_array as $k => &$queue) {
if ($k != 'scheduler') $worker_array[$k]['jobCount'] = CakeResque::getQueueSize($k);
if (!isset($queue['workers'])) {
$workerIssueCount++;
$queue['ok'] = false;
}
}
return $worker_array;
}
public function retrieveCurrentSettings($branch, $subString) {
$settings = array();
foreach ($this->serverSettings[$branch] as $settingName => $setting) {
@ -1674,4 +1707,64 @@ class Server extends AppModel {
}
return $settings;
}
public function killWorker($pid, $user) {
$this->ResqueStatus = new ResqueStatus\ResqueStatus(Resque::redis());
$workers = $this->ResqueStatus->getWorkers();
$this->Log = ClassRegistry::init('Log');
if (isset($workers[$pid])) {
$worker = $workers[$pid];
if (file_exists("/proc/$pid")) {
shell_exec('kill ' . $pid . ' > /dev/null &');
$this->Log->create();
$this->Log->save(array(
'org' => $user['org'],
'model' => 'User',
'model_id' => $user['id'],
'email' => $user['email'],
'action' => 'stop_worker',
'user_id' => $user['id'],
'title' => 'Stopping a worker.',
'change' => 'Stopping a worker. Worker was of type ' . $worker['queue'] . ' with pid ' . $pid
));
} else {
$this->ResqueStatus->removeWorker($pid);
$this->Log->create();
$this->Log->save(array(
'org' => $user['org'],
'model' => 'User',
'model_id' => $user['id'],
'email' => $user['email'],
'action' => 'remove_dead_workers',
'user_id' => $user['id'],
'title' => 'Removing a dead worker.',
'change' => 'Removind dead worker data. Worker was of type ' . $worker['queue'] . ' with pid ' . $pid
));
}
$this->ResqueStatus->removeWorker($pid);
}
}
public function workerRemoveDead($user) {
$this->ResqueStatus = new ResqueStatus\ResqueStatus(Resque::redis());
$workers = $this->ResqueStatus->getWorkers();
$this->Log = ClassRegistry::init('Log');
foreach ($workers as $pid => $worker) {
$test = $pid;
if (!file_exists("/proc/$pid")) {
$this->ResqueStatus->removeWorker($pid);
$this->Log->create();
$this->Log->save(array(
'org' => $user['org'],
'model' => 'User',
'model_id' => $user['id'],
'email' => $user['email'],
'action' => 'remove_dead_workers',
'user_id' => $user['id'],
'title' => 'Removing a dead worker.',
'change' => 'Removind dead worker data. Worker was of type ' . $worker['queue'] . ' with pid ' . $pid
));
}
}
}
}

View File

@ -572,8 +572,9 @@ class User extends AppModel {
}
public function fetchPGPKey($email) {
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
App::uses('SyncTool', 'Tools');
$syncTool = new SyncTool();
$HttpSocket = $syncTool->setupHttpSocket();
$response = $HttpSocket->get('https://pgp.mit.edu/pks/lookup?search=' . $email . '&op=index&fingerprint=on');
if ($response->code != 200) return $response->code;
$string = str_replace(array("\r", "\n"), "", $response->body);

View File

@ -1,35 +1,92 @@
<table class="table table-hover table-condensed" style="border:1px solid #dddddd; margin-top:1px; width:100%; padding:10px">
<tr>
<th>Worker Type</th>
<th>Worker Id</th>
<th>Status</th>
</tr>
<?php
foreach ($worker_array as $type => $workers):
if (empty($workers)):
?>
<tr>
<td class="short" style="background-color:red; color:white;"><?php echo (h($type));?></td>
<td class="short" style="background-color:red; color:white;">N/A</td>
<td style="background-color:red; color:white;">Worker not running!</td>
</tr>
<?php
<div style="border:1px solid #dddddd; margin-top:1px; width:100%; padding:10px">
<?php
foreach ($worker_array as $type => $data):
$queueStatusMessage = "Issues prevent jobs from being processed. Please resolve them below.";
$queueStatus = false;
if ($data['ok']) {
$queueStatus = true;
$queueStatusMessage = "OK";
} else if (!empty($data['workers'])) {
foreach ($data['workers'] as $worker) {
if ($worker['alive']) {
$queueStatus = true;
$queueStatusMessage = "There are issues with the worker(s), but at least one healthy worker is monitoring the queue.";
}
}
}
?>
<h3><?php echo 'Worker type: ' . h($type);?></h3>
<?php if ($type !== 'scheduler'): ?>
<p><b>Jobs in the queue: </b><?php echo h($data['jobCount']);?></p>
<p><b>Queue status: </b><?php echo ($queueStatus ? '<span style="color:green;">' . $queueStatusMessage . '</span>' : '<span style="color:red;font-weight:bold;">' . $queueStatusMessage . '</span>' )?></p>
<?php endif; ?>
<table class="table table-hover table-condensed" style="border:1px solid #dddddd; margin-top:1px; width:100%; padding:10px">
<tr>
<th>Worker PID</th>
<th>User</th>
<th>Worker process</th>
<th>Information</th>
<th>Actions</th>
</tr>
<?php
if (empty($data['workers'])):
?>
<tr>
<td class="shortish" style="background-color:red; color:white;">N/A</td>
<td class="short" style="background-color:red; color:white;">N/A</td>
<td style="background-color:red; color:white;">N/A</td>
<td style="background-color:red; color:white;">Worker not running!</td>
<td style="background-color:red; color:white;">&nbsp;</td>
</tr>
<?php
else:
foreach ($workers as $worker):
$style = "";
foreach ($data['workers'] as $worker):
$style = "color:green;";
$process = 'OK';
$message = 'The worker appears to be healthy.';
$icon_modifier = '';
if (!$worker['correct_user']) {
$message = 'The worker was started with a user other than the apache user.';
$style = "color:white;background-color:red;";
$icon_modifier = ' icon-white';
}
if (!$worker['alive']) {
$process = 'Dead';
$message = 'The Worker appears to be dead.';
$style = "color:white;background-color:red;";
$icon_modifier = ' icon-white';
}
$status = '<span style="color:green;">OK</span>';
?>
<tr>
<td class="shortish" style="<?php echo $style; ?>"><?php echo h($worker['pid']);?></td>
<td class="short" style="<?php echo $style; ?>"><?php echo h($worker['user']); ?></td>
<td class="short" style="<?php echo $style; ?>"><?php echo $process; ?></td>
<td style="<?php echo $style; ?>"><?php echo $message; ?></td>
<td class="actions short" style="<?php echo $style; ?>">
<?php
echo $this->Form->postLink('', '/servers/stopWorker/' . h($worker['pid']), array('class' => 'icon-trash' . $icon_modifier, 'title' => 'Stop (if still running) and remove this worker. This will immediately terminate any jobs that are being executed by it.'));
?>
</td>
</tr>
<?php
endforeach;
endif;
?>
</table>
<?php
echo $this->Form->create('Server', array('url' => '/servers/startWorker/' . h($type)));
echo $this->Form->button('Start a worker', array('class' => 'btn btn-inverse'));
echo $this->Form->end();
endforeach;
?>
</div>
<?php echo $this->Form->create('Server', array('url' => '/servers/restartWorkers'));
echo $this->Form->button('Restart all workers', array('class' => 'btn btn-primary'));
echo $this->Form->end();
?>
<tr>
<td class="short" <?php echo $style; ?>><?php echo h($type);?></td>
<td class="short" <?php echo $style; ?>><?php echo h($worker); ?></td>
<td <?php echo $style; ?>><?php echo $status; ?></td>
</tr>
<?php
endforeach;
endif;
?>
<?php
endforeach;
?>
</table>
<a href="/servers/restartWorkers" class="btn btn-primary">Restart all workers</a> This will start / restart all of the workers and refresh the page. Keep in mind, this process can take a few seconds to complete, so refresh the page again in 5-10 seconds to see the correct results.