Skip to content

Commit

Permalink
Version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzegit committed Dec 12, 2024
1 parent 5d352c7 commit f9e4d5a
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 313 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ InstantCMS2 component. Pauses the site at a given load threshold.

## Resources

* [InstantCMS official site](http://www.instantcms.ru/)
* [Component page](http://addons.instantcms.ru/addons/loadaverage.html)
* [InstantCMS official site](https://instantcms.ru/)
* [Component page](https://instantcms.ru/addons/loadaverage.html)
9 changes: 4 additions & 5 deletions manifest.en.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ name = "loadaverage"

[version]
major = "2"
minor = "0"
build = "1"
date = "20170119"
minor = "1"
build = "0"
date = "20241212"

[depends]
core = "2.5.0"
core = "2.16.3"

[author]
name = "InstantMedia Team"
url = "https://instantvideo.ru/"
email = "[email protected]"

[description]
text[] = "Pauses the site at a given load threshold."
9 changes: 4 additions & 5 deletions manifest.ru.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ name = "loadaverage"

[version]
major = "2"
minor = "0"
build = "1"
date = "20170119"
minor = "1"
build = "0"
date = "20241212"

[depends]
core = "2.5.0"
core = "2.16.3"

[author]
name = "InstantMedia Team"
url = "https://instantvideo.ru/"
email = "[email protected]"

[description]
text[] = "Приостанавливает работу сайта при"
Expand Down
41 changes: 21 additions & 20 deletions package/system/controllers/loadaverage/backend.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
<?php
/******************************************************************************/
// //
// InstantMedia 2017 //
// https://instantvideo.ru/, [email protected] //
// written by Fuze //
// https://instantvideo.ru/copyright.html //
// //
/******************************************************************************/

class backendLoadaverage extends cmsBackend {

public $useDefaultOptionsAction = true;
protected $useOptions = true;
protected $useOptions = true;

public function getOptionsToolbar() {

if(file_exists($this->cms_config->cache_path.'is_temporary_unavailable_email_me')){
return [[
'class' => 'refresh',
'title' => LANG_LA_CLEAR_SEND_EMAIL,
'href' => $this->cms_template->href_to('clear_send_email')
]];
}
}

public function actionIndex(){
$this->redirectToAction('options');
public function actionIndex() {
return $this->redirectToAction('options');
}

public function getCurrentloadAverage($sensor) {

$la = sys_getloadavg();

return round(100*($la[$sensor]/$this->options['cpu_count']));

return round(100 * ($la[$sensor] / $this->options['cpu_count']));
}

public function actionClearSendEmail(){
public function actionClearSendEmail() {

@unlink($this->cms_config->cache_path.'is_temporary_unavailable_email_me');
@unlink($this->cms_config->cache_path . 'is_temporary_unavailable_email_me');

cmsUser::addSessionMessage(LANG_LA_CLEAR_SUCCESS, 'success');

$this->redirectToAction('options');

return $this->redirectToAction('options');
}

public function actionDeleteComponent(){
public function actionDeleteComponent() {

$model = new cmsModel();

Expand All @@ -44,8 +46,7 @@ public function actionDeleteComponent(){

cmsUser::addSessionMessage(sprintf(LANG_CP_COMPONENT_IS_DELETED, LANG_LOADAVERAGE_CONTROLLER), 'success');

$this->redirectTo('admin', 'controllers');

return $this->redirectTo('admin', 'controllers');
}

}
104 changes: 45 additions & 59 deletions package/system/controllers/loadaverage/backend/forms/form_options.php
Original file line number Diff line number Diff line change
@@ -1,96 +1,82 @@
<?php
/******************************************************************************/
// //
// InstantMedia 2017 //
// https://instantvideo.ru/, [email protected] //
// written by Fuze //
// https://instantvideo.ru/copyright.html //
// //
/******************************************************************************/

class formLoadaverageOptions extends cmsForm {

public function init() {

$cpu_count = get_cpu_count();

return array(

array(
'type' => 'fieldset',
'childs' => array(

new fieldNumber('max_load', array(
return [
[
'type' => 'fieldset',
'childs' => [
new fieldNumber('max_load', [
'title' => LANG_LA_MAX_LOAD,
'units' => '%',
'hint' => LANG_LA_MAX_LOAD_HINT,
'default' => 90
)),

new fieldString('cpu_count', array(
]),
new fieldString('cpu_count', [
'title' => LANG_LA_CPU_COUNT,
'hint' => LANG_LA_CPU_COUNT_HINT,
'suffix' => ($cpu_count ? html_bool_span('<span title="' . LANG_LA_AUTO_PATH_CLICK . '" class="auto_copy tooltip" data-path="' . $cpu_count . '">' . LANG_LA_AUTO_PATH . ' ' . $cpu_count . '</span>', true) : html_bool_span(LANG_LA_NO_AUTO_PATH, false)),
'suffix' => ($cpu_count ? '<span title="' . LANG_LA_AUTO_PATH_CLICK . '" class="auto_copy_value" data-value="' . $cpu_count . '">' . LANG_LA_AUTO_PATH . ' ' . $cpu_count . '</span>' : LANG_LA_NO_AUTO_PATH),
'default' => ($cpu_count ? $cpu_count : 1),
'rules' => array(
array('number'),
array('min', 1)
)
)),

new fieldList('sensor', array(
'title' => LANG_LA_SENSOR,
'items' => array(
'rules' => [
['number'],
['min', 1]
]
]),
new fieldList('sensor', [
'title' => LANG_LA_SENSOR,
'items' => [
LANG_LA_SENSOR1,
LANG_LA_SENSOR5,
LANG_LA_SENSOR15
),
],
'default' => 2
)),

new fieldList('action', array(
'title' => LANG_LA_ACTION,
'is_multiple' => true,
'items' => array(
LANG_LA_ACTION0,
LANG_LA_ACTION1
),
'default' => array(0)
)),

new fieldString('email', array(
]),
new fieldCheckbox('action:block', [
'title' => LANG_LA_ACTION0,
'default' => 1
]),
new fieldCheckbox('action:email', [
'title' => LANG_LA_ACTION1,
'default' => 0
]),
new fieldString('email', [
'title' => LANG_LA_EMAIL,
'hint' => LANG_LA_EMAIL_HINT,
'rules' => array(
array('email')
)
))

)
)

);

'rules' => [
['email']
],
'visible_depend' => ['action:email' => ['show' => ['1']]]
])
]
]
];
}

}

function get_cpu_count(){
if(!function_exists('exec') || !function_exists('sys_getloadavg')){
function get_cpu_count() {

if (!function_exists('exec') || !function_exists('sys_getloadavg')) {
return false;
}
$buffer = array();

$buffer = [];
$err = '';
$result = exec("grep -P '^physical id' /proc/cpuinfo|wc -l", $buffer, $err);
if($err !== 127){
if(!isset($buffer[0])){
if ($err !== 127) {
if (!isset($buffer[0])) {
$buffer[0] = $result;
}
$b = mb_strtolower($buffer[0]);
if(mb_strstr($b,'error') || mb_strstr($b,' no ') || mb_strstr($b,'not found') || mb_strstr($b,'No such file or directory')){
if (mb_strstr($b, 'error') || mb_strstr($b, ' no ') || mb_strstr($b, 'not found') || mb_strstr($b, 'No such file or directory')) {
return false;
}
} else {
return false;
}
return (int)$buffer[0];
return (int) $buffer[0];
}
62 changes: 34 additions & 28 deletions package/system/controllers/loadaverage/frontend.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
<?php
/******************************************************************************/
// //
// InstantMedia 2017 //
// https://instantvideo.ru/, [email protected] //
// written by Fuze //
// https://instantvideo.ru/copyright.html //
// InstantMedia //
// http://instantmedia.ru/ //
// written by Fuze //
// //
/******************************************************************************/

class loadaverage extends cmsFrontend {

protected $useOptions = true;

public function displayPeekError() {

if (href_to('auth', 'login') != href_to_current()){
if (href_to('auth', 'login') !== href_to_current()) {

if (!cmsUser::isAdmin()) {

if (cmsUser::isLogged()){
if (cmsUser::isLogged()) {

cmsUser::addSessionMessage(LANG_LOGIN_ADMIN_ONLY, 'error');

cmsUser::logout();
$this->redirectToHome();

return $this->redirectToHome();
}

header('HTTP/1.0 503 Too busy, try again later');
header('Status: 503 Too busy, try again later');
if (isset($this->cms_core->response)) {

$this->cms_core->response->setStatusCode(503);

} else {
http_response_code(503);
}

$this->cms_template->setContext($this);

return $this->cms_template->renderPlain('temporary_unavailable');

}
}

Expand All @@ -38,37 +46,37 @@ public function getCurrentloadAverage($sensor) {

$la = sys_getloadavg();

return round(100*($la[$sensor]/$this->options['cpu_count']));

return round(100 * ($la[$sensor] / $this->options['cpu_count']));
}

public function fixOverload($current_load_average) {

$model = new cmsModel();

$model->insert('load_average_peak', array(
'value'=>$current_load_average
));
$model->insert('load_average_peak', [
'value' => $current_load_average
]);

return $this;

}

public function sendEmail($emails, $current_load_average){
public function sendEmail($emails, $current_load_average) {

$emails = explode(',', $emails);

$letter_text = cmsCore::getLanguageTextFile('letters/temporary_unavailable');
if (!$letter_text){ return false; }
if (!$letter_text) {
return false;
}

$letter_text = string_replace_keys_values($letter_text, array(
'site' => $this->cms_config->sitename,
'host' => $this->cms_config->host,
'ip' => $_SERVER['SERVER_ADDR'],
'la' => $current_load_average,
'date' => html_date(),
'time' => html_time()
));
$letter_text = string_replace_keys_values($letter_text, [
'site' => $this->cms_config->sitename,
'host' => $this->cms_config->host,
'ip' => $_SERVER['SERVER_ADDR'],
'la' => $current_load_average,
'date' => html_date(),
'time' => html_time()
]);

foreach ($emails as $email) {

Expand All @@ -81,11 +89,9 @@ public function sendEmail($emails, $current_load_average){
$mailer->setBodyHTML(nl2br($letter_text))->send();

$mailer->clearTo()->clearAttachments();

}

return $this;

}

}
Loading

0 comments on commit f9e4d5a

Please sign in to comment.