forked from NEMSLinux/legacy-nems-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadlogger.sh
More file actions
executable file
·34 lines (30 loc) · 1.43 KB
/
loadlogger.sh
File metadata and controls
executable file
·34 lines (30 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/php
<?php
// This script logs the 15 minute load average over the course of 1 week every 15 minutes self-maintaining.
// Then, nems-info loadaverage will tell you our load average in a much more accurate overview than just a 15 minute load average (ie., you can see an average based on the entire week, not just the moment you're running it, which may be after a reboot).
// This script should ONLY be called by cron, otherwise the results will be wrong!
// If you're hoping to see the results, please use: nems-info loadaverage
if (@$argv[1] != 'cron') exit('Do not run this script manually.' . PHP_EOL);
// Get the existing log
if (file_exists('/var/log/nems/load-average.ser')) {
$loads = unserialize(trim(file_get_contents('/var/log/nems/load-average.ser')));
}
if (isset($loads) && is_array($loads) && count($loads) > ((60*24*7)/15) ) { // how many times 15 minutes goes into a week
unset($loads[0]);
}
// Find the current load average over 15 minutes
$load = sys_getloadavg();
if (isset($load[2])) {
$loads[] = $load[2];
$loads = array_values($loads);
file_put_contents('/var/log/nems/load-average.ser',serialize($loads));
}
if (is_array($loads)) {
$total = 0;
foreach ($loads as $thisload) {
$total=($total+$thisload);
}
$average = ($total/count($loads));
file_put_contents('/var/log/nems/load-average.log', $average); // this file has the current average
}
?>