Skip to content

Commit

Permalink
Add a setting for cpu amount so the threshold is based on overall
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Nov 23, 2023
1 parent 5575324 commit da4fbfd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ docker run \
-l net.unraid.docker.icon='https://golift.io/crontabs.png' \
-p '9999:80/tcp' \
-v '/mnt/disk1/appdata/dockwatch/config':'/config':'rw' \
-v '/proc':'/proc':'rw' \
-v '/var/run/docker.sock':'/var/run/docker.sock':'rw' 'ghcr.io/notifiarr/dockwatch:main'
```

Expand All @@ -81,6 +82,7 @@ dockwatch:
volumes:
- /appdata/dockwatch/config:/config
- /var/run/docker.sock:/var/run/docker.sock
- /proc:/proc
```

### Manual
Expand All @@ -93,6 +95,7 @@ Volumes
| Name | Host | Container |
| ----- | ----- | ----- |
| App Config | /mnt/disk1/appdata/dockwatch/config | /config |
| Proc | /proc | /proc |
| Docker sock | /var/run/docker.sock | /var/run/docker.sock |

Ports
Expand Down
11 changes: 11 additions & 0 deletions root/app/www/public/ajax/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

if ($_POST['m'] == 'init') {
$globalSettings = $settings['global'];
$cpus = cpuTotal();
if ($cpus == 0) {
$cpus = '0 (Could not get cpu count from /proc/cpuinfo)';
}
?>
<div class="container-fluid pt-4 px-4">
<div class="bg-secondary rounded h-100 p-4">
Expand Down Expand Up @@ -98,6 +102,13 @@
</td>
<td>If a container usage is above this number, send a notification (if notification is enabled)</td>
</tr>
<tr>
<th scope="row">CPUs</th>
<td>
<input class="form-control" type="number" id="globalSetting-cpuAmount" value="<?= $globalSettings['cpuAmount'] ?>">
</td>
<td>Detected count: <?= $cpus ?></td>
</tr>
<tr>
<th scope="row">Memory<sup>1</sup></th>
<td>
Expand Down
8 changes: 7 additions & 1 deletion root/app/www/public/crons/state.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@
//-- CHECK FOR HIGH CPU USAGE CONTAINERS
if ($settings['notifications']['triggers']['cpuHigh']['active'] && floatval($settings['global']['cpuThreshold']) > 0) {
if ($currentState['stats']['CPUPerc']) {
$cpu = floatval(str_replace('%', '', $currentState['stats']['CPUPerc']));
$cpu = floatval(str_replace('%', '', $currentState['stats']['CPUPerc']));
$cpuAmount = intval($settings['global']['cpuAmount']);

if ($cpuAmount > 0) {
$cpu = number_format(($cpu / $cpuAmount), 2);
}

if ($cpu > floatval($settings['global']['cpuThreshold'])) {
$notify['usage']['cpu'][] = ['container' => $currentState['Names'], 'usage' => $cpu];
}
Expand Down
16 changes: 16 additions & 0 deletions root/app/www/public/functions/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@ function calculateDaysFromString($str)
} elseif (strpos($str, 'm') !== false) { //-- [1-12]m
return intval(str_replace('m', '', $str)) * 30;
}
}

function cpuTotal()
{
$cpus = 0;
$cmd = 'cat /proc/cpuinfo';
$cpuinfo = shell_exec($cmd . ' 2>&1');
$lines = explode("\n", $cpuinfo);

foreach ($lines as $line) {
if (strpos($line, 'processor') !== false) {
$cpus++;
}
}

return $cpus;
}

0 comments on commit da4fbfd

Please sign in to comment.