Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS Mojave 'stats' are broken #193

Open
Archigos opened this issue Nov 16, 2018 · 13 comments
Open

macOS Mojave 'stats' are broken #193

Archigos opened this issue Nov 16, 2018 · 13 comments

Comments

@Archigos
Copy link

Your environment:

  • Operating system: macOS Mojave (10.14)
  • Webserver: Apache 2.4.34
  • PHP Version: 7.1.19 x64 NTS
  • Monitorr Version: 1.7.7d

Describe your issue:

This information isn't on the Wiki and I attempted to talk about this in Discord. (John told me to make this Issue). Monitorr stats do not work on macOS Mojave so I managed to alter the code and get most of the stats working. These first two screenshots show the error messages you'll get running it on Mojave before code changes.
image
image

While messing with the code, I posted the following screenshot showing that I managed to fix both Uptime and CPU%. In the screenshot itself, I just faked the RAM to get rid of the error.
image
As you can see, the uptime is in a slightly different format (Mojave uptime gives a string similar to: 14:20 up 2 days, 10:39, 3 users, load averages: 10.05 11.16 10.82 {Current Time, Uptime, User Count, etc.} They don't provide seconds so I tweaked it.

After that screenshot, I managed to get the RAM more accurate by getting currently used memory and hard coding the total memory since I couldn't figure out a way to pull that and wanted to still give an accurate percent.

I'm not exactly sure of line numbers to reference since I was moving stuff around a bit while editing, but around line 425? in functions.php in regards to Uptime, I assume you'll have to do a system check, but when you determine it's a Mac, change the following:

$days = floor($uptime) / 60 / 60 / 24;
$days_padded = sprintf("%02d", $days);
$hours = round($uptime) / 60 / 60 % 24;
$hours_padded = sprintf("%02d", $hours);
$mins = round($uptime) / 60 % 60;
$mins_padded = sprintf("%02d", $mins);
$secs = round($uptime) % 60;
$secs_padded = sprintf("%02d", $secs);
$total_uptime = "$days_padded:$hours_padded:$mins_padded:$secs_padded";

to:

$uptime       = preg_split("/[\s]+/", trim(shell_exec('uptime')));
$days         = $uptime[2];
$days_padded  = sprintf("%02d", $days);
$uptime_time  = explode(":", $uptime[4]);
$hours        = $uptime_time[0];
$hours_padded = sprintf("%02d", $hours);
$mins         = $uptime_time[1];
$mins_padded  = sprintf("%02d", $mins);
$total_uptime = "{$days_padded}d:{$hours_padded}h:{$mins_padded}m";

For CPU:
Instead of calling getServerLoad() (around line 160)
Your two line:

$cpuLoad = getServerLoad();
$cpuPercent = round($cpuLoad, 2);

Ignore that function and do:

$exec_loads = sys_getloadavg();
$exec_cores = trim(shell_exec("grep -P '^processor' /proc/cpuinfo | wc -l"));
$cpuPercent = round($exec_loads[1] / ($exec_cores + 1), 0);

For RAM, as of now, best I could do was hard code total memory:

function getRamTotal()
{
  $result = 0;
  if(PHP_OS == 'WINNT')
  {
  ...... // a few lines of code
  } else {
    return 40 * 1024;  // My system has 40GB
    $fh = fopen('/proc/meminfo', 'r');
    .... // rest of your function
}

In getRamFree(), again inside the 'else' from the systetem detect:

if(PHP_OS == 'WINNT')
{
  ... // a few lines of code
} else {
  if($fh = fopen('/proc/meminfo', 'r') !== false)
  {
    ... your code
  } else {
      $memoryFree   = round(memory_get_usage(true) / 1024, 2);
      // sadly while messing around getting this to work, I deleted some of your code, so don't remember
         if you need to return $memoryFree or tweak it for percentage still... I'm sure you'll figure it out.
  }
}

I think that's it, if not, feel free to contact me.

@Archigos
Copy link
Author

Just figured I'd show a few more little tweaks I tried...
screenshot 2018-11-18 10 16 13

@seanvree
Copy link
Member

seanvree commented Mar 5, 2019

TO DO:

Add a "default" RAM setting which can be changed by the user. When Monitorr can't get RAM total, will use default setting.

@jonathanfinley
Copy link
Member

jonathanfinley commented Mar 5, 2019 via email

@seanvree
Copy link
Member

hey @Archigos can you send me a screenshot of your main settings page pls?

image

@Archigos
Copy link
Author

Here you go....

Screenshot 2019-12-23 08 12 20

@Archigos
Copy link
Author

To help out... I noticed you updated the Alpha branch a few hours ago so I made a second copy of Monitor from that branch and I'll do a better job documenting the changes I make to include my changes that worked above and will post here with the info later.

@seanvree
Copy link
Member

@Archigos thanks my man.

@Archigos
Copy link
Author

Archigos commented Dec 23, 2019

Your environment:

  • Operating system: macOS Catalina (10.15)
  • Monitorr Version: 1.7.8a

Describe your issue:

Edit the file assets/php/functions.php

To fix CPU:

Change: Lines 166-168

// register variable for getServerLoad()
$cpuLoad = getServerLoad();
$cpuPercent = round($cpuLoad, 2);

to: Lines 166-175

// register variable for getServerLoad()
if(PHP_OS == 'Darwin') {
  $exec_loads = sys_getloadavg();
  $exec_cores = trim(shell_exec("grep -P '^processor' /Line proc/cpuinfo | wc -l"));
  $cpuLoad    = 'Found';
  $cpuPercent = round($exec_loads[1] / ($exec_cores + Line 1), 0);
} else {
  $cpuLoad = getServerLoad();
  $cpuPercent = round($cpuLoad, 2);
}

Marking $cpuLoad = 'Found'; eleminates the console log error that would be generated. Or you can just move the error block into the else to solve it as well.

To fix RAM:

Add into getRamTotal() on 195:

} elseif(PHP_OS == 'Darwin') {
  return (int)shell_exec('sysctl -n hw.memsize');

Add into getRamFree() on 231:

} elseif(PHP_OS == 'Darwin') {
  $totalRam = getRamTotal();
  $used     = (int) (memory_get_usage(TRUE) * 1024);
  $free     = $totalRam - $used;
  return $free;

As you may notice, unlike the 'fix' I submitted prior, this one does
NOT hardcode the total memory for macOS and reports everything properly.

To fix Uptime:
--Edit-- I put in the wrong info for Uptime.... now corrected.
Starting on Line 419

function mac_uptime() {
  $boottime = shell_exec('sysctl kern.boottime');
  $split    = preg_split("/[\s]+/", $boottime);
  $split    = $split[4];
  $fixed    = trim($split, ',');
  $num      = time() - $fixed;

  $min      = 0;
  $hour     = 0;
  $day      = 0;
  if($num > 59) {
    $sec = $num % 60;
    $num = $num / 60;
    if($num > 59) {
      $min = $num % 60;
      $num = $num / 60;
      if($num > 23) {
        $hour = $num % 24;
        $day = floor($num / 24);
      } else {
        $hour = floor($num);
      }
    } else {
      $min = floor($num);
    }
  } else {
    $sec = floor($num);
  }
  $hour = sprintf("%02d", $hour);
  $day  = sprintf("%02d", $day);
  $min  = sprintf("%02d", $min);

  return "{$day}d:{$hour}h:{$min}m";
}

if(PHP_OS == 'Darwin') {
  $total_uptime = mac_uptime();
} else {
  $uptime = shell_exec("cut -d. -f1 /proc/uptime");
  $days = floor($uptime) / 60 / 60 / 24;
  $days_padded = sprintf("%02d", $days);
  $hours = round($uptime) / 60 / 60 % 24;
  $hours_padded = sprintf("%02d", $hours);
  $mins = round($uptime) / 60 % 60;
  $mins_padded = sprintf("%02d", $mins);
  $secs = round($uptime) % 60;
  $secs_padded = sprintf("%02d", $secs);
  $total_uptime = "$days_padded:$hours_padded:$mins_padded:$secs_padded";
}

I think that's all of what I did before. Let me know if it all works or you need more info.

@snorkrat
Copy link

snorkrat commented Mar 4, 2020

I added the above lines into mine (also on Catalina) but it still throws out some errors. Granted I'm a total newbie to this. Any way you could upload your functions.php for me to copy?

@Archigos
Copy link
Author

Archigos commented Mar 4, 2020

I'll provide the file, but curious at to what errors you're getting so I can try to fix those as well.

Just rename this file or copy/paste over your existing one.
functions.php.txt

Github won't let me put up a .php file, so I just renamed it to .txt in case you're wondering.

@snorkrat
Copy link

snorkrat commented Mar 15, 2020

Apologies for the late reply.

When I had tried to edit the file I think I was putting a few things in the wrong lines... {} weren't closed properly etc.

Whenever I use your .php file I get the following errors:

Warning
: Division by zero in
/.../assets/php/functions.php
on line
170


Notice
: Undefined offset: 4 in
/.../assets/php/functions.php
on line
293


Warning
: A non-numeric value encountered in
/.../assets/php/functions.php
on line
295

Also the Ram is at INF%, Uptime is 18336d:17h:50m (which is incorrect) and CPU is 4% which also doesn't seem right.

@Archigos
Copy link
Author

I can't remember if I modified other files as well and if they are causing some of the issues, but I managed to at least track down some of the issues you're having. I'm not on my Mac right now, but if you can provide some more information I can attempt to fix these.

The below aren't solutions to fix the current issues, just more info that can help me track down how to change the code.

Open the "About My Mac" and tell me the macOS version listed and also let me know what your total system RAM is.

Uptime
Next, open Terminal and run the following commands and let me know the results:

sysctl kern.boottime
and hopefully, you get a result like
kern.boottime: { sec = 1584151704, usec = 615570 } Fri Mar 13 22:08:24 2020
I originally was trying to use uptime but that was a complete pain in the butt with the context used so I may have to go back to that if this doesn't work.

RAM
In terminal try:
sysctl -n hw.memsize
You should get something like:
42949672960

CPU
I'm pretty sure you're correct with the CPU being wrong, but I don't remember if I couldn't get it working or gave up with what I had as I'm sure it's wrong as well, but don't remember getting any more accurate with it. Sadly, this one will probably have to be solved by one of the devs.

Sadly, with the exception of the CPU probably being wrong, all of these appear to work properly on my system so I'm not sure what else to try. I can package up my copy and give that to you to see if it helps, obviously either back up yours first, or use a different directory while testing. :)

@snorkrat
Copy link

First of thanks for the help, and secondly apologies for the late replies!

Here are the results of what you wanted:

macOS version: 10.15.3
Total RAM: 16GB

Uptime - result of sysctl kern.boottime: { sec = 1583634819, usec = 178477 } Sun Mar 8 02:33:39 2020

RAM - result of sysctl -n hw.memsize: 17179869184

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants