save log_users in a separate table, so that we can add additional data to each log_user in the near future

This commit is contained in:
Davy Rolink 2014-10-01 20:50:37 +02:00
parent e13ca5e1b6
commit 6aabe1eca7
3 changed files with 81 additions and 40 deletions

View File

@ -199,22 +199,42 @@ function psm_update_conf($key, $value) {
* everything should have been handled when calling this function * everything should have been handled when calling this function
* *
* @param string $server_id * @param string $server_id
* @param string $type
* @param string $message * @param string $message
*
* @return int log_id
*/ */
function psm_add_log($server_id, $type, $message, $user_id = null) { function psm_add_log($server_id, $type, $message) {
global $db; global $db;
$db->save( return $db->save(
PSM_DB_PREFIX.'log', PSM_DB_PREFIX.'log',
array( array(
'server_id' => $server_id, 'server_id' => $server_id,
'type' => $type, 'type' => $type,
'message' => $message, 'message' => $message,
'user_id' => ($user_id === null) ? '' : $user_id,
) )
); );
} }
/**
* This function just adds a user to the log_users table.
*
* @param $log_id
* @param $user_id
*/
function psm_add_log_user($log_id, $user_id) {
global $db;
$db->save(
PSM_DB_PREFIX . 'log_users',
array(
'log_id' => $log_id,
'user_id' => $user_id,
)
);
}
/** /**
* This function adds the result of a check to the uptime table for logging purposes. * This function adds the result of a check to the uptime table for logging purposes.
* *

View File

@ -204,9 +204,13 @@ class Installer {
`type` enum('status','email','sms','pushover') NOT NULL, `type` enum('status','email','sms','pushover') NOT NULL,
`message` varchar(255) NOT NULL, `message` varchar(255) NOT NULL,
`datetime` timestamp NOT NULL default CURRENT_TIMESTAMP, `datetime` timestamp NOT NULL default CURRENT_TIMESTAMP,
`user_id` varchar(255) NOT NULL,
PRIMARY KEY (`log_id`) PRIMARY KEY (`log_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;", ) ENGINE=MyISAM DEFAULT CHARSET=utf8;",
PSM_DB_PREFIX . 'log_users' => "CREATE TABLE `" . PSM_DB_PREFIX . "log_users` (
`log_id` int(11) UNSIGNED NOT NULL ,
`user_id` int(11) UNSIGNED NOT NULL ,
PRIMARY KEY (`log_id`, `user_id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8;",
PSM_DB_PREFIX . 'servers' => "CREATE TABLE `" . PSM_DB_PREFIX . "servers` ( PSM_DB_PREFIX . 'servers' => "CREATE TABLE `" . PSM_DB_PREFIX . "servers` (
`server_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `server_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ip` varchar(100) NOT NULL, `ip` varchar(100) NOT NULL,

View File

@ -198,8 +198,6 @@ class StatusNotifier {
* @return boolean * @return boolean
*/ */
protected function notifyByEmail($users) { protected function notifyByEmail($users) {
$userlist = array();
// build mail object with some default values // build mail object with some default values
$mail = psm_build_mail(); $mail = psm_build_mail();
$mail->Subject = psm_parse_msg($this->status_new, 'email_subject', $this->server); $mail->Subject = psm_parse_msg($this->status_new, 'email_subject', $this->server);
@ -209,19 +207,23 @@ class StatusNotifier {
$mail->Body = $body; $mail->Body = $body;
$mail->AltBody = str_replace('<br/>', "\n", $body); $mail->AltBody = str_replace('<br/>', "\n", $body);
// Log
if(psm_get_conf('log_email')) {
$log_id = psm_add_log($this->server_id, 'email', $body);
}
// go through empl // go through empl
foreach ($users as $user) { foreach ($users as $user) {
// Log
if(!empty($log_id)) {
psm_add_log_user($log_id, $user['user_id']);
}
// we sent a seperate email to every single user. // we sent a seperate email to every single user.
$userlist[] = $user['user_id'];
$mail->AddAddress($user['email'], $user['name']); $mail->AddAddress($user['email'], $user['name']);
$mail->Send(); $mail->Send();
$mail->ClearAddresses(); $mail->ClearAddresses();
} }
if(psm_get_conf('log_email')) {
// save to log
psm_add_log($this->server_id, 'email', $body, implode(',', $userlist));
}
} }
/** /**
@ -231,38 +233,51 @@ class StatusNotifier {
* @return boolean * @return boolean
*/ */
protected function notifyByPushover($users) { protected function notifyByPushover($users) {
$userlist = array(); // Clean-up users
$pushover = psm_build_pushover(); foreach($users as $k => $user) {
if (trim($user['pushover_key']) == '') {
unset($users[$k]);
}
}
if($this->status_new === true) { // Validation
$pushover->setPriority(0); if (empty($users)) {
} else { return;
$pushover->setPriority(2); }
$pushover->setRetry(300); //Used with Priority = 2; Pushover will resend the notification every 60 seconds until the user accepts.
$pushover->setExpire(3600); //Used with Priority = 2; Pushover will resend the notification every 60 seconds for 3600 seconds. After that point, it stops sending notifications.
}
$message = psm_parse_msg($this->status_new, 'pushover_message', $this->server);
// Pushover
$message = psm_parse_msg($this->status_new, 'pushover_message', $this->server);
$pushover = psm_build_pushover();
if($this->status_new === true) {
$pushover->setPriority(0);
} else {
$pushover->setPriority(2);
$pushover->setRetry(300); //Used with Priority = 2; Pushover will resend the notification every 60 seconds until the user accepts.
$pushover->setExpire(3600); //Used with Priority = 2; Pushover will resend the notification every 60 seconds for 3600 seconds. After that point, it stops sending notifications.
}
$pushover->setTitle(psm_parse_msg($this->status_new, 'pushover_title', $this->server)); $pushover->setTitle(psm_parse_msg($this->status_new, 'pushover_title', $this->server));
$pushover->setMessage(str_replace('<br/>', "\n", $message)); $pushover->setMessage(str_replace('<br/>', "\n", $message));
$pushover->setUrl(psm_build_url()); $pushover->setUrl(psm_build_url());
$pushover->setUrlTitle(psm_get_lang('system', 'title')); $pushover->setUrlTitle(psm_get_lang('system', 'title'));
// Log
if(psm_get_conf('log_pushover')) {
$log_id = psm_add_log($this->server_id, 'pushover', $message);
}
foreach($users as $user) { foreach($users as $user) {
if(trim($user['pushover_key']) == '') { // Log
continue; if(!empty($log_id)) {
} psm_add_log_user($log_id, $user['user_id']);
$userlist[] = $user['user_id']; }
// Set recipient + send
$pushover->setUser($user['pushover_key']); $pushover->setUser($user['pushover_key']);
if($user['pushover_device'] != '') { if($user['pushover_device'] != '') {
$pushover->setDevice($user['pushover_device']); $pushover->setDevice($user['pushover_device']);
} }
$pushover->send(); $pushover->send();
} }
if(psm_get_conf('log_pushover')) {
psm_add_log($this->server_id, 'pushover', $message, implode(',', $userlist));
}
} }
/** /**
@ -277,24 +292,26 @@ class StatusNotifier {
return false; return false;
} }
// we have to build an userlist for the log table.. $message = psm_parse_msg($this->status_new, 'sms', $this->server);
$userlist = array();
// Log
if(psm_get_conf('log_sms')) {
psm_add_log($this->server_id, 'sms', $message);
}
// add all users to the recipients list // add all users to the recipients list
foreach ($users as $user) { foreach ($users as $user) {
$userlist[] = $user['user_id']; // Log
if(!empty($log_id)) {
psm_add_log_user($log_id, $user['user_id']);
}
$sms->addRecipients($user['mobile']); $sms->addRecipients($user['mobile']);
} }
$message = psm_parse_msg($this->status_new, 'sms', $this->server);
// Send sms // Send sms
$result = $sms->sendSMS($message); $result = $sms->sendSMS($message);
if(psm_get_conf('log_sms')) {
// save to log
psm_add_log($this->server_id, 'sms', $message, implode(',', $userlist));
}
return $result; return $result;
} }